4612ca6d60a40639ed51f0aa8cff87d3a10689d9
[sdc.git] /
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
23 import {DataTypeModel} from '../../../../../../../models/data-types';
24 import {SchemaPropertyGroupModel} from '../../../../../../../models/schema-property';
25 import {DerivedPropertyType, PropertyBEModel} from '../../../../../../../models/properties-inputs/property-be-model';
26 import {PROPERTY_DATA, PROPERTY_TYPES} from '../../../../../../../utils/constants';
27 import {ToscaFunction} from '../../../../../../../models/tosca-function';
28 import {ToscaFunctionValidationEvent} from "../../../../../../../ng2/pages/properties-assignment/tosca-function/tosca-function.component";
29 import {InstanceFeDetails} from "../../../../../../../models/instance-fe-details";
30
31 @Component({
32   selector: 'app-input-list-item',
33   templateUrl: './input-list-item.component.html',
34   styleUrls: ['./input-list-item.component.less']
35 })
36 export class InputListItemComponent implements OnInit {
37
38   @Input() valueObjRef: any;
39   @Input() name: string;
40   @Input() dataTypeMap: Map<string, DataTypeModel>;
41   @Input() type: DataTypeModel;
42   @Input() schema: SchemaPropertyGroupModel;
43   @Input() nestingLevel: number;
44   @Input() isListChild: boolean = false;
45   @Input() isMapChild: boolean = false;
46   @Input() showToscaFunctionOption: boolean = false;
47   @Input() listIndex: number;
48   @Input() isViewOnly: boolean;
49   @Input() allowDeletion: boolean = false;
50   @Input() toscaFunction: ToscaFunction;
51   @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map();
52   @Output('onValueChange') onValueChangeEvent: EventEmitter<any> = new EventEmitter<any>();
53   @Output('onDelete') onDeleteEvent: EventEmitter<string> = new EventEmitter<string>();
54   @Output('onChildListItemDelete') onChildListItemDeleteEvent: EventEmitter<number> = new EventEmitter<number>();
55
56   isExpanded: boolean = false;
57   mapEntryName: string;
58   isToscaFunction: boolean = false;
59   property: PropertyBEModel;
60
61   ngOnInit() {
62     if (!this.nestingLevel) {
63       this.nestingLevel = 0;
64     }
65     if (this.type.properties) {
66       this.type.properties.forEach(property => {
67         this.initEmptyPropertyInValueObjRef(property);
68       });
69     }
70
71     this.property = new PropertyBEModel();
72     this.property.type = this.type.name;
73     if (this.schema) {
74       this.property.schema = this.schema;
75       this.property.schemaType = this.schema.property.type;
76     }
77     if (this.toscaFunction) {
78       this.property.toscaFunction = this.toscaFunction;
79       this.valueObjRef = this.toscaFunction.value;
80       this.isToscaFunction = true;
81     }
82   }
83
84   private initEmptyPropertyInValueObjRef(property: PropertyBEModel) {
85     if (this.valueObjRef[property.name] == undefined) {
86       if (this.isTypeComplex(property.type) || this.isTypeMap(property.type)) {
87         this.valueObjRef[property.name] = {};
88       } else if (this.isTypeList(property.type)) {
89         this.valueObjRef[property.name] = [];
90       } else {
91         this.valueObjRef[property.name] = null;
92       }
93     }
94   }
95
96   getType(typeName: string): DerivedPropertyType {
97     if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(typeName) > -1) {
98       return DerivedPropertyType.SIMPLE;
99     } else if (typeName === PROPERTY_TYPES.LIST) {
100       return DerivedPropertyType.LIST;
101     } else if (typeName === PROPERTY_TYPES.MAP) {
102       return DerivedPropertyType.MAP;
103     } else {
104       return DerivedPropertyType.COMPLEX;
105     }
106   }
107
108   isTypeSimple(typeName: string): boolean {
109     return this.getType(typeName) == DerivedPropertyType.SIMPLE;
110   }
111
112   isTypeList(typeName: string): boolean {
113     return this.getType(typeName) == DerivedPropertyType.LIST;
114   }
115
116   isTypeMap(typeName: string): boolean {
117     return this.getType(typeName) == DerivedPropertyType.MAP;
118   }
119
120   isTypeComplex(typeName: string): boolean {
121     return !this.isTypeSimple(typeName) && !this.isTypeList(typeName) && !this.isTypeMap(typeName);
122   }
123
124   expandAndCollapse() {
125     this.isExpanded = !this.isExpanded;
126   }
127
128   getDataType(type: string) {
129     return this.dataTypeMap.get(type);
130   }
131
132   onValueTypeChange () {
133     if ( !this.isToscaFunction ) {
134       this.onValueChange(this.valueObjRef);
135     }
136   }
137
138   onToscaFunctionValidityChange(validationEvent: ToscaFunctionValidationEvent):void {
139     if (validationEvent.isValid) {
140       this.emitValueChangeEvent(validationEvent.toscaFunction, true);
141       return;
142     }
143     this.emitValueChangeEvent(undefined, true);
144   }
145
146   onValueChange(value: any): void {
147     if (this.isNumber(this.type.name)) {
148       this.emitValueChangeEvent(this.parseNumber(value));
149       return;
150     }
151     if (this.type.name == PROPERTY_TYPES.BOOLEAN) {
152       this.emitValueChangeEvent(this.parseBoolean(value));
153       return;
154     }
155     this.emitValueChangeEvent(value);
156   }
157
158   onListValueChange(): void {
159     this.emitValueChangeEvent(this.valueObjRef);
160   }
161
162   onPropertyValueChange($event: any) {
163     this.valueObjRef[$event.name] = $event.value;
164     this.emitValueChangeEvent(this.valueObjRef);
165   }
166
167   private emitValueChangeEvent(value: any, isToscaFunction=false) {
168     let emitValue = {
169       name: this.name,
170       value: value,
171       isToscaFunction:isToscaFunction
172     };
173     this.onValueChangeEvent.emit(emitValue);
174   }
175
176   isRoot(): boolean {
177     return this.nestingLevel === 0;
178   }
179
180   showListItemDelete(): boolean {
181     return !this.isViewOnly && (this.isListChild && this.nestingLevel > 0);
182   }
183
184   showInputDelete(): boolean {
185     return this.allowDeletion && !this.isViewOnly && (this.isRoot() || this.isMapChild);
186   }
187
188   resolveType(): string {
189     if (this.isTypeList(this.type.name)) {
190       return `list of value type ${this.schema.property.type}`
191     }
192     if (this.isTypeMap(this.type.name)) {
193       return `map of 'string' keys and '${this.schema.property.type}' values`
194     }
195     return this.type.name;
196   }
197
198   onInputDelete() {
199     this.onDeleteEvent.emit(this.name);
200   }
201
202   onListItemDelete(index: number): void {
203     this.valueObjRef.splice(index, 1);
204     this.emitValueChangeEvent(this.valueObjRef);
205   }
206
207   addListElement() {
208     if (!this.valueObjRef) {
209       this.valueObjRef = [];
210     }
211     if (this.isTypeSimple(this.schema.property.type)) {
212       this.valueObjRef.push('');
213     } else if (this.isTypeComplex(this.schema.property.type) || this.isTypeMap(this.schema.property.type)) {
214       this.valueObjRef.push({});
215     } else if (this.isTypeList(this.schema.property.type)) {
216       this.valueObjRef.push([]);
217     }
218   }
219
220   trackByIndex(index: number, value: string): number {
221     return index;
222   }
223
224   onChildListItemDelete() {
225     this.onChildListItemDeleteEvent.emit(this.listIndex);
226   }
227
228   getObjectEntries(valueObjRef: object) {
229     return Object.keys(valueObjRef);
230   }
231
232   onMapValueChange() {
233     this.emitValueChangeEvent(this.valueObjRef);
234   }
235
236   onMapKeyDelete(key: string) {
237     delete this.valueObjRef[key]
238     this.emitValueChangeEvent(this.valueObjRef);
239   }
240
241   addMapEntry() {
242     let newKey;
243     if (this.mapEntryName) {
244       newKey = this.mapEntryName.trim();
245     }
246     if (!newKey) {
247       return;
248     }
249     if (Object.keys(this.valueObjRef).indexOf(newKey) !== -1) {
250       return;
251     }
252     this.mapEntryName = '';
253     if (this.isTypeSimple(this.schema.property.type)) {
254       this.valueObjRef[newKey] = '';
255     } else if (this.isTypeComplex(this.schema.property.type) || this.isTypeMap(this.schema.property.type)) {
256       this.valueObjRef[newKey] = {};
257     } else if (this.isTypeList(this.schema.property.type)) {
258       this.valueObjRef[newKey] = [];
259     }
260     this.emitValueChangeEvent(this.valueObjRef);
261   }
262
263   getSimpleValueInputType() {
264     if (this.isNumber(this.type.name)){
265       return 'number';
266     }
267     return 'text';
268   }
269
270   isNumber(type: string): boolean {
271     return type === PROPERTY_TYPES.INTEGER || type === PROPERTY_TYPES.FLOAT;
272   }
273
274   private parseBoolean(value: any) {
275     if (value === 'true') {
276       return true;
277     }
278     if (value === 'false') {
279       return false;
280     }
281     return null;
282   }
283
284   private parseNumber(value: any) {
285     const number = parseInt(value);
286     return isNaN(number) ? null : number;
287   }
288
289 }