blob: fa54c6f92a2f500dcf638bea3ca0f45354e75701 [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 Scandolobddf31d2019-05-17 15:54:21 -070075 $inject = ['$onInit', '$onDestroy', '$scope', 'XosFormHelpers', '$log'];
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
Matteo Scandolobddf31d2019-05-17 15:54:21 -070081 private hideFeedback: IXosFeedback = {
82 show: false,
83 message: 'Form submitted successfully !!!',
84 type: 'success',
85 closeBtn: true
86 };
87
Matteo Scandoloee655a12016-12-19 15:38:43 -080088 constructor (
89 private $scope: ng.IScope,
Matteo Scandolobddf31d2019-05-17 15:54:21 -070090 private $log: angular.ILogService,
Matteo Scandolob2225a02017-04-11 15:23:04 -070091 private XosFormHelpers: IXosFormHelpersService
Matteo Scandoloee655a12016-12-19 15:38:43 -080092 ) {
Matteo Scandoloee655a12016-12-19 15:38:43 -080093 }
94
95 $onInit() {
Matteo Scandolobddf31d2019-05-17 15:54:21 -070096 this.$log.debug('[xosForm] Init component');
Matteo Scandoloee655a12016-12-19 15:38:43 -080097 if (!this.config) {
98 throw new Error('[xosForm] Please provide a configuration via the "config" attribute');
99 }
100
101 if (!this.config.actions) {
102 throw new Error('[xosForm] Please provide an action list in the configuration');
103 }
104
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800105 if (!this.config.formName) {
106 throw new Error('[xosForm] Please provide a formName property in the config');
107 }
108
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800109 // NOTE needed to avoid xosAlert throw an error
Matteo Scandoloee655a12016-12-19 15:38:43 -0800110 if (!this.config.feedback) {
Matteo Scandolobddf31d2019-05-17 15:54:21 -0700111 this.config.feedback = this.hideFeedback;
Matteo Scandoloee655a12016-12-19 15:38:43 -0800112 }
113
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800114 // remove excluded inputs
Matteo Scandolob2225a02017-04-11 15:23:04 -0700115 if (angular.isDefined(this.config.exclude) && this.config.exclude.leading > 0) {
116 _.remove(this.config.inputs, i => this.config.exclude.indexOf(i.name) > -1);
117 }
Matteo Scandoloee655a12016-12-19 15:38:43 -0800118 }
Matteo Scandolobddf31d2019-05-17 15:54:21 -0700119
120 $onDestroy() {
121 this.$log.debug('[xosForm] Destroying component');
122 this.config.feedback = this.hideFeedback;
123 }
Matteo Scandoloee655a12016-12-19 15:38:43 -0800124}
125
126export const xosForm: angular.IComponentOptions = {
127 template: require('./form.html'),
128 controllerAs: 'vm',
129 controller: FormCtrl,
130 bindings: {
131 ngModel: '=',
132 config: '='
133 }
134};