Skip to content

Commit

Permalink
Add support for sliders with ranges where min = max (#236, #359, #578,
Browse files Browse the repository at this point in the history
  • Loading branch information
leongersen committed Aug 14, 2021
1 parent 757901f commit 7343d66
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion documentation/slider-values.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<section>

<div class="view">
<p>All values on the slider are part of a range. The range has a minimum and maximum value. The minimum value <a href="https://github.com/leongersen/noUiSlider/issues/676">cannot be equal to the maximum value</a>.</p>
<p>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.</p>

<div class="example">
<div id="slider-range"></div>
Expand Down
24 changes: 19 additions & 5 deletions src/nouislider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/slider.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<script src="slider_classes.js"></script>
<script src="slider_set-exactInput.js"></script>
<script src="slider_drag_all.js"></script>
<script src="slider_no_size.js"></script>

<script src="addon_pips.js"></script>

Expand Down
10 changes: 0 additions & 10 deletions tests/slider_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions tests/slider_no_size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
QUnit.test("Test init with min = max", function (assert) {

document.getElementById('qunit-fixture').innerHTML = '<div class="slider"></div>';

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]);
});

0 comments on commit 7343d66

Please sign in to comment.