Skip to content

Commit

Permalink
Progressbar: Added max option. Fixes #6681 - Progressbar: add max opt…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
fx authored and scottgonzalez committed Nov 22, 2010
1 parent d69f2ec commit d23fe49
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
3 changes: 2 additions & 1 deletion tests/unit/progressbar/progressbar_defaults.js
Expand Up @@ -4,7 +4,8 @@

var progressbar_defaults = {
disabled: false,
value: 0
value: 0,
max: 100
};

commonWidgetTests('progressbar', { defaults: progressbar_defaults });
13 changes: 13 additions & 0 deletions tests/unit/progressbar/progressbar_events.js
Expand Up @@ -5,6 +5,19 @@

module("progressbar: events");

test("create", function() {
expect(1);
$("#progressbar").progressbar({
value: 5,
create: function() {
same(5, $(this).progressbar("value") );
},
change: function() {
ok(false, 'create() has triggered change()');
}
})
});

test("change", function() {
expect(1);
$("#progressbar").progressbar({
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/progressbar/progressbar_options.js
Expand Up @@ -31,4 +31,12 @@ test("{ value : 105 }", function() {
same( 100, $("#progressbar").progressbar("value") );
});

test("{ max : 5, value : 10 }", function() {
$("#progressbar").progressbar({
max: 5,
value: 10
});
same( 5, $("#progressbar").progressbar("value") );
});

})(jQuery);
27 changes: 19 additions & 8 deletions ui/jquery.ui.progressbar.js
Expand Up @@ -15,25 +15,26 @@

$.widget( "ui.progressbar", {
options: {
value: 0
value: 0,
max: 100
},

min: 0,
max: 100,

_create: function() {
this.element
.addClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
.attr({
role: "progressbar",
"aria-valuemin": this.min,
"aria-valuemax": this.max,
"aria-valuemax": this.options.max,
"aria-valuenow": this._value()
});

this.valueDiv = $( "<div class='ui-progressbar-value ui-widget-header ui-corner-left'></div>" )
.appendTo( this.element );

this.oldValue = this._value();
this._refreshValue();
},

Expand Down Expand Up @@ -63,8 +64,7 @@ $.widget( "ui.progressbar", {
if ( key === "value" ) {
this.options.value = value;
this._refreshValue();
this._trigger( "change" );
if ( this._value() === this.max ) {
if ( this._value() === this.options.max ) {
this._trigger( "complete" );
}
}
Expand All @@ -78,14 +78,25 @@ $.widget( "ui.progressbar", {
if ( typeof val !== "number" ) {
val = 0;
}
return Math.min( this.max, Math.max( this.min, val ) );
return Math.min( this.options.max, Math.max( this.min, val ) );
},

_percentage: function() {
return 100 * this._value() / this.options.max;
},

_refreshValue: function() {
var value = this.value();
var percentage = this._percentage();

if ( this.oldValue !== value ) {
this.oldValue = value;
this._trigger( "change" );
}

this.valueDiv
.toggleClass( "ui-corner-right", value === this.max )
.width( value + "%" );
.toggleClass( "ui-corner-right", value === this.options.max )
.width( percentage.toFixed(0) + "%" );
this.element.attr( "aria-valuenow", value );
}
});
Expand Down

0 comments on commit d23fe49

Please sign in to comment.