blob: 1c10c33737e669bcd79a885f4f180e7f9f1d0ee9 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloee655a12016-12-19 15:38:43 -080019// TODO clean this mess
20
21import * as _ from 'lodash';
22import {IXosFormHelpersService} from './form-helpers';
Matteo Scandoloee655a12016-12-19 15:38:43 -080023
Matteo Scandolo80c3a652017-01-06 10:48:31 -080024export interface IXosFormAction {
25 label: string;
26 icon: string;
27 class: string;
28 cb(item: any, form: any): void;
29}
30
31export interface IXosFeedback {
32 show: boolean;
33 message: string;
34 type: string; // NOTE is possible to enumerate success, error, warning, info?
Matteo Scandolo6f45e262017-01-09 14:47:26 -080035 autoHide?: number;
36 closeBtn?: boolean;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080037}
38
39export interface IXosFormInputValidator {
40 minlength?: number;
41 maxlength?: number;
42 required?: boolean;
43 min?: number;
44 max?: number;
45 custom?(value: any): any;
46 // do your validation here and return true | false
47 // alternatively you can return an array [errorName, true|false]
48}
49
Matteo Scandolo07e2f622017-01-09 10:54:13 -080050export interface IXosFormInputOptions {
51 id: number;
52 label: string;
53}
54
Matteo Scandolo80c3a652017-01-06 10:48:31 -080055export interface IXosFormInput {
56 name: string;
57 label: string;
58 type: string; // options are: [date, boolean, number, email, string, select],
Matteo Scandolod67adee2018-03-08 16:27:05 -080059 read_only: boolean;
Matteo Scandolo6f45e262017-01-09 14:47:26 -080060 hint?: string;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080061 validators: IXosFormInputValidator;
Matteo Scandolo07e2f622017-01-09 10:54:13 -080062 options?: IXosFormInputOptions[];
Matteo Scandoloe7e052d2017-07-31 19:54:31 -070063 default?: any | null;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080064}
65
Matteo Scandolo1aee1982017-02-17 08:33:23 -080066export interface IXosFormCfg {
Matteo Scandolo80c3a652017-01-06 10:48:31 -080067 exclude?: string[];
68 actions: IXosFormAction[];
69 feedback?: IXosFeedback;
70 inputs: IXosFormInput[];
71 formName: string;
72}
73
Matteo Scandoloee655a12016-12-19 15:38:43 -080074class FormCtrl {
Matteo Scandolob2225a02017-04-11 15:23:04 -070075 $inject = ['$onInit', '$scope', 'XosFormHelpers'];
Matteo Scandoloee655a12016-12-19 15:38:43 -080076
77 public ngModel: any;
Matteo Scandoloee655a12016-12-19 15:38:43 -080078 public formField: any;
79 private config: any;
80
81 constructor (
82 private $scope: ng.IScope,
Matteo Scandolob2225a02017-04-11 15:23:04 -070083 private XosFormHelpers: IXosFormHelpersService
Matteo Scandoloee655a12016-12-19 15:38:43 -080084 ) {
Matteo Scandoloee655a12016-12-19 15:38:43 -080085 }
86
87 $onInit() {
88 if (!this.config) {
89 throw new Error('[xosForm] Please provide a configuration via the "config" attribute');
90 }
91
92 if (!this.config.actions) {
93 throw new Error('[xosForm] Please provide an action list in the configuration');
94 }
95
Matteo Scandolo80c3a652017-01-06 10:48:31 -080096 if (!this.config.formName) {
97 throw new Error('[xosForm] Please provide a formName property in the config');
98 }
99
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800100 // NOTE needed to avoid xosAlert throw an error
Matteo Scandoloee655a12016-12-19 15:38:43 -0800101 if (!this.config.feedback) {
102 this.config.feedback = {
103 show: false,
104 message: 'Form submitted successfully !!!',
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800105 type: 'success',
106 closeBtn: true
Matteo Scandoloee655a12016-12-19 15:38:43 -0800107 };
108 }
109
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800110 // remove excluded inputs
Matteo Scandolob2225a02017-04-11 15:23:04 -0700111 if (angular.isDefined(this.config.exclude) && this.config.exclude.leading > 0) {
112 _.remove(this.config.inputs, i => this.config.exclude.indexOf(i.name) > -1);
113 }
Matteo Scandoloee655a12016-12-19 15:38:43 -0800114 }
115}
116
117export const xosForm: angular.IComponentOptions = {
118 template: require('./form.html'),
119 controllerAs: 'vm',
120 controller: FormCtrl,
121 bindings: {
122 ngModel: '=',
123 config: '='
124 }
125};