Catalog alignment
[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     
41     constraints:string[];
42
43     getValidationPattern(type:string):RegExp;
44     validateIntRange(value:string):boolean;
45     addListItem():void;
46     deleteListItem(listItemIndex:number):void
47 }
48
49
50 export class TypeListDirective implements ng.IDirective {
51
52     constructor(private DataTypesService:DataTypesService,
53                 private PropertyNameValidationPattern:RegExp,
54                 private ValidationUtils:ValidationUtils) {  
55     }
56
57     scope = {
58         valueObjRef: '=',//ref to list object in the parent value object
59         schemaProperty: '=',//get the schema.property object
60         parentFormObj: '=',//ref to parent form (get angular form object)
61         fieldsPrefixName: '=',//prefix for form fields names
62         readOnly: '=',//is form read only
63         defaultValue: '@',//this list default value
64         maxLength: '=',
65         constraints: '='
66     };
67
68     restrict = 'E';
69     replace = true;
70     template = ():string => {
71         return require('./type-list-directive.html');
72     };
73
74     link = (scope:ITypeListScope, element:any, $attr:any) => {
75         scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
76
77         //reset valueObjRef when schema type is changed
78         scope.$watchCollection('schemaProperty.type', (newData:any):void => {
79             scope.isSchemaTypeDataType = this.DataTypesService.isDataTypeForSchemaType(scope.schemaProperty);
80             //insert 1 empty item dt by default
81             if (scope.isSchemaTypeDataType && (!scope.valueObjRef || !scope.valueObjRef.length)) {
82                 scope.valueObjRef = scope.valueObjRef || [];
83                 scope.valueObjRef.push({});
84             }
85         });
86
87         //when user brows between properties in "edit property form"
88         scope.$watchCollection('fieldsPrefixName', (newData:any):void => {
89             scope.listNewItem = {value: ''};
90
91             if ($attr.defaultValue) {
92                 scope.listDefaultValue = JSON.parse($attr.defaultValue);
93             }
94         });
95
96         scope.getValidationPattern = (type:string):RegExp => {
97             return this.ValidationUtils.getValidationPattern(type);
98         };
99
100         scope.validateIntRange = (value:string):boolean => {
101             return !value || this.ValidationUtils.validateIntRange(value);
102         };
103
104         scope.addListItem = ():void => {
105             scope.valueObjRef = scope.valueObjRef || [];
106             let newVal = ((scope.schemaProperty.simpleType || scope.schemaProperty.type) == PROPERTY_TYPES.STRING ? scope.listNewItem.value : JSON.parse(scope.listNewItem.value));
107             scope.valueObjRef.push(newVal);
108             scope.listNewItem.value = "";
109         };
110
111         scope.deleteListItem = (listItemIndex:number):void => {
112             scope.valueObjRef.splice(listItemIndex, 1);
113             if (!scope.valueObjRef.length) {
114                 if (scope.listDefaultValue) {
115                     angular.copy(scope.listDefaultValue, scope.valueObjRef);
116                 }
117             }
118         };
119     };
120
121     public static factory = (DataTypesService:DataTypesService,
122                              PropertyNameValidationPattern:RegExp,
123                              ValidationUtils:ValidationUtils)=> {
124         return new TypeListDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
125     };
126 }
127
128 TypeListDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];
129