jquery.ui.progressbar.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*!
  2. * jQuery UI Progressbar v1.9 stable
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2012 jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * http://api.jqueryui.com/progressbar/
  10. *
  11. * Depends:
  12. * jquery.ui.core.js
  13. * jquery.ui.widget.js
  14. */
  15. (function( $, undefined ) {
  16. $.widget( "ui.progressbar", {
  17. version: "@VERSION",
  18. options: {
  19. value: 0,
  20. max: 100
  21. },
  22. min: 0,
  23. _create: function() {
  24. this.element
  25. .addClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
  26. .attr({
  27. role: "progressbar",
  28. "aria-valuemin": this.min,
  29. "aria-valuemax": this.options.max,
  30. "aria-valuenow": this._value()
  31. });
  32. this.valueDiv = $( "<div class='ui-progressbar-value ui-widget-header ui-corner-left'></div>" )
  33. .appendTo( this.element );
  34. this.oldValue = this._value();
  35. this._refreshValue();
  36. },
  37. _destroy: function() {
  38. this.element
  39. .removeClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
  40. .removeAttr( "role" )
  41. .removeAttr( "aria-valuemin" )
  42. .removeAttr( "aria-valuemax" )
  43. .removeAttr( "aria-valuenow" );
  44. this.valueDiv.remove();
  45. },
  46. value: function( newValue ) {
  47. if ( newValue === undefined ) {
  48. return this._value();
  49. }
  50. this._setOption( "value", newValue );
  51. return this;
  52. },
  53. _setOption: function( key, value ) {
  54. if ( key === "value" ) {
  55. this.options.value = value;
  56. this._refreshValue();
  57. if ( this._value() === this.options.max ) {
  58. this._trigger( "complete" );
  59. }
  60. }
  61. this._super( key, value );
  62. },
  63. _value: function() {
  64. var val = this.options.value;
  65. // normalize invalid value
  66. if ( typeof val !== "number" ) {
  67. val = 0;
  68. }
  69. return Math.min( this.options.max, Math.max( this.min, val ) );
  70. },
  71. _percentage: function() {
  72. return 100 * this._value() / this.options.max;
  73. },
  74. _refreshValue: function() {
  75. var value = this.value(),
  76. percentage = this._percentage();
  77. if ( this.oldValue !== value ) {
  78. this.oldValue = value;
  79. this._trigger( "change" );
  80. }
  81. this.valueDiv
  82. .toggle( value > this.min )
  83. .toggleClass( "ui-corner-right", value === this.options.max )
  84. .width( percentage.toFixed(0) + "%" );
  85. this.element.attr( "aria-valuenow", value );
  86. }
  87. });
  88. })( jQuery );