blob: 273e9b1c039307ef1c2436d99ebe8f794b6f95e4 [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';
Matteo Scandoloee655a12016-12-19 15:38:43 -08005
Matteo Scandolo80c3a652017-01-06 10:48:31 -08006export interface IXosFormAction {
7 label: string;
8 icon: string;
9 class: string;
10 cb(item: any, form: any): void;
11}
12
13export interface IXosFeedback {
14 show: boolean;
15 message: string;
16 type: string; // NOTE is possible to enumerate success, error, warning, info?
Matteo Scandolo6f45e262017-01-09 14:47:26 -080017 autoHide?: number;
18 closeBtn?: boolean;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080019}
20
21export interface IXosFormInputValidator {
22 minlength?: number;
23 maxlength?: number;
24 required?: boolean;
25 min?: number;
26 max?: number;
27 custom?(value: any): any;
28 // do your validation here and return true | false
29 // alternatively you can return an array [errorName, true|false]
30}
31
Matteo Scandolo07e2f622017-01-09 10:54:13 -080032export interface IXosFormInputOptions {
33 id: number;
34 label: string;
35}
36
Matteo Scandolo80c3a652017-01-06 10:48:31 -080037export interface IXosFormInput {
38 name: string;
39 label: string;
40 type: string; // options are: [date, boolean, number, email, string, select],
Matteo Scandolo6f45e262017-01-09 14:47:26 -080041 hint?: string;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080042 validators: IXosFormInputValidator;
Matteo Scandolo07e2f622017-01-09 10:54:13 -080043 options?: IXosFormInputOptions[];
Matteo Scandoloe7e052d2017-07-31 19:54:31 -070044 default?: any | null;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080045}
46
Matteo Scandolo1aee1982017-02-17 08:33:23 -080047export interface IXosFormCfg {
Matteo Scandolo80c3a652017-01-06 10:48:31 -080048 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 {
Matteo Scandolob2225a02017-04-11 15:23:04 -070056 $inject = ['$onInit', '$scope', 'XosFormHelpers'];
Matteo Scandoloee655a12016-12-19 15:38:43 -080057
58 public ngModel: any;
Matteo Scandoloee655a12016-12-19 15:38:43 -080059 public formField: any;
60 private config: any;
61
62 constructor (
63 private $scope: ng.IScope,
Matteo Scandolob2225a02017-04-11 15:23:04 -070064 private XosFormHelpers: IXosFormHelpersService
Matteo Scandoloee655a12016-12-19 15:38:43 -080065 ) {
Matteo Scandoloee655a12016-12-19 15:38:43 -080066 }
67
68 $onInit() {
69 if (!this.config) {
70 throw new Error('[xosForm] Please provide a configuration via the "config" attribute');
71 }
72
73 if (!this.config.actions) {
74 throw new Error('[xosForm] Please provide an action list in the configuration');
75 }
76
Matteo Scandolo80c3a652017-01-06 10:48:31 -080077 if (!this.config.formName) {
78 throw new Error('[xosForm] Please provide a formName property in the config');
79 }
80
Matteo Scandolo6f45e262017-01-09 14:47:26 -080081 // NOTE needed to avoid xosAlert throw an error
Matteo Scandoloee655a12016-12-19 15:38:43 -080082 if (!this.config.feedback) {
83 this.config.feedback = {
84 show: false,
85 message: 'Form submitted successfully !!!',
Matteo Scandolo6f45e262017-01-09 14:47:26 -080086 type: 'success',
87 closeBtn: true
Matteo Scandoloee655a12016-12-19 15:38:43 -080088 };
89 }
90
Matteo Scandolo80c3a652017-01-06 10:48:31 -080091 // remove excluded inputs
Matteo Scandolob2225a02017-04-11 15:23:04 -070092 if (angular.isDefined(this.config.exclude) && this.config.exclude.leading > 0) {
93 _.remove(this.config.inputs, i => this.config.exclude.indexOf(i.name) > -1);
94 }
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};