From 8edf5921686a4798994ea104e023f39b591d589f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20R=C3=BCckmann?= Date: Fri, 24 Mar 2017 12:27:32 +0100 Subject: [PATCH] #243 introduce setPositionByValue + use it on update and on programmatic value changes --- src/rangeslider.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/rangeslider.js b/src/rangeslider.js index 4f14624..3ef5af4 100644 --- a/src/rangeslider.js +++ b/src/rangeslider.js @@ -274,9 +274,8 @@ return; } - var value = e.target.value, - pos = _this.getPositionFromValue(value); - _this.setPosition(pos); + var value = e.target.value; + _this.setPositionByValue(value); }); } @@ -310,8 +309,7 @@ } else { this.$range.removeClass(this.options.disabledClass); } - - this.setPosition(this.position, triggerSlide); + this.setPositionByValue(this.value, triggerSlide); }; Plugin.prototype.handleDown = function(e) { @@ -393,6 +391,30 @@ } }; + Plugin.prototype.setPositionByValue = function(value, triggerSlide) { + var newPos; + + if (triggerSlide === undefined) { + triggerSlide = true; + } + + // Snapping steps + newPos = this.getPositionFromValue(value); + + // Update ui + this.$fill[0].style[this.DIMENSION] = (newPos + this.grabPos) + 'px'; + this.$handle[0].style[this.DIRECTION_STYLE] = newPos + 'px'; + this.setValue(value); + + // Update globals + this.position = newPos; + this.value = value; + + if (triggerSlide && this.onSlide && typeof this.onSlide === 'function') { + this.onSlide(newPos, value); + } + }; + // Returns element position relative to the parent Plugin.prototype.getPositionFromNode = function(node) { var i = 0;