blob: 582cb8af5ff6002a5b95cc68e543ba330b553ef7 [file] [log] [blame]
Matteo Scandoloa5d03d52016-07-21 11:35:46 -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 const 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 an email
57 if(this._isEmail(value)){
58 return 'email';
59 }
60
61 // if null return string
62 if(angular.isString(value) || value === null){
63 return 'text';
64 }
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 * hint: 'A Custom hint for the field'
86 * }
87 * }
88 * ```
89 **/
90
91 this.buildFormStructure = (modelField, customField, model) => {
92
93 modelField = angular.extend(modelField, customField);
94 customField = customField || {};
95
96 return _.reduce(Object.keys(modelField), (form, f) => {
97
98 form[f] = {
99 label: (customField[f] && customField[f].label) ? `${customField[f].label}:` : LabelFormatter.format(f),
100 type: (customField[f] && customField[f].type) ? customField[f].type : this._getFieldFormat(model[f]),
101 validators: (customField[f] && customField[f].validators) ? customField[f].validators : {},
102 hint: (customField[f] && customField[f].hint)? customField[f].hint : '',
103 };
104
105 if(customField[f] && customField[f].options){
106 form[f].options = customField[f].options;
107 }
108 if(customField[f] && customField[f].properties){
109 form[f].properties = customField[f].properties;
110 }
111 if(form[f].type === 'date'){
112 model[f] = new Date(model[f]);
113 }
114
115 if(form[f].type === 'number'){
116 model[f] = parseInt(model[f], 10);
117 }
118
119 return form;
120 }, {});
121 };
122
123 /**
124 * @ngdoc method
125 * @name xos.uiComponents.XosFormHelpers#parseModelField
126 * @methodOf xos.uiComponents.XosFormHelpers
127 * @description
128 * Helpers for buildFormStructure, convert a list of model properties in an object used to build the form structure, eg:
129 * ```
130 * // input:
131 * ['id', 'name'm 'mail']
132 *
133 * // output
134 * {
135 * id: {},
136 * name: {},
137 * mail: {}
138 * }
139 * ```
140 * @param {array} fields An array of fields representing the model properties
141 * @returns {object} An object containing one property for each field of the model
142 **/
143
144 this.parseModelField = (fields) => {
145 return _.reduce(fields, (form, f) => {
146 form[f] = {};
147 return form;
148 }, {});
149 }
150
151 });
152})();