blob: c0bd206ca7cbe22a70c2878e65c040c230ee04a4 [file] [log] [blame]
Matteo Scandoloee655a12016-12-19 15:38:43 -08001// TODO clean this mess
2
3import * as _ from 'lodash';
4import {IXosFormHelpersService} from './form-helpers';
5import {IXosConfigHelpersService} from '../services/helpers/config.helpers';
6
Matteo Scandolo80c3a652017-01-06 10:48:31 -08007export interface IXosFormAction {
8 label: string;
9 icon: string;
10 class: string;
11 cb(item: any, form: any): void;
12}
13
14export interface IXosFeedback {
15 show: boolean;
16 message: string;
17 type: string; // NOTE is possible to enumerate success, error, warning, info?
Matteo Scandolo6f45e262017-01-09 14:47:26 -080018 autoHide?: number;
19 closeBtn?: boolean;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080020}
21
22export 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 Scandolo07e2f622017-01-09 10:54:13 -080033export interface IXosFormInputOptions {
34 id: number;
35 label: string;
36}
37
Matteo Scandolo80c3a652017-01-06 10:48:31 -080038export interface IXosFormInput {
39 name: string;
40 label: string;
41 type: string; // options are: [date, boolean, number, email, string, select],
Matteo Scandolo6f45e262017-01-09 14:47:26 -080042 hint?: string;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080043 validators: IXosFormInputValidator;
Matteo Scandolo07e2f622017-01-09 10:54:13 -080044 options?: IXosFormInputOptions[];
Matteo Scandolo80c3a652017-01-06 10:48:31 -080045}
46
47export interface IXosFormConfig {
48 exclude?: string[];
49 actions: IXosFormAction[];
50 feedback?: IXosFeedback;
51 inputs: IXosFormInput[];
52 formName: string;
53}
54
Matteo Scandoloee655a12016-12-19 15:38:43 -080055class 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 Scandoloee655a12016-12-19 15:38:43 -080068 }
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 Scandolo80c3a652017-01-06 10:48:31 -080079 if (!this.config.formName) {
80 throw new Error('[xosForm] Please provide a formName property in the config');
81 }
82
Matteo Scandolo6f45e262017-01-09 14:47:26 -080083 // NOTE needed to avoid xosAlert throw an error
Matteo Scandoloee655a12016-12-19 15:38:43 -080084 if (!this.config.feedback) {
85 this.config.feedback = {
86 show: false,
87 message: 'Form submitted successfully !!!',
Matteo Scandolo6f45e262017-01-09 14:47:26 -080088 type: 'success',
89 closeBtn: true
Matteo Scandoloee655a12016-12-19 15:38:43 -080090 };
91 }
92
Matteo Scandolo80c3a652017-01-06 10:48:31 -080093 // remove excluded inputs
94 _.remove(this.config.inputs, i => this.config.exclude.indexOf(i.name) > -1);
Matteo Scandoloee655a12016-12-19 15:38:43 -080095 }
96}
97
98export const xosForm: angular.IComponentOptions = {
99 template: require('./form.html'),
100 controllerAs: 'vm',
101 controller: FormCtrl,
102 bindings: {
103 ngModel: '=',
104 config: '='
105 }
106};