Skip to content

Commit

Permalink
Slider: Fix max calculation, when step is float
Browse files Browse the repository at this point in the history
Fixes #10721
Closes gh-1398
  • Loading branch information
dekajp authored and scottgonzalez committed Jan 12, 2015
1 parent a3b43ee commit ae1d6d5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
14 changes: 13 additions & 1 deletion tests/unit/slider/slider_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test( "disabled", function(){
});

test( "max", function() {
expect( 4 );
expect( 5 );
element = $( "<div></div>" );

options = {
Expand Down Expand Up @@ -72,6 +72,18 @@ test( "max", function() {
ok( element.slider( "value" ) === options.max, "value method will max, step is changed" );
element.slider( "destroy" );

options = {
max: 60,
min: 50,
orientation: "horizontal",
step: 0.1,
value: 60
};

element.slider( options );
ok( element.slider( "value" ) === options.max, "value method will max, step is changed and step is float" );
element.slider( "destroy" );

});

test( "min", function() {
Expand Down
22 changes: 20 additions & 2 deletions ui/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,26 @@ return $.widget( "ui.slider", $.ui.mouse, {
},

_calculateNewMax: function() {
var remainder = ( this.options.max - this._valueMin() ) % this.options.step;
this.max = this.options.max - remainder;
var max = this.options.max,
min = this._valueMin(),
step = this.options.step,
aboveMin = Math.floor( ( max - min ) / step ) * step;
max = aboveMin + min;
this.max = parseFloat( max.toFixed( this._precision() ) );
},

_precision: function() {
var precision = this._precisionOf( this.options.step );
if ( this.options.min !== null ) {
precision = Math.max( precision, this._precisionOf( this.options.min ) );
}
return precision;
},

_precisionOf: function( num ) {
var str = num.toString(),
decimal = str.indexOf( "." );
return decimal === -1 ? 0 : str.length - decimal - 1;
},

_valueMin: function() {
Expand Down

1 comment on commit ae1d6d5

@jirikolda
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, unfortunately this doesn't solve the bug completely. Please take a look here: http://jsfiddle.net/uvLzcd0f/ Some min/max values are still causing problems.

Please sign in to comment.