]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | import { Validators } from '@angular/forms'; |
11fdf7f2 TL |
2 | |
3 | import * as _ from 'lodash'; | |
4 | ||
81eedcae TL |
5 | import { CdValidators } from '../../forms/cd-validators'; |
6 | import { ConfigFormModel } from './config-option.model'; | |
7 | ||
11fdf7f2 TL |
8 | export class ConfigOptionTypes { |
9 | // TODO: I18N | |
10 | private static knownTypes: Array<any> = [ | |
11 | { | |
12 | name: 'uint', | |
13 | inputType: 'number', | |
14 | humanReadable: 'Unsigned integer value', | |
15 | defaultMin: 0, | |
16 | patternHelpText: 'The entered value needs to be an unsigned number.', | |
17 | isNumberType: true, | |
18 | allowsNegative: false | |
19 | }, | |
20 | { | |
21 | name: 'int', | |
22 | inputType: 'number', | |
23 | humanReadable: 'Integer value', | |
24 | patternHelpText: 'The entered value needs to be a number.', | |
25 | isNumberType: true, | |
26 | allowsNegative: true | |
27 | }, | |
28 | { | |
29 | name: 'size', | |
30 | inputType: 'number', | |
31 | humanReadable: 'Unsigned integer value (>=16bit)', | |
32 | defaultMin: 0, | |
33 | patternHelpText: 'The entered value needs to be a unsigned number.', | |
34 | isNumberType: true, | |
35 | allowsNegative: false | |
36 | }, | |
37 | { | |
38 | name: 'secs', | |
39 | inputType: 'number', | |
40 | humanReadable: 'Number of seconds', | |
41 | defaultMin: 1, | |
42 | patternHelpText: 'The entered value needs to be a number >= 1.', | |
43 | isNumberType: true, | |
44 | allowsNegative: false | |
45 | }, | |
46 | { | |
47 | name: 'float', | |
48 | inputType: 'number', | |
49 | humanReadable: 'Double value', | |
50 | patternHelpText: 'The entered value needs to be a number or decimal.', | |
51 | isNumberType: true, | |
52 | allowsNegative: true | |
53 | }, | |
54 | { name: 'str', inputType: 'text', humanReadable: 'Text', isNumberType: false }, | |
55 | { | |
56 | name: 'addr', | |
57 | inputType: 'text', | |
58 | humanReadable: 'IPv4 or IPv6 address', | |
59 | patternHelpText: 'The entered value needs to be a valid IP address.', | |
60 | isNumberType: false | |
61 | }, | |
62 | { | |
63 | name: 'uuid', | |
64 | inputType: 'text', | |
65 | humanReadable: 'UUID', | |
66 | patternHelpText: | |
67 | 'The entered value is not a valid UUID, e.g.: 67dcac9f-2c03-4d6c-b7bd-1210b3a259a8', | |
68 | isNumberType: false | |
69 | }, | |
70 | { name: 'bool', inputType: 'checkbox', humanReadable: 'Boolean value', isNumberType: false } | |
71 | ]; | |
72 | ||
73 | public static getType(type: string): any { | |
74 | const currentType = _.find(this.knownTypes, (t) => { | |
75 | return t.name === type; | |
76 | }); | |
77 | ||
78 | if (currentType !== undefined) { | |
79 | return currentType; | |
80 | } | |
81 | ||
82 | throw new Error('Found unknown type "' + type + '" for config option.'); | |
83 | } | |
84 | ||
85 | public static getTypeValidators(configOption: ConfigFormModel): any { | |
86 | const typeParams = ConfigOptionTypes.getType(configOption.type); | |
87 | ||
88 | if (typeParams.name === 'bool' || typeParams.name === 'str') { | |
89 | return; | |
90 | } | |
91 | ||
9f95a23c TL |
92 | const typeValidators: Record<string, any> = { |
93 | validators: [], | |
94 | patternHelpText: typeParams.patternHelpText | |
95 | }; | |
11fdf7f2 TL |
96 | |
97 | if (typeParams.isNumberType) { | |
98 | if (configOption.max && configOption.max !== '') { | |
99 | typeValidators['max'] = configOption.max; | |
100 | typeValidators.validators.push(Validators.max(configOption.max)); | |
101 | } | |
102 | ||
103 | if (configOption.min && configOption.min !== '') { | |
104 | typeValidators['min'] = configOption.min; | |
105 | typeValidators.validators.push(Validators.min(configOption.min)); | |
106 | } else if ('defaultMin' in typeParams) { | |
107 | typeValidators['min'] = typeParams.defaultMin; | |
108 | typeValidators.validators.push(Validators.min(typeParams.defaultMin)); | |
109 | } | |
110 | ||
111 | if (configOption.type === 'float') { | |
112 | typeValidators.validators.push(CdValidators.decimalNumber()); | |
113 | } else { | |
114 | typeValidators.validators.push(CdValidators.number(typeParams.allowsNegative)); | |
115 | } | |
116 | } else if (configOption.type === 'addr') { | |
117 | typeValidators.validators = [CdValidators.ip()]; | |
118 | } else if (configOption.type === 'uuid') { | |
119 | typeValidators.validators = [CdValidators.uuid()]; | |
120 | } | |
121 | ||
122 | return typeValidators; | |
123 | } | |
81eedcae TL |
124 | |
125 | public static getTypeStep(type: string, value: number): number | undefined { | |
126 | const numberTypes = ['uint', 'int', 'size', 'secs']; | |
127 | ||
128 | if (numberTypes.includes(type)) { | |
129 | return 1; | |
130 | } | |
131 | ||
132 | if (type === 'float') { | |
133 | if (value !== null) { | |
134 | const stringVal = value.toString(); | |
135 | if (stringVal.indexOf('.') !== -1) { | |
136 | // Value type float and contains decimal characters | |
137 | const decimal = value.toString().split('.'); | |
138 | return Math.pow(10, -decimal[1].length); | |
139 | } | |
140 | } | |
141 | ||
142 | return 0.1; | |
143 | } | |
144 | ||
145 | return undefined; | |
146 | } | |
11fdf7f2 | 147 | } |