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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
23 import {DataTypeModel} from '../../../../../../../models/data-types';
24 import {SchemaProperty, SchemaPropertyGroupModel} from '../../../../../../../models/schema-property';
25 import {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 {ToscaFunctionType} from "../../../../../../../models/tosca-function-type.enum";
29 import {ToscaFunctionValidationEvent} from "../../../../../properties-assignment/tosca-function/tosca-function.component";
30 import {InstanceFeDetails} from "../../../../../../../models/instance-fe-details";
31 import {ToscaTypeHelper} from "app/utils/tosca-type-helper";
32 import {CustomToscaFunction} from "../../../../../../../models/default-custom-functions";
33 import {SubPropertyToscaFunction} from "../../../../../../../models/sub-property-tosca-function";
36 selector: 'app-input-list-item',
37 templateUrl: './input-list-item.component.html',
38 styleUrls: ['./input-list-item.component.less']
40 export class InputListItemComponent implements OnInit {
42 @Input() valueObjRef: any;
43 @Input() name: string;
44 @Input() dataTypeMap: Map<string, DataTypeModel>;
45 @Input() type: DataTypeModel;
46 @Input() schema: SchemaPropertyGroupModel;
47 @Input() nestingLevel: number;
48 @Input() isExpanded: boolean = false;
49 @Input() isListChild: boolean = false;
50 @Input() isMapChild: boolean = false;
51 @Input() showToscaFunctionOption: boolean = false;
52 @Input() listIndex: number;
53 @Input() subPropertyToscaFunctions: SubPropertyToscaFunction[];
54 @Input() isViewOnly: boolean;
55 @Input() allowDeletion: boolean = false;
56 @Input() toscaFunction: ToscaFunction;
57 @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map();
58 @Input() customToscaFunctions: Array<CustomToscaFunction> = [];
59 @Output('onValueChange') onValueChangeEvent: EventEmitter<any> = new EventEmitter<any>();
60 @Output('onDelete') onDeleteEvent: EventEmitter<string> = new EventEmitter<string>();
61 @Output('onChildListItemDelete') onChildListItemDeleteEvent: EventEmitter<number> = new EventEmitter<number>();
64 isToscaFunction: boolean = false;
65 property: PropertyBEModel;
68 if (!this.nestingLevel) {
69 this.nestingLevel = 0;
71 if (this.type.properties) {
72 this.type.properties.forEach(property => {
73 this.initEmptyPropertyInValueObjRef(property);
77 this.property = new PropertyBEModel();
78 this.property.type = this.type.name;
80 this.property.schema = this.schema;
81 this.property.schemaType = this.schema.property.type;
83 if (this.toscaFunction) {
84 this.property.toscaFunction = this.toscaFunction;
85 this.valueObjRef = this.toscaFunction.value;
86 this.isToscaFunction = true;
91 if (this.isToscaFunction) {
92 this.property.toscaFunction = this.toscaFunction;
93 this.valueObjRef = this.toscaFunction.value;
95 this.property.toscaFunction = undefined;
99 private initEmptyPropertyInValueObjRef(property: PropertyBEModel) {
100 if (this.valueObjRef[property.name] == undefined) {
101 if (this.isTypeComplex(property.type) || this.isTypeMap(property.type)) {
102 this.valueObjRef[property.name] = {};
103 } else if (this.isTypeList(property.type) || this.isTypeRange(property.type)) {
104 this.valueObjRef[property.name] = [];
106 this.valueObjRef[property.name] = null;
111 getToscaFunction(key: any): any {
112 if (this.subPropertyToscaFunctions) {
113 for (let subPropertyToscaFunction of this.subPropertyToscaFunctions) {
114 let found = subPropertyToscaFunction.subPropertyPath ? subPropertyToscaFunction.subPropertyPath.find(value => value === key) : false;
116 return subPropertyToscaFunction.toscaFunction;
123 isTypeSimple(typeName: string): boolean {
124 return ToscaTypeHelper.isTypeSimple(typeName) || this.isTypeDerivedFromSimple(typeName) && (this.isTypeWithoutProperties(typeName));
127 isTypeRange(typeName: string): boolean {
128 return ToscaTypeHelper.isTypeRange(typeName);
131 isTypeWithoutProperties(typeName: string): boolean {
132 if (this.dataTypeMap.get(typeName) === undefined) {
135 return this.dataTypeMap.get(typeName).properties === undefined ||
136 this.dataTypeMap.get(typeName).properties.length == 0;
139 isTypeDerivedFromSimple(typeName: string): boolean {
140 if (typeName === undefined) {
143 if (this.dataTypeMap.get(typeName) !== undefined) {
144 if (this.isTypeRange(typeName)) {
147 if (this.dataTypeMap.get(typeName).derivedFromName == PROPERTY_DATA.ROOT_DATA_TYPE) {
149 } else if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.dataTypeMap.get(typeName).derivedFromName) > -1) {
152 return this.isTypeDerivedFromSimple(this.dataTypeMap.get(typeName).derivedFromName);
158 isTypeList(typeName: string): boolean {
159 return ToscaTypeHelper.isTypeList(typeName);
162 isTypeMap(typeName: string): boolean {
163 return ToscaTypeHelper.isTypeMap(typeName);
166 isTypeComplex(typeName: string): boolean {
167 return ToscaTypeHelper.isTypeComplex(typeName);
170 isTypeNumber(type: string): boolean {
171 return ToscaTypeHelper.isTypeNumber(type);
174 isTypeBoolean(type: string): boolean {
175 return ToscaTypeHelper.isTypeBoolean(type);
178 isTypeLiteral(type: string): boolean {
179 return ToscaTypeHelper.isTypeLiteral(type);
182 expandAndCollapse() {
183 this.isExpanded = !this.isExpanded;
186 getDataType(type: string) {
187 return this.dataTypeMap.get(type);
190 onValueTypeChange () {
191 if ( !this.isToscaFunction ) {
192 this.onValueChange(this.valueObjRef);
196 onToscaFunctionValidityChange(validationEvent: ToscaFunctionValidationEvent):void {
197 if (validationEvent.isValid) {
198 this.emitValueChangeEvent(validationEvent.toscaFunction, true);
201 this.emitValueChangeEvent(undefined, true);
204 onValueChange(value: any): void {
205 if (this.isTypeNumber(this.type.name)) {
206 this.emitValueChangeEvent(this.parseNumber(value));
209 if (this.type.name == PROPERTY_TYPES.BOOLEAN) {
210 this.emitValueChangeEvent(this.parseBoolean(value));
213 this.emitValueChangeEvent(value);
216 onListValueChange(): void {
217 this.emitValueChangeEvent(this.valueObjRef);
220 onPropertyValueChange($event: any) {
221 this.valueObjRef[$event.name] = $event.value;
222 this.emitValueChangeEvent(this.valueObjRef);
225 private emitValueChangeEvent(value: any, isToscaFunction=false) {
229 isToscaFunction:isToscaFunction
231 this.onValueChangeEvent.emit(emitValue);
235 return this.nestingLevel === 0;
238 showListItemDelete(): boolean {
239 return !this.isViewOnly && (this.isListChild && this.nestingLevel > 0);
242 showInputDelete(): boolean {
243 return this.allowDeletion && !this.isViewOnly && (this.isRoot() || this.isMapChild);
246 resolveType(): string {
247 if (this.isTypeList(this.type.name)) {
248 return `list of value type ${this.schema.property.type}`
250 if (this.isTypeMap(this.type.name)) {
251 return `map of 'string' keys and '${this.schema.property.type}' values`
253 return this.type.name;
257 this.onDeleteEvent.emit(this.name);
260 onListItemDelete(index: number): void {
261 this.valueObjRef.splice(index, 1);
262 this.emitValueChangeEvent(this.valueObjRef);
266 if (!this.valueObjRef) {
267 this.valueObjRef = [];
269 if (this.isTypeSimple(this.schema.property.type)) {
270 this.valueObjRef.push('');
271 } else if (this.isTypeComplex(this.schema.property.type) || this.isTypeMap(this.schema.property.type)) {
272 this.valueObjRef.push({});
273 } else if (this.isTypeList(this.schema.property.type)) {
274 this.valueObjRef.push([]);
278 trackByIndex(index: number, value: string): number {
282 onChildListItemDelete() {
283 this.onChildListItemDeleteEvent.emit(this.listIndex);
286 getObjectEntries(valueObjRef: object) {
287 return Object.keys(valueObjRef);
291 this.emitValueChangeEvent(this.valueObjRef);
294 onMapKeyDelete(key: string) {
295 delete this.valueObjRef[key]
296 this.emitValueChangeEvent(this.valueObjRef);
301 if (this.mapEntryName) {
302 newKey = this.mapEntryName.trim();
307 if (Object.keys(this.valueObjRef).indexOf(newKey) !== -1) {
310 this.mapEntryName = '';
311 if (this.isTypeSimple(this.schema.property.type)) {
312 this.valueObjRef[newKey] = '';
313 } else if (this.isTypeComplex(this.schema.property.type) || this.isTypeMap(this.schema.property.type)) {
314 this.valueObjRef[newKey] = {};
315 } else if (this.isTypeList(this.schema.property.type)) {
316 this.valueObjRef[newKey] = [];
318 this.emitValueChangeEvent(this.valueObjRef);
321 getSimpleValueInputType() {
322 if (this.isTypeNumber(this.type.name)){
328 buildSchemaGroupProperty(): SchemaPropertyGroupModel {
329 const schemaProperty = new SchemaProperty();
330 if (this.schema.property.type === PROPERTY_TYPES.MAP || this.schema.property.type === PROPERTY_TYPES.LIST) {
331 schemaProperty.type = PROPERTY_TYPES.STRING;
333 schemaProperty.type = this.schema.property.type
335 return new SchemaPropertyGroupModel(schemaProperty);
338 private parseBoolean(value: any) {
339 if (value === 'true') {
342 if (value === 'false') {
348 private parseNumber(value: any) {
349 const number = parseInt(value);
350 return isNaN(number) ? null : number;