Fix unable to delete default value for a complex property
[sdc.git] / catalog-ui / src / app / directives / property-types / type-list / type-list-directive.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /**
22  * Created by rcohen on 9/15/2016.
23  */
24 'use strict';
25 import {SchemaProperty} from "app/models";
26 import {ValidationUtils, PROPERTY_TYPES} from "app/utils";
27 import {DataTypesService} from "app/services";
28
29 export interface ITypeListScope extends ng.IScope {
30     parentFormObj:ng.IFormController;
31     schemaProperty:SchemaProperty;
32     isSchemaTypeDataType:boolean;
33     valueObjRef:any;
34     propertyNameValidationPattern:RegExp;
35     fieldsPrefixName:string;
36     readOnly:boolean;
37     listDefaultValue:any;
38     listNewItem:any;
39     maxLength:number;
40     stringSchema: SchemaProperty;
41
42     constraints:string[];
43
44     getValidationPattern(type:string):RegExp;
45     validateIntRange(value:string):boolean;
46     addListItem():void;
47     deleteListItem(listItemIndex:number):void;
48     getStringSchemaProperty():SchemaProperty;
49 }
50
51
52 export class TypeListDirective implements ng.IDirective {
53
54     private readonly stringSchema: SchemaProperty;
55
56     constructor(private DataTypesService:DataTypesService,
57                 private PropertyNameValidationPattern:RegExp,
58                 private ValidationUtils:ValidationUtils) {
59         this.stringSchema = new SchemaProperty();
60         this.stringSchema.type = PROPERTY_TYPES.STRING;
61         this.stringSchema.isSimpleType = true;
62         this.stringSchema.isDataType = false;
63     }
64
65     scope = {
66         valueObjRef: '=',//ref to list object in the parent value object
67         schemaProperty: '=',//get the schema.property object
68         parentFormObj: '=',//ref to parent form (get angular form object)
69         fieldsPrefixName: '=',//prefix for form fields names
70         readOnly: '=',//is form read only
71         defaultValue: '@',//this list default value
72         maxLength: '=',
73         constraints: '='
74     };
75
76     restrict = 'E';
77     replace = true;
78     template = ():string => {
79         return require('./type-list-directive.html');
80     };
81
82     link = (scope:ITypeListScope, element:any, $attr:any) => {
83         scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
84         scope.stringSchema = this.stringSchema;
85
86         //reset valueObjRef when schema type is changed
87         scope.$watchCollection('schemaProperty.type', (newData:any):void => {
88             scope.isSchemaTypeDataType = this.DataTypesService.isDataTypeForSchemaType(scope.schemaProperty);
89         });
90
91         //when user brows between properties in "edit property form"
92         scope.$watchCollection('fieldsPrefixName', (newData:any):void => {
93             scope.listNewItem = {value: ''};
94
95             if ($attr.defaultValue) {
96                 scope.listDefaultValue = JSON.parse($attr.defaultValue);
97             }
98         });
99
100         scope.getValidationPattern = (type:string):RegExp => {
101             return this.ValidationUtils.getValidationPattern(type);
102         };
103
104         scope.validateIntRange = (value:string):boolean => {
105             return !value || this.ValidationUtils.validateIntRange(value);
106         };
107
108         scope.addListItem = ():void => {
109             scope.valueObjRef = scope.valueObjRef || [];
110             let newVal;
111             if (scope.schemaProperty.type === PROPERTY_TYPES.MAP) {
112                 newVal = {"": ""};
113             } else if ((scope.schemaProperty.simpleType || scope.schemaProperty.type) == PROPERTY_TYPES.STRING) {
114                 newVal = scope.listNewItem.value;
115             } else {
116                 newVal = JSON.parse(scope.listNewItem.value);
117             }
118             scope.valueObjRef.push(newVal);
119             scope.listNewItem.value = "";
120         };
121
122         scope.deleteListItem = (listItemIndex: number): void => {
123             scope.valueObjRef.splice(listItemIndex, 1);
124             if (!scope.valueObjRef.length && scope.listDefaultValue) {
125                 angular.copy(scope.listDefaultValue, scope.valueObjRef);
126             }
127         };
128     };
129
130     public static factory = (DataTypesService:DataTypesService,
131                              PropertyNameValidationPattern:RegExp,
132                              ValidationUtils:ValidationUtils)=> {
133         return new TypeListDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
134     };
135 }
136
137 TypeListDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];
138