1
0

progressbar.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*!
  2. * jQuery UI Progressbar 1.11.4
  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. * http://api.jqueryui.com/progressbar/
  10. */
  11. (function( factory ) {
  12. if ( typeof define === "function" && define.amd ) {
  13. // AMD. Register as an anonymous module.
  14. define([
  15. "jquery",
  16. "./core",
  17. "./widget"
  18. ], factory );
  19. } else {
  20. // Browser globals
  21. factory( jQuery );
  22. }
  23. }(function( $ ) {
  24. return $.widget( "ui.progressbar", {
  25. version: "1.11.4",
  26. options: {
  27. max: 100,
  28. value: 0,
  29. change: null,
  30. complete: null
  31. },
  32. min: 0,
  33. _create: function() {
  34. // Constrain initial value
  35. this.oldValue = this.options.value = this._constrainedValue();
  36. this.element
  37. .addClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
  38. .attr({
  39. // Only set static values, aria-valuenow and aria-valuemax are
  40. // set inside _refreshValue()
  41. role: "progressbar",
  42. "aria-valuemin": this.min
  43. });
  44. this.valueDiv = $( "<div class='ui-progressbar-value ui-widget-header ui-corner-left'></div>" )
  45. .appendTo( this.element );
  46. this._refreshValue();
  47. },
  48. _destroy: function() {
  49. this.element
  50. .removeClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" )
  51. .removeAttr( "role" )
  52. .removeAttr( "aria-valuemin" )
  53. .removeAttr( "aria-valuemax" )
  54. .removeAttr( "aria-valuenow" );
  55. this.valueDiv.remove();
  56. },
  57. value: function( newValue ) {
  58. if ( newValue === undefined ) {
  59. return this.options.value;
  60. }
  61. this.options.value = this._constrainedValue( newValue );
  62. this._refreshValue();
  63. },
  64. _constrainedValue: function( newValue ) {
  65. if ( newValue === undefined ) {
  66. newValue = this.options.value;
  67. }
  68. this.indeterminate = newValue === false;
  69. // sanitize value
  70. if ( typeof newValue !== "number" ) {
  71. newValue = 0;
  72. }
  73. return this.indeterminate ? false :
  74. Math.min( this.options.max, Math.max( this.min, newValue ) );
  75. },
  76. _setOptions: function( options ) {
  77. // Ensure "value" option is set after other values (like max)
  78. var value = options.value;
  79. delete options.value;
  80. this._super( options );
  81. this.options.value = this._constrainedValue( value );
  82. this._refreshValue();
  83. },
  84. _setOption: function( key, value ) {
  85. if ( key === "max" ) {
  86. // Don't allow a max less than min
  87. value = Math.max( this.min, value );
  88. }
  89. if ( key === "disabled" ) {
  90. this.element
  91. .toggleClass( "ui-state-disabled", !!value )
  92. .attr( "aria-disabled", value );
  93. }
  94. this._super( key, value );
  95. },
  96. _percentage: function() {
  97. return this.indeterminate ? 100 : 100 * ( this.options.value - this.min ) / ( this.options.max - this.min );
  98. },
  99. _refreshValue: function() {
  100. var value = this.options.value,
  101. percentage = this._percentage();
  102. this.valueDiv
  103. .toggle( this.indeterminate || value > this.min )
  104. .toggleClass( "ui-corner-right", value === this.options.max )
  105. .width( percentage.toFixed(0) + "%" );
  106. this.element.toggleClass( "ui-progressbar-indeterminate", this.indeterminate );
  107. if ( this.indeterminate ) {
  108. this.element.removeAttr( "aria-valuenow" );
  109. if ( !this.overlayDiv ) {
  110. this.overlayDiv = $( "<div class='ui-progressbar-overlay'></div>" ).appendTo( this.valueDiv );
  111. }
  112. } else {
  113. this.element.attr({
  114. "aria-valuemax": this.options.max,
  115. "aria-valuenow": value
  116. });
  117. if ( this.overlayDiv ) {
  118. this.overlayDiv.remove();
  119. this.overlayDiv = null;
  120. }
  121. }
  122. if ( this.oldValue !== value ) {
  123. this.oldValue = value;
  124. this._trigger( "change" );
  125. }
  126. if ( value === this.options.max ) {
  127. this._trigger( "complete" );
  128. }
  129. }
  130. });
  131. }));