Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 1 | // TODO clean this mess |
| 2 | |
| 3 | import * as _ from 'lodash'; |
| 4 | import {IXosFormHelpersService} from './form-helpers'; |
| 5 | import {IXosConfigHelpersService} from '../services/helpers/config.helpers'; |
| 6 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 7 | export interface IXosFormAction { |
| 8 | label: string; |
| 9 | icon: string; |
| 10 | class: string; |
| 11 | cb(item: any, form: any): void; |
| 12 | } |
| 13 | |
| 14 | export interface IXosFeedback { |
| 15 | show: boolean; |
| 16 | message: string; |
| 17 | type: string; // NOTE is possible to enumerate success, error, warning, info? |
| 18 | } |
| 19 | |
| 20 | export interface IXosFormInputValidator { |
| 21 | minlength?: number; |
| 22 | maxlength?: number; |
| 23 | required?: boolean; |
| 24 | min?: number; |
| 25 | max?: number; |
| 26 | custom?(value: any): any; |
| 27 | // do your validation here and return true | false |
| 28 | // alternatively you can return an array [errorName, true|false] |
| 29 | } |
| 30 | |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 31 | export interface IXosFormInputOptions { |
| 32 | id: number; |
| 33 | label: string; |
| 34 | } |
| 35 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 36 | export interface IXosFormInput { |
| 37 | name: string; |
| 38 | label: string; |
| 39 | type: string; // options are: [date, boolean, number, email, string, select], |
| 40 | validators: IXosFormInputValidator; |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 41 | options?: IXosFormInputOptions[]; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | export interface IXosFormConfig { |
| 45 | exclude?: string[]; |
| 46 | actions: IXosFormAction[]; |
| 47 | feedback?: IXosFeedback; |
| 48 | inputs: IXosFormInput[]; |
| 49 | formName: string; |
| 50 | } |
| 51 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 52 | class FormCtrl { |
| 53 | $inject = ['$onInit', '$scope', 'XosFormHelpers', 'ConfigHelpers']; |
| 54 | |
| 55 | public ngModel: any; |
| 56 | public excludedField: string[]; |
| 57 | public formField: any; |
| 58 | private config: any; |
| 59 | |
| 60 | constructor ( |
| 61 | private $scope: ng.IScope, |
| 62 | private XosFormHelpers: IXosFormHelpersService, |
| 63 | private ConfigHelpers: IXosConfigHelpersService |
| 64 | ) { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | $onInit() { |
| 68 | if (!this.config) { |
| 69 | throw new Error('[xosForm] Please provide a configuration via the "config" attribute'); |
| 70 | } |
| 71 | |
| 72 | if (!this.config.actions) { |
| 73 | throw new Error('[xosForm] Please provide an action list in the configuration'); |
| 74 | } |
| 75 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 76 | if (!this.config.formName) { |
| 77 | throw new Error('[xosForm] Please provide a formName property in the config'); |
| 78 | } |
| 79 | |
| 80 | // NOTE is needed ?? |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 81 | if (!this.config.feedback) { |
| 82 | this.config.feedback = { |
| 83 | show: false, |
| 84 | message: 'Form submitted successfully !!!', |
| 85 | type: 'success' |
| 86 | }; |
| 87 | } |
| 88 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 89 | // remove excluded inputs |
| 90 | _.remove(this.config.inputs, i => this.config.exclude.indexOf(i.name) > -1); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | export const xosForm: angular.IComponentOptions = { |
| 95 | template: require('./form.html'), |
| 96 | controllerAs: 'vm', |
| 97 | controller: FormCtrl, |
| 98 | bindings: { |
| 99 | ngModel: '=', |
| 100 | config: '=' |
| 101 | } |
| 102 | }; |