summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/drawingBoard/service-planning/objectsToTree/dynamicInputs.service.ts
blob: cb0e7cf5d2a7ba24bd37363e3c9769c70321940b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {Injectable} from "@angular/core";
import {Utils} from "../../../shared/utils/utils";
import {Constants} from "../../../shared/utils/constants";
import Parameter = Constants.Parameter;

@Injectable()
export class DynamicInputsService {
  public getArbitraryInputs(inputs) {
    let parameter;
    let parameterList = [];
    for (let key in inputs) {
      parameter = {
        id: key,
        type: Parameter.STRING,
        name: key,
        value: inputs[key][Parameter.DEFAULT],
        isRequired: inputs[key][Parameter.REQUIRED],
        description: inputs[key][Parameter.DESCRIPTION]
      };
      switch (inputs[key][Parameter.TYPE]) {
        case Parameter.INTEGER:
          parameter.type = Parameter.NUMBER;
          break;
        case Parameter.BOOLEAN:
          parameter.type = Parameter.BOOLEAN;
          break;
        case Parameter.RANGE:
          break;
        case Parameter.LIST:
          parameter.type = Parameter.LIST;
          break;
        case Parameter.MAP:
          parameter.type = Parameter.MAP;
          break;
      }
      if (Utils.hasContents(inputs[key][Parameter.CONSTRAINTS])
        && ( inputs[key][Parameter.CONSTRAINTS].length > 0 )) {
        let constraintsArray = inputs[key][Parameter.CONSTRAINTS];
        this.addConstraintParameters(parameterList, constraintsArray, key, inputs, parameter);
      }
      else {

        parameterList.push(parameter);
      }
    }
    return parameterList;
  }

  private addConstraintParameters(parameterList, constraintsArray, key, inputs, parameter) {
    // If there are constraints and the operator is "valid_values",
    // use a select parameter type.
    let i: number = constraintsArray.length;
    let parameterPushed: boolean = false;
    if (i > 0) {
      while ((i--) && (!parameterPushed)) {
        let keys = Object.keys(constraintsArray[i]);
        for (let operator in keys) {
          switch (keys[operator]) {
            case Parameter.VALID_VALUES:
              let j: number = constraintsArray[i][Parameter.VALID_VALUES].length;
              if (j > 0) {
                let oList = [];
                let option;
                while (j--) {
                  option = {
                    name: constraintsArray[i][Parameter.VALID_VALUES][j],
                    isDefault: false
                  };
                  if ((Utils.hasContents(inputs[key][Parameter.DEFAULT]) )
                    && (inputs[key][Parameter.DEFAULT] === constraintsArray[i][Parameter.VALID_VALUES][j] )) {
                    option = {
                      name: constraintsArray[i][Parameter.VALID_VALUES][j],
                      isDefault: true
                    }
                  }
                  oList.push(option);
                }
                parameter.type = Parameter.SELECT;
                parameter.optionList = oList;
                parameterList.push(parameter);
                parameterPushed = true;
              }
              break;

            case Parameter.EQUAL:
              if (constraintsArray[i][Parameter.EQUAL] != null) {
                parameter.type = Parameter.STRING;
                parameter.isReadOnly = true;
                parameter.value = constraintsArray[i][Parameter.EQUAL];
                parameterList.push(parameter);
                parameterPushed = true;
              }
              break;

            case Parameter.LENGTH:
              if (constraintsArray[i][Parameter.LENGTH] != null) {
                parameter.minLength = constraintsArray[i][Parameter.LENGTH];
                parameter.maxLength = constraintsArray[i][Parameter.LENGTH];
                parameterList.push(parameter);
                parameterPushed = true;
              }
              break;
            case Parameter.MAX_LENGTH:
              if (constraintsArray[i][Parameter.MAX_LENGTH] != null) {
                parameter.maxLength = constraintsArray[i][Parameter.MAX_LENGTH];
                parameterList.push(parameter);
                parameterPushed = true;
              }
              break;
            case Parameter.MIN_LENGTH:
              if (constraintsArray[i][Parameter.MIN_LENGTH] != null) {
                parameter.minLength = constraintsArray[i][Parameter.MIN_LENGTH];
                parameterList.push(parameter);
                parameterPushed = true;
              }
              break;
            case Parameter.IN_RANGE:
              if (constraintsArray[i][Parameter.IN_RANGE] != null) {
                if (constraintsArray[i][Parameter.IN_RANGE].length > 1) {
                  parameter.min = constraintsArray[i][Parameter.IN_RANGE][0];
                  parameter.max = constraintsArray[i][Parameter.IN_RANGE][1];
                  parameter.type = Parameter.NUMBER;
                  parameter.value = inputs[key][Parameter.DEFAULT];
                  parameterList.push(parameter);
                  parameterPushed = true;
                }
              }
              break;
            case Parameter.GREATER_THAN:
              if (constraintsArray[i][Parameter.GREATER_THAN] != null) {
                parameter.type = Parameter.NUMBER;
                parameter.min = constraintsArray[i][Parameter.GREATER_THAN];
                parameter.value = inputs[key][Parameter.DEFAULT];
                parameterList.push(parameter);
                parameterPushed = true;
              }
              break;
          }
        }
      }
    }
  };
}