Fixed #3716 - slider: range handles should not be able to cross

This commit is contained in:
Richard Worth 2008-12-31 19:55:16 +00:00
parent 5f39555e79
commit 3bffcdffa3

View File

@ -21,6 +21,8 @@ $.widget("ui.slider", $.extend({}, $.ui.mouse, {
this._keySliding = false; this._keySliding = false;
this._handleIndex = null;
this._mouseInit(); this._mouseInit();
this.element this.element
@ -128,7 +130,7 @@ $.widget("ui.slider", $.extend({}, $.ui.mouse, {
break; break;
} }
self._slide(event, newVal); self._slide(event, index, newVal);
}).keyup(function(event) { }).keyup(function(event) {
if (self._keySliding) { if (self._keySliding) {
self._stop(event); self._stop(event);
@ -180,20 +182,23 @@ $.widget("ui.slider", $.extend({}, $.ui.mouse, {
var normValue = this._normValueFromMouse(position); var normValue = this._normValueFromMouse(position);
var distance = this._valueMax(), closestHandle; var distance = this._valueMax(), closestHandle;
var self = this; var self = this, index;
this.handles.each(function(i) { this.handles.each(function(i) {
var thisDistance = Math.abs(normValue - self.values(i)); var thisDistance = Math.abs(normValue - self.values(i));
if (distance > thisDistance) { if (distance > thisDistance) {
distance = thisDistance; distance = thisDistance;
closestHandle = $(this); closestHandle = $(this);
index = i;
} }
}); });
self._handleIndex = index;
closestHandle closestHandle
.addClass("ui-state-active") .addClass("ui-state-active")
.focus(); .focus();
this._slide(event, normValue); this._slide(event, index, normValue);
return true; return true;
@ -207,7 +212,7 @@ $.widget("ui.slider", $.extend({}, $.ui.mouse, {
var position = { x: event.pageX, y: event.pageY }; var position = { x: event.pageX, y: event.pageY };
var normValue = this._normValueFromMouse(position); var normValue = this._normValueFromMouse(position);
this._slide(event, normValue); this._slide(event, this._handleIndex, normValue);
return false; return false;
}, },
@ -216,6 +221,7 @@ $.widget("ui.slider", $.extend({}, $.ui.mouse, {
this.handles.removeClass("ui-state-active"); this.handles.removeClass("ui-state-active");
this._stop(event); this._stop(event);
this._change(event); this._change(event);
this._handleIndex = null;
return false; return false;
}, },
@ -257,10 +263,12 @@ $.widget("ui.slider", $.extend({}, $.ui.mouse, {
}); });
}, },
_slide: function(event, newVal) { _slide: function(event, index, newVal) {
if (this.options.values && this.options.values.length) { if (this.options.values && this.options.values.length) {
var handle = this.handles.filter(".ui-state-active"); var handle = this.handles[index];
var index = handle.data("index.ui-slider-handle"); var otherVal = this.values(index ? 0 : 1);
if ((index == 0 && newVal >= otherVal) || (index == 1 && newVal <= otherVal))
newVal = otherVal;
if (newVal != this.values(index)) { if (newVal != this.values(index)) {
var newValues = this.values(); var newValues = this.values();
newValues[index] = newVal; newValues[index] = newVal;
@ -270,9 +278,11 @@ $.widget("ui.slider", $.extend({}, $.ui.mouse, {
value: newVal, value: newVal,
values: newValues values: newValues
}); });
if (allowed !== false) var otherVal = this.values(index ? 0 : 1);
if (allowed !== false) {
this.values(index, newVal); this.values(index, newVal);
} }
}
} else { } else {
if (newVal != this.value()) { if (newVal != this.value()) {
// A slide can be canceled by returning false from the slide callback // A slide can be canceled by returning false from the slide callback