mirror of
https://github.com/jquery/jquery-ui.git
synced 2024-11-21 11:04:24 +00:00
Progressbar: Refactor to better handle option changes and sanitize values. Fixes #8785 - Progressbar: Remove _value() and always sanitize value option
This commit is contained in:
parent
1e19e5e90b
commit
8976bc7e3d
@ -60,3 +60,21 @@ test( "{ max : 5, value : 10 }", function() {
|
|||||||
});
|
});
|
||||||
deepEqual( 5, $( "#progressbar" ).progressbar( "value" ) );
|
deepEqual( 5, $( "#progressbar" ).progressbar( "value" ) );
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test( "{ value : 10, max : 5 }", function() {
|
||||||
|
expect( 1 );
|
||||||
|
$("#progressbar").progressbar({
|
||||||
|
max: 5,
|
||||||
|
value: 10
|
||||||
|
});
|
||||||
|
deepEqual( 5, $( "#progressbar" ).progressbar( "value" ) );
|
||||||
|
});
|
||||||
|
|
||||||
|
test( "{ max : 5 }", function() {
|
||||||
|
expect( 1 );
|
||||||
|
$("#progressbar").progressbar({
|
||||||
|
max: 10,
|
||||||
|
value: 10
|
||||||
|
}).progressbar( "option", "max", 5 );
|
||||||
|
deepEqual( 5, $( "#progressbar" ).progressbar( "value" ) );
|
||||||
|
});
|
||||||
|
83
ui/jquery.ui.progressbar.js
vendored
83
ui/jquery.ui.progressbar.js
vendored
@ -24,19 +24,22 @@ $.widget( "ui.progressbar", {
|
|||||||
min: 0,
|
min: 0,
|
||||||
|
|
||||||
_create: function() {
|
_create: function() {
|
||||||
|
// Constrain initial value
|
||||||
|
this.options.value = this._constrainedValue();
|
||||||
|
|
||||||
this.element
|
this.element
|
||||||
.addClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
|
.addClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
|
||||||
.attr({
|
.attr({
|
||||||
role: "progressbar",
|
role: "progressbar",
|
||||||
"aria-valuemin": this.min,
|
"aria-valuemin": this.min,
|
||||||
"aria-valuemax": this.options.max,
|
"aria-valuemax": this.options.max,
|
||||||
"aria-valuenow": this._value()
|
"aria-valuenow": this.options.value
|
||||||
});
|
});
|
||||||
|
|
||||||
this.valueDiv = $( "<div class='ui-progressbar-value ui-widget-header ui-corner-left'></div>" )
|
this.valueDiv = $( "<div class='ui-progressbar-value ui-widget-header ui-corner-left'></div>" )
|
||||||
.appendTo( this.element );
|
.appendTo( this.element );
|
||||||
|
|
||||||
this.oldValue = this._value();
|
this.oldValue = this.options.value;
|
||||||
this._refreshValue();
|
this._refreshValue();
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -53,52 +56,82 @@ $.widget( "ui.progressbar", {
|
|||||||
|
|
||||||
value: function( newValue ) {
|
value: function( newValue ) {
|
||||||
if ( newValue === undefined ) {
|
if ( newValue === undefined ) {
|
||||||
return this._value();
|
return this.options.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._setOption( "value", newValue );
|
this._setOption( "value", this._constrainedValue( newValue ) );
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
_setOption: function( key, value ) {
|
_constrainedValue: function( newValue ) {
|
||||||
if ( key === "value" ) {
|
var val;
|
||||||
this.options.value = value;
|
if ( newValue === undefined ) {
|
||||||
this._refreshValue();
|
val = this.options.value;
|
||||||
if ( this._value() === this.options.max ) {
|
} else {
|
||||||
this._trigger( "complete" );
|
val = newValue;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this._super( key, value );
|
// sanitize value
|
||||||
},
|
|
||||||
|
|
||||||
_value: function() {
|
|
||||||
var val = this.options.value;
|
|
||||||
// normalize invalid value
|
|
||||||
if ( typeof val !== "number" ) {
|
if ( typeof val !== "number" ) {
|
||||||
val = 0;
|
val = 0;
|
||||||
}
|
}
|
||||||
return Math.min( this.options.max, Math.max( this.min, val ) );
|
return Math.min( this.options.max, Math.max( this.min, val ) );
|
||||||
},
|
},
|
||||||
|
|
||||||
|
_setOptions: function( options ) {
|
||||||
|
var key, val;
|
||||||
|
|
||||||
|
for ( key in options ) {
|
||||||
|
if ( key === "value" ) {
|
||||||
|
// Store value to update last in case max is being updated at the same time
|
||||||
|
val = options[ key ];
|
||||||
|
} else {
|
||||||
|
this._setOption( key, options[ key ] );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( val !== undefined ) {
|
||||||
|
this._setOption( "value", val );
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
_setOption: function( key, value ) {
|
||||||
|
if ( key === "max" ) {
|
||||||
|
// Don't allow a max less than min
|
||||||
|
this.options.max = Math.max( this.min, value );
|
||||||
|
this.options.value = this._constrainedValue();
|
||||||
|
}
|
||||||
|
if ( key === "value" ) {
|
||||||
|
this.options.value = this._constrainedValue( value );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this._super( key, value );
|
||||||
|
}
|
||||||
|
|
||||||
|
this._refreshValue();
|
||||||
|
},
|
||||||
|
|
||||||
_percentage: function() {
|
_percentage: function() {
|
||||||
return 100 * this._value() / this.options.max;
|
return 100 * this.options.value / this.options.max;
|
||||||
},
|
},
|
||||||
|
|
||||||
_refreshValue: function() {
|
_refreshValue: function() {
|
||||||
var value = this.value(),
|
var percentage = this._percentage();
|
||||||
percentage = this._percentage();
|
|
||||||
|
|
||||||
if ( this.oldValue !== value ) {
|
if ( this.oldValue !== this.options.value ) {
|
||||||
this.oldValue = value;
|
this.oldValue = this.options.value;
|
||||||
this._trigger( "change" );
|
this._trigger( "change" );
|
||||||
}
|
}
|
||||||
|
if ( this.options.value === this.options.max ) {
|
||||||
|
this._trigger( "complete" );
|
||||||
|
}
|
||||||
|
|
||||||
this.valueDiv
|
this.valueDiv
|
||||||
.toggle( value > this.min )
|
.toggle( this.options.value > this.min )
|
||||||
.toggleClass( "ui-corner-right", value === this.options.max )
|
.toggleClass( "ui-corner-right", this.options.value === this.options.max )
|
||||||
.width( percentage.toFixed(0) + "%" );
|
.width( percentage.toFixed(0) + "%" );
|
||||||
this.element.attr( "aria-valuenow", value );
|
this.element.attr( "aria-valuemax", this.options.max );
|
||||||
|
this.element.attr( "aria-valuenow", this.options.value );
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user