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} from "app/models";
26 import {ValidationUtils, PROPERTY_TYPES} 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>;
49 getValidationPattern(type:string):RegExp;
50 validateIntRange(value:string):boolean;
52 addValueToList(value:string,index:number);
53 deleteListItem(listItemIndex:number):void;
54 getStringSchemaProperty():SchemaProperty;
55 getNumber(num:number):Array<any>;
56 onEnableTosca(toscaFlag:boolean,index:number);
57 onGetToscaFunction(toscaGetFunction: ToscaGetFunction, index:number);
61 export class TypeListDirective implements ng.IDirective {
63 private readonly stringSchema: SchemaProperty;
65 constructor(private DataTypesService:DataTypesService,
66 private PropertyNameValidationPattern:RegExp,
67 private ValidationUtils:ValidationUtils) {
68 this.stringSchema = new SchemaProperty();
69 this.stringSchema.type = PROPERTY_TYPES.STRING;
70 this.stringSchema.isSimpleType = true;
71 this.stringSchema.isDataType = false;
75 valueObjRef: '=',//ref to list object in the parent value object
76 schemaProperty: '=',//get the schema.property object
77 componentInstanceMap: '=',
79 parentFormObj: '=',//ref to parent form (get angular form object)
80 fieldsPrefixName: '=',//prefix for form fields names
81 readOnly: '=',//is form read only
82 defaultValue: '@',//this list default value
89 template = ():string => {
90 return require('./type-list-directive.html');
93 link = (scope:ITypeListScope, element:any, $attr:any) => {
94 scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
95 scope.stringSchema = this.stringSchema;
96 if (scope.valueObjRef.length == 0) {
97 scope.valueObjRef.push("");
99 scope.showToscaFunction = new Array(scope.valueObjRef.length);
100 scope.valueObjRef.forEach((value, index) => {
101 scope.showToscaFunction[index] = false;
102 let key : string = index.toString();
103 if (scope.parentProperty.subPropertyToscaFunctions != null) {
104 scope.parentProperty.subPropertyToscaFunctions.forEach(SubPropertyToscaFunction => {
105 if (SubPropertyToscaFunction.subPropertyPath.indexOf(key) != -1) {
106 scope.showToscaFunction[index] = true;
111 //reset valueObjRef when schema type is changed
112 scope.$watchCollection('schemaProperty.type', (newData:any):void => {
113 scope.isSchemaTypeDataType = this.DataTypesService.isDataTypeForSchemaType(scope.schemaProperty);
116 //when user brows between properties in "edit property form"
117 scope.$watchCollection('fieldsPrefixName', (newData:any):void => {
118 scope.listNewItem = {value: ''};
120 if ($attr.defaultValue) {
121 scope.listDefaultValue = JSON.parse($attr.defaultValue);
125 scope.getValidationPattern = (type:string):RegExp => {
126 return this.ValidationUtils.getValidationPattern(type);
129 scope.validateIntRange = (value:string):boolean => {
130 return !value || this.ValidationUtils.validateIntRange(value);
133 scope.addListItem = ():void => {
134 scope.valueObjRef = scope.valueObjRef || [];
136 if (scope.schemaProperty.type === PROPERTY_TYPES.MAP) {
138 } else if ((scope.schemaProperty.simpleType || scope.schemaProperty.type) == PROPERTY_TYPES.STRING) {
139 newVal = scope.listNewItem.value;
141 if (scope.listNewItem.value != "") {
142 newVal = JSON.parse(scope.listNewItem.value);
145 scope.valueObjRef.push(newVal);
146 scope.showToscaFunction.push(false);
147 scope.listNewItem.value = "";
150 //return dummy array in order to prevent rendering map-keys ng-repeat again when a map key is changed
151 scope.getNumber = (num:number):Array<any> => {
152 return new Array(num);
155 scope.addValueToList = (value:string,index:number):void => {
156 console.log("value : "+value+" , index : "+index);
157 scope.valueObjRef[index] = value;
158 scope.parentProperty.value = scope.valueObjRef;
161 scope.deleteListItem = (listItemIndex: number): void => {
162 scope.valueObjRef.splice(listItemIndex, 1);
163 let key : string = listItemIndex.toString();
164 if (scope.parentProperty.subPropertyToscaFunctions != null) {
165 let subToscaFunctionList : Array<SubPropertyToscaFunction> = [];
166 scope.parentProperty.subPropertyToscaFunctions.forEach((SubPropertyToscaFunction, index) => {
167 if (SubPropertyToscaFunction.subPropertyPath.indexOf(key) == -1) {
168 subToscaFunctionList.push(SubPropertyToscaFunction);
171 scope.parentProperty.subPropertyToscaFunctions = subToscaFunctionList;
173 if (!scope.valueObjRef.length && scope.listDefaultValue) {
174 angular.copy(scope.listDefaultValue, scope.valueObjRef);
178 scope.onEnableTosca = (toscaFlag:boolean,flagIndex:number):void => {
179 scope.showToscaFunction[flagIndex] = toscaFlag;
180 scope.valueObjRef[flagIndex] = "";
181 let key:string = flagIndex.toString();
183 if (scope.parentProperty.subPropertyToscaFunctions != null) {
184 let subToscaFunctionList : Array<SubPropertyToscaFunction> = [];
185 scope.parentProperty.subPropertyToscaFunctions.forEach((SubPropertyToscaFunction, index) => {
186 if (SubPropertyToscaFunction.subPropertyPath.indexOf(key) == -1) {
187 subToscaFunctionList.push(SubPropertyToscaFunction);
190 scope.parentProperty.subPropertyToscaFunctions = subToscaFunctionList;
195 scope.onGetToscaFunction = (toscaGetFunction: ToscaGetFunction, index:number): void => {
196 let key:string = index.toString();
197 if (scope.parentProperty.subPropertyToscaFunctions != null) {
198 scope.parentProperty.subPropertyToscaFunctions.forEach(SubPropertyToscaFunction => {
199 if (SubPropertyToscaFunction.subPropertyPath.indexOf(key) != -1) {
200 SubPropertyToscaFunction.toscaFunction = toscaGetFunction;
206 if (scope.parentProperty.subPropertyToscaFunctions == null){
207 scope.parentProperty.subPropertyToscaFunctions = [];
209 let subPropertyToscaFunction = new SubPropertyToscaFunction();
210 subPropertyToscaFunction.toscaFunction = toscaGetFunction;
211 subPropertyToscaFunction.subPropertyPath = [key];
212 scope.parentProperty.subPropertyToscaFunctions.push(subPropertyToscaFunction);
217 public static factory = (DataTypesService:DataTypesService,
218 PropertyNameValidationPattern:RegExp,
219 ValidationUtils:ValidationUtils)=> {
220 return new TypeListDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
224 TypeListDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];