Generalise Select Input button in Properties Assignment view
[sdc.git] / catalog-ui / src / app / ng2 / pages / properties-assignment / tosca-function / tosca-function.component.ts
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 import {Component} from '@angular/core';
21 import {
22     ComponentMetadata, DataTypeModel, PropertyBEModel
23 } from 'app/models';
24 import {TopologyTemplateService} from "../../../services/component-services/topology-template.service";
25 import {WorkspaceService} from "../../workspace/workspace.service";
26 import {PropertiesService} from "../../../services/properties.service";
27 import {PROPERTY_DATA} from "../../../../utils/constants";
28 import {DataTypeService} from "../../../services/data-type.service";
29 import {ToscaGetFunctionType} from "../../../../models/tosca-get-function-type.enum";
30 import {TranslateService} from "../../../shared/translator/translate.service";
31
32 @Component({
33     selector: 'tosca-function',
34     templateUrl: './tosca-function.component.html',
35     styleUrls: ['./tosca-function.component.less'],
36 })
37
38 export class ToscaFunctionComponent {
39
40     selectToscaFunction;
41     selectValue;
42     isLoading: boolean;
43     propertyType: string;
44     dropdownValues: Array<PropertyBEModel> = [];
45     toscaFunctions: Array<string> = [];
46     dropdownValuesLabel: string;
47
48     private dataTypeProperties: Array<PropertyBEModel> = [];
49     private componentMetadata: ComponentMetadata;
50
51     constructor(private topologyTemplateService: TopologyTemplateService,
52                 private workspaceService: WorkspaceService,
53                 private propertiesService: PropertiesService,
54                 private dataTypeService: DataTypeService,
55                 private translateService: TranslateService) {
56     }
57
58     ngOnInit() {
59         this.componentMetadata = this.workspaceService.metadata;
60         this.propertyType = this.propertiesService.getCheckedPropertyType();
61         this.loadToscaFunctions();
62     }
63
64     private loadToscaFunctions(): void {
65         this.toscaFunctions.push(ToscaGetFunctionType.GET_INPUT.toLowerCase());
66     }
67
68     onToscaFunctionChange(): void {
69         this.loadDropdownValueLabel();
70         this.loadDropdownValues();
71     }
72
73     private loadDropdownValueLabel(): void {
74         if (this.selectToscaFunction) {
75             if (this.selectToscaFunction === ToscaGetFunctionType.GET_INPUT.toLowerCase()) {
76                 this.dropdownValuesLabel = this.translateService.translate('INPUT_DROPDOWN_LABEL');
77             }
78         }
79     }
80
81     private loadDropdownValues(): void {
82         if (this.selectToscaFunction) {
83             this.dropdownValues = [];
84             if (this.selectToscaFunction === ToscaGetFunctionType.GET_INPUT.toLowerCase()) {
85                 this.loadInputValues(this.propertyType);
86             }
87         }
88     }
89
90     private loadInputValues(propertyType: string): void {
91         this.isLoading = true;
92         this.topologyTemplateService.getComponentInputsValues(this.componentMetadata.componentType, this.componentMetadata.uniqueId)
93         .subscribe((response) => {
94             response.inputs.forEach((inputProperty: any) => {
95                 if (propertyType === inputProperty.type) {
96                     this.dropdownValues.push(inputProperty);
97                 } else if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(inputProperty.type) === -1 && inputProperty.type !== propertyType) {
98                     this.buildInputDataForComplexType(inputProperty, propertyType);
99                 }
100             });
101         }, () => {
102             //error ignored
103         }, () => {
104             this.isLoading = false;
105         });
106     }
107
108     private buildInputDataForComplexType(inputProperty: PropertyBEModel, propertyType: string) {
109         let dataTypeFound: DataTypeModel = this.dataTypeService.getDataTypeByModelAndTypeName(this.componentMetadata.model, inputProperty.type);
110         if (dataTypeFound && dataTypeFound.properties) {
111             dataTypeFound.properties.forEach(dataTypeProperty => {
112                 let inputData = inputProperty.name + "->" + dataTypeProperty.name;
113                 dataTypeProperty.name = inputData;
114                 if (this.dataTypeProperties.indexOf(dataTypeProperty) === -1 && dataTypeProperty.type === propertyType) {
115                     this.dropdownValues.push(dataTypeProperty);
116                 }
117             });
118         }
119     }
120 }