UI Support for operation milestones
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / interface-operatons / operation-creator / filters-list / filters-list.component.ts
1 /*
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
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, EventEmitter, Input, OnInit, Output, SimpleChanges} from '@angular/core';
21 import {
22   AbstractControl,
23   FormArray,
24   FormControl,
25   FormGroup, ValidationErrors,
26   ValidatorFn,
27   Validators
28 } from '@angular/forms';
29 import {
30   FilterParameter,
31   IFilterParameterList
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';
39
40 @Component({
41   selector: 'filters-list',
42   templateUrl: './filters-list.component.html',
43   styleUrls: ['./filters-list.component.less']
44 })
45 export class FiltersListComponent implements OnInit {
46
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>();
53
54   isAProperty: Map<number, PropertyBEModel> = new Map();
55   operatorTypes: any[];
56   ConstraintTypesMapping = ConstraintTypesMapping;
57   filters: FilterParameter[] = [];
58   filterFormArray: FormArray = new FormArray([]);
59   filterForm: FormGroup = new FormGroup (
60     {
61       'filterFormList': this.filterFormArray
62     }
63   );
64   validationMessages = {
65     filter: [
66       { type: 'required', message: 'Filter name, constraint, and value is required'}
67     ]
68   };
69
70   ngOnInit () {
71     this.operatorTypes = Object.keys(ConstraintTypes).map((key) => ConstraintTypes[key]);
72   }
73
74   ngOnChanges (changes: SimpleChanges) {
75     this.filterForm.valueChanges.subscribe(() => {
76       this.emitOnFilterChange();
77     });
78     if (!changes.activitiesExist) {
79       return;
80     }
81     if (changes.activitiesExist.currentValue) {
82       this.initFilters();
83     } else {
84       this.filters = [];
85       this.filterFormArray = new FormArray([]);
86       this. filterForm = new FormGroup (
87         {
88           'filterFormList': this.filterFormArray
89         }
90       );
91     }
92   }
93
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()])
100         );
101       })
102     }
103   }
104
105   private emitOnFilterChange (): void {
106     this.filtersChangeEvent.emit({
107       filters: this.filters,
108       valid: this.filterForm.valid
109     });
110   }
111
112   addFilter () {
113     let filterParameter: FilterParameter = {
114       name: null,
115       constraint: null,
116       filterValue: null
117     }
118     this.filters.push(filterParameter);
119     this.filterFormArray.push(
120       new FormControl(filterParameter, [Validators.required, this.formControlValidator()])
121     );
122   }
123
124   private formControlValidator (): ValidatorFn {
125     return (control: AbstractControl): ValidationErrors | null => {
126       const filter = control.value;
127
128       if (!filter || !filter.name || !filter.constraint || !filter.filterValue) {
129         return {required:true};
130       }
131       return null;
132     }
133   }
134
135   removeFromFilters (index: number) {
136     this.filters.splice(index, 1);
137     this.filterFormArray.removeAt(index);
138   }
139
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 = [];
145     } else {
146       filter.filterValue = '';
147     }
148     this.filterFormArray.controls[index].setValue(filter);
149   }
150
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);
155   }
156
157   onFilterNameChange (value: any, index: number) {
158     let filter = this.filterFormArray.controls[index].value;
159     filter.name = value;
160     this.filterFormArray.controls[index].setValue(filter);
161   }
162
163   getInRangeValue (index: number, valueIndex: number): string {
164     const value = this.filters[index].filterValue;
165
166     if (!value || !value[valueIndex]) {
167       return '';
168     }
169
170     return value[valueIndex];
171   }
172
173   onChangeConstrainValueIndex (index: number, newValue: any, valueIndex: number) {
174     let filter = this.filterFormArray.controls[index].value;
175     if (!filter.filterValue) {
176       filter.filterValue = [];
177     }
178     filter.filterValue[valueIndex] = newValue;
179     this.filterFormArray.controls[index].setValue(filter);
180   }
181
182   constraintValuesArray (index: number) {
183     let filters = this.filterForm.get('filterFormList') as FormArray;
184     return filters.at(index).value.filterValue;
185   }
186
187   addToList (filterIndex: number) {
188     this.constraintValuesArray(filterIndex).push('');
189   }
190
191   removeFromList (filterIndex: number, valueIndex: number) {
192     this.constraintValuesArray(filterIndex).splice(valueIndex, 1);
193   }
194
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;
200       }
201       filter.filterValue = '';
202       this.filterFormArray.controls[index].setValue(filter);
203     } else {
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);
209       }
210     }
211   }
212
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);
221       return property;
222     }
223     return this.isAProperty.get(index);
224   }
225
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);
232     }
233   }
234
235   isToscaFunction(index: number): boolean {
236     let filter = this.filterFormArray.controls[index].value;
237     let toscaFunction = filter.toscaFunction;
238     return toscaFunction;
239   }
240
241 }