UI support for default custom function names
[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 import {InstanceFeDetails} from "../../../../../../models/instance-fe-details";
28 import {CustomToscaFunction} from "../../../../../../models/default-custom-functions";
29
30 @Component({
31   selector: 'input-list',
32   templateUrl: './input-list.component.html',
33   styleUrls: ['./input-list.component.less']
34 })
35 export class InputListComponent {
36
37   @Input() set inputs(inputs: Array<InputOperationParameter>) {
38     this._inputs = new Array<InputOperationParameter>();
39     if (inputs) {
40       inputs.forEach(input => {
41         const inputCopy = new InputOperationParameter(input);
42         this.initValue(inputCopy);
43
44         this._inputs.push(inputCopy);
45       });
46     }
47   }
48   @Input() dataTypeMap: Map<string, DataTypeModel>;
49   @Input() isViewOnly: boolean;
50   @Input() title: string;
51   @Input() emptyMessage: string;
52   @Input() showToscaFunctionOption: boolean = false;
53   @Input() allowDeletion: boolean = false;
54   @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map();
55   @Input() customToscaFunctions: Array<CustomToscaFunction> = [];
56   @Output('onInputsValidityChange') inputsValidityChangeEvent: EventEmitter<boolean> = new EventEmitter<boolean>();
57   @Output('onValueChange') inputValueChangeEvent: EventEmitter<InputOperationParameter> = new EventEmitter<InputOperationParameter>();
58   @Output('onDelete') inputDeleteEvent: EventEmitter<string> = new EventEmitter<string>();
59
60   _inputs: Array<InputOperationParameter> = [];
61
62   getDataType(type: string): DataTypeModel {
63     return this.dataTypeMap.get(type);
64   }
65
66   private initValue(input: InputOperationParameter): void {
67     if (input.value) {
68       try {
69         input.value = JSON.parse(input.value);
70       } catch (e) {
71         console.debug('Could not parse value', input.value, e);
72       }
73       return;
74     }
75
76     if (input.toscaDefaultValue) {
77       try {
78         input.value = JSON.parse(input.toscaDefaultValue);
79         return;
80       } catch (e) {
81         console.debug('Could not parse value', input.value, e);
82       }
83     }
84
85     if (this.isTypeComplex(input.type) || this.isTypeMap(input.type)) {
86       input.value = {};
87     } else if (this.isTypeList(input.type)) {
88       input.value = [];
89     } else {
90       input.value = undefined;
91     }
92   }
93
94   getType(typeName: string): DerivedPropertyType {
95     if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(typeName) > -1) {
96       return DerivedPropertyType.SIMPLE;
97     } else if (typeName === PROPERTY_TYPES.LIST) {
98       return DerivedPropertyType.LIST;
99     } else if (typeName === PROPERTY_TYPES.MAP) {
100       return DerivedPropertyType.MAP;
101     } else {
102       return DerivedPropertyType.COMPLEX;
103     }
104   }
105
106   isTypeSimple(typeName: string): boolean {
107     return this.getType(typeName) == DerivedPropertyType.SIMPLE;
108   }
109
110   isTypeList(typeName: string): boolean {
111     return this.getType(typeName) == DerivedPropertyType.LIST;
112   }
113
114   isTypeMap(typeName: string): boolean {
115     return this.getType(typeName) == DerivedPropertyType.MAP;
116   }
117
118   isTypeComplex(typeName: string): boolean {
119     return !this.isTypeSimple(typeName) && !this.isTypeList(typeName) && !this.isTypeMap(typeName);
120   }
121
122   onValueChange($event: any) {
123     const inputOperationParameter = this._inputs.find(input => input.name == $event.name);
124     if (inputOperationParameter) {
125       inputOperationParameter.valid = true;
126       if ($event.isToscaFunction) {
127         inputOperationParameter.toscaFunction = $event.value;
128         if (!inputOperationParameter.toscaFunction) {
129           inputOperationParameter.valid = false;
130         }
131       } else {
132         inputOperationParameter.value = $event.value;
133         inputOperationParameter.toscaFunction = null;
134       }
135       this.inputsValidityChangeEvent.emit(this._inputs.every(input => input.valid === true));
136       this.inputValueChangeEvent.emit(new InputOperationParameter(inputOperationParameter));
137     }
138   }
139
140   onDelete(inputName: string) {
141     this.inputDeleteEvent.emit(inputName);
142   }
143
144 }