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 {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";
32 selector: 'app-input-list-item',
33 templateUrl: './input-list-item.component.html',
34 styleUrls: ['./input-list-item.component.less']
36 export class InputListItemComponent implements OnInit {
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>();
56 isExpanded: boolean = false;
58 isToscaFunction: boolean = false;
59 property: PropertyBEModel;
62 if (!this.nestingLevel) {
63 this.nestingLevel = 0;
65 if (this.type.properties) {
66 this.type.properties.forEach(property => {
67 this.initEmptyPropertyInValueObjRef(property);
71 this.property = new PropertyBEModel();
72 this.property.type = this.type.name;
74 this.property.schema = this.schema;
75 this.property.schemaType = this.schema.property.type;
77 if (this.toscaFunction) {
78 this.property.toscaFunction = this.toscaFunction;
79 this.valueObjRef = this.toscaFunction.value;
80 this.isToscaFunction = true;
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] = [];
91 this.valueObjRef[property.name] = null;
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;
104 return DerivedPropertyType.COMPLEX;
108 isTypeSimple(typeName: string): boolean {
109 return this.getType(typeName) == DerivedPropertyType.SIMPLE;
112 isTypeList(typeName: string): boolean {
113 return this.getType(typeName) == DerivedPropertyType.LIST;
116 isTypeMap(typeName: string): boolean {
117 return this.getType(typeName) == DerivedPropertyType.MAP;
120 isTypeComplex(typeName: string): boolean {
121 return !this.isTypeSimple(typeName) && !this.isTypeList(typeName) && !this.isTypeMap(typeName);
124 expandAndCollapse() {
125 this.isExpanded = !this.isExpanded;
128 getDataType(type: string) {
129 return this.dataTypeMap.get(type);
132 onValueTypeChange () {
133 if ( !this.isToscaFunction ) {
134 this.onValueChange(this.valueObjRef);
138 onToscaFunctionValidityChange(validationEvent: ToscaFunctionValidationEvent):void {
139 if (validationEvent.isValid) {
140 this.emitValueChangeEvent(validationEvent.toscaFunction, true);
143 this.emitValueChangeEvent(undefined, true);
146 onValueChange(value: any): void {
147 if (this.isNumber(this.type.name)) {
148 this.emitValueChangeEvent(this.parseNumber(value));
151 if (this.type.name == PROPERTY_TYPES.BOOLEAN) {
152 this.emitValueChangeEvent(this.parseBoolean(value));
155 this.emitValueChangeEvent(value);
158 onListValueChange(): void {
159 this.emitValueChangeEvent(this.valueObjRef);
162 onPropertyValueChange($event: any) {
163 this.valueObjRef[$event.name] = $event.value;
164 this.emitValueChangeEvent(this.valueObjRef);
167 private emitValueChangeEvent(value: any, isToscaFunction=false) {
171 isToscaFunction:isToscaFunction
173 this.onValueChangeEvent.emit(emitValue);
177 return this.nestingLevel === 0;
180 showListItemDelete(): boolean {
181 return !this.isViewOnly && (this.isListChild && this.nestingLevel > 0);
184 showInputDelete(): boolean {
185 return this.allowDeletion && !this.isViewOnly && (this.isRoot() || this.isMapChild);
188 resolveType(): string {
189 if (this.isTypeList(this.type.name)) {
190 return `list of value type ${this.schema.property.type}`
192 if (this.isTypeMap(this.type.name)) {
193 return `map of 'string' keys and '${this.schema.property.type}' values`
195 return this.type.name;
199 this.onDeleteEvent.emit(this.name);
202 onListItemDelete(index: number): void {
203 this.valueObjRef.splice(index, 1);
204 this.emitValueChangeEvent(this.valueObjRef);
208 if (!this.valueObjRef) {
209 this.valueObjRef = [];
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([]);
220 trackByIndex(index: number, value: string): number {
224 onChildListItemDelete() {
225 this.onChildListItemDeleteEvent.emit(this.listIndex);
228 getObjectEntries(valueObjRef: object) {
229 return Object.keys(valueObjRef);
233 this.emitValueChangeEvent(this.valueObjRef);
236 onMapKeyDelete(key: string) {
237 delete this.valueObjRef[key]
238 this.emitValueChangeEvent(this.valueObjRef);
243 if (this.mapEntryName) {
244 newKey = this.mapEntryName.trim();
249 if (Object.keys(this.valueObjRef).indexOf(newKey) !== -1) {
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] = [];
260 this.emitValueChangeEvent(this.valueObjRef);
263 getSimpleValueInputType() {
264 if (this.isNumber(this.type.name)){
270 isNumber(type: string): boolean {
271 return type === PROPERTY_TYPES.INTEGER || type === PROPERTY_TYPES.FLOAT;
274 private parseBoolean(value: any) {
275 if (value === 'true') {
278 if (value === 'false') {
284 private parseNumber(value: any) {
285 const number = parseInt(value);
286 return isNaN(number) ? null : number;