blob: 331cdb063a891ad4c8d03182f0607fd9e2111e45 [file] [log] [blame]
Matteo Scandoloa5d03d52016-07-21 11:35:46 -07001(function () {
Steven Burrows84818482016-09-29 15:33:46 -07002
Matteo Scandoloa5d03d52016-07-21 11:35:46 -07003 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
Steven Burrows84818482016-09-29 15:33:46 -070096 this.buildFormStructure = (modelField, customField, model, order) => {
97
98 const orderedForm = {};
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070099
100 modelField = angular.extend(modelField, customField);
101 customField = customField || {};
102
Steven Burrows84818482016-09-29 15:33:46 -0700103 if (order) {
104 _.each(order, function (key) {
105 orderedForm[key] = {};
106 });
107 }
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700108
Steven Burrows84818482016-09-29 15:33:46 -0700109 _.each(Object.keys(modelField), (f) => {
110
111 orderedForm[f] = {
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700112 label: (customField[f] && customField[f].label) ? `${customField[f].label}:` : LabelFormatter.format(f),
113 type: (customField[f] && customField[f].type) ? customField[f].type : this._getFieldFormat(model[f]),
114 validators: (customField[f] && customField[f].validators) ? customField[f].validators : {},
115 hint: (customField[f] && customField[f].hint)? customField[f].hint : '',
116 };
117
118 if(customField[f] && customField[f].options){
Steven Burrows84818482016-09-29 15:33:46 -0700119 orderedForm[f].options = customField[f].options;
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700120 }
121 if(customField[f] && customField[f].properties){
Steven Burrows84818482016-09-29 15:33:46 -0700122 orderedForm[f].properties = customField[f].properties;
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700123 }
Steven Burrows84818482016-09-29 15:33:46 -0700124 if(orderedForm[f].type === 'date'){
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700125 model[f] = new Date(model[f]);
126 }
127
Steven Burrows84818482016-09-29 15:33:46 -0700128 if(orderedForm[f].type === 'number'){
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700129 model[f] = parseInt(model[f], 10);
130 }
Steven Burrows84818482016-09-29 15:33:46 -0700131 });
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700132
Steven Burrows84818482016-09-29 15:33:46 -0700133 return orderedForm;
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700134 };
135
136 /**
137 * @ngdoc method
138 * @name xos.uiComponents.XosFormHelpers#parseModelField
139 * @methodOf xos.uiComponents.XosFormHelpers
140 * @description
141 * Helpers for buildFormStructure, convert a list of model properties in an object used to build the form structure, eg:
142 * ```
143 * // input:
144 * ['id', 'name'm 'mail']
145 *
146 * // output
147 * {
148 * id: {},
149 * name: {},
150 * mail: {}
151 * }
152 * ```
153 * @param {array} fields An array of fields representing the model properties
154 * @returns {object} An object containing one property for each field of the model
155 **/
156
157 this.parseModelField = (fields) => {
158 return _.reduce(fields, (form, f) => {
159 form[f] = {};
160 return form;
161 }, {});
162 }
163
164 });
Steven Burrows84818482016-09-29 15:33:46 -0700165})();