Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 1 | import * as _ from 'lodash'; |
| 2 | import {IXosConfigHelpersService} from '../services/helpers/config.helpers'; |
| 3 | |
| 4 | export interface IXosFormHelpersService { |
| 5 | _getFieldFormat(value: any): string; |
| 6 | parseModelField(fields: any): any[]; |
| 7 | buildFormStructure(modelField: any[], customField: any[], model: any, order: string[]): any; |
| 8 | } |
| 9 | |
| 10 | export class XosFormHelpers { |
| 11 | static $inject = ['ConfigHelpers']; |
| 12 | |
| 13 | constructor ( |
| 14 | private ConfigHelpers: IXosConfigHelpersService |
| 15 | ) { |
| 16 | |
| 17 | } |
| 18 | |
| 19 | public _isEmail = (text) => { |
| 20 | 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,}))/; |
| 21 | return re.test(text); |
| 22 | }; |
| 23 | |
| 24 | public _getFieldFormat = (value) => { |
| 25 | if (angular.isArray(value)) { |
| 26 | return 'array'; |
| 27 | } |
| 28 | |
| 29 | // check if is date |
| 30 | if ( |
| 31 | angular.isDate(value) || |
| 32 | ( |
| 33 | !Number.isNaN(Date.parse(value)) && // Date.parse is a number |
| 34 | /^\d+-\d+-\d+\D\d+:\d+:\d+\.\d+\D/.test(value) // the format match ISO dates |
| 35 | )) { |
| 36 | return 'date'; |
| 37 | } |
| 38 | |
| 39 | // check if is boolean |
| 40 | // isNaN(false) = false, false is a number (0), true is a number (1) |
| 41 | if (typeof value === 'boolean') { |
| 42 | return 'boolean'; |
| 43 | } |
| 44 | |
| 45 | // check if a string is an email |
| 46 | if (this._isEmail(value)) { |
| 47 | return 'email'; |
| 48 | } |
| 49 | |
| 50 | // if null return string |
| 51 | if (angular.isString(value) || value === null) { |
| 52 | return 'text'; |
| 53 | } |
| 54 | |
| 55 | return typeof value; |
| 56 | }; |
| 57 | |
| 58 | public buildFormStructure = (modelField, customField, model, order) => { |
| 59 | // TODO take an array as input |
| 60 | // NOTE do we want to support auto-generated forms?? |
| 61 | // We can take that out of this component and autogenerate the config somewhere else |
| 62 | const orderedForm = {}; |
| 63 | |
| 64 | modelField = angular.extend(modelField, customField); |
| 65 | customField = customField || {}; |
| 66 | |
| 67 | if (order) { |
| 68 | _.each(order, function (key: string) { |
| 69 | orderedForm[key] = {}; |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | _.each(Object.keys(modelField), (f) => { |
| 74 | |
| 75 | orderedForm[f] = { |
| 76 | label: (customField[f] && customField[f].label) ? `${customField[f].label}:` : this.ConfigHelpers.toLabel(f), |
| 77 | type: (customField[f] && customField[f].type) ? customField[f].type : this._getFieldFormat(model[f]), |
| 78 | validators: (customField[f] && customField[f].validators) ? customField[f].validators : {}, |
| 79 | hint: (customField[f] && customField[f].hint) ? customField[f].hint : '', |
| 80 | }; |
| 81 | |
| 82 | if (customField[f] && customField[f].options) { |
| 83 | orderedForm[f].options = customField[f].options; |
| 84 | } |
| 85 | if (customField[f] && customField[f].properties) { |
| 86 | orderedForm[f].properties = customField[f].properties; |
| 87 | } |
| 88 | if (orderedForm[f].type === 'date') { |
| 89 | model[f] = new Date(model[f]); |
| 90 | } |
| 91 | |
| 92 | if (orderedForm[f].type === 'number') { |
| 93 | model[f] = parseInt(model[f], 10); |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | return orderedForm; |
| 98 | }; |
| 99 | |
| 100 | public parseModelField = (fields) => { |
| 101 | return _.reduce(fields, (form, f) => { |
| 102 | form[f] = {}; |
| 103 | return form; |
| 104 | }, {}); |
| 105 | } |
| 106 | } |