| 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289 |
1
1
1
8
8
5
5
5
5
10
10
10
10
10
10
2
2
2
2
1
30
62
62
62
62
62
62
62
7
7
7
7
57
3
7
13
13
13
13
13
3
10
2
2
8
2
2
10
10
7
10
7
6
12
4
8
12
11
11
7
7
7
1
1
6
6
6
1
1
5
3
3
3
1
| 'use strict';
;require.register("views/common/configs/widgets/time_interval_spinner_view", function (exports, require, module) {
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var App = require('app');
App.TimeIntervalSpinnerView = App.ConfigWidgetView.extend({
templateName: require('templates/common/configs/widgets/time_interval_spinner'),
classNames: ['widget-config', 'spinner-input-widget'],
supportSwitchToTextBox: true,
/**
* @property isValid
* @type {Boolean}
*/
isValid: true,
/**
* @property disabled
* @type {Boolean}
*/
disabled: false,
/**
* @property valueIsChanged
* @type {Boolean}
*/
valueIsChanged: false,
/**
* Default property value in widget format.
*
* @property savedValue
* @type {Object[]}
*/
savedValue: null,
/**
* Maximum property value in widget format.
*
* @property maxValue
* @type {Object[]}
*/
maxValue: function () {
return this.generateWidgetValue(this.get('config.stackConfigProperty.valueAttributes.maximum'));
}.property('config.stackConfigProperty.valueAttributes.maximum'),
/**
* Minimum property value in widget format.
*
* @property minValue
* @type {Object[]}
*/
minValue: function () {
return this.generateWidgetValue(this.get('config.stackConfigProperty.valueAttributes.minimum'));
}.property('config.stackConfigProperty.valueAttributes.minimum'),
propertyUnitBinding: 'config.stackConfigProperty.valueAttributes.unit',
/**
* @TODO move it to unit conversion view mixin?
* Map with maximum value per unit.
*
* @property timeMaxValueOverflow
* @type {Object}
*/
timeMaxValueOverflow: {
milliseconds: 999,
seconds: 59,
minutes: 59,
hours: 23,
days: 365
},
didInsertElement: function didInsertElement() {
this._super();
this.prepareContent();
this.toggleWidgetState();
this.initPopover();
},
/**
* Content setter.
* Affects to view attributes:
* @see savedValue
* content
* @method prepareContent
*/
prepareContent: function prepareContent() {
var property = this.get('config');
this.setValue(!isNaN(parseInt(property.get('value'))) ? property.get('value') : 0);
this.parseIncrement();
},
/**
* If <code>config.stackConfigProperty.valueAttributes.increment_step</code> exists, it should be used as increment
* for controls. Also if it's value greater than maximum value for last unit, it (unit) should be disabled
* @method parseIncrement
*/
parseIncrement: function parseIncrement() {
var property = this.get('config');
var step = property.get('stackConfigProperty.valueAttributes.increment_step');
if (step) {
var type = this.get('content.lastObject.type');
step = this.convertValue(step, property.get('stackConfigProperty.valueAttributes.unit'), type);
this.set('content.lastObject.incrementStep', step);
if (step > Em.get(this, 'timeMaxValueOverflow.' + type)) {
this.set('content.lastObject.enabled', false);
}
}
},
/**
* @TODO move it to unit conversion view mixin?
* Generate formatted value for widget.
*
* @param {String|Number} value
* @returns {Object[]}
* @method generateWidgetValue
*/
generateWidgetValue: function generateWidgetValue(value) {
return this.widgetValueByConfigAttributes(value, true).map(function (item) {
item.label = Em.I18n.t('common.' + item.type);
item.minValue = 0;
item.maxValue = Em.get(this, 'timeMaxValueOverflow.' + item.type);
item.incrementStep = 1;
item.enabled = true;
item.invertOnOverflow = true;
return item;
}, this);
},
/**
* Handle changes for widget value.
* * Capture validation errors.
* * Set original config value converted from widget format
* * Send recommendations.
*/
widgetValueObserver: function () {
this.checkModified();
this.setConfigValue();
this.checkErrors();
this.sendRequestRorDependentConfigs(this.get('config'));
}.observes('content.@each.value'),
/**
* Handle config value changes in raw mode.
* Check specific validation errors regarding widget format value.
*/
configValueObserverForAttributes: function () {
if (this.get('config.showAsTextBox')) {
this.checkErrors();
}
}.observes('config.value', 'config.showAsTextBox'),
/**
* Check for property modification.
* @method checkModified
*/
checkModified: function checkModified() {
this.set('valueIsChanged', this.configValueByWidget(this.get('content')) != parseInt(this.get('config.savedValue')));
},
/**
* Check for validation errors like minimum or maximum required value
* @param {*} [val]
* @method checkErrors
*/
checkErrors: function checkErrors(val) {
val = val || this.get('content');
// in raw mode we should use config value instead of converted from widget
var convertedValue = this.get('config.showAsTextBox') ? parseInt(this.get('config.value'), 10) : this.configValueByWidget(val);
var warnMessage = '';
var warn = false;
// if not a number ignore check since it validation failed in config model as well.
if (isNaN(convertedValue)) {
return;
}
if (convertedValue < parseInt(this.get('config.stackConfigProperty.valueAttributes.minimum'))) {
warnMessage = Em.I18n.t('config.warnMessage.outOfBoundaries.less').format(this.dateToText(this.get('minValue')));
warn = true;
} else if (convertedValue > parseInt(this.get('config.stackConfigProperty.valueAttributes.maximum'))) {
warnMessage = Em.I18n.t('config.warnMessage.outOfBoundaries.greater').format(this.dateToText(this.get('maxValue')));
warn = true;
}
this.setProperties({
isValid: !warnMessage,
warnMessage: warnMessage
});
this.get('config').setProperties({
warnMessage: warnMessage,
warn: warn
});
},
/**
* set appropriate attribute for configProperty model
* @method setConfigValue
*/
setConfigValue: function setConfigValue(value) {
this.set('config.value', '' + (value || this.configValueByWidget(this.get('content'))));
},
setValue: function setValue(value) {
if (value && !isNaN(value) || !isNaN(this.get('config.value'))) {
this.set('content', this.generateWidgetValue(value || this.get('config.value')));
}
},
setRecommendedValue: function setRecommendedValue() {
this._super();
this.setValue();
this.parseIncrement();
},
/**
* Convert value to readable format using widget value.
*
* @param {Object[]} widgetFormatValue - value formatted for widget @see convertToWidgetUnits
* @return {String}
* @method dateToText
*/
dateToText: function dateToText(widgetFormatValue) {
return widgetFormatValue.map(function (item) {
if (Em.get(item, 'value') > 0) {
return Em.get(item, 'value') + ' ' + Em.get(item, 'label');
} else {
return null;
}
}).compact().join(' ');
},
/**
* Restore value to default.
* @method restoreValue
*/
restoreValue: function restoreValue() {
this._super();
this.setValue(this.get('config.savedValue'));
this.parseIncrement();
},
isValueCompatibleWithWidget: function isValueCompatibleWithWidget() {
if (this._super()) {
var configValue = parseInt(this.get('config.value'));
if (isNaN(configValue)) return false;
Eif (this.get('config.stackConfigProperty.valueAttributes.minimum')) {
var min = parseInt(this.get('config.stackConfigProperty.valueAttributes.minimum'));
if (configValue < min) {
this.updateWarningsForCompatibilityWithWidget(Em.I18n.t('config.warnMessage.outOfBoundaries.less').format(this.dateToText(this.get('minValue'))));
return false;
}
}
Eif (this.get('config.stackConfigProperty.valueAttributes.maximum')) {
var max = parseInt(this.get('config.stackConfigProperty.valueAttributes.maximum'));
if (configValue > max) {
this.updateWarningsForCompatibilityWithWidget(Em.I18n.t('config.warnMessage.outOfBoundaries.greater').format(this.dateToText(this.get('maxValue'))));
return false;
}
}
if (this.get('config.stackConfigProperty.valueAttributes.increment_step')) {
if (configValue % this.get('config.stackConfigProperty.valueAttributes.increment_step') != 0) return false;
}
this.updateWarningsForCompatibilityWithWidget('');
return true;
}
return false;
}
});
}); |