blob: 1c01e311975d3ffc58a78af4448e64eb877f2d0a [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 Scandolo80c3a652017-01-06 10:48:31 -080044}
45
Matteo Scandolo1aee1982017-02-17 08:33:23 -080046export interface IXosFormCfg {
Matteo Scandolo80c3a652017-01-06 10:48:31 -080047 exclude?: string[];
48 actions: IXosFormAction[];
49 feedback?: IXosFeedback;
50 inputs: IXosFormInput[];
51 formName: string;
52}
53
Matteo Scandoloee655a12016-12-19 15:38:43 -080054class FormCtrl {
Matteo Scandolob2225a02017-04-11 15:23:04 -070055 $inject = ['$onInit', '$scope', 'XosFormHelpers'];
Matteo Scandoloee655a12016-12-19 15:38:43 -080056
57 public ngModel: any;
Matteo Scandoloee655a12016-12-19 15:38:43 -080058 public formField: any;
59 private config: any;
60
61 constructor (
62 private $scope: ng.IScope,
Matteo Scandolob2225a02017-04-11 15:23:04 -070063 private XosFormHelpers: IXosFormHelpersService
Matteo Scandoloee655a12016-12-19 15:38:43 -080064 ) {
Matteo Scandoloee655a12016-12-19 15:38:43 -080065 }
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 Scandolo80c3a652017-01-06 10:48:31 -080076 if (!this.config.formName) {
77 throw new Error('[xosForm] Please provide a formName property in the config');
78 }
79
Matteo Scandolo6f45e262017-01-09 14:47:26 -080080 // NOTE needed to avoid xosAlert throw an error
Matteo Scandoloee655a12016-12-19 15:38:43 -080081 if (!this.config.feedback) {
82 this.config.feedback = {
83 show: false,
84 message: 'Form submitted successfully !!!',
Matteo Scandolo6f45e262017-01-09 14:47:26 -080085 type: 'success',
86 closeBtn: true
Matteo Scandoloee655a12016-12-19 15:38:43 -080087 };
88 }
89
Matteo Scandolo80c3a652017-01-06 10:48:31 -080090 // remove excluded inputs
Matteo Scandolob2225a02017-04-11 15:23:04 -070091 if (angular.isDefined(this.config.exclude) && this.config.exclude.leading > 0) {
92 _.remove(this.config.inputs, i => this.config.exclude.indexOf(i.name) > -1);
93 }
Matteo Scandoloee655a12016-12-19 15:38:43 -080094 }
95}
96
97export const xosForm: angular.IComponentOptions = {
98 template: require('./form.html'),
99 controllerAs: 'vm',
100 controller: FormCtrl,
101 bindings: {
102 ngModel: '=',
103 config: '='
104 }
105};