blob: 28d4a18317225684eadf350dfd18b6f9290d0c16 [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
Matteo Scandoloe57712f2016-09-21 15:27:36 -070046 if (
47 angular.isDate(value) ||
48 (
49 !Number.isNaN(Date.parse(value)) && // Date.parse is a number
50 /^\d+-\d+-\d+\D\d+:\d+:\d+\.\d+\D/.test(value) // the format match ISO dates
51 )){
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070052 return 'date';
53 }
54
55 // check if is boolean
56 // isNaN(false) = false, false is a number (0), true is a number (1)
57 if(typeof value === 'boolean'){
58 return 'boolean';
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(angular.isString(value) || value === null){
68 return 'text';
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 * hint: 'A Custom hint for the field'
91 * }
92 * }
93 * ```
94 **/
95
96 this.buildFormStructure = (modelField, customField, model) => {
97
98 modelField = angular.extend(modelField, customField);
99 customField = customField || {};
100
101 return _.reduce(Object.keys(modelField), (form, f) => {
102
103 form[f] = {
104 label: (customField[f] && customField[f].label) ? `${customField[f].label}:` : LabelFormatter.format(f),
105 type: (customField[f] && customField[f].type) ? customField[f].type : this._getFieldFormat(model[f]),
106 validators: (customField[f] && customField[f].validators) ? customField[f].validators : {},
107 hint: (customField[f] && customField[f].hint)? customField[f].hint : '',
108 };
109
110 if(customField[f] && customField[f].options){
111 form[f].options = customField[f].options;
112 }
113 if(customField[f] && customField[f].properties){
114 form[f].properties = customField[f].properties;
115 }
116 if(form[f].type === 'date'){
117 model[f] = new Date(model[f]);
118 }
119
120 if(form[f].type === 'number'){
121 model[f] = parseInt(model[f], 10);
122 }
123
124 return form;
125 }, {});
126 };
127
128 /**
129 * @ngdoc method
130 * @name xos.uiComponents.XosFormHelpers#parseModelField
131 * @methodOf xos.uiComponents.XosFormHelpers
132 * @description
133 * Helpers for buildFormStructure, convert a list of model properties in an object used to build the form structure, eg:
134 * ```
135 * // input:
136 * ['id', 'name'm 'mail']
137 *
138 * // output
139 * {
140 * id: {},
141 * name: {},
142 * mail: {}
143 * }
144 * ```
145 * @param {array} fields An array of fields representing the model properties
146 * @returns {object} An object containing one property for each field of the model
147 **/
148
149 this.parseModelField = (fields) => {
150 return _.reduce(fields, (form, f) => {
151 form[f] = {};
152 return form;
153 }, {});
154 }
155
156 });
157})();