Newer
Older
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/**
* Created by gavorhes on 12/10/2015.
*/
"use strict";
var provide_1 = require('../util/provide');
var nm = provide_1.default('collections');
var $ = require('jquery');
var TipPresets = (function () {
function TipPresets(conf) {
this.label = conf.label;
this.presets = conf.presets;
this.domId = this.label.replace(/ /g, '').toLowerCase();
}
return TipPresets;
}());
var _Slider = (function () {
/**
* Slider constructor
* @param sliderConfig - the configuration
*/
function _Slider(sliderConfig) {
//let _this = this;
this._dropdownSelection = null;
this._weight = null;
this.name = sliderConfig.label;
this.domId = this.name.toLowerCase().replace(/ /g, '-');
this._locked = false;
this._min = 0.0;
this._max = 100;
this.labelLow = null;
this.labelHigh = null;
this.labelVal = null;
this.slider = null;
this.chk = null;
this.atMin = false;
this.atMax = false;
var sel = "<select class=\"" + (sliderConfig.yearOptions.length == 1 ? 'hidden-select' : 'show-select') + "\" id=\"" + this.domId + "_chg\">";
for (var i = 0; i < sliderConfig.yearOptions.length; i++) {
var itm = sliderConfig.yearOptions[i];
sel += "<option value=\"" + itm.column + "\">" + itm.label + "</option>";
}
sel += '</select>';
this.selectedParamDefault = this.selectedParam;
this.html = '<div class="slider-div">' +
("<label for=\"" + this.domId + "_chk\" class=\"slider-label\">" + this.name + "</label>") +
sel + "<br>" +
("<input id=\"" + this.domId + "_chk\" type=\"checkbox\" title=\"Lock/Unlock Slider\">") +
("<label id=\"" + this.domId + "_low\" class=\"low-high\"></label>") +
("<input id=\"" + this.domId + "\" type=\"range\" value=\"50\" min=\"0\" max=\"100\" step=\"0.1\">") +
("<label id=\"" + this.domId + "_high\" class=\"low-high\"></label>") +
("<label id=\"" + this.domId + "_lbl\" for=\"" + this.domId + "\" class=\"percent-label\"></label></div>");
}
/**
* add html to dom
* @param {jQuery} $container - container element
*/
_Slider.prototype.addToDom = function ($container) {
$container.append(this.html);
this.labelLow = $("#" + this.domId + "_low");
this.labelHigh = $("#" + this.domId + "_high");
this.labelVal = $("#" + this.domId + "_lbl");
this.slider = $("#" + this.domId);
this.selectionBox = $("#" + this.domId + "_chg");
this.chk = $("#" + this.domId + "_chk");
};
/**
* increment the slider
* @param {number} delta change delta
* @returns {number} the remainder not able to be allocated to this slider
*/
_Slider.prototype.increment = function (delta) {
var remainder = 0;
delta = Number(delta.toFixed(1));
this._weight += delta;
if (this._weight < this._min) {
remainder = this._min - this._weight;
this._weight = this._min;
this.atMin = true;
}
else if (this._weight > this._max) {
remainder = this._max - this._weight;
this._weight = this._max;
this.atMax = true;
}
else {
this.atMin = false;
this.atMax = false;
}
this.slider.val(this._weight.toFixed(1));
this.labelVal.html(this._weight.toFixed(1) + '%');
return remainder;
};
/**
* set the value and drop down
* @param {number} newVal the new value
* @param {string} selectedParam the selected parameter
*/
_Slider.prototype.setValAndDropDown = function (newVal, selectedParam) {
this.min = 0;
this.max = 100;
this.weight = newVal;
this.slider.val(newVal.toFixed(1));
this.selectionBox.val(selectedParam);
this.selectedParam = selectedParam;
this.locked = true;
};
Object.defineProperty(_Slider.prototype, "locked", {
/**
*
* @returns {boolean} if locked
*/
get: function () {
return this._locked;
},
/**
*
* @param {boolean} val if locked
*/
set: function (val) {
this._locked = val;
this.slider.prop('disabled', this._locked);
this.selectionBox.prop('disabled', this._locked);
this.chk.prop('checked', !this._locked);
},
enumerable: true,
configurable: true
});
Object.defineProperty(_Slider.prototype, "min", {
/**
*
* @returns {number} the minimum
*/
get: function () {
return this._min;
},
/**
*
* @param {number} newVal new minimum
*/
set: function (newVal) {
this._min = Number(newVal.toFixed(1));
if (this._min < 0) {
this._min = 0;
}
this.labelLow.html(this._min.toFixed(1));
this.slider.attr('min', this._min.toFixed(1));
this.atMin = this._weight == this._min;
},
enumerable: true,
configurable: true
});
Object.defineProperty(_Slider.prototype, "max", {
/**
*
* @returns {number} the maximum
*/
get: function () {
return this._max;
},
/**
*
* @param {number} newVal the maximum
*/
set: function (newVal) {
this._max = Number(newVal.toFixed(1));
if (this._max > 100) {
this._max = 100.0;
}
this.labelHigh.html(this._max.toFixed(1));
this.slider.attr('max', this._max.toFixed(1));
this.atMax = this._weight == this._max;
},
enumerable: true,
configurable: true
});
Object.defineProperty(_Slider.prototype, "weight", {
/**
*
* @returns {number} the weight
*/
get: function () {
return this._weight;
},
/**
*
* @param {number} newVal the weight
*/
set: function (newVal) {
this._weight = Number(newVal.toFixed(1));
this.labelVal.html(this._weight.toFixed(1) + '%');
if (this._weight <= this._min) {
this.atMin = true;
this.atMax = false;
}
else if (this._weight >= this._max) {
this.atMin = false;
this.atMax = true;
}
else {
this.atMin = false;
this.atMax = false;
}
},
enumerable: true,
configurable: true
});
return _Slider;
}());
nm._Slider = _Slider;
/**
* class to keep track of the sliders
*/
var TipSliders = (function () {
/**
*
* @param sliderConfigs
* @param presetConfig
* @param divId
* @param presetSelectorId
* @param regionSelectorId
* @param versionSelectorId
*/
function TipSliders(sliderConfigs, presetConfig, divId, presetSelectorId, regionSelectorId, versionSelectorId, chgCallback) {
var _this = this;
this.resetting = false;
this.reservedPercent = 0.0;
this.$container = $('#' + divId);
this.$container.addClass('slider-container');
this.changedCallback = typeof chgCallback == 'function' ? chgCallback : function () { };
this._$presetSelector = $('#' + presetSelectorId);
this._$regionSelector = $('#' + regionSelectorId);
this._$versionSelector = $('#' + versionSelectorId);
this._sliderList = [];
this._sliderLookup = {};
for (var i = 0; i < sliderConfigs.length; i++) {
var sld = new _Slider(sliderConfigs[i]);
this._sliderList.push(sld);
this._sliderLookup[sld.domId] = sld;
sld.addToDom(this.$container);
}
this._presetArray = [];
this._presetLookup = {};
for (var i = 0; i < presetConfig.length; i++) {
var preset = new TipPresets(presetConfig[i]);
var idx = (i + 1).toFixed();
this._presetLookup[idx] = preset;
this._presetArray.push(preset);
this._$presetSelector.append("<option value=\"" + idx + "\">" + preset.label + "</option>");
}
this._lockedList = [];
this._inRangeList = [];
this._atMinList = [];
this._atMaxList = [];
this.lockedCount = 10;
this.notLockedCount = 0;
this._splitSliderArray();
this._$presetSelector.change(function () {
_this.setPresetValues();
_this._runChangedCallback();
});
this._$regionSelector.change(function () {
_this._runChangedCallback();
});
this._$versionSelector.change(function () {
_this._runChangedCallback();
});
this._$presetSelector.trigger('change');
this._addEventListeners();
}
TipSliders.prototype._runChangedCallback = function () {
this.changedCallback(this.paramWeightsRegionVersion);
};
TipSliders.prototype.setPresetValues = function () {
var thePreset = this._presetLookup[this._$presetSelector.val()];
for (var i = 0; i < thePreset.presets.length; i++) {
var presetValues = thePreset.presets[i];
var theSlider = this._sliderList[i];
theSlider.locked = true;
theSlider.setValAndDropDown(presetValues.value, presetValues.column);
}
};
/**
* split array into subarrays holding the sliders
* @private
*/
TipSliders.prototype._splitSliderArray = function () {
this._lockedList = [];
this._inRangeList = [];
this._atMinList = [];
this._atMaxList = [];
for (var i = 0; i < this._sliderList.length; i++) {
var sld = this._sliderList[i];
if (sld.locked) {
this._lockedList.push(sld);
}
else if (sld.atMin) {
this._atMinList.push(sld);
}
else if (sld.atMax) {
this._atMaxList.push(sld);
}
else {
this._inRangeList.push(sld);
}
}
this.lockedCount = this._lockedList.length;
this.notLockedCount = this._sliderList.length - this.lockedCount;
};
/**
* handle remainder, recursive to take care of min max overshoots
* @param {number} remain the remainder
* @param {string} skipDomId - this dom id
* @private
*/
TipSliders.prototype._handleRemainder = function (remain, skipDomId) {
remain = Number(remain.toFixed(1));
if (remain == 0) {
return;
}
this._splitSliderArray();
var canChangeArray = [];
for (var i = 0; i < this._inRangeList.length; i++) {
var sld = this._inRangeList[i];
if (sld.domId == skipDomId) {
continue;
}
canChangeArray.push(sld);
}
if (remain > 0) {
for (var i = 0; i < this._atMaxList.length; i++) {
var sld = this._atMaxList[i];
if (sld.domId == skipDomId) {
continue;
}
canChangeArray.push(sld);
}
}
else {
for (var i = 0; i < this._atMinList.length; i++) {
var sld = this._atMinList[i];
if (sld.domId == skipDomId) {
continue;
}
canChangeArray.push(sld);
}
}
if (canChangeArray.length == 0) {
return;
}
var inc = -1 * Number((remain / canChangeArray.length).toFixed(1));
var newRemainder = 0;
for (var i = 0; i < canChangeArray.length; i++) {
newRemainder += canChangeArray[i].increment(inc);
}
this._handleRemainder(newRemainder, skipDomId);
};
/**
*
* @param {object} keyValList key and value list
*/
TipSliders.prototype.setValues = function (keyValList) {
this.resetting = true;
for (var k in keyValList) {
if (keyValList.hasOwnProperty(k)) {
this._sliderLookup[k].setValAndDropDown(keyValList[k][0], keyValList[k][1]);
}
}
this.resetting = false;
};
/**
* get the weight sum
* @returns {number} the weight sum
*/
TipSliders.prototype.getSum = function () {
var total = 0;
for (var i = 0; i < this._sliderList.length; i++) {
var sld = this._sliderList[i];
total += Number(sld.weight.toFixed(1));
}
return total;
};
/**
* get the parameter weights
* @returns {object} lookup with parameter weights
*/
TipSliders.prototype.getParams = function () {
var paramWeights = {};
for (var i = 0; i < this._sliderList.length; i++) {
var sld = this._sliderList[i];
paramWeights[sld.selectedParam] = Number(sld.weight.toFixed(1));
}
return paramWeights;
};
TipSliders.prototype._addEventListeners = function () {
var ___this = this;
//change function
this.$container.find('input[type="range"]').change(function () {
if (___this.resetting) {
return;
}
var $this = $(this);
var domId = this['id'];
var sldr = ___this._sliderLookup[domId];
var newValue = parseFloat($this.val());
var oldValue = sldr.weight;
var diff = newValue - oldValue;
diff = Number(diff.toFixed(1));
sldr.weight = Number(newValue.toFixed(1));
___this._handleRemainder(diff, domId);
//cleanup, make sure the sum is still 100
var sum = Number(___this.getSum().toFixed(1));
if (sum > 100) {
loop1: while (true) {
for (var i = 0; i < ___this._sliderList.length; i++) {
var sld = ___this._sliderList[i];
if (sld.domId == domId || sld.locked || sld.atMin) {
continue;
}
sld.increment(-0.1);
sum -= 0.1;
if (sum.toFixed(1) == '100.0') {
break loop1;
}
}
}
}
else if (sum < 100) {
loop1: while (true) {
for (var i = 0; i < ___this._sliderList.length; i++) {
var sld = ___this._sliderList[i];
if (sld.domId == domId || sld.locked || sld.atMax) {
continue;
}
sld.increment(0.1);
sum += 0.1;
if (sum.toFixed(1) == '100.0') {
break loop1;
}
}
}
}
___this._$presetSelector.val('0');
___this._runChangedCallback();
});
//update the selected parameter when the selection is changed
$('.show-select').change(function () {
if (___this.resetting) {
return;
}
___this._sliderLookup[this['id'].replace('_chg', '')].selectedParam = $(this).val();
___this._$presetSelector.val('0');
___this._runChangedCallback();
});
//lock the slider and update the reserved percent
this.$container.find('input[type="checkbox"]').change(function () {
var domEl = this;
___this._sliderLookup[domEl.id.replace('_chk', '')].locked = !domEl.checked;
___this.reservedPercent = 0.0;
___this.notLockedCount = 0;
var notLockedSliders = [];
for (var i = 0; i < ___this._sliderList.length; i++) {
var sld = ___this._sliderList[i];
if (sld.locked) {
___this.reservedPercent += sld.weight;
continue;
}
notLockedSliders.push(sld);
___this.notLockedCount++;
}
for (var i = 0; i < ___this._sliderList.length; i++) {
var sld = ___this._sliderList[i];
if (sld.locked) {
continue;
}
sld.max = 100 - ___this.reservedPercent;
}
if (notLockedSliders.length == 1) {
notLockedSliders[0].min = notLockedSliders[0].weight;
}
else {
for (var i = 0; i < notLockedSliders.length; i++) {
notLockedSliders[i].min = 0;
}
}
});
};
Object.defineProperty(TipSliders.prototype, "paramWeightsRegionVersion", {
get: function () {
return { paramWeights: this.getParams(),
region: this._$regionSelector.val(), mmVersion: this._$versionSelector.val() };
},
enumerable: true,
configurable: true
});
return TipSliders;
}());
exports.TipSliders = TipSliders;
nm.Sliders = TipSliders;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = TipSliders;
//# sourceMappingURL=Sliders.js.map