2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * Created by rcohen on 9/15/2016.
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";
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;
39 propertyNameValidationPattern:RegExp;
40 fieldsPrefixName:string;
45 stringSchema: SchemaProperty;
46 showToscaFunction: Array<boolean>;
51 getValidationPattern(type:string):RegExp;
52 validateIntRange(value:string):boolean;
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);
63 export class TypeListDirective implements ng.IDirective {
65 private readonly stringSchema: SchemaProperty;
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;
77 valueObjRef: '=',//ref to list object in the parent value object
78 schemaProperty: '=',//get the schema.property object
79 componentInstanceMap: '=',
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
93 template = ():string => {
94 return require('./type-list-directive.html');
97 private isDataTypeForSchemaType = (property:SchemaProperty, types:DataTypesMap):boolean=> {
98 property.simpleType = "";
99 if (property.type && PROPERTY_DATA.TYPES.indexOf(property.type) > -1) {
102 let simpleType = this.getTypeForDataTypeDerivedFromSimple(property.type, types);
104 property.simpleType = simpleType;
110 private getTypeForDataTypeDerivedFromSimple = (dataTypeName:string, types:DataTypesMap):string => {
111 if (!types[dataTypeName]) {
114 if (types[dataTypeName].derivedFromName == "tosca.datatypes.Root" || types[dataTypeName].properties) {
117 if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(types[dataTypeName].derivedFromName) > -1) {
118 return types[dataTypeName].derivedFromName
120 return this.getTypeForDataTypeDerivedFromSimple(types[dataTypeName].derivedFromName, types);
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("");
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;
142 //reset valueObjRef when schema type is changed
143 scope.$watchCollection('schemaProperty.type', (newData:any):void => {
144 scope.isSchemaTypeDataType = this.DataTypesService.isDataTypeForSchemaType(scope.schemaProperty);
147 //when user brows between properties in "edit property form"
148 scope.$watchCollection('fieldsPrefixName', (newData:any):void => {
149 scope.listNewItem = {value: ''};
151 if ($attr.defaultValue) {
152 scope.listDefaultValue = JSON.parse($attr.defaultValue);
156 scope.getValidationPattern = (type:string):RegExp => {
157 return this.ValidationUtils.getValidationPattern(type);
160 scope.validateIntRange = (value:string):boolean => {
161 return !value || this.ValidationUtils.validateIntRange(value);
164 scope.addListItem = ():void => {
165 scope.valueObjRef = scope.valueObjRef || [];
167 if (scope.schemaProperty.type === PROPERTY_TYPES.MAP) {
169 } else if ((scope.schemaProperty.simpleType || scope.schemaProperty.type) == PROPERTY_TYPES.STRING) {
170 newVal = scope.listNewItem.value;
172 if (scope.listNewItem.value != "") {
173 newVal = JSON.parse(scope.listNewItem.value);
176 scope.valueObjRef.push(newVal);
177 scope.showToscaFunction.push(false);
178 scope.listNewItem.value = "";
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);
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;
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);
202 scope.parentProperty.subPropertyToscaFunctions = subToscaFunctionList;
204 if (!scope.valueObjRef.length && scope.listDefaultValue) {
205 angular.copy(scope.listDefaultValue, scope.valueObjRef);
209 scope.onEnableTosca = (toscaFlag:boolean,flagIndex:number):void => {
210 scope.showToscaFunction[flagIndex] = toscaFlag;
211 scope.valueObjRef[flagIndex] = "";
212 let key:string = flagIndex.toString();
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);
221 scope.parentProperty.subPropertyToscaFunctions = subToscaFunctionList;
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;
237 if (scope.parentProperty.subPropertyToscaFunctions == null){
238 scope.parentProperty.subPropertyToscaFunctions = [];
240 let subPropertyToscaFunction = new SubPropertyToscaFunction();
241 subPropertyToscaFunction.toscaFunction = toscaGetFunction;
242 subPropertyToscaFunction.subPropertyPath = [key];
243 scope.parentProperty.subPropertyToscaFunctions.push(subPropertyToscaFunction);
248 public static factory = (DataTypesService:DataTypesService,
249 PropertyNameValidationPattern:RegExp,
250 ValidationUtils:ValidationUtils)=> {
251 return new TypeListDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
255 TypeListDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];