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';
29 selector: 'app-input-list-item',
30 templateUrl: './input-list-item.component.html',
31 styleUrls: ['./input-list-item.component.less']
33 export class InputListItemComponent implements OnInit {
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 @Output('onValueChange') onValueChangeEvent: EventEmitter<any> = new EventEmitter<any>();
46 @Output('onDelete') onDeleteEvent: EventEmitter<string> = new EventEmitter<string>();
47 @Output('onChildListItemDelete') onChildListItemDeleteEvent: EventEmitter<number> = new EventEmitter<number>();
49 isExpanded: boolean = false;
53 if (!this.nestingLevel) {
54 this.nestingLevel = 0;
56 if (this.type.properties) {
57 this.type.properties.forEach(property => {
58 this.initEmptyPropertyInValueObjRef(property);
63 private initEmptyPropertyInValueObjRef(property: PropertyBEModel) {
64 if (this.valueObjRef[property.name] == undefined) {
65 if (this.isTypeComplex(property.type) || this.isTypeMap(property.type)) {
66 this.valueObjRef[property.name] = {};
67 } else if (this.isTypeList(property.type)) {
68 this.valueObjRef[property.name] = [];
70 this.valueObjRef[property.name] = null;
75 getType(typeName: string): DerivedPropertyType {
76 if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(typeName) > -1) {
77 return DerivedPropertyType.SIMPLE;
78 } else if (typeName === PROPERTY_TYPES.LIST) {
79 return DerivedPropertyType.LIST;
80 } else if (typeName === PROPERTY_TYPES.MAP) {
81 return DerivedPropertyType.MAP;
83 return DerivedPropertyType.COMPLEX;
87 isTypeSimple(typeName: string): boolean {
88 return this.getType(typeName) == DerivedPropertyType.SIMPLE;
91 isTypeList(typeName: string): boolean {
92 return this.getType(typeName) == DerivedPropertyType.LIST;
95 isTypeMap(typeName: string): boolean {
96 return this.getType(typeName) == DerivedPropertyType.MAP;
99 isTypeComplex(typeName: string): boolean {
100 return !this.isTypeSimple(typeName) && !this.isTypeList(typeName) && !this.isTypeMap(typeName);
103 expandAndCollapse() {
104 this.isExpanded = !this.isExpanded;
107 getDataType(type: string) {
108 return this.dataTypeMap.get(type);
111 onValueChange(value: any): void {
112 if (this.type.name == PROPERTY_TYPES.INTEGER || this.type.name == PROPERTY_TYPES.FLOAT) {
113 this.emitValueChangeEvent(this.parseNumber(value));
116 if (this.type.name == PROPERTY_TYPES.BOOLEAN) {
117 this.emitValueChangeEvent(this.parseBoolean(value));
120 this.emitValueChangeEvent(value);
123 onListValueChange(): void {
124 this.emitValueChangeEvent(this.valueObjRef);
127 onPropertyValueChange($event: any) {
128 this.valueObjRef[$event.name] = $event.value;
129 this.emitValueChangeEvent(this.valueObjRef);
132 private emitValueChangeEvent(value: any) {
133 this.onValueChangeEvent.emit({
140 return this.nestingLevel === 0;
143 showListItemDelete(): boolean {
144 return !this.isViewOnly && (this.isListChild && this.nestingLevel > 0);
147 showInputDelete(): boolean {
148 return !this.isViewOnly && (this.isRoot() || this.isMapChild);
151 resolveType(): string {
152 if (this.isTypeList(this.type.name)) {
153 return `list of value type ${this.schema.property.type}`
155 if (this.isTypeMap(this.type.name)) {
156 return `map of 'string' keys and '${this.schema.property.type}' values`
158 return this.type.name;
162 this.onDeleteEvent.emit(this.name);
165 onListItemDelete(index: number): void {
166 this.valueObjRef.splice(index, 1);
167 this.emitValueChangeEvent(this.valueObjRef);
171 if (this.isTypeSimple(this.schema.property.type)) {
172 this.valueObjRef.push('');
173 } else if (this.isTypeComplex(this.schema.property.type) || this.isTypeMap(this.schema.property.type)) {
174 this.valueObjRef.push({});
175 } else if (this.isTypeList(this.schema.property.type)) {
176 this.valueObjRef.push([]);
180 trackByIndex(index: number, value: string): number {
184 onChildListItemDelete() {
185 this.onChildListItemDeleteEvent.emit(this.listIndex);
188 getObjectEntries(valueObjRef: object) {
189 return Object.keys(valueObjRef);
193 this.emitValueChangeEvent(this.valueObjRef);
196 onMapKeyDelete(key: string) {
197 delete this.valueObjRef[key]
198 this.emitValueChangeEvent(this.valueObjRef);
203 if (this.mapEntryName) {
204 newKey = this.mapEntryName.trim();
209 if (Object.keys(this.valueObjRef).indexOf(newKey) !== -1) {
212 this.mapEntryName = '';
213 if (this.isTypeSimple(this.schema.property.type)) {
214 this.valueObjRef[newKey] = '';
215 } else if (this.isTypeComplex(this.schema.property.type) || this.isTypeMap(this.schema.property.type)) {
216 this.valueObjRef[newKey] = {};
217 } else if (this.isTypeList(this.schema.property.type)) {
218 this.valueObjRef[newKey] = [];
220 this.emitValueChangeEvent(this.valueObjRef);
223 getSimpleValueInputType() {
224 if (this.type.name == PROPERTY_TYPES.INTEGER || this.type.name == PROPERTY_TYPES.FLOAT) {
230 private parseBoolean(value: any) {
231 if (value === 'true') {
234 if (value === 'false') {
240 private parseNumber(value: any) {
241 const number = parseInt(value);
242 return isNaN(number) ? null : number;