blob: 92f49cacbf7741dfbbd50abb9dd0198368dcbbb9 [file] [log] [blame]
Matteo Scandolo686547a2017-08-08 13:05:25 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070019(function () {
Steven Burrows84818482016-09-29 15:33:46 -070020
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070021 angular.module('xos.uiComponents')
22
23 /**
24 * @ngdoc service
25 * @name xos.uiComponents.XosFormHelpers
26 * @requires xos.uiComponents.LabelFormatter
27 * @requires xos.helpers._
28 **/
29
30 .service('XosFormHelpers', function(_, LabelFormatter){
31
32 /**
33 * @ngdoc method
34 * @name xos.uiComponents.XosFormHelpers#_isEmail
35 * @methodOf xos.uiComponents.XosFormHelpers
36 * @description
37 * Return true if the string is an email address
38 * @param {string} text The string to be evaluated
39 * @returns {boolean} If the string match an email format
40 **/
41
42 this._isEmail = (text) => {
43 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,}))/;
44 return re.test(text);
45 };
46
47 /**
48 * @ngdoc method
49 * @name xos.uiComponents.XosFormHelpers#_getFieldFormat
50 * @methodOf xos.uiComponents.XosFormHelpers
51 * @description
52 * Return the type of the input
53 * @param {mixed} value The data to be evaluated
54 * @returns {string} The type of the input
55 **/
56
57 this._getFieldFormat = (value) => {
58
59 if(angular.isArray(value)){
60 return 'array';
61 }
62
63 // check if is date
Matteo Scandoloe57712f2016-09-21 15:27:36 -070064 if (
65 angular.isDate(value) ||
66 (
67 !Number.isNaN(Date.parse(value)) && // Date.parse is a number
68 /^\d+-\d+-\d+\D\d+:\d+:\d+\.\d+\D/.test(value) // the format match ISO dates
69 )){
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070070 return 'date';
71 }
72
73 // check if is boolean
74 // isNaN(false) = false, false is a number (0), true is a number (1)
75 if(typeof value === 'boolean'){
76 return 'boolean';
77 }
78
79 // check if a string is an email
80 if(this._isEmail(value)){
81 return 'email';
82 }
83
84 // if null return string
85 if(angular.isString(value) || value === null){
86 return 'text';
87 }
88
89 return typeof value;
90 };
91
92 /**
93 * @ngdoc method
94 * @name xos.uiComponents.XosFormHelpers#buildFormStructure
95 * @methodOf xos.uiComponents.XosFormHelpers
96 * @description
97 * Return the type of the input
98 * @param {object} modelField An object containing one property for each field of the model
99 * @param {object} customField An object containing one property for each field custom field
100 * @param {object} model The actual model on wich build the form structure (it is used to determine the type of the input)
101 * @returns {object} An object describing the form structure in the form of:
102 * ```
103 * {
104 * 'field-name': {
105 * label: 'Label',
106 * type: 'number', //typeof field
107 * validators: {}, // see xosForm for more details
108 * hint: 'A Custom hint for the field'
109 * }
110 * }
111 * ```
112 **/
113
Steven Burrows84818482016-09-29 15:33:46 -0700114 this.buildFormStructure = (modelField, customField, model, order) => {
Steven Burrows84818482016-09-29 15:33:46 -0700115 const orderedForm = {};
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700116
117 modelField = angular.extend(modelField, customField);
118 customField = customField || {};
119
Steven Burrows84818482016-09-29 15:33:46 -0700120 if (order) {
121 _.each(order, function (key) {
122 orderedForm[key] = {};
123 });
124 }
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700125
Steven Burrows84818482016-09-29 15:33:46 -0700126 _.each(Object.keys(modelField), (f) => {
127
128 orderedForm[f] = {
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700129 label: (customField[f] && customField[f].label) ? `${customField[f].label}:` : LabelFormatter.format(f),
130 type: (customField[f] && customField[f].type) ? customField[f].type : this._getFieldFormat(model[f]),
131 validators: (customField[f] && customField[f].validators) ? customField[f].validators : {},
132 hint: (customField[f] && customField[f].hint)? customField[f].hint : '',
133 };
134
135 if(customField[f] && customField[f].options){
Steven Burrows84818482016-09-29 15:33:46 -0700136 orderedForm[f].options = customField[f].options;
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700137 }
138 if(customField[f] && customField[f].properties){
Steven Burrows84818482016-09-29 15:33:46 -0700139 orderedForm[f].properties = customField[f].properties;
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700140 }
Steven Burrows84818482016-09-29 15:33:46 -0700141 if(orderedForm[f].type === 'date'){
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700142 model[f] = new Date(model[f]);
143 }
144
Steven Burrows84818482016-09-29 15:33:46 -0700145 if(orderedForm[f].type === 'number'){
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700146 model[f] = parseInt(model[f], 10);
147 }
Steven Burrows84818482016-09-29 15:33:46 -0700148 });
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700149
Steven Burrows84818482016-09-29 15:33:46 -0700150 return orderedForm;
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700151 };
152
153 /**
154 * @ngdoc method
155 * @name xos.uiComponents.XosFormHelpers#parseModelField
156 * @methodOf xos.uiComponents.XosFormHelpers
157 * @description
158 * Helpers for buildFormStructure, convert a list of model properties in an object used to build the form structure, eg:
159 * ```
160 * // input:
161 * ['id', 'name'm 'mail']
162 *
163 * // output
164 * {
165 * id: {},
166 * name: {},
167 * mail: {}
168 * }
169 * ```
170 * @param {array} fields An array of fields representing the model properties
171 * @returns {object} An object containing one property for each field of the model
172 **/
173
174 this.parseModelField = (fields) => {
175 return _.reduce(fields, (form, f) => {
176 form[f] = {};
177 return form;
178 }, {});
179 }
180
181 });
Steven Burrows84818482016-09-29 15:33:46 -0700182})();