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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
21 import {ComponentMetadata, PropertyBEModel} from 'app/models';
22 import {TopologyTemplateService} from "../../../services/component-services/topology-template.service";
23 import {WorkspaceService} from "../../workspace/workspace.service";
24 import {ToscaGetFunctionType} from "../../../../models/tosca-get-function-type";
25 import {InstanceFeDetails} from "../../../../models/instance-fe-details";
26 import {ToscaGetFunction} from "../../../../models/tosca-get-function";
27 import {FormControl, FormGroup, Validators} from "@angular/forms";
28 import {ToscaFunctionType} from "../../../../models/tosca-function-type.enum";
29 import {ToscaGetFunctionValidationEvent} from "./tosca-get-function/tosca-get-function.component";
30 import {ToscaFunction} from "../../../../models/tosca-function";
31 import {ToscaConcatFunctionValidationEvent} from "./tosca-concat-function/tosca-concat-function.component";
32 import {PROPERTY_TYPES} from "../../../../utils/constants";
35 selector: 'tosca-function',
36 templateUrl: './tosca-function.component.html',
37 styleUrls: ['./tosca-function.component.less'],
39 export class ToscaFunctionComponent implements OnInit {
41 @Input() property: PropertyBEModel;
42 @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map<string, InstanceFeDetails>();
43 @Input() allowClear: boolean = true;
44 @Output() onValidFunction: EventEmitter<ToscaGetFunction> = new EventEmitter<ToscaGetFunction>();
45 @Output() onValidityChange: EventEmitter<ToscaFunctionValidationEvent> = new EventEmitter<ToscaFunctionValidationEvent>();
47 toscaFunctionForm: FormControl = new FormControl(undefined, [Validators.required]);
48 toscaFunctionTypeForm: FormControl = new FormControl(undefined, Validators.required);
49 formGroup: FormGroup = new FormGroup({
50 'toscaFunction': this.toscaFunctionForm,
51 'toscaFunctionType': this.toscaFunctionTypeForm,
54 isLoading: boolean = false;
55 toscaFunction: ToscaFunction;
56 toscaFunctions: Array<string> = [];
58 private isInitialized: boolean = false;
59 private componentMetadata: ComponentMetadata;
61 constructor(private topologyTemplateService: TopologyTemplateService,
62 private workspaceService: WorkspaceService) {
66 this.componentMetadata = this.workspaceService.metadata;
67 this.toscaFunction = this.property.toscaFunction ? this.property.toscaFunction : undefined;
68 this.loadToscaFunctions();
69 this.formGroup.valueChanges.subscribe(() => {
70 if (!this.isInitialized) {
73 this.emitValidityChange();
74 if (this.formGroup.valid) {
75 this.onValidFunction.emit(this.toscaFunctionForm.value);
78 this.initToscaGetFunction();
79 this.emitValidityChange();
80 this.isInitialized = true;
84 return (!this.toscaFunctionForm.value && !this.toscaFunctionTypeForm.value) || this.formGroup.valid;
87 private initToscaGetFunction() {
88 if (!this.property.isToscaGetFunction()) {
91 this.toscaFunctionForm.setValue(this.property.toscaFunction);
92 this.toscaFunctionTypeForm.setValue(this.property.toscaFunction.type);
95 private loadToscaFunctions(): void {
96 this.toscaFunctions.push(ToscaFunctionType.GET_ATTRIBUTE);
97 this.toscaFunctions.push(ToscaFunctionType.GET_INPUT);
98 this.toscaFunctions.push(ToscaFunctionType.GET_PROPERTY);
99 if (this.property.type === PROPERTY_TYPES.STRING) {
100 this.toscaFunctions.push(ToscaFunctionType.CONCAT);
104 private resetForm(): void {
105 this.formGroup.reset();
106 this.toscaFunction = undefined;
109 private isGetPropertySelected(): boolean {
110 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_PROPERTY;
113 private isGetAttributeSelected(): boolean {
114 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_ATTRIBUTE;
117 private isGetInputSelected(): boolean {
118 return this.formGroup.get('toscaFunctionType').value === ToscaGetFunctionType.GET_INPUT;
121 isConcatSelected(): boolean {
122 return this.formGroup.get('toscaFunctionType').value === ToscaFunctionType.CONCAT;
125 isGetFunctionSelected(): boolean {
126 return this.isGetInputSelected() || this.isGetPropertySelected() || this.isGetAttributeSelected();
133 showClearButton(): boolean {
134 return this.allowClear && this.toscaFunctionTypeForm.value;
137 onConcatFunctionValidityChange(validationEvent: ToscaConcatFunctionValidationEvent) {
138 if (validationEvent.isValid) {
139 this.toscaFunctionForm.setValue(validationEvent.toscaConcatFunction);
141 this.toscaFunctionForm.setValue(undefined);
145 onGetFunctionValidityChange(validationEvent: ToscaGetFunctionValidationEvent) {
146 if (validationEvent.isValid) {
147 this.toscaFunctionForm.setValue(validationEvent.toscaGetFunction);
149 this.toscaFunctionForm.setValue(undefined);
153 private emitValidityChange() {
154 const isValid = this.validate();
155 this.onValidityChange.emit({
157 toscaFunction: isValid ? this.toscaFunctionForm.value : undefined
162 export class ToscaFunctionValidationEvent {
164 toscaFunction: ToscaFunction;