Support complex types in artifact properties
[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   @Input() title: string;
49   @Input() emptyMessage: string;
50   @Input() allowDeletion: boolean = false;
51   @Output('onValueChange') inputValueChangeEvent: EventEmitter<InputOperationParameter> = new EventEmitter<InputOperationParameter>();
52   @Output('onDelete') inputDeleteEvent: EventEmitter<string> = new EventEmitter<string>();
53
54   _inputs: Array<InputOperationParameter> = [];
55
56   getDataType(type: string): DataTypeModel {
57     return this.dataTypeMap.get(type);
58   }
59
60   private initValue(input: InputOperationParameter): void {
61     if (input.value) {
62       try {
63         input.value = JSON.parse(input.value);
64       } catch (e) {
65         console.debug('Could not parse value', input.value, e);
66       }
67       return;
68     }
69
70     if (input.toscaDefaultValue) {
71       try {
72         input.value = JSON.parse(input.toscaDefaultValue);
73         return;
74       } catch (e) {
75         console.debug('Could not parse value', input.value, e);
76       }
77     }
78
79     if (this.isTypeComplex(input.type) || this.isTypeMap(input.type)) {
80       input.value = {};
81     } else if (this.isTypeList(input.type)) {
82       input.value = [];
83     } else {
84       input.value = undefined;
85     }
86   }
87
88   getType(typeName: string): DerivedPropertyType {
89     if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(typeName) > -1) {
90       return DerivedPropertyType.SIMPLE;
91     } else if (typeName === PROPERTY_TYPES.LIST) {
92       return DerivedPropertyType.LIST;
93     } else if (typeName === PROPERTY_TYPES.MAP) {
94       return DerivedPropertyType.MAP;
95     } else {
96       return DerivedPropertyType.COMPLEX;
97     }
98   }
99
100   isTypeSimple(typeName: string): boolean {
101     return this.getType(typeName) == DerivedPropertyType.SIMPLE;
102   }
103
104   isTypeList(typeName: string): boolean {
105     return this.getType(typeName) == DerivedPropertyType.LIST;
106   }
107
108   isTypeMap(typeName: string): boolean {
109     return this.getType(typeName) == DerivedPropertyType.MAP;
110   }
111
112   isTypeComplex(typeName: string): boolean {
113     return !this.isTypeSimple(typeName) && !this.isTypeList(typeName) && !this.isTypeMap(typeName);
114   }
115
116   onValueChange($event: any) {
117     const inputOperationParameter = this._inputs.find(input => input.name == $event.name);
118     if (inputOperationParameter) {
119       inputOperationParameter.value = $event.value;
120       this.inputValueChangeEvent.emit(new InputOperationParameter(inputOperationParameter));
121     }
122   }
123
124   onDelete(inputName: string) {
125     this.inputDeleteEvent.emit(inputName);
126   }
127
128 }