ef4d6fd95411b1d8772bedf2ca5675385059779b
[sdc.git] / catalog-ui / src / app / ng2 / pages / properties-assignment / constraints / constraints.component.ts
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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 } from '@angular/core';
21 import { PropertyBEModel } from "app/models";
22
23 @Component({
24   selector: 'app-constraints',
25   templateUrl: './constraints.component.html',
26   styleUrls: ['./constraints.component.less']
27 })
28 export class ConstraintsComponent implements OnInit {
29
30   @Input() set property(property: PropertyBEModel) {
31     this.constraints = new Array();
32     if(property.constraints) {
33       this._property = property;
34       property.constraints.forEach((constraint: any) => {
35         this.constraints.push(this.getConstraintFromPropertyBEModel(constraint));
36       });
37     }
38   }
39   @Input() isViewOnly: boolean = false;
40   @Output() onConstraintChange: EventEmitter<any[]> = new EventEmitter<any[]>();
41
42   constraints: Constraint[];
43   constraintTypes: string[];
44   ConstraintTypesMapping = ConstraintTypesMapping;
45   newConstraintType: any = ConstraintTypes.equal;
46   newConstraintValue: any = null;
47   _property: PropertyBEModel;
48
49   ngOnInit() {
50     this.constraintTypes = Object.keys(ConstraintTypes).map(key => ConstraintTypes[key]);
51   }
52
53   private getConstraintFromPropertyBEModel(constraint: any):Constraint {
54     let constraintType: ConstraintTypes;
55     let constraintValue: any;
56     if(constraint.validValues){
57       constraintType = ConstraintTypes.valid_values;
58       constraintValue = constraint.validValues;
59     } else if(constraint.equal) {
60       constraintType = ConstraintTypes.equal;
61       constraintValue = constraint.equal;
62     } else if(constraint.greaterThan) {
63       constraintType = ConstraintTypes.greater_than;
64       constraintValue = constraint.greaterThan;
65     } else if(constraint.greaterOrEqual) {
66       constraintType = ConstraintTypes.greater_or_equal;
67       constraintValue = constraint.greaterOrEqual;
68     } else if(constraint.lessThan) {
69       constraintType = ConstraintTypes.less_than;
70       constraintValue = constraint.lessThan;
71     } else if(constraint.lessOrEqual) {
72       constraintType = ConstraintTypes.less_or_equal;
73       constraintValue = constraint.lessOrEqual;
74     } else if(constraint.rangeMinValue && constraint.rangeMaxValue) {
75       constraintType = ConstraintTypes.in_range;
76       constraintValue = new Array(constraint.rangeMinValue, constraint.rangeMaxValue);
77     } else if(constraint.length) {
78       constraintType = ConstraintTypes.length;
79       constraintValue = constraint.length;
80     } else if(constraint.minLength) {
81       constraintType = ConstraintTypes.min_length;
82       constraintValue = constraint.minLength;
83     } else if(constraint.maxLength) {
84       constraintType = ConstraintTypes.max_length;
85       constraintValue = constraint.maxLength;
86     } else if(constraint.pattern) {
87       constraintType = ConstraintTypes.pattern;
88       constraintValue = constraint.pattern;
89     }
90     return {
91       type:constraintType,
92       value:constraintValue
93     }
94   }
95
96   private getConstraintsFormat(): any[] {
97     let constraintArray = new Array();
98     this.constraints.forEach((constraint: Constraint) => {
99       constraintArray.push(this.getConstraintFormat(constraint))
100     });
101     return constraintArray;
102   }
103
104   private getConstraintFormat(constraint: Constraint) {
105     switch (constraint.type) {
106       case ConstraintTypes.equal:
107         return {
108           [ConstraintTypes.equal]: constraint.value
109         }
110       case ConstraintTypes.less_or_equal:
111         return {
112           [ConstraintTypes.less_or_equal]: constraint.value
113         }
114       case ConstraintTypes.less_than:
115         return {
116           [ConstraintTypes.less_than]: constraint.value
117         }
118       case ConstraintTypes.greater_or_equal:
119         return {
120           [ConstraintTypes.greater_or_equal]: constraint.value
121         }
122       case ConstraintTypes.greater_than:
123         return {
124           [ConstraintTypes.greater_than]: constraint.value
125         }
126       case ConstraintTypes.in_range:
127         return {
128           [ConstraintTypes.in_range]: constraint.value
129         }
130       case ConstraintTypes.length:
131         return {
132           [ConstraintTypes.length]: constraint.value
133         }
134       case ConstraintTypes.max_length:
135         return {
136           [ConstraintTypes.max_length]: constraint.value
137         }
138       case ConstraintTypes.min_length:
139         return {
140           [ConstraintTypes.min_length]: constraint.value
141         }
142       case ConstraintTypes.pattern:
143         return {
144           [ConstraintTypes.pattern]: constraint.value
145         }
146       case ConstraintTypes.valid_values:
147         return {
148           [ConstraintTypes.valid_values]: constraint.value
149         }
150       default:
151         return;
152     }
153   }
154
155   removeFromList(constraintIndex: number, valueIndex: number){
156     this.constraints[constraintIndex].value.splice(valueIndex, 1);
157   }
158
159   addToList(constraintIndex: number){
160     if (!this.constraints[constraintIndex].value) {
161       this.constraints[constraintIndex].value = new Array();
162     }
163     this.constraints[constraintIndex].value.push("");
164   }
165
166   onChangeConstraintType(constraintIndex: number, newType: ConstraintTypes) {
167     this.constraints[constraintIndex].type = newType;
168     if ((newType == ConstraintTypes.in_range || newType == ConstraintTypes.valid_values) && !Array.isArray(this.constraints[constraintIndex].value)) {
169       this.constraints[constraintIndex].value = new Array()
170     }
171   }
172
173   onChangeConstraintValue(constraintIndex: number, newValue: any) {
174     this.constraints[constraintIndex].value = newValue;
175   }
176
177   onChangeConstrainValueIndex(constraintIndex: number, newValue: any, valueIndex: number) {
178     if(!this.constraints[constraintIndex].value) {
179       this.constraints[constraintIndex].value = new Array();
180     }
181     this.constraints[constraintIndex].value[valueIndex] = newValue;
182   }
183
184   removeConstraint(constraintIndex: number) {
185     this.constraints.splice(constraintIndex, 1);
186     this.onConstraintChange.emit(this.getConstraintsFormat());
187 }
188
189   addConstraint() {
190     let newConstraint: Constraint = {
191       type:this.newConstraintType,
192       value: this.newConstraintValue
193     }
194     this.constraints.push(newConstraint);
195     this.newConstraintValue = null;
196     this.onConstraintChange.emit(this.getConstraintsFormat());
197   }
198
199   getInRangeValue(constraintIndex: number, valueIndex: number): string {
200     if(!this.constraints[constraintIndex].value || !this.constraints[constraintIndex].value[valueIndex]) {
201       return "";
202     }
203     return this.constraints[constraintIndex].value[valueIndex];
204   }
205
206 }
207
208 export enum ConstraintTypes {
209   equal= "equal",
210   greater_than = "greaterThan",
211   greater_or_equal = "greaterOrEqual",
212   less_than = "lessThan",
213   less_or_equal = "lessOrEqual",
214   in_range = "inRange",
215   valid_values = "validValues",
216   length = "length",
217   min_length = "minLength",
218   max_length = "maxLength",
219   pattern = "pattern"
220 }
221
222 export const ConstraintTypesMapping = {
223   [ConstraintTypes.equal]: "equal",
224   [ConstraintTypes.greater_than]: "greater_than",
225   [ConstraintTypes.greater_or_equal]: "greater_or_equal",
226   [ConstraintTypes.less_than]: "less_than",
227   [ConstraintTypes.less_or_equal]: "less_or_equal",
228   [ConstraintTypes.in_range]: "in_range",
229   [ConstraintTypes.valid_values]: "valid_values",
230   [ConstraintTypes.length]: "length",
231   [ConstraintTypes.min_length]: "min_length",
232   [ConstraintTypes.max_length]: "max_length",
233   [ConstraintTypes.pattern]: "pattern"
234 };
235
236 export interface Constraint {
237   type:ConstraintTypes,
238   value:any
239
240 }