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