Skip to content

Commit

Permalink
Fix leongersen#965 - move right-most handle when click right of overl…
Browse files Browse the repository at this point in the history
…apping handles
  • Loading branch information
leongersen committed Jun 19, 2019
1 parent be5350e commit 85bdb97
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/nouislider.js
Original file line number Diff line number Diff line change
Expand Up @@ -1566,8 +1566,8 @@
}

// Find handle closest to a certain percentage on the slider
function getClosestHandle(proposal) {
var closest = 100;
function getClosestHandle(clickedPosition) {
var smallestDifference = 100;
var handleNumber = false;

scope_Handles.forEach(function(handle, index) {
Expand All @@ -1576,11 +1576,19 @@
return;
}

var pos = Math.abs(scope_Locations[index] - proposal);
var handlePosition = scope_Locations[index];
var differenceWithThisHandle = Math.abs(handlePosition - clickedPosition);

if (pos < closest || (pos === 100 && closest === 100)) {
// Initial state
var clickAtEdge = differenceWithThisHandle === 100 && smallestDifference === 100;

// Difference with this handle is smaller than the previously checked handle
var isCloser = differenceWithThisHandle < smallestDifference;
var isCloserAfter = differenceWithThisHandle <= smallestDifference && clickedPosition > handlePosition;

if (isCloser || isCloserAfter || clickAtEdge) {
handleNumber = index;
closest = pos;
smallestDifference = differenceWithThisHandle;
}
});

Expand Down
5 changes: 5 additions & 0 deletions tests/slider_three_or_more_handles.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ QUnit.test("Slider with three or more handles", function (assert) {
simulateMousedown(clickTarget, click2x, clickY);
assert.deepEqual(slider2.noUiSlider.get(), ["10.00", "17.00", "20.00"], "Expect click nearer middle & right handles to move middle handle");

slider2.noUiSlider.set([10, 10, 10]);

simulateMousedown(clickTarget, click2x, clickY);
assert.deepEqual(slider2.noUiSlider.get(), ["10.00", "10.00", "17.00"], "Expect click after last handle with all in the same position to move the last handle");

});

0 comments on commit 85bdb97

Please sign in to comment.