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
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 { PropertyBEModel } from "app/models";
24 selector: 'app-constraints',
25 templateUrl: './constraints.component.html',
26 styleUrls: ['./constraints.component.less']
28 export class ConstraintsComponent implements OnInit {
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));
39 @Input() isViewOnly: boolean = false;
40 @Output() onConstraintChange: EventEmitter<any[]> = new EventEmitter<any[]>();
42 constraints: Constraint[];
43 constraintTypes: string[];
44 ConstraintTypesMapping = ConstraintTypesMapping;
45 newConstraintType: any = ConstraintTypes.equal;
46 newConstraintValue: any = null;
47 _property: PropertyBEModel;
50 this.constraintTypes = Object.keys(ConstraintTypes).map(key => ConstraintTypes[key]);
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;
96 private getConstraintsFormat(): any[] {
97 let constraintArray = new Array();
98 this.constraints.forEach((constraint: Constraint) => {
99 constraintArray.push(this.getConstraintFormat(constraint))
101 return constraintArray;
104 private getConstraintFormat(constraint: Constraint) {
105 switch (constraint.type) {
106 case ConstraintTypes.equal:
108 [ConstraintTypes.equal]: constraint.value
110 case ConstraintTypes.less_or_equal:
112 [ConstraintTypes.less_or_equal]: constraint.value
114 case ConstraintTypes.less_than:
116 [ConstraintTypes.less_than]: constraint.value
118 case ConstraintTypes.greater_or_equal:
120 [ConstraintTypes.greater_or_equal]: constraint.value
122 case ConstraintTypes.greater_than:
124 [ConstraintTypes.greater_than]: constraint.value
126 case ConstraintTypes.in_range:
128 [ConstraintTypes.in_range]: constraint.value
130 case ConstraintTypes.length:
132 [ConstraintTypes.length]: constraint.value
134 case ConstraintTypes.max_length:
136 [ConstraintTypes.max_length]: constraint.value
138 case ConstraintTypes.min_length:
140 [ConstraintTypes.min_length]: constraint.value
142 case ConstraintTypes.pattern:
144 [ConstraintTypes.pattern]: constraint.value
146 case ConstraintTypes.valid_values:
148 [ConstraintTypes.valid_values]: constraint.value
155 removeFromList(constraintIndex: number, valueIndex: number){
156 this.constraints[constraintIndex].value.splice(valueIndex, 1);
159 addToList(constraintIndex: number){
160 if (!this.constraints[constraintIndex].value) {
161 this.constraints[constraintIndex].value = new Array();
163 this.constraints[constraintIndex].value.push("");
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()
173 onChangeConstraintValue(constraintIndex: number, newValue: any) {
174 this.constraints[constraintIndex].value = newValue;
177 onChangeConstrainValueIndex(constraintIndex: number, newValue: any, valueIndex: number) {
178 if(!this.constraints[constraintIndex].value) {
179 this.constraints[constraintIndex].value = new Array();
181 this.constraints[constraintIndex].value[valueIndex] = newValue;
184 removeConstraint(constraintIndex: number) {
185 this.constraints.splice(constraintIndex, 1);
186 this.onConstraintChange.emit(this.getConstraintsFormat());
190 let newConstraint: Constraint = {
191 type:this.newConstraintType,
192 value: this.newConstraintValue
194 this.constraints.push(newConstraint);
195 this.newConstraintValue = null;
196 this.onConstraintChange.emit(this.getConstraintsFormat());
199 getInRangeValue(constraintIndex: number, valueIndex: number): string {
200 if(!this.constraints[constraintIndex].value || !this.constraints[constraintIndex].value[valueIndex]) {
203 return this.constraints[constraintIndex].value[valueIndex];
208 export enum ConstraintTypes {
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",
217 min_length = "minLength",
218 max_length = "maxLength",
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"
236 export interface Constraint {
237 type:ConstraintTypes,