-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-slider.js
1608 lines (1344 loc) · 84.5 KB
/
angular-slider.js
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
angular-slider
(c) 2013-2014 Venturocket, Inc. http://github.com/Venturocket
License: MIT
*/
angular.module('vr.directives.slider', ['ngTouch']).directive('slider',
['$timeout', '$document', '$interpolate', '$swipe',
function($timeout, $document, $interpolate, $swipe) {
/**
* How sticky the knobs feel...ew
* @type {number}
*/
var KNOB_STICKINESS = 3;
/**
* You want custom start and end symbols? You can have custom start and end symbols
* @type {string}
*/
var startSymbol = $interpolate.startSymbol(), endSymbol = $interpolate.endSymbol();
/**
* Convert a regular element to an angular element...Oh fancy! *waves hands*
* @param {object} element
* @returns {object} the new element
*/
function angularize(element) {
return angular.element(element);
}
/**
* Adds 'px' to the end of a number...Yes, that's really all it does.
* @param {(string|Number)} position
* @returns {string}
*/
function pixelize(position) {
return "" + position + "px";
}
/**
* I'm not completely sure what this does, but I have a feeling it has something to do with opacity
* @param {object} element
* @param {Number} opacity
* @returns {object} the element
*/
function setOpacity(element, opacity) {
return element.css({
opacity: opacity
});
}
/**
* Sets the element's opacity to 0
* @param {object} element
* @returns {object} the element
*/
function hide(element) {
return setOpacity(element, 0);
}
/**
* Sets the element's opacity to 1
* @param {object} element
* @returns {object} the element
*/
function show(element) {
return setOpacity(element, 1);
}
/**
* offsets the element from the left the given amount
* @param {object} element
* @param {Number|string} position
* @returns {object} the element
*/
function offset(element, position) {
return element.css({
left: position
});
}
/**
* Determines the width of the element...somehow
* @param {object} element
* @returns {number}
*/
function width(element) {
var width = parseFloat(element.css('width'));
return isNaN(width) ? element[0].offsetWidth : width;
}
/**
* You wouldn't believe me if I told you, but this will tell you how wide half of the given element is!
* @param {object} element
* @returns {number}
*/
function halfWidth(element) {
return width(element) / 2;
}
/**
* Determine the amount of space to the left of the element
* @param {object} element
* @returns {Number}
*/
function offsetLeft(element) {
try {return element.offset().left;} catch(e) {}
return element[0].getBoundingClientRect().left; // + scrollX;
}
/**
* Compute the gap between the two given elements
* @param {object} element1
* @param {object} element2
* @returns {number}
*/
function gap(element1, element2) {
if(offsetLeft(element1) > offsetLeft(element2)) {
return offsetLeft(element1) - offsetLeft(element2) - width(element2);
}
return offsetLeft(element2) - offsetLeft(element1) - width(element1);
}
/**
* Binds the given html string to the given element
* @param {object} element
* @param {string} html
* @returns {object} the element
*/
function bindHtml(element, html) {
return element.attr('ng-bind-template', html);
}
/**
* Computes the nearest full step
* @param {Number} [value = 0]
* @param {Number} [precision = 0]
* @param {Number} [step = 1/Math.pow(10, precision)]
* @param {Number} [floor = 0]
* @param {Number} [ceiling]
* @returns {Number}
*/
function roundToStep(value, precision, step, floor, ceiling) {
// precision is optional
if(angular.isUndefined(precision) || !precision) {
precision = 0;
}
// step is optional
if(angular.isUndefined(step) || !step || step == 0) {
step = 1 / Math.pow(10, precision);
}
// floor is optional
if(angular.isUndefined(floor) || !floor) {
floor = 0;
}
// value is optional
if(angular.isUndefined(value) || !value) {
value = 0;
}
// how far from a step is the value
var remainder = (value - floor) % step;
// round the value to a step
var roundedValue = remainder > (step / 2) ? value + step - remainder : value - remainder;
// ceiling is optional
if(angular.isUndefined(ceiling) || !ceiling) {
ceiling = roundedValue;
}
// bring the value back in range
roundedValue = Math.min(Math.max(roundedValue, floor), ceiling);
// set the precision
return parseFloat(roundedValue.toFixed(precision));
}
/**
* Round the given number to an arbitrary step
* @param {number} value
* @param {number} step
* @returns {number}
*/
function roundTo(value, step) {
return Math.floor((value / step) + 0.5) * step;
}
/**
* Rounds the buffer up to the nearest full step
* @param {Number} step
* @param {Number} buffer
* @returns {Number}
*/
function stepBuffer(step, buffer) {
if(step > 0 && !isNaN(buffer)) {
return Math.ceil(buffer / step) * step;
}
return buffer;
}
/**
* Wraps the given expression in whatever start and end symbol this app uses
* @param {string} exp
* @returns {string}
*/
function expression(exp) {
return startSymbol + " " + exp + " " + endSymbol;
}
return {
restrict: 'EA',
require: '?ngModel',
scope: {
floor : '@', // the minimum possible value
ceiling : '@', // the maximum possible value
step : '@', // how wide is each step, omit or set to 0 for no steps
stepWidth : '@', // alias of step to avoid collisions
precision : '@', // how many decimal places do we care about
buffer : '@', // how close can the two knobs of a dual knob slider get?
stickiness : '@', // how sticky should the knobs feel...seriously, how did this get all sticky? gross
showSteps : '@', // show the step value bubbles?
ngModel : '=', // single knob/dual know low value binding
ngModelRange : '=', // dual knob high value binding
ngDisabled : '=', // should the slider be disabled?
ngChange : '&', // what should we do when a value is changed
translateFn : '&', // how to translate the values displayed in the bubbles
translateRangeFn : '&', // how to translate the range bubble
translateCombinedFn : '&', // how to translate the combined bubble
scaleFn : '&', // how to scale the values
inverseScaleFn : '&' // how to unscale the values
},
template: // bar background
"<span class='bar full'></span>" + // secondary bars used for dual knobs
"<span class='bar steps'><span class='bubble step' ng-repeat='step in stepBubbles()'></span></span>" + // step bubbles
"<span class='bar selection'></span><span class='bar unselected low'></span><span class='bar unselected high'></span>" + // the knobs
"<span class='pointer low'></span><span class='pointer high'></span>" + // current value bubbles
"<span class='bubble low'></span><span class='bubble high'></span><span class='bubble middle'></span><span class='bubble selection'></span>" + // low, high, middle and selection bubbles
"<span class='bubble limit floor'></span><span class='bubble limit ceiling'></span>" + // upper and lower limit bubbles
"<input type='range' class='input low' /><input type='range' class='input high' /><input type='range' class='input selection' />", // range sliders used for browsers that support them
compile: function(element, attributes) {
// are we gonna show the step bubbles?
var showSteps = attributes.showSteps;
// are we using 'step' or 'step-width'?
var stepWidth = attributes.stepWidth?'stepWidth':'step';
// dual knob?
var isDualKnob = attributes.ngModelRange != null,
// init element references
refs = {},
// which properties do we want to use?
refLow = 'ngModel', refHigh = 'ngModelRange', refSel = 'selectBar',
// which properties to we want to watch for changes?
watchables = ['floor', 'ceiling', 'stickiness', refLow];
/**
* Get references to all the children of the given element
* @param {object} [el = element]
* @returns {Array} the children of el
*/
function getReferences(el) {
if(!el) {
el = element;
}
var refs = [];
angular.forEach(el.children(), function(el) {
refs.push(angularize(el));
});
return refs;
}
/**
* Set the references for use later
* @param {Array} refs
* @param {Boolean} [dual = false] is this a dual knob slider?
* @param {Boolean} [inputs = false] are we using range inputs?
*/
function setReferences(refs, dual, inputs) {
return {
fullBar : refs[0], // background bar
stepBubs : refs[1], // the steps bubbles
selBar : dual ? refs[2] : null, // dual knob: the bar between knobs
unSelBarLow : dual ? refs[3] : null, // dual knob: the bar to the left of the low knob
unSelBarHigh: dual ? refs[4] : null, // dual knob: the bar to the right of the high knob
minPtr : dual ? refs[5] : refs[2], // single knob: the knob, dual knob: the low knob
maxPtr : dual ? refs[6] : null, // dual knob: the high knob
lowBub : dual ? refs[7] : refs[3], // single knob: the value bubble, dual knob: the low value bubble
highBub : dual ? refs[8] : null, // dual knob: the high value bubble
cmbBub : dual ? refs[9] : null, // dual knob: the range values bubble
selBub : dual ? refs[10] : null, // dual knob: the range width bubble
flrBub : dual ? refs[11] : refs[4], // the lower limit bubble
ceilBub : dual ? refs[12] : refs[5], // the upper limit bubble
minInput : inputs ? (dual ? refs[13] : refs[6]) : null, // single knob: the actual slider input, dual knob: the low value slider input
maxInput : inputs ? (dual ? refs[14] : null) : null, // dual knob: the high value slider input
selInput : inputs ? (dual ? refs[15] : null) : null // dual knob: the selection slider input
};
}
// set up the references
refs = (function() {
var _ref = getReferences();
var _results = [];
for(var _i = 0, _len = _ref.length; _i < _len; _i++) {
var e = _ref[_i];
e = angularize(e);
e.css({
'white-space': 'nowrap',
position : 'absolute',
display : 'block',
'z-index' : 1
});
_results.push(e);
}
return _results;
})();
refs = setReferences(refs, true, true);
// set up the translation function
if(attributes.translateFn) {
attributes.$set('translateFn', "" + attributes.translateFn + "(value)");
}
// set up the translation function for the range bubble
if(attributes.translateRangeFn) {
attributes.$set('translateRangeFnFn', "" + attributes.translateRangeFn + "(low,high)");
}
// set up the translation function for the center bubble
if(attributes.translateCombinedFn) {
attributes.$set('translateCombinedFnFn', "" + attributes.translateCombinedFn + "(low,high)");
}
// set up the encoding function
if(attributes.scaleFn) {
attributes.$set('scaleFn', "" + attributes.scaleFn + "(value)");
}
// set up the decoding function
if(attributes.inverseScaleFn) {
attributes.$set('inverseScaleFn', "" + attributes.inverseScaleFn + "(value)");
}
// set up the background bar so it fills the entire width of the slider
refs.fullBar.css({
left : 0,
right: 0
});
// set up range inputs
if(AngularSlider.inputtypes.range) {
// we can use range inputs
/**
* default range input styles
* @type {{position: string, margin: number, padding: number, opacity: number, height: string}}
*/
var inputStyles = {
position: 'absolute',
margin : 0,
padding : 0,
opacity : 0,
height : '100%'
};
// set up the low value range input
refs.minInput.attr('step', expression("inputSteps()")); // set the number of steps
refs.minInput.attr('min', expression("floor")); // set the minimum possible value
refs.minInput.css(inputStyles); // apply the default styles
refs.minInput.css('left', 0); // stick it to the left
if(isDualKnob) {
// this is a dual knob slider
refs.minInput.attr('max', expression("ngModelRange - (buffer / 2)")); // set the maximum value of the low range input so it doesn't overlap the high range input's minimum value
// set up the high value range input
refs.maxInput.attr('step', expression("inputSteps()")); // set the number of steps
refs.maxInput.attr('min', expression("ngModel + (buffer / 2)")); // set the minimum value of the high range input so it doesn't overlap the low range input's maximum value
refs.maxInput.attr('max', expression("ceiling")); // set the maximum possible value
refs.maxInput.css(inputStyles); // apply the default styles
// set up the selection range input
refs.selInput.attr('step', expression("inputSteps()")); // set the number of steps
refs.selInput.attr('min', expression("ngModel")); // set up the minimum value
refs.selInput.attr('max', expression("ngModelRange")); // set up the maximum falue
refs.selInput.css(inputStyles); // apply the default styles
} else {
// this is single knob slider
refs.minInput.attr('max', expression("ceiling")); // set the maximum possible value
refs.minInput.css({ width: '100%' }); // make sure it fills the entire slider
refs.maxInput.remove(); // get rid of the high value range input
refs.selInput.remove(); // get rid of the selection value range input
}
} else {
// we can't use range inputs :(
refs.minInput.remove();
refs.maxInput.remove();
refs.selInput.remove();
}
// set up bubbles
bindHtml(refs.stepBubs.children().eq(0), expression("translation(step)"));
bindHtml(refs.ceilBub, expression("translation(ceiling)"));
bindHtml(refs.flrBub, expression("translation(floor)"));
bindHtml(refs.selBub, expression("rangeTranslation(" + refLow + "," + refHigh + ")"));
bindHtml(refs.lowBub, expression("translation(" + refLow + ")"));
bindHtml(refs.highBub, expression("translation(" + refHigh + ")"));
bindHtml(refs.cmbBub, expression("combinedTranslation(" + refLow + "," + refHigh + ")"));
// start to compile watchables
if(isDualKnob) {
// dual knob so also watch the high value and buffer
watchables.push(refHigh);
watchables.unshift('buffer');
} else {
// single knob so get rid of what we don't need
var _ref1 = [refs.selBar, refs.unSelBarLow, refs.unSelBarHigh, refs.maxPtr, refs.selBub, refs.highBub, refs.cmbBub];
for(var _i = 0, _len = _ref1.length; _i < _len; _i++) {
element = _ref1[_i];
element.remove();
}
}
// make sure the precision and step are first in the list
watchables.unshift('precision', stepWidth);
if(!showSteps) {
// we're not displaying the step bubbles this time
refs.stepBubs.children().remove();
}
return {
post: function(scope, element, attributes, ctrl) {
// re-set references locally to avoid any cross contamination and disassociation when using transcluded scopes (namely ng-repeat)
var refs = setReferences(getReferences(element), isDualKnob, AngularSlider.inputtypes.range);
/**
* Save the decoded values so we don't have to decode every...single...time....ugh
* @type {{floor: number, ceiling: number, step: number, precision: number, buffer: number, stickiness: number, ngModel: number, ngModel: number, ngModelRange: number}}
*/
scope.decodedValues = {
floor : 0,
ceiling : 0,
step : 0,
stepWidth : 0,
precision : 0,
buffer : 0,
stickiness : 0,
ngModel : 0,
ngModelRange: 0
};
/**
* Apply the supplied translation function if necessary
* @param {(string|Number)} value
* @returns {string}
*/
scope.translation = function(value) {
value = parseFloat(value).toFixed(scope.precision);
if(angular.isUndefined(attributes.translateFn)) {
return '' + value;
}
return scope.translateFn({value: value});
};
/**
* Apply the supplied translation function for the range if necessary
* @param {(string|Number)} low
* @param {(string|number)} high
* @returns {string}
*/
scope.rangeTranslation = function(low, high) {
if(angular.isUndefined(attributes.translateRangeFn)) {
return "Range: " + scope.translation((high - low).toFixed(scope.precision));
}
return scope.translateRangeFn({low: low, high: high});
};
/**
* Apply the supplied translation function for the center if necessary
* @param {(string|Number)} low
* @param {(string|number)} high
* @returns {string}
*/
scope.combinedTranslation = function(low, high) {
if(angular.isUndefined(attributes.translateCombinedFn)) {
return scope.translation(low) + " - " + scope.translation(high);
}
return scope.translateCombinedFn({low: low, high: high});
};
/**
* Encode the value given
* @param {number} value
* @returns {number}
*/
scope.encode = function(value) {
if(angular.isUndefined(attributes.scaleFn) || attributes.scaleFn == '') {
return value;
}
return scope.scaleFn({value: value});
};
/**
* Decode the value given
* @param {number} value
* @returns {number}
*/
scope.decode = function(value) {
if(angular.isUndefined(attributes.inverseScaleFn) || attributes.inverseScaleFn == '') {
return value;
}
return scope.inverseScaleFn({value: value});
};
if(Math.round(scope.encode(scope.decode(1))) != 1 || Math.round(scope.encode(scope.decode(100))) != 100) {
console.warn("The scale and inverseScale functions are not perfect inverses: 1 = "+scope.encode(scope.decode(1))+" 100 = "+scope.encode(scope.decode(100)));
}
/**
* Decode the value of the given reference
* @param {string} ref
* @returns {number}
*/
scope.decodeRef = function(ref) {
return scope.decode(scope[ref]);
};
/**
* How precise do the range inputs need to be?
* @returns {number}
*/
scope.inputSteps = function() {
return Math.pow(10, scope.precision * -1);
};
/**
* The width of the background bar
* @type {number}
*/
var barWidth = 0;
/**
* Half the width of the knob/bar in use
* @type {number}
*/
var pointerHalfWidth = 0;
/**
* Left most possible position
* @type {number}
*/
var minOffset = 0;
/**
* Right most possible position
* @type {number}
*/
var maxOffset = 0;
/**
* How much width do we have to work with
* @type {number}
*/
var offsetRange = 0;
/**
* The minimum value of the slider
* @type {number}
*/
var minValue = 0;
/**
* The minimum value of the slider (decoded)
* @type {number}
*/
var minValueDecoded = 0;
/**
* The maximum value of the slider
* @type {number}
*/
var maxValue = 0;
/**
* The maximum value of the slider (decoded)
* @type {number}
*/
var maxValueDecoded = 0;
/**
* The total range of the slider
* @type {number}
*/
var valueRange = 0;
/**
* The total range of the slider (decoded)
* @type {number}
*/
var valueRangeDecoded = 0;
/**
* The normalized width in percent of a step
* @type {number}
*/
var stepRange = 1;
/**
* How far from a step is the low knob?
* @type {number}
*/
var stickyOffsetLow = 0;
/**
* How far from a step is the high knob?
* @type {number}
*/
var stickyOffsetHigh = 0;
/**
* Have the events been bound to the necessary inputs/elements
* @type {boolean}
*/
var eventsBound = false;
/**
* Update the necessary dimensions
*/
function dimensions() {
// make sure the watchables are all valid
angular.forEach(watchables, function(watchable) {
// parse them to floats
scope[watchable] = parseFloat(scope[watchable]);
if(watchable == refLow || watchable == refHigh) {
// this is the low or high value so bring them back in line with the steps
scope[watchable] = roundToStep(scope[watchable], scope.precision, scope[stepWidth], scope.floor, scope.ceiling);
} else if(watchable == 'buffer') {
if(!scope.buffer || isNaN(scope.buffer) || scope.buffer < 0) {
// the buffer is not valid, so set to 0
scope.buffer = 0;
} else {
// this is the buffer so make sure it aligns with the steps
scope.buffer = stepBuffer(scope[stepWidth], scope.buffer);
}
} else if(watchable == 'precision') {
// make sure the precision is valid
if(!scope.precision || isNaN(scope.precision)) {
scope.precision = 0;
} else {
scope.precision = parseInt(scope.precision);
}
} else if(watchable == stepWidth) {
// make sure the step is valid
if(!scope[stepWidth] || isNaN(scope[stepWidth])) {
scope[stepWidth] = 1 / Math.pow(10, scope.precision);
} else {
scope[stepWidth] = parseFloat(scope[stepWidth].toFixed(scope.precision));
}
} else if(watchable == 'stickiness') {
// make sure the stickiness is valid
if(isNaN(scope.stickiness)) {
scope.stickiness = KNOB_STICKINESS;
} else if(scope.stickiness < 1) {
scope.stickiness = 1;
}
}
// save the decoded values
scope.decodedValues[watchable] = scope.decodeRef(watchable);
});
if(isDualKnob) {
// if this is a dual knob slider
// make sure the low value is actually lower than the high value
if(scope[refHigh] < scope[refLow]) {
var temp = scope[refHigh];
scope[refHigh] = scope[refLow];
scope[refLow] = temp;
}
// get the difference between the knobs, but make sure it's rounded to a step
var diff = roundToStep(scope[refHigh] - scope[refLow], scope.precision, scope[stepWidth]);
if(scope.buffer > 0 && diff < scope.buffer) {
// we need a buffer but the difference is smaller than the required buffer
// so find the middle
var avg = scope.encode((scope.decodedValues[refLow] + scope.decodedValues[refHigh]) / 2);
// and set the knobs so they straddle the middle with the required amount of buffer
scope[refLow] = roundToStep(avg - (scope.buffer / 2), scope.precision, scope[stepWidth], scope.floor, scope.ceiling);
scope[refHigh] = scope[refLow] + scope.buffer;
if(scope[refHigh] > scope.ceiling) {
// the high value is out of range
// so set the high value to the maximum
scope[refHigh] = scope.ceiling;
// but keep the buffer correct
scope[refLow] = scope.ceiling - scope.buffer;
}
}
}
// save the various dimensions we'll need
barWidth = width(refs.fullBar);
pointerHalfWidth = halfWidth(refs.minPtr);
minOffset = offsetLeft(refs.fullBar);
maxOffset = minOffset + barWidth - width(refs.minPtr);
offsetRange = maxOffset - minOffset;
minValue = scope.floor;
minValueDecoded = scope.decodedValues.floor;
maxValue = scope.ceiling;
maxValueDecoded = scope.decodedValues.ceiling;
valueRange = maxValue - minValue;
valueRangeDecoded = maxValueDecoded - minValueDecoded;
stepRange = roundTo(valueRangeDecoded, scope.decodedValues[stepWidth]);
}
/**
* Lets make everything look good
*/
function updateDOM() {
var dragRange, // is the user dragging the entire range and not just one knob?
lowValueOffset, // where did the low knob start
highValueOffset, // where did the high knob start
pointer, // which knob/bar is being dragged
ref; // which value should we be changing
// update the dimensions
dimensions();
// set the limit bubble positions
offset(refs.flrBub, 0);
offset(refs.ceilBub, pixelize(barWidth - width(refs.ceilBub)));
/**
* Get the offset percentage from the given absolute offset
* @param {number} offset
* @returns {number}
*/
function percentFromOffset(offset) {
return ((offset - minOffset) / offsetRange) * 100;
}
/**
* Get the decoded value from the given offset
* @param {number} offset
* @returns {number}
*/
function decodedValueFromOffset(offset) {
return percentFromOffset(offset)/100 * valueRangeDecoded + minValueDecoded;
}
/**
* Get the value from the given offset
* @param {number} offset
* @returns {number}
*/
function valueFromOffset(offset) {
return scope.encode(decodedValueFromOffset(offset));
}
/**
* Get the absolute offset from the given decoded value
* @param {number} value
* @returns {number}
*/
function offsetFromDecodedValue(value) {
return ((value - minValueDecoded) * offsetRange) + minOffset;
}
/**
* Get the absolute offset from the given value
* @param {number} value
* @returns {number}
*/
function offsetFromValue(value) {
return offsetFromDecodedValue(scope.decode(value));
}
/**
* Get the offset percentage from the given decoded value
* @param {number} value
* @returns {number}
*/
function percentFromDecodedValue(value) {
var percent = value - minValueDecoded;
if(valueRange == valueRangeDecoded) {
percent = roundTo(percent, scope.decodedValues[stepWidth]) / stepRange;
} else {
percent /= valueRangeDecoded;
}
return percent * 100;
}
/**
* Get the offset percentage from the given value
* @param {number} value
* @returns {number}
*/
function percentFromValue(value) {
return percentFromDecodedValue(scope.decode(value));
}
/**
* Get the absolute offset (in px) from the given offset percentage
* @param {number} percent
* @returns {string}
*/
function offsetFromPercent(percent) {
return pixelize(percent * offsetRange / 100);
}
/**
* Bring the offset back in range of the slider
* @param {number} offset
* @returns {number}
*/
function bringOffsetInRange(offset) {
return Math.min(Math.max(offset, minOffset), maxOffset);
}
/**
* Bring the element back within the confines of the slider
* @param {object} element
* @returns {Object}
*/
function fitToBar(element) {
return offset(element, offsetFromPercent(percentFromOffset(bringOffsetInRange(offsetLeft(element)))));
}
/**
* Compute the amount of stretch
* @param {number} percent the mouse offset from the start position
* @param {number} [maxPercent = 100] the maximum stretch
* @param {boolean} [end = false] are we beyond the max stretch?
* @returns {number}
*/
function percentStretch(percent, maxPercent, end) {
// which direction?
var sign = percent > 0 ? 1 : -1;
// if the maxPercent is 0 or not given apply no limit (i.e. set it to 100)
maxPercent = !maxPercent ? 100 : maxPercent;
if(end) {
// compute the max stretch amount
return (
Math.sin((
Math.min(Math.abs(percent / maxPercent), 1) * Math.PI
) - (Math.PI / 2)) + 1
) * sign * maxPercent / 6;
}
// compute the current stretch amount
return (
sign * Math.pow(Math.min(Math.abs(percent / maxPercent * 2), 1), scope.stickiness) * maxPercent / 2
);
}
/**
* Update the pointers in the DOM
*/
function setPointers() {
/**
* The base percent for the low knob
* @type {number}
*/
var rawLowPercent = percentFromDecodedValue(scope.decodedValues[refLow]);
/**
* The width in percent of a step above the low value
* @type {number}
*/
var stepWidthPercentAboveLow = percentFromValue(scope[refLow] + scope[stepWidth]) - rawLowPercent;
/**
* The width in percent of a step below the low value
* @type {number}
*/
var stepWidthPercentBelowLow = rawLowPercent - percentFromValue(scope[refLow] - scope[stepWidth]);
/**
* The width in percent of the buffer above the low value
* @type {number}
*/
var bufferWidthPercentLow = percentFromValue(scope[refLow] + scope.buffer) - rawLowPercent;
/**
* The width in percent of the pointer
* @type {number}
*/
var ptrHalfWidthPercent = percentFromOffset(pointerHalfWidth + minOffset);
/**
* The percent for the low knob after the stretch has been applied
* @type {number}
*/
var stretchedLowPercent = rawLowPercent + percentStretch(stickyOffsetLow, stickyOffsetLow > 0?stepWidthPercentAboveLow:stepWidthPercentBelowLow);
// set the low knob's and bubble's new positions
offset(refs.minPtr, offsetFromPercent(stretchedLowPercent));
offset(refs.lowBub,
offsetFromPercent(percentFromOffset(offsetLeft(refs.minPtr) - halfWidth(refs.lowBub) + pointerHalfWidth)));
if(isDualKnob) {
// dual knob slider
/**
* The base percent for the high knob
* @type {number}
*/
var rawHighPercent = percentFromDecodedValue(scope.decodedValues[refHigh]);
/**
* The width in percent of a step above the high value
* @type {number}
*/
var stepWidthPercentAboveHigh = percentFromValue(scope[refHigh] + scope[stepWidth]) - rawHighPercent;
/**
* The width in percent of a step below the high value
* @type {number}
*/
var stepWidthPercentBelowHigh = rawHighPercent - percentFromValue(scope[refHigh] - scope[stepWidth]);
/**
* The width in percent of the buffer below the high value
* @type {number}
*/
var bufferWidthPercentHigh = rawHighPercent - percentFromValue(scope[refHigh] - scope.buffer);
/**
* The percent for the high knob after the stretch has been applied
* @type {number}
*/
var stretchedHighPercent = rawHighPercent + percentStretch(stickyOffsetHigh, stickyOffsetHigh > 0?stepWidthPercentAboveHigh:stepWidthPercentBelowHigh);
if(stretchedLowPercent > rawHighPercent - bufferWidthPercentHigh) {
// if the low knob has reached its maximum
// get the new stretch amount for the low knob
stretchedLowPercent = rawLowPercent + percentStretch(stickyOffsetLow, bufferWidthPercentLow, true);
// and re-set the low knob's and bubble's new positions
offset(refs.minPtr, offsetFromPercent(stretchedLowPercent));
offset(refs.lowBub, offsetFromPercent(percentFromOffset(offsetLeft(refs.minPtr) - halfWidth(refs.lowBub) +
pointerHalfWidth)));
}
if(stretchedHighPercent < rawLowPercent + bufferWidthPercentLow) {
// if the high knob has reached its minimum
// get the new stretch amount for the high knob
stretchedHighPercent = rawHighPercent + percentStretch(stickyOffsetHigh, bufferWidthPercentHigh, true);
}
// set the high knob's and bubble's new positions
offset(refs.maxPtr, offsetFromPercent(stretchedHighPercent));
offset(refs.highBub, offsetFromPercent(percentFromOffset(offsetLeft(refs.maxPtr) - halfWidth(refs.highBub) +
pointerHalfWidth)));
// set the selection bar's new position and width
offset(refs.selBar, offsetFromPercent(stretchedLowPercent + ptrHalfWidthPercent));
refs.selBar.css({
width: offsetFromPercent(stretchedHighPercent - stretchedLowPercent)
});
// set the selection bubbles' new positions
offset(refs.selBub, offsetFromPercent(((stretchedLowPercent + stretchedHighPercent) / 2) - percentFromOffset(halfWidth(refs.selBub) + minOffset) + ptrHalfWidthPercent));
offset(refs.cmbBub, offsetFromPercent(((stretchedLowPercent + stretchedHighPercent) / 2) - percentFromOffset(halfWidth(refs.cmbBub) + minOffset) + ptrHalfWidthPercent));
// set the low unselected bar's new position and width
refs.unSelBarLow.css({
left : 0,
width: offsetFromPercent(stretchedLowPercent + ptrHalfWidthPercent)