Support complex types in interface operation inputs
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / interface-operatons / operation-creator / input-list / input-list.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, Output} from '@angular/core';
23 import {InputOperationParameter} from "../../../../../../models/interfaceOperation";
24 import {DataTypeModel} from "../../../../../../models/data-types";
25 import {DerivedPropertyType} from "../../../../../../models/properties-inputs/property-be-model";
26 import {PROPERTY_DATA, PROPERTY_TYPES} from "../../../../../../utils/constants";
27
28 @Component({
29   selector: 'input-list',
30   templateUrl: './input-list.component.html',
31   styleUrls: ['./input-list.component.less']
32 })
33 export class InputListComponent {
34
35   @Input() set inputs(inputs: Array<InputOperationParameter>) {
36     this._inputs = new Array<InputOperationParameter>();
37     if (inputs) {
38       inputs.forEach(input => {
39         const inputCopy = new InputOperationParameter(input);
40         this.initValue(inputCopy);
41
42         this._inputs.push(inputCopy);
43       });
44     }
45   }
46   @Input() dataTypeMap: Map<string, DataTypeModel>;
47   @Input() isViewOnly: boolean;
48   @Output('onValueChange') inputValueChangeEvent: EventEmitter<InputOperationParameter> = new EventEmitter<InputOperationParameter>();
49   @Output('onDelete') inputDeleteEvent: EventEmitter<string> = new EventEmitter<string>();
50
51   _inputs: Array<InputOperationParameter>;
52
53   getDataType(type: string): DataTypeModel {
54     return this.dataTypeMap.get(type);
55   }
56
57   private initValue(input: InputOperationParameter): void {
58     if (input.value) {
59       try {
60         input.value = JSON.parse(input.value);
61       } catch (e) {
62         console.debug('Could not parse value', input.value, e);
63       }
64       return;
65     }
66
67     if (input.toscaDefaultValue) {
68       try {
69         input.value = JSON.parse(input.toscaDefaultValue);
70         return;
71       } catch (e) {
72         console.debug('Could not parse value', input.value, e);
73       }
74     }
75
76     if (this.isTypeComplex(input.type) || this.isTypeMap(input.type)) {
77       input.value = {};
78     } else if (this.isTypeList(input.type)) {
79       input.value = [];
80     } else {
81       input.value = undefined;
82     }
83   }
84
85   getType(typeName: string): DerivedPropertyType {
86     if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(typeName) > -1) {
87       return DerivedPropertyType.SIMPLE;
88     } else if (typeName === PROPERTY_TYPES.LIST) {
89       return DerivedPropertyType.LIST;
90     } else if (typeName === PROPERTY_TYPES.MAP) {
91       return DerivedPropertyType.MAP;
92     } else {
93       return DerivedPropertyType.COMPLEX;
94     }
95   }
96
97   isTypeSimple(typeName: string): boolean {
98     return this.getType(typeName) == DerivedPropertyType.SIMPLE;
99   }
100
101   isTypeList(typeName: string): boolean {
102     return this.getType(typeName) == DerivedPropertyType.LIST;
103   }
104
105   isTypeMap(typeName: string): boolean {
106     return this.getType(typeName) == DerivedPropertyType.MAP;
107   }
108
109   isTypeComplex(typeName: string): boolean {
110     return !this.isTypeSimple(typeName) && !this.isTypeList(typeName) && !this.isTypeMap(typeName);
111   }
112
113   onValueChange($event: any) {
114     const inputOperationParameter = this._inputs.find(input => input.name == $event.name);
115     if (inputOperationParameter) {
116       inputOperationParameter.value = $event.value;
117       this.inputValueChangeEvent.emit(new InputOperationParameter(inputOperationParameter));
118     }
119   }
120
121   onDelete(inputName: string) {
122     this.inputDeleteEvent.emit(inputName);
123   }
124
125 }