blob: cb0e7cf5d2a7ba24bd37363e3c9769c70321940b [file] [log] [blame]
Ittay Stern6f900cc2018-08-29 17:01:32 +03001import {Injectable} from "@angular/core";
2import {Utils} from "../../../shared/utils/utils";
3import {Constants} from "../../../shared/utils/constants";
4import Parameter = Constants.Parameter;
5
6@Injectable()
7export class DynamicInputsService {
8 public getArbitraryInputs(inputs) {
9 let parameter;
10 let parameterList = [];
11 for (let key in inputs) {
12 parameter = {
13 id: key,
14 type: Parameter.STRING,
15 name: key,
16 value: inputs[key][Parameter.DEFAULT],
17 isRequired: inputs[key][Parameter.REQUIRED],
18 description: inputs[key][Parameter.DESCRIPTION]
19 };
20 switch (inputs[key][Parameter.TYPE]) {
21 case Parameter.INTEGER:
22 parameter.type = Parameter.NUMBER;
23 break;
24 case Parameter.BOOLEAN:
25 parameter.type = Parameter.BOOLEAN;
26 break;
27 case Parameter.RANGE:
28 break;
29 case Parameter.LIST:
30 parameter.type = Parameter.LIST;
31 break;
32 case Parameter.MAP:
33 parameter.type = Parameter.MAP;
34 break;
35 }
36 if (Utils.hasContents(inputs[key][Parameter.CONSTRAINTS])
37 && ( inputs[key][Parameter.CONSTRAINTS].length > 0 )) {
38 let constraintsArray = inputs[key][Parameter.CONSTRAINTS];
39 this.addConstraintParameters(parameterList, constraintsArray, key, inputs, parameter);
40 }
41 else {
42
43 parameterList.push(parameter);
44 }
45 }
46 return parameterList;
47 }
48
49 private addConstraintParameters(parameterList, constraintsArray, key, inputs, parameter) {
50 // If there are constraints and the operator is "valid_values",
51 // use a select parameter type.
52 let i: number = constraintsArray.length;
53 let parameterPushed: boolean = false;
54 if (i > 0) {
55 while ((i--) && (!parameterPushed)) {
56 let keys = Object.keys(constraintsArray[i]);
57 for (let operator in keys) {
58 switch (keys[operator]) {
59 case Parameter.VALID_VALUES:
60 let j: number = constraintsArray[i][Parameter.VALID_VALUES].length;
61 if (j > 0) {
62 let oList = [];
63 let option;
64 while (j--) {
65 option = {
66 name: constraintsArray[i][Parameter.VALID_VALUES][j],
67 isDefault: false
68 };
69 if ((Utils.hasContents(inputs[key][Parameter.DEFAULT]) )
70 && (inputs[key][Parameter.DEFAULT] === constraintsArray[i][Parameter.VALID_VALUES][j] )) {
71 option = {
72 name: constraintsArray[i][Parameter.VALID_VALUES][j],
73 isDefault: true
74 }
75 }
76 oList.push(option);
77 }
78 parameter.type = Parameter.SELECT;
79 parameter.optionList = oList;
80 parameterList.push(parameter);
81 parameterPushed = true;
82 }
83 break;
84
85 case Parameter.EQUAL:
86 if (constraintsArray[i][Parameter.EQUAL] != null) {
87 parameter.type = Parameter.STRING;
88 parameter.isReadOnly = true;
89 parameter.value = constraintsArray[i][Parameter.EQUAL];
90 parameterList.push(parameter);
91 parameterPushed = true;
92 }
93 break;
94
95 case Parameter.LENGTH:
96 if (constraintsArray[i][Parameter.LENGTH] != null) {
97 parameter.minLength = constraintsArray[i][Parameter.LENGTH];
98 parameter.maxLength = constraintsArray[i][Parameter.LENGTH];
99 parameterList.push(parameter);
100 parameterPushed = true;
101 }
102 break;
103 case Parameter.MAX_LENGTH:
104 if (constraintsArray[i][Parameter.MAX_LENGTH] != null) {
105 parameter.maxLength = constraintsArray[i][Parameter.MAX_LENGTH];
106 parameterList.push(parameter);
107 parameterPushed = true;
108 }
109 break;
110 case Parameter.MIN_LENGTH:
111 if (constraintsArray[i][Parameter.MIN_LENGTH] != null) {
112 parameter.minLength = constraintsArray[i][Parameter.MIN_LENGTH];
113 parameterList.push(parameter);
114 parameterPushed = true;
115 }
116 break;
117 case Parameter.IN_RANGE:
118 if (constraintsArray[i][Parameter.IN_RANGE] != null) {
119 if (constraintsArray[i][Parameter.IN_RANGE].length > 1) {
120 parameter.min = constraintsArray[i][Parameter.IN_RANGE][0];
121 parameter.max = constraintsArray[i][Parameter.IN_RANGE][1];
122 parameter.type = Parameter.NUMBER;
123 parameter.value = inputs[key][Parameter.DEFAULT];
124 parameterList.push(parameter);
125 parameterPushed = true;
126 }
127 }
128 break;
129 case Parameter.GREATER_THAN:
130 if (constraintsArray[i][Parameter.GREATER_THAN] != null) {
131 parameter.type = Parameter.NUMBER;
132 parameter.min = constraintsArray[i][Parameter.GREATER_THAN];
133 parameter.value = inputs[key][Parameter.DEFAULT];
134 parameterList.push(parameter);
135 parameterPushed = true;
136 }
137 break;
138 }
139 }
140 }
141 }
142 };
143}