Temp fix to allow tosca functions in op props
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / interface-operatons / operation-creator / input-list / input-list-item / input-list-item.component.ts
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
28 @Component({
29   selector: 'app-input-list-item',
30   templateUrl: './input-list-item.component.html',
31   styleUrls: ['./input-list-item.component.less']
32 })
33 export class InputListItemComponent implements OnInit {
34
35   @Input() valueObjRef: any;
36   @Input() name: string;
37   @Input() dataTypeMap: Map<string, DataTypeModel>;
38   @Input() type: DataTypeModel;
39   @Input() schema: SchemaPropertyGroupModel;
40   @Input() nestingLevel: number;
41   @Input() isListChild: boolean = false;
42   @Input() isMapChild: boolean = false;
43   @Input() listIndex: number;
44   @Input() isViewOnly: boolean;
45   @Input() allowDeletion: boolean = false;
46   @Output('onValueChange') onValueChangeEvent: EventEmitter<any> = new EventEmitter<any>();
47   @Output('onDelete') onDeleteEvent: EventEmitter<string> = new EventEmitter<string>();
48   @Output('onChildListItemDelete') onChildListItemDeleteEvent: EventEmitter<number> = new EventEmitter<number>();
49
50   isExpanded: boolean = false;
51   mapEntryName: string;
52
53   ngOnInit() {
54     if (!this.nestingLevel) {
55       this.nestingLevel = 0;
56     }
57     if (this.type.properties) {
58       this.type.properties.forEach(property => {
59         this.initEmptyPropertyInValueObjRef(property);
60       });
61     }
62   }
63
64   private initEmptyPropertyInValueObjRef(property: PropertyBEModel) {
65     if (this.valueObjRef[property.name] == undefined) {
66       if (this.isTypeComplex(property.type) || this.isTypeMap(property.type)) {
67         this.valueObjRef[property.name] = {};
68       } else if (this.isTypeList(property.type)) {
69         this.valueObjRef[property.name] = [];
70       } else {
71         this.valueObjRef[property.name] = null;
72       }
73     }
74   }
75
76   getType(typeName: string): DerivedPropertyType {
77     if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(typeName) > -1) {
78       return DerivedPropertyType.SIMPLE;
79     } else if (typeName === PROPERTY_TYPES.LIST) {
80       return DerivedPropertyType.LIST;
81     } else if (typeName === PROPERTY_TYPES.MAP) {
82       return DerivedPropertyType.MAP;
83     } else {
84       return DerivedPropertyType.COMPLEX;
85     }
86   }
87
88   isTypeSimple(typeName: string): boolean {
89     return this.getType(typeName) == DerivedPropertyType.SIMPLE;
90   }
91
92   isTypeList(typeName: string): boolean {
93     return this.getType(typeName) == DerivedPropertyType.LIST;
94   }
95
96   isTypeMap(typeName: string): boolean {
97     return this.getType(typeName) == DerivedPropertyType.MAP;
98   }
99
100   isTypeComplex(typeName: string): boolean {
101     return !this.isTypeSimple(typeName) && !this.isTypeList(typeName) && !this.isTypeMap(typeName);
102   }
103
104   expandAndCollapse() {
105     this.isExpanded = !this.isExpanded;
106   }
107
108   getDataType(type: string) {
109     return this.dataTypeMap.get(type);
110   }
111
112   onValueChange(value: any): void {
113     this.emitValueChangeEvent(value);
114   }
115
116   onListValueChange(): void {
117     this.emitValueChangeEvent(this.valueObjRef);
118   }
119
120   onPropertyValueChange($event: any) {
121     this.valueObjRef[$event.name] = $event.value;
122     this.emitValueChangeEvent(this.valueObjRef);
123   }
124
125   private emitValueChangeEvent(value: any) {
126     this.onValueChangeEvent.emit({
127       name: this.name,
128       value: value
129     });
130   }
131
132   isRoot(): boolean {
133     return this.nestingLevel === 0;
134   }
135
136   showListItemDelete(): boolean {
137     return !this.isViewOnly && (this.isListChild && this.nestingLevel > 0);
138   }
139
140   showInputDelete(): boolean {
141     return this.allowDeletion && !this.isViewOnly && (this.isRoot() || this.isMapChild);
142   }
143
144   resolveType(): string {
145     if (this.isTypeList(this.type.name)) {
146       return `list of value type ${this.schema.property.type}`
147     }
148     if (this.isTypeMap(this.type.name)) {
149       return `map of 'string' keys and '${this.schema.property.type}' values`
150     }
151     return this.type.name;
152   }
153
154   onInputDelete() {
155     this.onDeleteEvent.emit(this.name);
156   }
157
158   onListItemDelete(index: number): void {
159     this.valueObjRef.splice(index, 1);
160     this.emitValueChangeEvent(this.valueObjRef);
161   }
162
163   addListElement() {
164     if (this.isTypeSimple(this.schema.property.type)) {
165       this.valueObjRef.push('');
166     } else if (this.isTypeComplex(this.schema.property.type) || this.isTypeMap(this.schema.property.type)) {
167       this.valueObjRef.push({});
168     } else if (this.isTypeList(this.schema.property.type)) {
169       this.valueObjRef.push([]);
170     }
171   }
172
173   trackByIndex(index: number, value: string): number {
174     return index;
175   }
176
177   onChildListItemDelete() {
178     this.onChildListItemDeleteEvent.emit(this.listIndex);
179   }
180
181   getObjectEntries(valueObjRef: object) {
182     return Object.keys(valueObjRef);
183   }
184
185   onMapValueChange() {
186     this.emitValueChangeEvent(this.valueObjRef);
187   }
188
189   onMapKeyDelete(key: string) {
190     delete this.valueObjRef[key]
191     this.emitValueChangeEvent(this.valueObjRef);
192   }
193
194   addMapEntry() {
195     let newKey;
196     if (this.mapEntryName) {
197       newKey = this.mapEntryName.trim();
198     }
199     if (!newKey) {
200       return;
201     }
202     if (Object.keys(this.valueObjRef).indexOf(newKey) !== -1) {
203       return;
204     }
205     this.mapEntryName = '';
206     if (this.isTypeSimple(this.schema.property.type)) {
207       this.valueObjRef[newKey] = '';
208     } else if (this.isTypeComplex(this.schema.property.type) || this.isTypeMap(this.schema.property.type)) {
209       this.valueObjRef[newKey] = {};
210     } else if (this.isTypeList(this.schema.property.type)) {
211       this.valueObjRef[newKey] = [];
212     }
213     this.emitValueChangeEvent(this.valueObjRef);
214   }
215
216   getSimpleValueInputType() {
217     return 'text';
218   }
219
220   private parseBoolean(value: any) {
221     if (value === 'true') {
222       return true;
223     }
224     if (value === 'false') {
225       return false;
226     }
227     return null;
228   }
229
230   private parseNumber(value: any) {
231     const number = parseInt(value);
232     return isNaN(number) ? null : number;
233   }
234
235 }