jquery-ui/ui/jquery.ui.progressbar.js

146 lines
3.3 KiB
JavaScript
Raw Normal View History

/*!
* jQuery UI Progressbar @VERSION
2012-07-04 13:08:08 +00:00
* http://jqueryui.com
*
2013-01-10 13:52:20 +00:00
* Copyright 2013 jQuery Foundation and other contributors
2012-08-09 14:13:24 +00:00
* Released under the MIT license.
* http://jquery.org/license
*
2012-09-26 23:06:20 +00:00
* http://api.jqueryui.com/progressbar/
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
*/
(function( $, undefined ) {
$.widget( "ui.progressbar", {
version: "@VERSION",
options: {
max: 100,
value: 0,
change: null,
complete: null
},
2010-07-29 09:38:24 +00:00
min: 0,
_create: function() {
// Constrain initial value
2012-12-06 15:10:07 +00:00
this.oldValue = this.options.value = this._constrainedValue();
this.element
.addClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
.attr({
// Only set static values, aria-valuenow and aria-valuemax are
// set inside _refreshValue()
role: "progressbar",
"aria-valuemin": this.min
});
this.valueDiv = $( "<div class='ui-progressbar-value ui-widget-header ui-corner-left'></div>" )
.appendTo( this.element );
2008-12-05 13:35:11 +00:00
this._refreshValue();
},
2008-09-20 11:22:54 +00:00
_destroy: function() {
2008-11-21 04:12:18 +00:00
this.element
.removeClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
.removeAttr( "role" )
.removeAttr( "aria-valuemin" )
.removeAttr( "aria-valuemax" )
.removeAttr( "aria-valuenow" );
2008-12-05 13:35:11 +00:00
this.valueDiv.remove();
},
value: function( newValue ) {
if ( newValue === undefined ) {
return this.options.value;
}
2012-12-06 15:10:07 +00:00
this.options.value = this._constrainedValue( newValue );
this._refreshValue();
2008-12-05 13:35:11 +00:00
},
_constrainedValue: function( newValue ) {
if ( newValue === undefined ) {
2012-12-06 15:10:07 +00:00
newValue = this.options.value;
2008-09-20 11:22:54 +00:00
}
2008-12-05 13:35:11 +00:00
2012-12-06 15:10:07 +00:00
this.indeterminate = newValue === false;
// sanitize value
2012-12-06 15:10:07 +00:00
if ( typeof newValue !== "number" ) {
newValue = 0;
}
2012-12-06 15:10:07 +00:00
return this.indeterminate ? false :
Math.min( this.options.max, Math.max( this.min, newValue ) );
},
_setOptions: function( options ) {
// Ensure "value" option is set after other values (like max)
2012-12-06 15:10:07 +00:00
var value = options.value;
delete options.value;
2012-12-06 15:10:07 +00:00
this._super( options );
2012-12-06 15:10:07 +00:00
this.options.value = this._constrainedValue( value );
this._refreshValue();
},
_setOption: function( key, value ) {
if ( key === "max" ) {
// Don't allow a max less than min
2012-12-06 15:10:07 +00:00
value = Math.max( this.min, value );
}
2012-12-06 15:10:07 +00:00
this._super( key, value );
},
_percentage: function() {
return this.indeterminate ? 100 : 100 * ( this.options.value - this.min ) / ( this.options.max - this.min );
},
2008-12-05 13:35:11 +00:00
_refreshValue: function() {
var value = this.options.value,
percentage = this._percentage();
this.valueDiv
.toggle( this.indeterminate || value > this.min )
.toggleClass( "ui-corner-right", value === this.options.max )
.width( percentage.toFixed(0) + "%" );
this.element.toggleClass( "ui-progressbar-indeterminate", this.indeterminate );
if ( this.indeterminate ) {
this.element.removeAttr( "aria-valuenow" );
if ( !this.overlayDiv ) {
this.overlayDiv = $( "<div class='ui-progressbar-overlay'></div>" ).appendTo( this.valueDiv );
}
} else {
this.element.attr({
"aria-valuemax": this.options.max,
"aria-valuenow": value
});
if ( this.overlayDiv ) {
this.overlayDiv.remove();
this.overlayDiv = null;
}
}
if ( this.oldValue !== value ) {
this.oldValue = value;
this._trigger( "change" );
}
if ( value === this.options.max ) {
this._trigger( "complete" );
}
}
});
})( jQuery );