blob: c850f48aad95a1c0c1026024184c87c7bdf9903e [file] [log] [blame]
Matteo Scandolo974c0e42016-05-25 16:02:16 -07001(function () {
2
3 angular.module('xos.uiComponents')
4
5 /**
6 * @ngdoc service
7 * @name xos.uiComponents.XosFormHelpers
8 * @requires xos.uiComponents.LabelFormatter
9 * @requires xos.helpers._
10 **/
11
12 .service('XosFormHelpers', function(_, LabelFormatter){
13
14 /**
15 * @ngdoc method
16 * @name xos.uiComponents.XosFormHelpers#_isEmail
17 * @methodOf xos.uiComponents.XosFormHelpers
18 * @description
19 * Return true if the string is an email address
20 * @param {string} text The string to be evaluated
21 * @returns {boolean} If the string match an email format
22 **/
23
24 this._isEmail = (text) => {
25 var re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
26 return re.test(text);
27 };
28
29 /**
30 * @ngdoc method
31 * @name xos.uiComponents.XosFormHelpers#_getFieldFormat
32 * @methodOf xos.uiComponents.XosFormHelpers
33 * @description
34 * Return the type of the input
35 * @param {mixed} value The data to be evaluated
36 * @returns {string} The type of the input
37 **/
38
39 this._getFieldFormat = (value) => {
40
41 if(angular.isArray(value)){
42 return 'array';
43 }
44
45 // check if is date
46 if (_.isDate(value) || (!Number.isNaN(Date.parse(value)) && new Date(value).getTime() > 631180800000)){
47 return 'date';
48 }
49
50 // check if is boolean
51 // isNaN(false) = false, false is a number (0), true is a number (1)
52 if(typeof value === 'boolean'){
53 return 'boolean';
54 }
55
Matteo Scandolo974c0e42016-05-25 16:02:16 -070056 // check if a string is an email
57 if(this._isEmail(value)){
58 return 'email';
59 }
60
61 // if null return string
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070062 if(typeof value === 'string' || value === null){
63 return 'text';
Matteo Scandolo974c0e42016-05-25 16:02:16 -070064 }
65
66 return typeof value;
67 };
68
69 /**
70 * @ngdoc method
71 * @name xos.uiComponents.XosFormHelpers#buildFormStructure
72 * @methodOf xos.uiComponents.XosFormHelpers
73 * @description
74 * Return the type of the input
75 * @param {object} modelField An object containing one property for each field of the model
76 * @param {object} customField An object containing one property for each field custom field
77 * @param {object} model The actual model on wich build the form structure (it is used to determine the type of the input)
78 * @returns {object} An object describing the form structure in the form of:
79 * ```
80 * {
81 * 'field-name': {
82 * label: 'Label',
83 * type: 'number', //typeof field
84 * validators: {} // see xosForm for more details
85 * }
86 * }
87 * ```
88 **/
89
90 this.buildFormStructure = (modelField, customField, model) => {
91
92 modelField = Object.keys(modelField).length > 0 ? modelField : customField; //if no model field are provided, check custom
93 customField = customField || {};
94
95 return _.reduce(Object.keys(modelField), (form, f) => {
96
97 form[f] = {
98 label: (customField[f] && customField[f].label) ? `${customField[f].label}:` : LabelFormatter.format(f),
99 type: (customField[f] && customField[f].type) ? customField[f].type : this._getFieldFormat(model[f]),
100 validators: (customField[f] && customField[f].validators) ? customField[f].validators : {}
101 };
102
103 if(form[f].type === 'date'){
104 model[f] = new Date(model[f]);
105 }
106
107 if(form[f].type === 'number'){
108 model[f] = parseInt(model[f], 10);
109 }
110
111 return form;
112 }, {});
113 };
114
115 /**
116 * @ngdoc method
117 * @name xos.uiComponents.XosFormHelpers#parseModelField
118 * @methodOf xos.uiComponents.XosFormHelpers
119 * @description
120 * Helpers for buildFormStructure, convert a list of model properties in an object used to build the form structure, eg:
121 * ```
122 * // input:
123 * ['id', 'name'm 'mail']
124 *
125 * // output
126 * {
127 * id: {},
128 * name: {},
129 * mail: {}
130 * }
131 * ```
132 * @param {array} fields An array of fields representing the model properties
133 * @returns {object} An object containing one property for each field of the model
134 **/
135
136 this.parseModelField = (fields) => {
137 return _.reduce(fields, (form, f) => {
138 form[f] = {};
139 return form;
140 }, {});
141 }
142
143 });
144})();