Skip to content

Commit a188632

Browse files
jpkamikesherov
authored andcommitted
Slider: when handles overlap, clicking and dragging will now pick the last one that was moved. Fixed #3467 - Sliders Handles can overlap, only small sliver of slider is selectable
1 parent ccdcedd commit a188632

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

tests/unit/slider/slider_events.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,55 @@ test( "programmatic event triggers", function() {
104104

105105
});
106106

107+
test( "mouse based interaction part two: when handles overlap", function() {
108+
expect(4);
109+
110+
var el = $( "#slider1" )
111+
.slider({
112+
values: [ 0, 0, 0 ],
113+
start: function( event, ui ) {
114+
equal(handles.index(ui.handle), 2, "rightmost handle activated when overlapping at minimum (#3736)");
115+
}
116+
}),
117+
handles = el.find( ".ui-slider-handle" );
118+
handles.eq(0).simulate( "drag", { dx: 10 } );
119+
120+
QUnit.reset();
121+
el = $( "#slider1" )
122+
.slider({
123+
values: [ 10, 10, 10 ],
124+
max: 10,
125+
start: function( event, ui ) {
126+
equal(handles.index(ui.handle), 0, "leftmost handle activated when overlapping at maximum");
127+
}
128+
}),
129+
handles = el.find( ".ui-slider-handle" );
130+
handles.eq(0).simulate( "drag", { dx: -10 } );
131+
132+
QUnit.reset();
133+
el = $( "#slider1" )
134+
.slider({
135+
values: [ 19, 20 ]
136+
}),
137+
handles = el.find( ".ui-slider-handle" );
138+
handles.eq(0).simulate( "drag", { dx: 10 } );
139+
el.on("slidestart", function(event, ui) {
140+
equal(handles.index(ui.handle), 0, "left handle activated if left was moved last");
141+
});
142+
handles.eq(0).simulate( "drag", { dx: 10 } );
143+
144+
QUnit.reset();
145+
el = $( "#slider1" )
146+
.slider({
147+
values: [ 19, 20 ]
148+
}),
149+
handles = el.find( ".ui-slider-handle" );
150+
handles.eq(1).simulate( "drag", { dx: -10 } );
151+
el.on("slidestart", function(event, ui) {
152+
equal(handles.index(ui.handle), 1, "right handle activated if right was moved last (#3467)");
153+
});
154+
handles.eq(0).simulate( "drag", { dx: 10 } );
155+
156+
});
157+
107158
}( jQuery ) );

ui/jquery.ui.slider.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,15 @@ $.widget( "ui.slider", $.ui.mouse, {
233233
distance = this._valueMax() - this._valueMin() + 1;
234234
this.handles.each(function( i ) {
235235
var thisDistance = Math.abs( normValue - that.values(i) );
236-
if ( distance > thisDistance ) {
236+
if (( distance > thisDistance ) ||
237+
( distance === thisDistance &&
238+
(i === that._lastChangedValue || that.values(i) === o.min ))) {
237239
distance = thisDistance;
238240
closestHandle = $( this );
239241
index = i;
240242
}
241243
});
242244

243-
// workaround for bug #3736 (if both handles of a range are at 0,
244-
// the first is always used as the one with least distance,
245-
// and moving it is obviously prevented by preventing negative ranges)
246-
if( o.range === true && this.values(1) === o.min ) {
247-
index += 1;
248-
closestHandle = $( this.handles[index] );
249-
}
250-
251245
allowed = this._start( event, index );
252246
if ( allowed === false ) {
253247
return false;
@@ -419,6 +413,9 @@ $.widget( "ui.slider", $.ui.mouse, {
419413
uiHash.values = this.values();
420414
}
421415

416+
//store the last changed value index for reference when handles overlap
417+
this._lastChangedValue = index;
418+
422419
this._trigger( "change", event, uiHash );
423420
}
424421
},

0 commit comments

Comments
 (0)