From 2f554c062420ba42f1913e3d6f1a64f4539dda21 Mon Sep 17 00:00:00 2001 From: Lalit Umbarkar Date: Mon, 5 Oct 2020 02:29:26 +0530 Subject: [PATCH] fix(slider): onchange is triggered without moving slider 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. --- src/definitions/modules/slider.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/definitions/modules/slider.js b/src/definitions/modules/slider.js index 1a600518d9..673673d003 100644 --- a/src/definitions/modules/slider.js +++ b/src/definitions/modules/slider.js @@ -88,6 +88,7 @@ $.fn.slider = function(parameters) { precision, isTouch, gapRatio = 1, + previousValue, initialPosition, initialLoad, @@ -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(); } @@ -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) { @@ -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){ @@ -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); }