-
Notifications
You must be signed in to change notification settings - Fork 4
/
nouislider.js
2637 lines (2145 loc) · 94.5 KB
/
nouislider.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
/*! nouislider - 14.5.0 - 5/11/2020 */
(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === "object") {
// Node/CommonJS
module.exports = factory();
} else {
// Browser globals
window.noUiSlider = factory();
}
})(function() {
"use strict";
var VERSION = "14.5.0";
//region Helper Methods
function isValidFormatter(entry) {
return typeof entry === "object" && typeof entry.to === "function" && typeof entry.from === "function";
}
function removeElement(el) {
el.parentElement.removeChild(el);
}
function isSet(value) {
return value !== null && value !== undefined;
}
// Bindable version
function preventDefault(e) {
e.preventDefault();
}
// Removes duplicates from an array.
function unique(array) {
return array.filter(function(a) {
return !this[a] ? (this[a] = true) : false;
}, {});
}
// Round a value to the closest 'to'.
function closest(value, to) {
return Math.round(value / to) * to;
}
// Current position of an element relative to the document.
function offset(elem, orientation) {
var rect = elem.getBoundingClientRect();
var doc = elem.ownerDocument;
var docElem = doc.documentElement;
var pageOffset = getPageOffset(doc);
// getBoundingClientRect contains left scroll in Chrome on Android.
// I haven't found a feature detection that proves this. Worst case
// scenario on mis-match: the 'tap' feature on horizontal sliders breaks.
if (/webkit.*Chrome.*Mobile/i.test(navigator.userAgent)) {
pageOffset.x = 0;
}
return orientation
? rect.top + pageOffset.y - docElem.clientTop
: rect.left + pageOffset.x - docElem.clientLeft;
}
// Checks whether a value is numerical.
function isNumeric(a) {
return typeof a === "number" && !isNaN(a) && isFinite(a);
}
// Sets a class and removes it after [duration] ms.
function addClassFor(element, className, duration) {
if (duration > 0) {
addClass(element, className);
setTimeout(function() {
removeClass(element, className);
}, duration);
}
}
// Limits a value to 0 - 100
function limit(a) {
return Math.max(Math.min(a, 100), 0);
}
// Wraps a variable as an array, if it isn't one yet.
// Note that an input array is returned by reference!
function asArray(a) {
return Array.isArray(a) ? a : [a];
}
// Counts decimals
function countDecimals(numStr) {
numStr = String(numStr);
var pieces = numStr.split(".");
return pieces.length > 1 ? pieces[1].length : 0;
}
// http://youmightnotneedjquery.com/#add_class
function addClass(el, className) {
if (el.classList && !/\s/.test(className)) {
el.classList.add(className);
} else {
el.className += " " + className;
}
}
// http://youmightnotneedjquery.com/#remove_class
function removeClass(el, className) {
if (el.classList && !/\s/.test(className)) {
el.classList.remove(className);
} else {
el.className = el.className.replace(
new RegExp("(^|\\b)" + className.split(" ").join("|") + "(\\b|$)", "gi"),
" "
);
}
}
// https://plainjs.com/javascript/attributes/adding-removing-and-testing-for-classes-9/
function hasClass(el, className) {
return el.classList
? el.classList.contains(className)
: new RegExp("\\b" + className + "\\b").test(el.className);
}
// https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY#Notes
function getPageOffset(doc) {
var supportPageOffset = window.pageXOffset !== undefined;
var isCSS1Compat = (doc.compatMode || "") === "CSS1Compat";
var x = supportPageOffset
? window.pageXOffset
: isCSS1Compat
? doc.documentElement.scrollLeft
: doc.body.scrollLeft;
var y = supportPageOffset
? window.pageYOffset
: isCSS1Compat
? doc.documentElement.scrollTop
: doc.body.scrollTop;
return {
x: x,
y: y
};
}
// we provide a function to compute constants instead
// of accessing window.* as soon as the module needs it
// so that we do not compute anything if not needed
function getActions() {
// Determine the events to bind. IE11 implements pointerEvents without
// a prefix, which breaks compatibility with the IE10 implementation.
return window.navigator.pointerEnabled
? {
start: "pointerdown",
move: "pointermove",
end: "pointerup"
}
: window.navigator.msPointerEnabled
? {
start: "MSPointerDown",
move: "MSPointerMove",
end: "MSPointerUp"
}
: {
start: "mousedown touchstart",
move: "mousemove touchmove",
end: "mouseup touchend"
};
}
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
// Issue #785
function getSupportsPassive() {
var supportsPassive = false;
/* eslint-disable */
try {
var opts = Object.defineProperty({}, "passive", {
get: function() {
supportsPassive = true;
}
});
window.addEventListener("test", null, opts);
} catch (e) {}
/* eslint-enable */
return supportsPassive;
}
function getSupportsTouchActionNone() {
return window.CSS && CSS.supports && CSS.supports("touch-action", "none");
}
//endregion
//region Range Calculation
// Determine the size of a sub-range in relation to a full range.
function subRangeRatio(pa, pb) {
return 100 / (pb - pa);
}
// (percentage) How many percent is this value of this range?
function fromPercentage(range, value, startRange) {
return (value * 100) / (range[startRange + 1] - range[startRange]);
}
// (percentage) Where is this value on this range?
function toPercentage(range, value) {
return fromPercentage(range, range[0] < 0 ? value + Math.abs(range[0]) : value - range[0], 0);
}
// (value) How much is this percentage on this range?
function isPercentage(range, value) {
return (value * (range[1] - range[0])) / 100 + range[0];
}
function getJ(value, arr) {
var j = 1;
while (value >= arr[j]) {
j += 1;
}
return j;
}
// (percentage) Input a value, find where, on a scale of 0-100, it applies.
function toStepping(xVal, xPct, value) {
if (value >= xVal.slice(-1)[0]) {
return 100;
}
var j = getJ(value, xVal);
var va = xVal[j - 1];
var vb = xVal[j];
var pa = xPct[j - 1];
var pb = xPct[j];
return pa + toPercentage([va, vb], value) / subRangeRatio(pa, pb);
}
// (value) Input a percentage, find where it is on the specified range.
function fromStepping(xVal, xPct, value) {
// There is no range group that fits 100
if (value >= 100) {
return xVal.slice(-1)[0];
}
var j = getJ(value, xPct);
var va = xVal[j - 1];
var vb = xVal[j];
var pa = xPct[j - 1];
var pb = xPct[j];
return isPercentage([va, vb], (value - pa) * subRangeRatio(pa, pb));
}
// (percentage) Get the step that applies at a certain value.
function getStep(xPct, xSteps, snap, value) {
if (value === 100) {
return value;
}
var j = getJ(value, xPct);
var a = xPct[j - 1];
var b = xPct[j];
// If 'snap' is set, steps are used as fixed points on the slider.
if (snap) {
// Find the closest position, a or b.
if (value - a > (b - a) / 2) {
return b;
}
return a;
}
if (!xSteps[j - 1]) {
return value;
}
return xPct[j - 1] + closest(value - xPct[j - 1], xSteps[j - 1]);
}
function handleEntryPoint(index, value, that) {
var percentage;
// Wrap numerical input in an array.
if (typeof value === "number") {
value = [value];
}
// Reject any invalid input, by testing whether value is an array.
if (!Array.isArray(value)) {
throw new Error("noUiSlider (" + VERSION + "): 'range' contains invalid value.");
}
// Covert min/max syntax to 0 and 100.
if (index === "min") {
percentage = 0;
} else if (index === "max") {
percentage = 100;
} else {
percentage = parseFloat(index);
}
// Check for correct input.
if (!isNumeric(percentage) || !isNumeric(value[0])) {
throw new Error("noUiSlider (" + VERSION + "): 'range' value isn't numeric.");
}
// Store values.
that.xPct.push(percentage);
that.xVal.push(value[0]);
// NaN will evaluate to false too, but to keep
// logging clear, set step explicitly. Make sure
// not to override the 'step' setting with false.
if (!percentage) {
if (!isNaN(value[1])) {
that.xSteps[0] = value[1];
}
} else {
that.xSteps.push(isNaN(value[1]) ? false : value[1]);
}
that.xHighestCompleteStep.push(0);
}
function handleStepPoint(i, n, that) {
// Ignore 'false' stepping.
if (!n) {
return;
}
// Step over zero-length ranges (#948);
if (that.xVal[i] === that.xVal[i + 1]) {
that.xSteps[i] = that.xHighestCompleteStep[i] = that.xVal[i];
return;
}
// Factor to range ratio
that.xSteps[i] =
fromPercentage([that.xVal[i], that.xVal[i + 1]], n, 0) / subRangeRatio(that.xPct[i], that.xPct[i + 1]);
var totalSteps = (that.xVal[i + 1] - that.xVal[i]) / that.xNumSteps[i];
var highestStep = Math.ceil(Number(totalSteps.toFixed(3)) - 1);
var step = that.xVal[i] + that.xNumSteps[i] * highestStep;
that.xHighestCompleteStep[i] = step;
}
//endregion
//region Spectrum
function Spectrum(entry, snap, singleStep) {
this.xPct = [];
this.xVal = [];
this.xSteps = [singleStep || false];
this.xNumSteps = [false];
this.xHighestCompleteStep = [];
this.snap = snap;
var index;
var ordered = []; // [0, 'min'], [1, '50%'], [2, 'max']
// Map the object keys to an array.
for (index in entry) {
if (entry.hasOwnProperty(index)) {
ordered.push([entry[index], index]);
}
}
// Sort all entries by value (numeric sort).
if (ordered.length && typeof ordered[0][0] === "object") {
ordered.sort(function(a, b) {
return a[0][0] - b[0][0];
});
} else {
ordered.sort(function(a, b) {
return a[0] - b[0];
});
}
// Convert all entries to subranges.
for (index = 0; index < ordered.length; index++) {
handleEntryPoint(ordered[index][1], ordered[index][0], this);
}
// Store the actual step values.
// xSteps is sorted in the same order as xPct and xVal.
this.xNumSteps = this.xSteps.slice(0);
// Convert all numeric steps to the percentage of the subrange they represent.
for (index = 0; index < this.xNumSteps.length; index++) {
handleStepPoint(index, this.xNumSteps[index], this);
}
}
Spectrum.prototype.getDistance = function(value) {
var index;
var distances = [];
for (index = 0; index < this.xNumSteps.length - 1; index++) {
// last "range" can't contain step size as it is purely an endpoint.
var step = this.xNumSteps[index];
if (step && (value / step) % 1 !== 0) {
throw new Error(
"noUiSlider (" +
VERSION +
"): 'limit', 'margin' and 'padding' of " +
this.xPct[index] +
"% range must be divisible by step."
);
}
// Calculate percentual distance in current range of limit, margin or padding
distances[index] = fromPercentage(this.xVal, value, index);
}
return distances;
};
// Calculate the percentual distance over the whole scale of ranges.
// direction: 0 = backwards / 1 = forwards
Spectrum.prototype.getAbsoluteDistance = function(value, distances, direction) {
var xPct_index = 0;
// Calculate range where to start calculation
if (value < this.xPct[this.xPct.length - 1]) {
while (value > this.xPct[xPct_index + 1]) {
xPct_index++;
}
} else if (value === this.xPct[this.xPct.length - 1]) {
xPct_index = this.xPct.length - 2;
}
// If looking backwards and the value is exactly at a range separator then look one range further
if (!direction && value === this.xPct[xPct_index + 1]) {
xPct_index++;
}
var start_factor;
var rest_factor = 1;
var rest_rel_distance = distances[xPct_index];
var range_pct = 0;
var rel_range_distance = 0;
var abs_distance_counter = 0;
var range_counter = 0;
// Calculate what part of the start range the value is
if (direction) {
start_factor = (value - this.xPct[xPct_index]) / (this.xPct[xPct_index + 1] - this.xPct[xPct_index]);
} else {
start_factor = (this.xPct[xPct_index + 1] - value) / (this.xPct[xPct_index + 1] - this.xPct[xPct_index]);
}
// Do until the complete distance across ranges is calculated
while (rest_rel_distance > 0) {
// Calculate the percentage of total range
range_pct = this.xPct[xPct_index + 1 + range_counter] - this.xPct[xPct_index + range_counter];
// Detect if the margin, padding or limit is larger then the current range and calculate
if (distances[xPct_index + range_counter] * rest_factor + 100 - start_factor * 100 > 100) {
// If larger then take the percentual distance of the whole range
rel_range_distance = range_pct * start_factor;
// Rest factor of relative percentual distance still to be calculated
rest_factor = (rest_rel_distance - 100 * start_factor) / distances[xPct_index + range_counter];
// Set start factor to 1 as for next range it does not apply.
start_factor = 1;
} else {
// If smaller or equal then take the percentual distance of the calculate percentual part of that range
rel_range_distance = ((distances[xPct_index + range_counter] * range_pct) / 100) * rest_factor;
// No rest left as the rest fits in current range
rest_factor = 0;
}
if (direction) {
abs_distance_counter = abs_distance_counter - rel_range_distance;
// Limit range to first range when distance becomes outside of minimum range
if (this.xPct.length + range_counter >= 1) {
range_counter--;
}
} else {
abs_distance_counter = abs_distance_counter + rel_range_distance;
// Limit range to last range when distance becomes outside of maximum range
if (this.xPct.length - range_counter >= 1) {
range_counter++;
}
}
// Rest of relative percentual distance still to be calculated
rest_rel_distance = distances[xPct_index + range_counter] * rest_factor;
}
return value + abs_distance_counter;
};
Spectrum.prototype.toStepping = function(value) {
value = toStepping(this.xVal, this.xPct, value);
return value;
};
Spectrum.prototype.fromStepping = function(value) {
return fromStepping(this.xVal, this.xPct, value);
};
Spectrum.prototype.getStep = function(value) {
value = getStep(this.xPct, this.xSteps, this.snap, value);
return value;
};
Spectrum.prototype.getDefaultStep = function(value, isDown, size) {
var j = getJ(value, this.xPct);
// When at the top or stepping down, look at the previous sub-range
if (value === 100 || (isDown && value === this.xPct[j - 1])) {
j = Math.max(j - 1, 1);
}
return (this.xVal[j] - this.xVal[j - 1]) / size;
};
Spectrum.prototype.getNearbySteps = function(value) {
var j = getJ(value, this.xPct);
return {
stepBefore: {
startValue: this.xVal[j - 2],
step: this.xNumSteps[j - 2],
highestStep: this.xHighestCompleteStep[j - 2]
},
thisStep: {
startValue: this.xVal[j - 1],
step: this.xNumSteps[j - 1],
highestStep: this.xHighestCompleteStep[j - 1]
},
stepAfter: {
startValue: this.xVal[j],
step: this.xNumSteps[j],
highestStep: this.xHighestCompleteStep[j]
}
};
};
Spectrum.prototype.countStepDecimals = function() {
var stepDecimals = this.xNumSteps.map(countDecimals);
return Math.max.apply(null, stepDecimals);
};
// Outside testing
Spectrum.prototype.convert = function(value) {
return this.getStep(this.toStepping(value));
};
//endregion
//region Options
/* Every input option is tested and parsed. This'll prevent
endless validation in internal methods. These tests are
structured with an item for every option available. An
option can be marked as required by setting the 'r' flag.
The testing function is provided with three arguments:
- The provided value for the option;
- A reference to the options object;
- The name for the option;
The testing function returns false when an error is detected,
or true when everything is OK. It can also modify the option
object, to make sure all values can be correctly looped elsewhere. */
//region Defaults
var defaultFormatter = {
to: function(value) {
return value !== undefined && value.toFixed(2);
},
from: Number
};
var cssClasses = {
target: "target",
base: "base",
origin: "origin",
handle: "handle",
handleLower: "handle-lower",
handleUpper: "handle-upper",
touchArea: "touch-area",
horizontal: "horizontal",
vertical: "vertical",
background: "background",
connect: "connect",
connects: "connects",
ltr: "ltr",
rtl: "rtl",
textDirectionLtr: "txt-dir-ltr",
textDirectionRtl: "txt-dir-rtl",
draggable: "draggable",
drag: "state-drag",
tap: "state-tap",
active: "active",
tooltip: "tooltip",
pips: "pips",
pipsHorizontal: "pips-horizontal",
pipsVertical: "pips-vertical",
marker: "marker",
markerHorizontal: "marker-horizontal",
markerVertical: "marker-vertical",
markerNormal: "marker-normal",
markerLarge: "marker-large",
markerSub: "marker-sub",
value: "value",
valueHorizontal: "value-horizontal",
valueVertical: "value-vertical",
valueNormal: "value-normal",
valueLarge: "value-large",
valueSub: "value-sub"
};
//endregion
function validateFormat(entry) {
// Any object with a to and from method is supported.
if (isValidFormatter(entry)) {
return true;
}
throw new Error("noUiSlider (" + VERSION + "): 'format' requires 'to' and 'from' methods.");
}
function testStep(parsed, entry) {
if (!isNumeric(entry)) {
throw new Error("noUiSlider (" + VERSION + "): 'step' is not numeric.");
}
// The step option can still be used to set stepping
// for linear sliders. Overwritten if set in 'range'.
parsed.singleStep = entry;
}
function testRange(parsed, entry) {
// Filter incorrect input.
if (typeof entry !== "object" || Array.isArray(entry)) {
throw new Error("noUiSlider (" + VERSION + "): 'range' is not an object.");
}
// Catch missing start or end.
if (entry.min === undefined || entry.max === undefined) {
throw new Error("noUiSlider (" + VERSION + "): Missing 'min' or 'max' in 'range'.");
}
// Catch equal start or end.
if (entry.min === entry.max) {
throw new Error("noUiSlider (" + VERSION + "): 'range' 'min' and 'max' cannot be equal.");
}
parsed.spectrum = new Spectrum(entry, parsed.snap, parsed.singleStep);
}
function testStart(parsed, entry) {
entry = asArray(entry);
// Validate input. Values aren't tested, as the public .val method
// will always provide a valid location.
if (!Array.isArray(entry) || !entry.length) {
throw new Error("noUiSlider (" + VERSION + "): 'start' option is incorrect.");
}
// Store the number of handles.
parsed.handles = entry.length;
// When the slider is initialized, the .val method will
// be called with the start options.
parsed.start = entry;
}
function testSnap(parsed, entry) {
// Enforce 100% stepping within subranges.
parsed.snap = entry;
if (typeof entry !== "boolean") {
throw new Error("noUiSlider (" + VERSION + "): 'snap' option must be a boolean.");
}
}
function testAnimate(parsed, entry) {
// Enforce 100% stepping within subranges.
parsed.animate = entry;
if (typeof entry !== "boolean") {
throw new Error("noUiSlider (" + VERSION + "): 'animate' option must be a boolean.");
}
}
function testAnimationDuration(parsed, entry) {
parsed.animationDuration = entry;
if (typeof entry !== "number") {
throw new Error("noUiSlider (" + VERSION + "): 'animationDuration' option must be a number.");
}
}
function testConnect(parsed, entry) {
var connect = [false];
var i;
// Map legacy options
if (entry === "lower") {
entry = [true, false];
} else if (entry === "upper") {
entry = [false, true];
}
// Handle boolean options
if (entry === true || entry === false) {
for (i = 1; i < parsed.handles; i++) {
connect.push(entry);
}
connect.push(false);
}
// Reject invalid input
else if (!Array.isArray(entry) || !entry.length || entry.length !== parsed.handles + 1) {
throw new Error("noUiSlider (" + VERSION + "): 'connect' option doesn't match handle count.");
} else {
connect = entry;
}
parsed.connect = connect;
}
function testOrientation(parsed, entry) {
// Set orientation to an a numerical value for easy
// array selection.
switch (entry) {
case "horizontal":
parsed.ort = 0;
break;
case "vertical":
parsed.ort = 1;
break;
default:
throw new Error("noUiSlider (" + VERSION + "): 'orientation' option is invalid.");
}
}
function testMargin(parsed, entry) {
if (!isNumeric(entry)) {
throw new Error("noUiSlider (" + VERSION + "): 'margin' option must be numeric.");
}
// Issue #582
if (entry === 0) {
return;
}
parsed.margin = parsed.spectrum.getDistance(entry);
}
function testLimit(parsed, entry) {
if (!isNumeric(entry)) {
throw new Error("noUiSlider (" + VERSION + "): 'limit' option must be numeric.");
}
parsed.limit = parsed.spectrum.getDistance(entry);
if (!parsed.limit || parsed.handles < 2) {
throw new Error(
"noUiSlider (" +
VERSION +
"): 'limit' option is only supported on linear sliders with 2 or more handles."
);
}
}
function testPadding(parsed, entry) {
var index;
if (!isNumeric(entry) && !Array.isArray(entry)) {
throw new Error(
"noUiSlider (" + VERSION + "): 'padding' option must be numeric or array of exactly 2 numbers."
);
}
if (Array.isArray(entry) && !(entry.length === 2 || isNumeric(entry[0]) || isNumeric(entry[1]))) {
throw new Error(
"noUiSlider (" + VERSION + "): 'padding' option must be numeric or array of exactly 2 numbers."
);
}
if (entry === 0) {
return;
}
if (!Array.isArray(entry)) {
entry = [entry, entry];
}
// 'getDistance' returns false for invalid values.
parsed.padding = [parsed.spectrum.getDistance(entry[0]), parsed.spectrum.getDistance(entry[1])];
for (index = 0; index < parsed.spectrum.xNumSteps.length - 1; index++) {
// last "range" can't contain step size as it is purely an endpoint.
if (parsed.padding[0][index] < 0 || parsed.padding[1][index] < 0) {
throw new Error("noUiSlider (" + VERSION + "): 'padding' option must be a positive number(s).");
}
}
var totalPadding = entry[0] + entry[1];
var firstValue = parsed.spectrum.xVal[0];
var lastValue = parsed.spectrum.xVal[parsed.spectrum.xVal.length - 1];
if (totalPadding / (lastValue - firstValue) > 1) {
throw new Error("noUiSlider (" + VERSION + "): 'padding' option must not exceed 100% of the range.");
}
}
function testDirection(parsed, entry) {
// Set direction as a numerical value for easy parsing.
// Invert connection for RTL sliders, so that the proper
// handles get the connect/background classes.
switch (entry) {
case "ltr":
parsed.dir = 0;
break;
case "rtl":
parsed.dir = 1;
break;
default:
throw new Error("noUiSlider (" + VERSION + "): 'direction' option was not recognized.");
}
}
function testBehaviour(parsed, entry) {
// Make sure the input is a string.
if (typeof entry !== "string") {
throw new Error("noUiSlider (" + VERSION + "): 'behaviour' must be a string containing options.");
}
// Check if the string contains any keywords.
// None are required.
var tap = entry.indexOf("tap") >= 0;
var drag = entry.indexOf("drag") >= 0;
var fixed = entry.indexOf("fixed") >= 0;
var snap = entry.indexOf("snap") >= 0;
var hover = entry.indexOf("hover") >= 0;
var unconstrained = entry.indexOf("unconstrained") >= 0;
if (fixed) {
if (parsed.handles !== 2) {
throw new Error("noUiSlider (" + VERSION + "): 'fixed' behaviour must be used with 2 handles");
}
// Use margin to enforce fixed state
testMargin(parsed, parsed.start[1] - parsed.start[0]);
}
if (unconstrained && (parsed.margin || parsed.limit)) {
throw new Error(
"noUiSlider (" + VERSION + "): 'unconstrained' behaviour cannot be used with margin or limit"
);
}
parsed.events = {
tap: tap || snap,
drag: drag,
fixed: fixed,
snap: snap,
hover: hover,
unconstrained: unconstrained
};
}
function testTooltips(parsed, entry) {
if (entry === false) {
return;
}
if (entry === true) {
parsed.tooltips = [];
for (var i = 0; i < parsed.handles; i++) {
parsed.tooltips.push(true);
}
} else {
parsed.tooltips = asArray(entry);
if (parsed.tooltips.length !== parsed.handles) {
throw new Error("noUiSlider (" + VERSION + "): must pass a formatter for all handles.");
}
parsed.tooltips.forEach(function(formatter) {
if (
typeof formatter !== "boolean" &&
(typeof formatter !== "object" || typeof formatter.to !== "function")
) {
throw new Error("noUiSlider (" + VERSION + "): 'tooltips' must be passed a formatter or 'false'.");
}
});
}
}
function testAriaFormat(parsed, entry) {
parsed.ariaFormat = entry;
validateFormat(entry);
}
function testFormat(parsed, entry) {
parsed.format = entry;
validateFormat(entry);
}
function testKeyboardSupport(parsed, entry) {
parsed.keyboardSupport = entry;
if (typeof entry !== "boolean") {
throw new Error("noUiSlider (" + VERSION + "): 'keyboardSupport' option must be a boolean.");
}
}
function testDocumentElement(parsed, entry) {
// This is an advanced option. Passed values are used without validation.
parsed.documentElement = entry;
}
function testCssPrefix(parsed, entry) {
if (typeof entry !== "string" && entry !== false) {
throw new Error("noUiSlider (" + VERSION + "): 'cssPrefix' must be a string or `false`.");
}
parsed.cssPrefix = entry;
}
function testCssClasses(parsed, entry) {
if (typeof entry !== "object") {
throw new Error("noUiSlider (" + VERSION + "): 'cssClasses' must be an object.");
}
if (typeof parsed.cssPrefix === "string") {
parsed.cssClasses = {};
for (var key in entry) {
if (!entry.hasOwnProperty(key)) {
continue;
}
parsed.cssClasses[key] = parsed.cssPrefix + entry[key];
}
} else {
parsed.cssClasses = entry;
}
}
// Test all developer settings and parse to assumption-safe values.
function testOptions(options) {
// To prove a fix for #537, freeze options here.
// If the object is modified, an error will be thrown.
// Object.freeze(options);
var parsed = {
margin: 0,
limit: 0,
padding: 0,
animate: true,
animationDuration: 300,
ariaFormat: defaultFormatter,
format: defaultFormatter
};
// Tests are executed in the order they are presented here.
var tests = {
step: { r: false, t: testStep },
start: { r: true, t: testStart },
connect: { r: true, t: testConnect },
direction: { r: true, t: testDirection },
snap: { r: false, t: testSnap },
animate: { r: false, t: testAnimate },
animationDuration: { r: false, t: testAnimationDuration },
range: { r: true, t: testRange },
orientation: { r: false, t: testOrientation },
margin: { r: false, t: testMargin },
limit: { r: false, t: testLimit },
padding: { r: false, t: testPadding },