From 7343d6689d60a62536e064453ae308974beb0107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on=20Gersen?= Date: Sat, 14 Aug 2021 20:29:14 +0200 Subject: [PATCH] Add support for sliders with ranges where min = max (#236, #359, #578, #633, #676, #790) --- documentation/slider-values.php | 2 +- src/nouislider.ts | 24 +++++++++++++++++++----- tests/slider.html | 1 + tests/slider_errors.js | 10 ---------- tests/slider_no_size.js | 16 ++++++++++++++++ 5 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 tests/slider_no_size.js diff --git a/documentation/slider-values.php b/documentation/slider-values.php index f31553da..4c4bfa56 100644 --- a/documentation/slider-values.php +++ b/documentation/slider-values.php @@ -54,7 +54,7 @@
-

All values on the slider are part of a range. The range has a minimum and maximum value. The minimum value cannot be equal to the maximum value.

+

All values on the slider are part of a range. The range has a minimum and maximum value. If the minimum value is equal to the maximum value, handles are evenly spread across the slider.

diff --git a/src/nouislider.ts b/src/nouislider.ts index 32178655..e80a04f8 100644 --- a/src/nouislider.ts +++ b/src/nouislider.ts @@ -765,6 +765,10 @@ class Spectrum { return Math.max.apply(null, stepDecimals); } + public hasNoSize(): boolean { + return this.xVal[0] === this.xVal[this.xVal.length - 1]; + } + // Outside testing public convert(value: number): number { return this.getStep(this.toStepping(value)); @@ -950,11 +954,6 @@ function testRange(parsed: ParsedOptions, entry: Range): void { throw new Error("noUiSlider: Missing 'min' or 'max' in 'range'."); } - // Catch equal start or end. - if (entry.min === entry.max) { - throw new Error("noUiSlider: 'range' 'min' and 'max' cannot be equal."); - } - parsed.spectrum = new Spectrum(entry, parsed.snap || false, parsed.singleStep); } @@ -2738,6 +2737,21 @@ function scope(target: TargetElement, options: ParsedOptions, originalOptions: O let i = scope_HandleNumbers.length === 1 ? 0 : 1; + // Spread handles evenly across the slider if the range has no size (min=max) + if (isInit && scope_Spectrum.hasNoSize()) { + exactInput = true; + + scope_Locations[0] = 0; + + if (scope_HandleNumbers.length > 1) { + const space = 100 / (scope_HandleNumbers.length - 1); + + scope_HandleNumbers.forEach(function(handleNumber) { + scope_Locations[handleNumber] = handleNumber * space; + }); + } + } + // Secondary passes. Now that all base values are set, apply constraints. // Iterate all handles to ensure constraints are applied for the entire slider (Issue #1009) for (; i < scope_HandleNumbers.length; ++i) { diff --git a/tests/slider.html b/tests/slider.html index 2be8aadc..7e9e8f5a 100644 --- a/tests/slider.html +++ b/tests/slider.html @@ -86,6 +86,7 @@ + diff --git a/tests/slider_errors.js b/tests/slider_errors.js index 0c2f7936..c8180275 100644 --- a/tests/slider_errors.js +++ b/tests/slider_errors.js @@ -81,16 +81,6 @@ QUnit.test("Errors", function (assert) { }); }); - assert.throws(function () { - noUiSlider.create(slider, { - start: 10, - range: { - 'min': 10, - 'max': 10 - } - }); - }); - assert.throws(function () { noUiSlider.create(slider, { start: 10, diff --git a/tests/slider_no_size.js b/tests/slider_no_size.js new file mode 100644 index 00000000..b70a1cac --- /dev/null +++ b/tests/slider_no_size.js @@ -0,0 +1,16 @@ +QUnit.test("Test init with min = max", function (assert) { + + document.getElementById('qunit-fixture').innerHTML = '
'; + + var slider = document.getElementById('qunit-fixture').querySelector('.slider'); + + noUiSlider.create(slider, { + start: [0, 0, 0], + range: { + 'min': 0, + 'max': 0 + } + }); + + assert.deepEqual(slider.noUiSlider.getPositions(), [0, 50, 100]); +});