blob: 0a9d3ec0496069f755a884e2eb194c449b761909 [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
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070024
Matteo Scandolo974c0e42016-05-25 16:02:16 -070025 this._isEmail = (text) => {
26 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,}))/;
27 return re.test(text);
28 };
29
30 /**
31 * @ngdoc method
32 * @name xos.uiComponents.XosFormHelpers#_getFieldFormat
33 * @methodOf xos.uiComponents.XosFormHelpers
34 * @description
35 * Return the type of the input
36 * @param {mixed} value The data to be evaluated
37 * @returns {string} The type of the input
38 **/
39
40 this._getFieldFormat = (value) => {
41
42 if(angular.isArray(value)){
43 return 'array';
44 }
45
46 // check if is date
47 if (_.isDate(value) || (!Number.isNaN(Date.parse(value)) && new Date(value).getTime() > 631180800000)){
48 return 'date';
49 }
50
51 // check if is boolean
52 // isNaN(false) = false, false is a number (0), true is a number (1)
53 if(typeof value === 'boolean'){
54 return 'boolean';
55 }
56
Matteo Scandolo974c0e42016-05-25 16:02:16 -070057 // check if a string is an email
58 if(this._isEmail(value)){
59 return 'email';
60 }
61
62 // if null return string
Matteo Scandolob585d382016-06-15 14:53:33 -070063 if(angular.isString(value) || value === null){
Matteo Scandolo03d8b8e2016-05-25 17:37:37 -070064 return 'text';
Matteo Scandolo974c0e42016-05-25 16:02:16 -070065 }
66
67 return typeof value;
68 };
69
70 /**
71 * @ngdoc method
72 * @name xos.uiComponents.XosFormHelpers#buildFormStructure
73 * @methodOf xos.uiComponents.XosFormHelpers
74 * @description
75 * Return the type of the input
76 * @param {object} modelField An object containing one property for each field of the model
77 * @param {object} customField An object containing one property for each field custom field
78 * @param {object} model The actual model on wich build the form structure (it is used to determine the type of the input)
79 * @returns {object} An object describing the form structure in the form of:
80 * ```
81 * {
82 * 'field-name': {
83 * label: 'Label',
84 * type: 'number', //typeof field
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070085 * validators: {}, // see xosForm for more details
86 * hint: 'A Custom hint for the field'
Matteo Scandolo974c0e42016-05-25 16:02:16 -070087 * }
88 * }
89 * ```
90 **/
91
92 this.buildFormStructure = (modelField, customField, model) => {
93
Matteo Scandolo314e7e12016-06-06 15:21:21 -070094 modelField = angular.extend(modelField, customField);
Matteo Scandolo974c0e42016-05-25 16:02:16 -070095 customField = customField || {};
96
97 return _.reduce(Object.keys(modelField), (form, f) => {
98
99 form[f] = {
100 label: (customField[f] && customField[f].label) ? `${customField[f].label}:` : LabelFormatter.format(f),
101 type: (customField[f] && customField[f].type) ? customField[f].type : this._getFieldFormat(model[f]),
Matteo Scandolo5dfcb082016-05-31 15:15:00 -0700102 validators: (customField[f] && customField[f].validators) ? customField[f].validators : {},
arpiagariu47cd6892016-06-03 16:41:39 -0700103 hint: (customField[f] && customField[f].hint)? customField[f].hint : '',
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700104 };
105
arpiagariu47cd6892016-06-03 16:41:39 -0700106 if(customField[f] && customField[f].options){
107 form[f].options = customField[f].options;
108 }
Matteo Scandolodee14362016-06-08 15:11:39 -0700109 if(customField[f] && customField[f].properties){
110 form[f].properties = customField[f].properties;
111 }
Matteo Scandolo974c0e42016-05-25 16:02:16 -0700112 if(form[f].type === 'date'){
113 model[f] = new Date(model[f]);
114 }
115
116 if(form[f].type === 'number'){
117 model[f] = parseInt(model[f], 10);
118 }
119
120 return form;
121 }, {});
122 };
123
124 /**
125 * @ngdoc method
126 * @name xos.uiComponents.XosFormHelpers#parseModelField
127 * @methodOf xos.uiComponents.XosFormHelpers
128 * @description
129 * Helpers for buildFormStructure, convert a list of model properties in an object used to build the form structure, eg:
130 * ```
131 * // input:
132 * ['id', 'name'm 'mail']
133 *
134 * // output
135 * {
136 * id: {},
137 * name: {},
138 * mail: {}
139 * }
140 * ```
141 * @param {array} fields An array of fields representing the model properties
142 * @returns {object} An object containing one property for each field of the model
143 **/
144
145 this.parseModelField = (fields) => {
146 return _.reduce(fields, (form, f) => {
147 form[f] = {};
148 return form;
149 }, {});
150 }
151
152 });
153})();