Skip to content

Commit

Permalink
feat(slider): add preventCrossover setting for range sliders
Browse files Browse the repository at this point in the history
Adds a new setting called preventCrossover which prevents the lower thumb to crossover the upper thumb.

Does not prevent calling $("#slider").slider("set rangeValue", 50, 25);

BREAKING CHANGE: `preventCrossover ` is set to true by default, making this a breaking change.
  • Loading branch information
ColinFrick authored and Sean committed Oct 18, 2019
1 parent 8eb1705 commit 4b1d548
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/definitions/modules/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ $.fn.slider = function(parameters) {
isTouch,
gapRatio = 1,

initialPosition,
initialLoad,
module
;
Expand Down Expand Up @@ -323,7 +324,13 @@ $.fn.slider = function(parameters) {
eventPos = module.determine.eventPos(event, originalEvent),
newPos = module.determine.pos(eventPos)
;
$currThumb = module.determine.closestThumb(newPos);
// Special handling if range mode and both thumbs have the same value
if(module.is.range() && settings.preventCrossover && module.thumbVal === module.secondThumbVal) {
initialPosition = newPos;
$currThumb = undefined;
} else {
$currThumb = module.determine.closestThumb(newPos);
}
}
if(!module.is.disabled()) {
module.bind.slidingEvents();
Expand All @@ -332,15 +339,30 @@ $.fn.slider = function(parameters) {
move: function(event, originalEvent) {
event.preventDefault();
var value = module.determine.valueFromEvent(event, originalEvent);
if($currThumb === undefined) {
var
eventPos = module.determine.eventPos(event, originalEvent),
newPos = module.determine.pos(eventPos)
;
$currThumb = initialPosition > newPos ? $thumb : $secondThumb;
}
if(module.get.step() == 0 || module.is.smooth()) {
var
thumbVal = module.thumbVal,
secondThumbVal = module.secondThumbVal,
thumbSmoothVal = module.determine.smoothValueFromEvent(event, originalEvent)
;
if(!$currThumb.hasClass('second')) {
if(settings.preventCrossover) {
value = Math.min(secondThumbVal, value);
thumbSmoothVal = Math.min(secondThumbVal, thumbSmoothVal);
}
thumbVal = value;
} else {
if(settings.preventCrossover) {
value = Math.max(thumbVal, value);
thumbSmoothVal = Math.max(thumbVal, thumbSmoothVal);
}
secondThumbVal = value;
}
value = Math.abs(thumbVal - (secondThumbVal || 0));
Expand All @@ -359,6 +381,9 @@ $.fn.slider = function(parameters) {
module.unbind.slidingEvents();
},
keydown: function(event, first) {
if(module.is.range() && settings.preventCrossover && module.thumbVal === module.secondThumbVal) {
$currThumb = undefined;
}
if(module.is.focused()) {
$(document).trigger(event);
}
Expand Down Expand Up @@ -577,7 +602,7 @@ $.fn.slider = function(parameters) {
return value;
},
currentThumbValue: function() {
return $currThumb.hasClass('second') ? module.secondThumbVal : module.thumbVal;
return $currThumb !== undefined && $currThumb.hasClass('second') ? module.secondThumbVal : module.thumbVal;
},
thumbValue: function(which) {
switch(which) {
Expand Down Expand Up @@ -653,6 +678,9 @@ $.fn.slider = function(parameters) {
secondThumbPos = parseFloat(module.determine.thumbPos($secondThumb)),
secondThumbDelta = Math.abs(eventPos - secondThumbPos)
;
if(thumbDelta === secondThumbDelta && module.get.thumbValue() === module.get.min()) {
return $secondThumb;
}
return thumbDelta <= secondThumbDelta ? $thumb : $secondThumb;
},
closestThumbPos: function(eventPos) {
Expand Down Expand Up @@ -891,9 +919,18 @@ $.fn.slider = function(parameters) {
value = newValue;
module.thumbVal = value;
} else {
if($currThumb === undefined) {
$currThumb = newValue <= module.get.currentThumbValue() ? $thumb : $secondThumb;
}
if(!$currThumb.hasClass('second')) {
if(settings.preventCrossover) {
newValue = Math.min(module.secondThumbVal, newValue);
}
module.thumbVal = newValue;
} else {
if(settings.preventCrossover) {
newValue = Math.max(module.thumbVal, newValue);
}
module.secondThumbVal = newValue;
}
value = Math.abs(module.thumbVal - module.secondThumbVal);
Expand Down Expand Up @@ -1229,6 +1266,7 @@ $.fn.slider.settings = {
smooth : false,
autoAdjustLabels : true,
labelDistance : 100,
preventCrossover : true,
fireOnInit : false,

//the decimal place to round to if step is undefined
Expand Down

0 comments on commit 4b1d548

Please sign in to comment.