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 @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>();
50 isExpanded: boolean = false;
54 if (!this.nestingLevel) {
55 this.nestingLevel = 0;
57 if (this.type.properties) {
58 this.type.properties.forEach(property => {
59 this.initEmptyPropertyInValueObjRef(property);
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] = [];
71 this.valueObjRef[property.name] = null;
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;
84 return DerivedPropertyType.COMPLEX;
88 isTypeSimple(typeName: string): boolean {
89 return this.getType(typeName) == DerivedPropertyType.SIMPLE;
92 isTypeList(typeName: string): boolean {
93 return this.getType(typeName) == DerivedPropertyType.LIST;
96 isTypeMap(typeName: string): boolean {
97 return this.getType(typeName) == DerivedPropertyType.MAP;
100 isTypeComplex(typeName: string): boolean {
101 return !this.isTypeSimple(typeName) && !this.isTypeList(typeName) && !this.isTypeMap(typeName);
104 expandAndCollapse() {
105 this.isExpanded = !this.isExpanded;
108 getDataType(type: string) {
109 return this.dataTypeMap.get(type);
112 onValueChange(value: any): void {
113 this.emitValueChangeEvent(value);
116 onListValueChange(): void {
117 this.emitValueChangeEvent(this.valueObjRef);
120 onPropertyValueChange($event: any) {
121 this.valueObjRef[$event.name] = $event.value;
122 this.emitValueChangeEvent(this.valueObjRef);
125 private emitValueChangeEvent(value: any) {
126 this.onValueChangeEvent.emit({
133 return this.nestingLevel === 0;
136 showListItemDelete(): boolean {
137 return !this.isViewOnly && (this.isListChild && this.nestingLevel > 0);
140 showInputDelete(): boolean {
141 return this.allowDeletion && !this.isViewOnly && (this.isRoot() || this.isMapChild);
144 resolveType(): string {
145 if (this.isTypeList(this.type.name)) {
146 return `list of value type ${this.schema.property.type}`
148 if (this.isTypeMap(this.type.name)) {
149 return `map of 'string' keys and '${this.schema.property.type}' values`
151 return this.type.name;
155 this.onDeleteEvent.emit(this.name);
158 onListItemDelete(index: number): void {
159 this.valueObjRef.splice(index, 1);
160 this.emitValueChangeEvent(this.valueObjRef);
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([]);
173 trackByIndex(index: number, value: string): number {
177 onChildListItemDelete() {
178 this.onChildListItemDeleteEvent.emit(this.listIndex);
181 getObjectEntries(valueObjRef: object) {
182 return Object.keys(valueObjRef);
186 this.emitValueChangeEvent(this.valueObjRef);
189 onMapKeyDelete(key: string) {
190 delete this.valueObjRef[key]
191 this.emitValueChangeEvent(this.valueObjRef);
196 if (this.mapEntryName) {
197 newKey = this.mapEntryName.trim();
202 if (Object.keys(this.valueObjRef).indexOf(newKey) !== -1) {
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] = [];
213 this.emitValueChangeEvent(this.valueObjRef);
216 getSimpleValueInputType() {
220 private parseBoolean(value: any) {
221 if (value === 'true') {
224 if (value === 'false') {
230 private parseNumber(value: any) {
231 const number = parseInt(value);
232 return isNaN(number) ? null : number;