2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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, SimpleChanges} from '@angular/core';
25 FormGroup, ValidationErrors,
28 } from '@angular/forms';
32 } from '../../../../../../models/interfaceOperation';
33 import { InstanceFeDetails } from '../../../../../../models/instance-fe-details';
34 import { ConstraintTypes, ConstraintTypesMapping } from '../../../../../pages/properties-assignment/constraints/constraints.component';
35 import {CustomToscaFunction } from '../../../../../../models/default-custom-functions';
36 import { ToscaFunction } from '../../../../../../models/tosca-function';
37 import { PropertyBEModel } from '../../../../../../models/properties-inputs/property-be-model';
38 import { ToscaFunctionValidationEvent } from '../../../../properties-assignment/tosca-function/tosca-function.component';
41 selector: 'filters-list',
42 templateUrl: './filters-list.component.html',
43 styleUrls: ['./filters-list.component.less']
45 export class FiltersListComponent implements OnInit {
47 @Input() customToscaFunctions: Array<CustomToscaFunction> = [];
48 @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map();
49 @Input() activitiesExist: boolean;
50 @Input() existingFilters: IFilterParameterList;
51 @Input() isViewOnly: boolean;
52 @Output('filtersChangeEvent') filtersChangeEvent: EventEmitter<any> = new EventEmitter<any>();
54 isAProperty: Map<number, PropertyBEModel> = new Map();
56 ConstraintTypesMapping = ConstraintTypesMapping;
57 filters: FilterParameter[] = [];
58 filterFormArray: FormArray = new FormArray([]);
59 filterForm: FormGroup = new FormGroup (
61 'filterFormList': this.filterFormArray
64 validationMessages = {
66 { type: 'required', message: 'Filter name, constraint, and value is required'}
71 this.operatorTypes = Object.keys(ConstraintTypes).map((key) => ConstraintTypes[key]);
74 ngOnChanges (changes: SimpleChanges) {
75 this.filterForm.valueChanges.subscribe(() => {
76 this.emitOnFilterChange();
78 if (!changes.activitiesExist) {
81 if (changes.activitiesExist.currentValue) {
85 this.filterFormArray = new FormArray([]);
86 this. filterForm = new FormGroup (
88 'filterFormList': this.filterFormArray
94 private initFilters () {
95 if (this.existingFilters && this.existingFilters.listToscaDataDefinition && this.existingFilters.listToscaDataDefinition.length > 0) {
96 this.existingFilters.listToscaDataDefinition.forEach(val => {
97 this.filters.push(val);
98 this.filterFormArray.push(
99 new FormControl(val, [Validators.required, this.formControlValidator()])
105 private emitOnFilterChange (): void {
106 this.filtersChangeEvent.emit({
107 filters: this.filters,
108 valid: this.filterForm.valid
113 let filterParameter: FilterParameter = {
118 this.filters.push(filterParameter);
119 this.filterFormArray.push(
120 new FormControl(filterParameter, [Validators.required, this.formControlValidator()])
124 private formControlValidator (): ValidatorFn {
125 return (control: AbstractControl): ValidationErrors | null => {
126 const filter = control.value;
128 if (!filter || !filter.name || !filter.constraint || !filter.filterValue) {
129 return {required:true};
135 removeFromFilters (index: number) {
136 this.filters.splice(index, 1);
137 this.filterFormArray.removeAt(index);
140 onFilterConstraintChange (type: string, index: number) {
141 let filter = this.filterFormArray.controls[index].value;
142 filter.constraint = type;
143 if (ConstraintTypes.valid_values == type || ConstraintTypes.in_range == type) {
144 filter.filterValue = [];
146 filter.filterValue = '';
148 this.filterFormArray.controls[index].setValue(filter);
151 onFilterValueChange (value: any, index: number) {
152 let filter = this.filterFormArray.controls[index].value;
153 filter.filterValue = value;
154 this.filterFormArray.controls[index].setValue(filter);
157 onFilterNameChange (value: any, index: number) {
158 let filter = this.filterFormArray.controls[index].value;
160 this.filterFormArray.controls[index].setValue(filter);
163 getInRangeValue (index: number, valueIndex: number): string {
164 const value = this.filters[index].filterValue;
166 if (!value || !value[valueIndex]) {
170 return value[valueIndex];
173 onChangeConstrainValueIndex (index: number, newValue: any, valueIndex: number) {
174 let filter = this.filterFormArray.controls[index].value;
175 if (!filter.filterValue) {
176 filter.filterValue = [];
178 filter.filterValue[valueIndex] = newValue;
179 this.filterFormArray.controls[index].setValue(filter);
182 constraintValuesArray (index: number) {
183 let filters = this.filterForm.get('filterFormList') as FormArray;
184 return filters.at(index).value.filterValue;
187 addToList (filterIndex: number) {
188 this.constraintValuesArray(filterIndex).push('');
191 removeFromList (filterIndex: number, valueIndex: number) {
192 this.constraintValuesArray(filterIndex).splice(valueIndex, 1);
195 onValueTypeChange (value: string, index: number) {
196 if (value === 'true') {
197 let filter = this.filterFormArray.controls[index].value;
198 if (!filter.toscaFunction) {
199 filter.toscaFunction = {} as ToscaFunction;
201 filter.filterValue = '';
202 this.filterFormArray.controls[index].setValue(filter);
204 let filter = this.filterFormArray.controls[index].value;
205 filter.toscaFunction = undefined;
206 this.filterFormArray.controls[index].setValue(filter);
207 if (this.isAProperty.has(index)) {
208 this.isAProperty.delete(index);
213 getAsProperty(index: number) {
214 if (!this.isAProperty.has(index)) {
215 let filter = this.filterFormArray.controls[index].value;
216 let property = new PropertyBEModel();
217 property.type = 'any';
218 property.toscaFunction = filter.toscaFunction;
219 property.value = filter.filterValue;
220 this.isAProperty.set(index, property);
223 return this.isAProperty.get(index);
226 onToscaFunctionValidityChange(validationEvent: ToscaFunctionValidationEvent, index: number):void {
227 if (validationEvent.isValid) {
228 let filter = this.filterFormArray.controls[index].value;
229 filter.toscaFunction = validationEvent.toscaFunction;
230 filter.filterValue = validationEvent.toscaFunction.buildValueString();
231 this.filterFormArray.controls[index].setValue(filter);
235 isToscaFunction(index: number): boolean {
236 let filter = this.filterFormArray.controls[index].value;
237 let toscaFunction = filter.toscaFunction;
238 return toscaFunction;