Skip to content

Commit c7b468f

Browse files
committed
Slider: Fix max calculation, when step is float
Fixes #10721 new _calculateNewMax method uses while loop, the cost should be very minimum since this method is called one time in life of control, in create method or when setOption(max) is called. below method uses math functions to avoid loop, but this was failing unit tests of slider_options. TODO : to avoid while loop in Max Calculation ``` _calculateNewMax: function() { var precision = this._precision(); var multiplier = Math.pow( 10 , precision ); var max = this.options.max * multiplier ; var min = this._valueMin() * multiplier ; var step = this.options.step * multiplier ; var remainder = ( max - min ) % step; this.max = ( ( max - remainder ) / multiplier ).toFixed(precision); } ```
1 parent b6bec79 commit c7b468f

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

tests/unit/slider/slider_options.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test( "disabled", function(){
4040
});
4141

4242
test( "max", function() {
43-
expect( 4 );
43+
expect( 5 );
4444
element = $( "<div></div>" );
4545

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

75+
options = {
76+
max: 60,
77+
min: 50,
78+
orientation: "horizontal",
79+
step: 0.1,
80+
value: 60
81+
};
82+
83+
element.slider( options );
84+
ok( element.slider( "value" ) === options.max, "value method will max, step is changed" );
85+
element.slider( "destroy" );
86+
7587
});
7688

7789
test( "min", function() {

ui/slider.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,27 @@ return $.widget( "ui.slider", $.ui.mouse, {
552552
},
553553

554554
_calculateNewMax: function() {
555-
var remainder = ( this.options.max - this._valueMin() ) % this.options.step;
556-
this.max = this.options.max - remainder;
555+
var max = this._valueMin(),
556+
step = this.options.step,
557+
precisiondigits = this._precision();
558+
while ( parseFloat( ( max + step ).toFixed( precisiondigits ) ) <= this.options.max ) {
559+
max = parseFloat( ( max + step ).toFixed( precisiondigits ) );
560+
}
561+
this.max = max;
562+
},
563+
564+
_precision: function() {
565+
var precision = this._precisionOf( this.options.step );
566+
if ( this.options.min !== null ) {
567+
precision = Math.max( precision, this._precisionOf( this.options.min ) );
568+
}
569+
return precision;
570+
},
571+
572+
_precisionOf: function( num ) {
573+
var str = num.toString(),
574+
decimal = str.indexOf( "." );
575+
return decimal === -1 ? 0 : str.length - decimal - 1;
557576
},
558577

559578
_valueMin: function() {

0 commit comments

Comments
 (0)