Skip to content

Commit 46e46ef

Browse files
committed
Slider: if rage option is true, the first handle value can't exceed the second one. Fixed #3762 - slider handles not restricted properly when set programmatically
By now, if range option is set to true, only two handles are supported. So, I created a function _verifyValues() and invoked it inside _createRange() and values( index, newValue ) whenever necessary. Also, I created a unit test.
1 parent d345a0d commit 46e46ef

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

tests/unit/slider/slider_options.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ test( "step", function() {
171171
//});
172172

173173
test( "values", function() {
174-
expect( 2 );
174+
expect( 3 );
175175

176176
// testing multiple ranges on the same page, the object reference to the values
177177
// property is preserved via multiple range elements, so updating options.values
@@ -197,6 +197,14 @@ test( "values", function() {
197197
ranges.eq( 1 ).slider( "values", 0 ),
198198
"the values for multiple sliders should be different"
199199
);
200+
201+
ranges.eq( 0 ).slider( "values", [ 50, 30 ] );
202+
203+
equal(
204+
ranges.eq( 0 ).slider( "values", 0 ),
205+
ranges.eq( 0 ).slider( "values", 1 ),
206+
"the values should be equated if the first is greater than the second"
207+
);
200208
});
201209

202210
test( "range", function() {

ui/jquery.ui.slider.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var numPages = 5;
2222
$.widget( "ui.slider", $.ui.mouse, {
2323
version: "@VERSION",
2424
widgetEventPrefix: "slide",
25-
25+
2626
options: {
2727
animate: false,
2828
distance: 0,
@@ -48,7 +48,7 @@ $.widget( "ui.slider", $.ui.mouse, {
4848
this._handleIndex = null;
4949
this._detectOrientation();
5050
this._mouseInit();
51-
51+
5252
this.element
5353
.addClass( "ui-slider" +
5454
" ui-slider-" + this.orientation +
@@ -75,7 +75,7 @@ $.widget( "ui.slider", $.ui.mouse, {
7575
existingHandles = this.element.find( ".ui-slider-handle" ).addClass( "ui-state-default ui-corner-all" ),
7676
handle = "<a class='ui-slider-handle ui-state-default ui-corner-all' href='#'></a>",
7777
handles = [];
78-
78+
7979
handleCount = ( options.values && options.values.length ) || 1;
8080

8181
if ( existingHandles.length > handleCount ) {
@@ -99,7 +99,7 @@ $.widget( "ui.slider", $.ui.mouse, {
9999
_createRange: function() {
100100
var options = this.options,
101101
classes = "";
102-
102+
103103
if ( options.range ) {
104104
if ( options.range === true ) {
105105
if ( !options.values ) {
@@ -109,8 +109,9 @@ $.widget( "ui.slider", $.ui.mouse, {
109109
} else if ( $.isArray( options.values ) ) {
110110
options.values = options.values.slice(0);
111111
}
112+
this._verifyValues();
112113
}
113-
114+
114115
if ( !this.range || !this.range.length ) {
115116
this.range = $( "<div></div>" )
116117
.appendTo( this.element );
@@ -380,7 +381,7 @@ $.widget( "ui.slider", $.ui.mouse, {
380381
var vals,
381382
newValues,
382383
i;
383-
384+
384385
if ( arguments.length > 1 ) {
385386
this.options.values[ index ] = this._trimAlignValue( newValue );
386387
this._refreshValue();
@@ -396,19 +397,40 @@ $.widget( "ui.slider", $.ui.mouse, {
396397
vals[ i ] = this._trimAlignValue( newValues[ i ] );
397398
this._change( null, i );
398399
}
400+
this._verifyValues();
399401
this._refreshValue();
400402
} else {
401403
if ( this.options.values && this.options.values.length ) {
404+
this._verifyValues();
402405
return this._values( index );
403406
} else {
404407
return this.value();
405408
}
406409
}
407410
} else {
411+
this._verifyValues();
408412
return this._values();
409413
}
410414
},
411-
415+
416+
_verifyValues: function( ) {
417+
var options = this.options;
418+
419+
if ( options.range ) {
420+
if ( options.range === true ) {
421+
if(options.values[0] < this._valueMin()) {
422+
options.values[0] = this._valueMin();
423+
}
424+
if(options.values[1] > this._valueMax()) {
425+
options.values[1] = this._valueMax();
426+
}
427+
if(options.values[0] > options.values[1]) {
428+
options.values[1] = options.values[0];
429+
}
430+
}
431+
}
432+
},
433+
412434
_setOption: function( key, value ) {
413435
var i,
414436
valsLength = 0;

0 commit comments

Comments
 (0)