progressbar.js (4195B)
1 /*! 2 * jQuery UI Progressbar 1.12.1 3 * http://jqueryui.com 4 * 5 * Copyright jQuery Foundation and other contributors 6 * Released under the MIT license. 7 * http://jquery.org/license 8 */ 9 10 //>>label: Progressbar 11 //>>group: Widgets 12 // jscs:disable maximumLineLength 13 //>>description: Displays a status indicator for loading state, standard percentage, and other progress indicators. 14 // jscs:enable maximumLineLength 15 //>>docs: http://api.jqueryui.com/progressbar/ 16 //>>demos: http://jqueryui.com/progressbar/ 17 //>>css.structure: ../../themes/base/core.css 18 //>>css.structure: ../../themes/base/progressbar.css 19 //>>css.theme: ../../themes/base/theme.css 20 21 ( function( factory ) { 22 if ( typeof define === "function" && define.amd ) { 23 24 // AMD. Register as an anonymous module. 25 define( [ 26 "jquery", 27 "./core" 28 ], factory ); 29 } else { 30 31 // Browser globals 32 factory( jQuery ); 33 } 34 }( function( $ ) { 35 36 return $.widget( "ui.progressbar", { 37 version: "1.12.1", 38 options: { 39 classes: { 40 "ui-progressbar": "ui-corner-all", 41 "ui-progressbar-value": "ui-corner-left", 42 "ui-progressbar-complete": "ui-corner-right" 43 }, 44 max: 100, 45 value: 0, 46 47 change: null, 48 complete: null 49 }, 50 51 min: 0, 52 53 _create: function() { 54 55 // Constrain initial value 56 this.oldValue = this.options.value = this._constrainedValue(); 57 58 this.element.attr( { 59 60 // Only set static values; aria-valuenow and aria-valuemax are 61 // set inside _refreshValue() 62 role: "progressbar", 63 "aria-valuemin": this.min 64 } ); 65 this._addClass( "ui-progressbar", "ui-widget ui-widget-content" ); 66 67 this.valueDiv = $( "<div>" ).appendTo( this.element ); 68 this._addClass( this.valueDiv, "ui-progressbar-value", "ui-widget-header" ); 69 this._refreshValue(); 70 }, 71 72 _destroy: function() { 73 this.element.removeAttr( "role aria-valuemin aria-valuemax aria-valuenow" ); 74 75 this.valueDiv.remove(); 76 }, 77 78 value: function( newValue ) { 79 if ( newValue === undefined ) { 80 return this.options.value; 81 } 82 83 this.options.value = this._constrainedValue( newValue ); 84 this._refreshValue(); 85 }, 86 87 _constrainedValue: function( newValue ) { 88 if ( newValue === undefined ) { 89 newValue = this.options.value; 90 } 91 92 this.indeterminate = newValue === false; 93 94 // Sanitize value 95 if ( typeof newValue !== "number" ) { 96 newValue = 0; 97 } 98 99 return this.indeterminate ? false : 100 Math.min( this.options.max, Math.max( this.min, newValue ) ); 101 }, 102 103 _setOptions: function( options ) { 104 105 // Ensure "value" option is set after other values (like max) 106 var value = options.value; 107 delete options.value; 108 109 this._super( options ); 110 111 this.options.value = this._constrainedValue( value ); 112 this._refreshValue(); 113 }, 114 115 _setOption: function( key, value ) { 116 if ( key === "max" ) { 117 118 // Don't allow a max less than min 119 value = Math.max( this.min, value ); 120 } 121 this._super( key, value ); 122 }, 123 124 _setOptionDisabled: function( value ) { 125 this._super( value ); 126 127 this.element.attr( "aria-disabled", value ); 128 this._toggleClass( null, "ui-state-disabled", !!value ); 129 }, 130 131 _percentage: function() { 132 return this.indeterminate ? 133 100 : 134 100 * ( this.options.value - this.min ) / ( this.options.max - this.min ); 135 }, 136 137 _refreshValue: function() { 138 var value = this.options.value, 139 percentage = this._percentage(); 140 141 this.valueDiv 142 .toggle( this.indeterminate || value > this.min ) 143 .width( percentage.toFixed( 0 ) + "%" ); 144 145 this 146 ._toggleClass( this.valueDiv, "ui-progressbar-complete", null, 147 value === this.options.max ) 148 ._toggleClass( "ui-progressbar-indeterminate", null, this.indeterminate ); 149 150 if ( this.indeterminate ) { 151 this.element.removeAttr( "aria-valuenow" ); 152 if ( !this.overlayDiv ) { 153 this.overlayDiv = $( "<div>" ).appendTo( this.valueDiv ); 154 this._addClass( this.overlayDiv, "ui-progressbar-overlay" ); 155 } 156 } else { 157 this.element.attr( { 158 "aria-valuemax": this.options.max, 159 "aria-valuenow": value 160 } ); 161 if ( this.overlayDiv ) { 162 this.overlayDiv.remove(); 163 this.overlayDiv = null; 164 } 165 } 166 167 if ( this.oldValue !== value ) { 168 this.oldValue = value; 169 this._trigger( "change" ); 170 } 171 if ( value === this.options.max ) { 172 this._trigger( "complete" ); 173 } 174 } 175 } ); 176 177 } ) );