@@ -24,19 +24,22 @@ $.widget( "ui.progressbar", {
24
24
min : 0 ,
25
25
26
26
_create : function ( ) {
27
+ // Constrain initial value
28
+ this . options . value = this . _constrainedValue ( ) ;
29
+
27
30
this . element
28
31
. addClass ( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
29
32
. attr ( {
30
33
role : "progressbar" ,
31
34
"aria-valuemin" : this . min ,
32
35
"aria-valuemax" : this . options . max ,
33
- "aria-valuenow" : this . _value ( )
36
+ "aria-valuenow" : this . options . value
34
37
} ) ;
35
38
36
39
this . valueDiv = $ ( "<div class='ui-progressbar-value ui-widget-header ui-corner-left'></div>" )
37
40
. appendTo ( this . element ) ;
38
41
39
- this . oldValue = this . _value ( ) ;
42
+ this . oldValue = this . options . value ;
40
43
this . _refreshValue ( ) ;
41
44
} ,
42
45
@@ -53,52 +56,82 @@ $.widget( "ui.progressbar", {
53
56
54
57
value : function ( newValue ) {
55
58
if ( newValue === undefined ) {
56
- return this . _value ( ) ;
59
+ return this . options . value ;
57
60
}
58
61
59
- this . _setOption ( "value" , newValue ) ;
62
+ this . _setOption ( "value" , this . _constrainedValue ( newValue ) ) ;
60
63
return this ;
61
64
} ,
62
65
63
- _setOption : function ( key , value ) {
64
- if ( key === "value" ) {
65
- this . options . value = value ;
66
- this . _refreshValue ( ) ;
67
- if ( this . _value ( ) === this . options . max ) {
68
- this . _trigger ( "complete" ) ;
69
- }
66
+ _constrainedValue : function ( newValue ) {
67
+ var val ;
68
+ if ( newValue === undefined ) {
69
+ val = this . options . value ;
70
+ } else {
71
+ val = newValue ;
70
72
}
71
73
72
- this . _super ( key , value ) ;
73
- } ,
74
-
75
- _value : function ( ) {
76
- var val = this . options . value ;
77
- // normalize invalid value
74
+ // sanitize value
78
75
if ( typeof val !== "number" ) {
79
76
val = 0 ;
80
77
}
81
78
return Math . min ( this . options . max , Math . max ( this . min , val ) ) ;
82
79
} ,
83
80
81
+ _setOptions : function ( options ) {
82
+ var key , val ;
83
+
84
+ for ( key in options ) {
85
+ if ( key === "value" ) {
86
+ // Store value to update last in case max is being updated at the same time
87
+ val = options [ key ] ;
88
+ } else {
89
+ this . _setOption ( key , options [ key ] ) ;
90
+ }
91
+ }
92
+
93
+ if ( val !== undefined ) {
94
+ this . _setOption ( "value" , val ) ;
95
+ }
96
+ } ,
97
+
98
+ _setOption : function ( key , value ) {
99
+ if ( key === "max" ) {
100
+ // Don't allow a max less than min
101
+ this . options . max = Math . max ( this . min , value ) ;
102
+ this . options . value = this . _constrainedValue ( ) ;
103
+ }
104
+ if ( key === "value" ) {
105
+ this . options . value = this . _constrainedValue ( value ) ;
106
+ }
107
+ else {
108
+ this . _super ( key , value ) ;
109
+ }
110
+
111
+ this . _refreshValue ( ) ;
112
+ } ,
113
+
84
114
_percentage : function ( ) {
85
- return 100 * this . _value ( ) / this . options . max ;
115
+ return 100 * this . options . value / this . options . max ;
86
116
} ,
87
117
88
118
_refreshValue : function ( ) {
89
- var value = this . value ( ) ,
90
- percentage = this . _percentage ( ) ;
119
+ var percentage = this . _percentage ( ) ;
91
120
92
- if ( this . oldValue !== value ) {
93
- this . oldValue = value ;
121
+ if ( this . oldValue !== this . options . value ) {
122
+ this . oldValue = this . options . value ;
94
123
this . _trigger ( "change" ) ;
95
124
}
125
+ if ( this . options . value === this . options . max ) {
126
+ this . _trigger ( "complete" ) ;
127
+ }
96
128
97
129
this . valueDiv
98
- . toggle ( value > this . min )
99
- . toggleClass ( "ui-corner-right" , value === this . options . max )
130
+ . toggle ( this . options . value > this . min )
131
+ . toggleClass ( "ui-corner-right" , this . options . value === this . options . max )
100
132
. width ( percentage . toFixed ( 0 ) + "%" ) ;
101
- this . element . attr ( "aria-valuenow" , value ) ;
133
+ this . element . attr ( "aria-valuemax" , this . options . max ) ;
134
+ this . element . attr ( "aria-valuenow" , this . options . value ) ;
102
135
}
103
136
} ) ;
104
137
0 commit comments