blob: 34b02873d546864b98f6f621230b86de6c501358 [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?
18}
19
20export 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 Scandolo07e2f622017-01-09 10:54:13 -080031export interface IXosFormInputOptions {
32 id: number;
33 label: string;
34}
35
Matteo Scandolo80c3a652017-01-06 10:48:31 -080036export interface IXosFormInput {
37 name: string;
38 label: string;
39 type: string; // options are: [date, boolean, number, email, string, select],
40 validators: IXosFormInputValidator;
Matteo Scandolo07e2f622017-01-09 10:54:13 -080041 options?: IXosFormInputOptions[];
Matteo Scandolo80c3a652017-01-06 10:48:31 -080042}
43
44export interface IXosFormConfig {
45 exclude?: string[];
46 actions: IXosFormAction[];
47 feedback?: IXosFeedback;
48 inputs: IXosFormInput[];
49 formName: string;
50}
51
Matteo Scandoloee655a12016-12-19 15:38:43 -080052class 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 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
80 // NOTE is needed ??
Matteo Scandoloee655a12016-12-19 15:38:43 -080081 if (!this.config.feedback) {
82 this.config.feedback = {
83 show: false,
84 message: 'Form submitted successfully !!!',
85 type: 'success'
86 };
87 }
88
Matteo Scandolo80c3a652017-01-06 10:48:31 -080089 // remove excluded inputs
90 _.remove(this.config.inputs, i => this.config.exclude.indexOf(i.name) > -1);
Matteo Scandoloee655a12016-12-19 15:38:43 -080091 }
92}
93
94export const xosForm: angular.IComponentOptions = {
95 template: require('./form.html'),
96 controllerAs: 'vm',
97 controller: FormCtrl,
98 bindings: {
99 ngModel: '=',
100 config: '='
101 }
102};