40a29da5fb965ffb2c998c3a0480584726173121
[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, PropertyModel, DataTypesMap} from "app/models";
26 import {ValidationUtils, PROPERTY_TYPES, PROPERTY_DATA} from "app/utils";
27 import {DataTypesService} from "app/services";
28 import {InstanceFeDetails} from "app/models/instance-fe-details";
29 import {ToscaGetFunction} from "app/models/tosca-get-function";
30 import {SubPropertyToscaFunction} from "app/models/sub-property-tosca-function";
31
32 export interface ITypeListScope extends ng.IScope {
33     parentFormObj:ng.IFormController;
34     schemaProperty:SchemaProperty;
35     parentProperty:PropertyModel;
36     componentInstanceMap: Map<string, InstanceFeDetails>;
37     isSchemaTypeDataType:boolean;
38     valueObjRef:any;
39     propertyNameValidationPattern:RegExp;
40     fieldsPrefixName:string;
41     readOnly:boolean;
42     listDefaultValue:any;
43     listNewItem:any;
44     maxLength:number;
45     stringSchema: SchemaProperty;
46     showToscaFunction: Array<boolean>;
47     constraints:string[];
48     types:DataTypesMap;
49     isService:boolean;
50
51     getValidationPattern(type:string):RegExp;
52     validateIntRange(value:string):boolean;
53     addListItem():void;
54     addValueToList(value:string,index:number);
55     deleteListItem(listItemIndex:number):void;
56     getStringSchemaProperty():SchemaProperty;
57     getNumber(num:number):Array<any>;
58     onEnableTosca(toscaFlag:boolean,index:number);
59     onGetToscaFunction(toscaGetFunction: ToscaGetFunction, index:number);
60 }
61
62
63 export class TypeListDirective implements ng.IDirective {
64
65     private readonly stringSchema: SchemaProperty;
66
67     constructor(private DataTypesService:DataTypesService,
68                 private PropertyNameValidationPattern:RegExp,
69                 private ValidationUtils:ValidationUtils) {
70         this.stringSchema = new SchemaProperty();
71         this.stringSchema.type = PROPERTY_TYPES.STRING;
72         this.stringSchema.isSimpleType = true;
73         this.stringSchema.isDataType = false;
74     }
75
76     scope = {
77         valueObjRef: '=',//ref to list object in the parent value object
78         schemaProperty: '=',//get the schema.property object
79         componentInstanceMap: '=',
80         parentProperty: '=',
81         parentFormObj: '=',//ref to parent form (get angular form object)
82         fieldsPrefixName: '=',//prefix for form fields names
83         readOnly: '=',//is form read only
84         defaultValue: '@',//this list default value
85         maxLength: '=',
86         constraints: '=',
87         types: '=',
88         isService: '='
89     };
90
91     restrict = 'E';
92     replace = true;
93     template = ():string => {
94         return require('./type-list-directive.html');
95     };
96     
97     private isDataTypeForSchemaType = (property:SchemaProperty, types:DataTypesMap):boolean=> {
98         property.simpleType = "";
99         if (property.type && PROPERTY_DATA.TYPES.indexOf(property.type) > -1) {
100             return false;
101         }
102         let simpleType = this.getTypeForDataTypeDerivedFromSimple(property.type, types);
103         if (simpleType) {
104             property.simpleType = simpleType;
105             return false;
106         }
107         return true;
108     };
109     
110     private getTypeForDataTypeDerivedFromSimple = (dataTypeName:string, types:DataTypesMap):string => {
111         if (!types[dataTypeName]) {
112             return 'string';
113         }
114         if (types[dataTypeName].derivedFromName == "tosca.datatypes.Root" || types[dataTypeName].properties) {
115             return null;
116         }
117         if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(types[dataTypeName].derivedFromName) > -1) {
118             return types[dataTypeName].derivedFromName
119         }
120         return this.getTypeForDataTypeDerivedFromSimple(types[dataTypeName].derivedFromName, types);
121     };
122
123     link = (scope:ITypeListScope, element:any, $attr:any) => {
124         scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
125         scope.stringSchema = this.stringSchema;
126         if (scope.valueObjRef == null || scope.valueObjRef.length == 0) {
127             scope.valueObjRef = [];
128             scope.valueObjRef.push("");
129         }
130         scope.showToscaFunction = new Array(scope.valueObjRef.length);
131         scope.valueObjRef.forEach((value, index) => {
132             scope.showToscaFunction[index] = false;
133             let key : string = index.toString();
134             if (scope.parentProperty.subPropertyToscaFunctions != null) {
135                 scope.parentProperty.subPropertyToscaFunctions.forEach(SubPropertyToscaFunction => {
136                     if (SubPropertyToscaFunction.subPropertyPath.indexOf(key) != -1) {
137                         scope.showToscaFunction[index] = true;
138                     }
139                 });
140             }
141         });
142         //reset valueObjRef when schema type is changed
143         scope.$watchCollection('schemaProperty.type', (newData:any):void => {
144             scope.isSchemaTypeDataType = this.DataTypesService.isDataTypeForSchemaType(scope.schemaProperty);
145         });
146
147         //when user brows between properties in "edit property form"
148         scope.$watchCollection('fieldsPrefixName', (newData:any):void => {
149             scope.listNewItem = {value: ''};
150
151             if ($attr.defaultValue) {
152                 scope.listDefaultValue = JSON.parse($attr.defaultValue);
153             }
154         });
155
156         scope.getValidationPattern = (type:string):RegExp => {
157             return this.ValidationUtils.getValidationPattern(type);
158         };
159
160         scope.validateIntRange = (value:string):boolean => {
161             return !value || this.ValidationUtils.validateIntRange(value);
162         };
163
164         scope.addListItem = ():void => {
165             scope.valueObjRef = scope.valueObjRef || [];
166             let newVal;
167             if (scope.schemaProperty.type === PROPERTY_TYPES.MAP) {
168                 newVal = {"": ""};
169             } else if ((scope.schemaProperty.simpleType || scope.schemaProperty.type) == PROPERTY_TYPES.STRING) {
170                 newVal = scope.listNewItem.value;
171             } else {
172                 if (scope.listNewItem.value != "") {
173                     newVal = JSON.parse(scope.listNewItem.value);
174                 }
175             }
176             scope.valueObjRef.push(newVal);
177             scope.showToscaFunction.push(false);
178             scope.listNewItem.value = "";
179         };
180
181         //return dummy array in order to prevent rendering map-keys ng-repeat again when a map key is changed
182         scope.getNumber = (num:number):Array<any> => {
183             return new Array(num);
184         };
185
186         scope.addValueToList = (value:string,index:number):void => {
187             console.log("value : "+value+" , index : "+index);
188             scope.valueObjRef[index] = value;
189             scope.parentProperty.value = scope.valueObjRef;
190         }
191
192         scope.deleteListItem = (listItemIndex: number): void => {
193             scope.valueObjRef.splice(listItemIndex, 1);
194             let key : string = listItemIndex.toString();
195             if (scope.parentProperty.subPropertyToscaFunctions != null) {
196                 let subToscaFunctionList : Array<SubPropertyToscaFunction> = [];
197                 scope.parentProperty.subPropertyToscaFunctions.forEach((SubPropertyToscaFunction, index) => {
198                     if (SubPropertyToscaFunction.subPropertyPath.indexOf(key) == -1) {
199                         subToscaFunctionList.push(SubPropertyToscaFunction);
200                     }
201                 });
202                 scope.parentProperty.subPropertyToscaFunctions = subToscaFunctionList;
203             }
204             if (!scope.valueObjRef.length && scope.listDefaultValue) {
205                 angular.copy(scope.listDefaultValue, scope.valueObjRef);
206             }
207         };
208
209         scope.onEnableTosca = (toscaFlag:boolean,flagIndex:number):void => {
210             scope.showToscaFunction[flagIndex] = toscaFlag;
211             scope.valueObjRef[flagIndex] = "";
212             let key:string = flagIndex.toString();
213             if (!toscaFlag) {
214                 if (scope.parentProperty.subPropertyToscaFunctions != null) {
215                     let subToscaFunctionList : Array<SubPropertyToscaFunction> = [];
216                     scope.parentProperty.subPropertyToscaFunctions.forEach((SubPropertyToscaFunction, index) => {
217                         if (SubPropertyToscaFunction.subPropertyPath.indexOf(key) == -1) {
218                             subToscaFunctionList.push(SubPropertyToscaFunction);
219                         }
220                     });
221                     scope.parentProperty.subPropertyToscaFunctions = subToscaFunctionList;
222                 }
223             }
224         };
225
226         scope.onGetToscaFunction = (toscaGetFunction: ToscaGetFunction, index:number): void => {
227             let key:string = index.toString();
228             if (scope.parentProperty.subPropertyToscaFunctions != null) {
229                 scope.parentProperty.subPropertyToscaFunctions.forEach(SubPropertyToscaFunction => {
230                     if (SubPropertyToscaFunction.subPropertyPath.indexOf(key) != -1) {
231                         SubPropertyToscaFunction.toscaFunction = toscaGetFunction;
232                         return;
233                     }
234                 });
235
236             }
237             if (scope.parentProperty.subPropertyToscaFunctions == null){
238                 scope.parentProperty.subPropertyToscaFunctions = [];
239             }
240             let subPropertyToscaFunction = new SubPropertyToscaFunction();
241             subPropertyToscaFunction.toscaFunction = toscaGetFunction;
242             subPropertyToscaFunction.subPropertyPath = [key];
243             scope.parentProperty.subPropertyToscaFunctions.push(subPropertyToscaFunction);
244         }
245
246     };
247
248     public static factory = (DataTypesService:DataTypesService,
249                              PropertyNameValidationPattern:RegExp,
250                              ValidationUtils:ValidationUtils)=> {
251         return new TypeListDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
252     };
253 }
254
255 TypeListDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];
256