Skip to content

Commit

Permalink
fix(slider): onchange is triggered without moving slider
Browse files Browse the repository at this point in the history
Store previous value to skip onChange trigger if no change is found in value.

Added a new variable previousValue and compare it with new value before triggering onChange and onMove.
If new changes are found then trigger both or else trigger only onMove.
  • Loading branch information
MrL1605 authored Oct 4, 2020
1 parent fcd221b commit 2f554c0
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/definitions/modules/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ $.fn.slider = function(parameters) {
precision,
isTouch,
gapRatio = 1,
previousValue,

initialPosition,
initialLoad,
Expand Down Expand Up @@ -329,7 +330,13 @@ $.fn.slider = function(parameters) {
} else {
$currThumb = module.determine.closestThumb(newPos);
}
if (previousValue === undefined) {
previousValue = module.get.currentThumbValue();
}
} else if (previousValue === undefined) {
previousValue = module.get.value();
}

if(!module.is.disabled()) {
module.bind.slidingEvents();
}
Expand Down Expand Up @@ -377,6 +384,9 @@ $.fn.slider = function(parameters) {
var value = module.determine.valueFromEvent(event);
module.set.value(value);
module.unbind.slidingEvents();
if (previousValue !== undefined) {
previousValue = undefined;
}
},
keydown: function(event, first) {
if(settings.preventCrossover && module.is.range() && module.thumbVal === module.secondThumbVal) {
Expand Down Expand Up @@ -852,19 +862,28 @@ $.fn.slider = function(parameters) {

set: {
value: function(newValue) {
var toReset = previousValue === undefined;
previousValue = previousValue === undefined ? module.get.value() : previousValue;
module.update.value(newValue, function(value, thumbVal, secondThumbVal) {
if (!initialLoad || settings.fireOnInit){
settings.onChange.call(element, value, thumbVal, secondThumbVal);
if (newValue !== previousValue) {
settings.onChange.call(element, value, thumbVal, secondThumbVal);
}
settings.onMove.call(element, value, thumbVal, secondThumbVal);
}
if (toReset) {
previousValue = undefined;
}
});
},
rangeValue: function(first, second) {
if(module.is.range()) {
var
min = module.get.min(),
max = module.get.max()
max = module.get.max(),
toReset = previousValue === undefined
;
previousValue = previousValue === undefined ? module.get.value() : previousValue;
if (first <= min) {
first = min;
} else if(first >= max){
Expand All @@ -881,9 +900,14 @@ $.fn.slider = function(parameters) {
module.update.position(module.thumbVal, $thumb);
module.update.position(module.secondThumbVal, $secondThumb);
if (!initialLoad || settings.fireOnInit) {
settings.onChange.call(element, value, module.thumbVal, module.secondThumbVal);
if (value !== previousValue) {
settings.onChange.call(element, value, module.thumbVal, module.secondThumbVal);
}
settings.onMove.call(element, value, module.thumbVal, module.secondThumbVal);
}
if (toReset) {
previousValue = undefined;
}
} else {
module.error(error.notrange);
}
Expand Down

0 comments on commit 2f554c0

Please sign in to comment.