blob: ce52272d7eed2fb6300558cc8c25cfb3594ad01a [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
56 // check if a string is a number
57 if(!isNaN(value) && value !== null){
58 return 'number';
59 }
60
61 // check if a string is an email
62 if(this._isEmail(value)){
63 return 'email';
64 }
65
66 // if null return string
67 if(value === null){
68 return 'string';
69 }
70
71 return typeof value;
72 };
73
74 /**
75 * @ngdoc method
76 * @name xos.uiComponents.XosFormHelpers#buildFormStructure
77 * @methodOf xos.uiComponents.XosFormHelpers
78 * @description
79 * Return the type of the input
80 * @param {object} modelField An object containing one property for each field of the model
81 * @param {object} customField An object containing one property for each field custom field
82 * @param {object} model The actual model on wich build the form structure (it is used to determine the type of the input)
83 * @returns {object} An object describing the form structure in the form of:
84 * ```
85 * {
86 * 'field-name': {
87 * label: 'Label',
88 * type: 'number', //typeof field
89 * validators: {} // see xosForm for more details
90 * }
91 * }
92 * ```
93 **/
94
95 this.buildFormStructure = (modelField, customField, model) => {
96
97 modelField = Object.keys(modelField).length > 0 ? modelField : customField; //if no model field are provided, check custom
98 customField = customField || {};
99
100 return _.reduce(Object.keys(modelField), (form, f) => {
101
102 form[f] = {
103 label: (customField[f] && customField[f].label) ? `${customField[f].label}:` : LabelFormatter.format(f),
104 type: (customField[f] && customField[f].type) ? customField[f].type : this._getFieldFormat(model[f]),
105 validators: (customField[f] && customField[f].validators) ? customField[f].validators : {}
106 };
107
108 if(form[f].type === 'date'){
109 model[f] = new Date(model[f]);
110 }
111
112 if(form[f].type === 'number'){
113 model[f] = parseInt(model[f], 10);
114 }
115
116 return form;
117 }, {});
118 };
119
120 /**
121 * @ngdoc method
122 * @name xos.uiComponents.XosFormHelpers#parseModelField
123 * @methodOf xos.uiComponents.XosFormHelpers
124 * @description
125 * Helpers for buildFormStructure, convert a list of model properties in an object used to build the form structure, eg:
126 * ```
127 * // input:
128 * ['id', 'name'm 'mail']
129 *
130 * // output
131 * {
132 * id: {},
133 * name: {},
134 * mail: {}
135 * }
136 * ```
137 * @param {array} fields An array of fields representing the model properties
138 * @returns {object} An object containing one property for each field of the model
139 **/
140
141 this.parseModelField = (fields) => {
142 return _.reduce(fields, (form, f) => {
143 form[f] = {};
144 return form;
145 }, {});
146 }
147
148 });
149})();