aboutsummaryrefslogtreecommitdiffstats
path: root/ui/jquery.ui.progressbar.js
blob: 5a7fd87d37e93dd1c7da9bff5291f392930ab6fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*!
 * jQuery UI Progressbar @VERSION
 * http://jqueryui.com
 *
 * Copyright 2012 jQuery Foundation and other contributors
 * Released under the MIT license.
 * http://jquery.org/license
 *
 * http://api.jqueryui.com/progressbar/
 *
 * Depends:
 *   jquery.ui.core.js
 *   jquery.ui.widget.js
 */
(function( $, undefined ) {

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

	min: 0,

	_create: function() {
		// Constrain initial value
		this.options.value = this._constrainedValue();

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

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

		this.oldValue = this.options.value;
		this._refreshValue();
	},

	_destroy: function() {
		this.element
			.removeClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
			.removeAttr( "role" )
			.removeAttr( "aria-valuemin" )
			.removeAttr( "aria-valuemax" )
			.removeAttr( "aria-valuenow" );

		this.valueDiv.remove();
	},

	value: function( newValue ) {
		if ( newValue === undefined ) {
			return this.options.value;
		}

		this._setOption( "value", this._constrainedValue( newValue ) );
		return this;
	},

	_constrainedValue: function( newValue ) {
		var val;
		if ( newValue === undefined ) {
			val = this.options.value;
		} else {
			val = newValue;
		}

		// sanitize value
		if ( typeof val !== "number" ) {
			val = 0;
		}
		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() {
		return 100 * this.options.value / this.options.max;
	},

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

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

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

})( jQuery );