Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 19 | // TODO clean this mess |
| 20 | |
| 21 | import * as _ from 'lodash'; |
| 22 | import {IXosFormHelpersService} from './form-helpers'; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 23 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 24 | export interface IXosFormAction { |
| 25 | label: string; |
| 26 | icon: string; |
| 27 | class: string; |
| 28 | cb(item: any, form: any): void; |
| 29 | } |
| 30 | |
| 31 | export interface IXosFeedback { |
| 32 | show: boolean; |
| 33 | message: string; |
| 34 | type: string; // NOTE is possible to enumerate success, error, warning, info? |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 35 | autoHide?: number; |
| 36 | closeBtn?: boolean; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | export 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 Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 50 | export interface IXosFormInputOptions { |
| 51 | id: number; |
| 52 | label: string; |
| 53 | } |
| 54 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 55 | export interface IXosFormInput { |
| 56 | name: string; |
| 57 | label: string; |
| 58 | type: string; // options are: [date, boolean, number, email, string, select], |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame] | 59 | read_only: boolean; |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 60 | hint?: string; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 61 | validators: IXosFormInputValidator; |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 62 | options?: IXosFormInputOptions[]; |
Matteo Scandolo | e7e052d | 2017-07-31 19:54:31 -0700 | [diff] [blame] | 63 | default?: any | null; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 66 | export interface IXosFormCfg { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 67 | exclude?: string[]; |
| 68 | actions: IXosFormAction[]; |
| 69 | feedback?: IXosFeedback; |
| 70 | inputs: IXosFormInput[]; |
| 71 | formName: string; |
| 72 | } |
| 73 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 74 | class FormCtrl { |
Matteo Scandolo | bddf31d | 2019-05-17 15:54:21 -0700 | [diff] [blame] | 75 | $inject = ['$onInit', '$onDestroy', '$scope', 'XosFormHelpers', '$log']; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 76 | |
| 77 | public ngModel: any; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 78 | public formField: any; |
| 79 | private config: any; |
| 80 | |
Matteo Scandolo | bddf31d | 2019-05-17 15:54:21 -0700 | [diff] [blame] | 81 | private hideFeedback: IXosFeedback = { |
| 82 | show: false, |
| 83 | message: 'Form submitted successfully !!!', |
| 84 | type: 'success', |
| 85 | closeBtn: true |
| 86 | }; |
| 87 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 88 | constructor ( |
| 89 | private $scope: ng.IScope, |
Matteo Scandolo | bddf31d | 2019-05-17 15:54:21 -0700 | [diff] [blame] | 90 | private $log: angular.ILogService, |
Matteo Scandolo | b2225a0 | 2017-04-11 15:23:04 -0700 | [diff] [blame] | 91 | private XosFormHelpers: IXosFormHelpersService |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 92 | ) { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | $onInit() { |
Matteo Scandolo | bddf31d | 2019-05-17 15:54:21 -0700 | [diff] [blame] | 96 | this.$log.debug('[xosForm] Init component'); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 97 | 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 Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 105 | if (!this.config.formName) { |
| 106 | throw new Error('[xosForm] Please provide a formName property in the config'); |
| 107 | } |
| 108 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 109 | // NOTE needed to avoid xosAlert throw an error |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 110 | if (!this.config.feedback) { |
Matteo Scandolo | bddf31d | 2019-05-17 15:54:21 -0700 | [diff] [blame] | 111 | this.config.feedback = this.hideFeedback; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 114 | // remove excluded inputs |
Matteo Scandolo | b2225a0 | 2017-04-11 15:23:04 -0700 | [diff] [blame] | 115 | 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 Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 118 | } |
Matteo Scandolo | bddf31d | 2019-05-17 15:54:21 -0700 | [diff] [blame] | 119 | |
| 120 | $onDestroy() { |
| 121 | this.$log.debug('[xosForm] Destroying component'); |
| 122 | this.config.feedback = this.hideFeedback; |
| 123 | } |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | export const xosForm: angular.IComponentOptions = { |
| 127 | template: require('./form.html'), |
| 128 | controllerAs: 'vm', |
| 129 | controller: FormCtrl, |
| 130 | bindings: { |
| 131 | ngModel: '=', |
| 132 | config: '=' |
| 133 | } |
| 134 | }; |