ProgressBar2.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-ProgressBar'>/**
  19. </span> * An updateable progress bar component. The progress bar supports two different modes: manual and automatic.
  20. *
  21. * In manual mode, you are responsible for showing, updating (via {@link #updateProgress}) and clearing the progress bar
  22. * as needed from your own code. This method is most appropriate when you want to show progress throughout an operation
  23. * that has predictable points of interest at which you can update the control.
  24. *
  25. * In automatic mode, you simply call {@link #wait} and let the progress bar run indefinitely, only clearing it once the
  26. * operation is complete. You can optionally have the progress bar wait for a specific amount of time and then clear
  27. * itself. Automatic mode is most appropriate for timed operations or asynchronous operations in which you have no need
  28. * for indicating intermediate progress.
  29. *
  30. * @example
  31. * var p = Ext.create('Ext.ProgressBar', {
  32. * renderTo: Ext.getBody(),
  33. * width: 300
  34. * });
  35. *
  36. * // Wait for 5 seconds, then update the status el (progress bar will auto-reset)
  37. * p.wait({
  38. * interval: 500, //bar will move fast!
  39. * duration: 50000,
  40. * increment: 15,
  41. * text: 'Updating...',
  42. * scope: this,
  43. * fn: function(){
  44. * p.updateText('Done!');
  45. * }
  46. * });
  47. */
  48. Ext.define('Ext.ProgressBar', {
  49. extend: 'Ext.Component',
  50. alias: 'widget.progressbar',
  51. requires: [
  52. 'Ext.Template',
  53. 'Ext.CompositeElement',
  54. 'Ext.TaskManager',
  55. 'Ext.layout.component.ProgressBar'
  56. ],
  57. uses: ['Ext.fx.Anim'],
  58. <span id='Ext-ProgressBar-cfg-value'> /**
  59. </span> * @cfg {Number} [value=0]
  60. * A floating point value between 0 and 1 (e.g., .5)
  61. */
  62. <span id='Ext-ProgressBar-cfg-textEl'> /**
  63. </span> * @cfg {String/HTMLElement/Ext.Element} textEl
  64. * The element to render the progress text to (defaults to the progress bar's internal text element)
  65. */
  66. <span id='Ext-ProgressBar-cfg-id'> /**
  67. </span> * @cfg {String} id
  68. * The progress bar element's id (defaults to an auto-generated id)
  69. */
  70. <span id='Ext-ProgressBar-cfg-baseCls'> /**
  71. </span> * @cfg {String} [baseCls='x-progress']
  72. * The base CSS class to apply to the progress bar's wrapper element.
  73. */
  74. baseCls: Ext.baseCSSPrefix + 'progress',
  75. <span id='Ext-ProgressBar-cfg-animate'> /**
  76. </span> * @cfg {Boolean} animate
  77. * True to animate the progress bar during transitions.
  78. */
  79. animate: false,
  80. <span id='Ext-ProgressBar-cfg-text'> /**
  81. </span> * @cfg {String} text
  82. * The text shown in the progress bar.
  83. */
  84. text: '',
  85. // private
  86. waitTimer: null,
  87. childEls: [
  88. 'bar'
  89. ],
  90. renderTpl: [
  91. '&lt;tpl if=&quot;internalText&quot;&gt;',
  92. '&lt;div class=&quot;{baseCls}-text {baseCls}-text-back&quot;&gt;{text}&lt;/div&gt;',
  93. '&lt;/tpl&gt;',
  94. '&lt;div id=&quot;{id}-bar&quot; class=&quot;{baseCls}-bar&quot; style=&quot;width:{percentage}%&quot;&gt;',
  95. '&lt;tpl if=&quot;internalText&quot;&gt;',
  96. '&lt;div class=&quot;{baseCls}-text&quot;&gt;',
  97. '&lt;div&gt;{text}&lt;/div&gt;',
  98. '&lt;/div&gt;',
  99. '&lt;/tpl&gt;',
  100. '&lt;/div&gt;'
  101. ],
  102. componentLayout: 'progressbar',
  103. // private
  104. initComponent: function() {
  105. this.callParent();
  106. this.addEvents(
  107. <span id='Ext-ProgressBar-event-update'> /**
  108. </span> * @event update
  109. * Fires after each update interval
  110. * @param {Ext.ProgressBar} this
  111. * @param {Number} value The current progress value
  112. * @param {String} text The current progress text
  113. */
  114. &quot;update&quot;
  115. );
  116. },
  117. initRenderData: function() {
  118. var me = this;
  119. return Ext.apply(me.callParent(), {
  120. internalText : !me.hasOwnProperty('textEl'),
  121. text : me.text || '&amp;#160;',
  122. percentage : me.value ? me.value * 100 : 0
  123. });
  124. },
  125. onRender : function() {
  126. var me = this;
  127. me.callParent(arguments);
  128. // External text display
  129. if (me.textEl) {
  130. me.textEl = Ext.get(me.textEl);
  131. me.updateText(me.text);
  132. }
  133. // Inline text display
  134. else {
  135. // This produces a composite w/2 el's (which is why we cannot use childEls or
  136. // renderSelectors):
  137. me.textEl = me.el.select('.' + me.baseCls + '-text');
  138. }
  139. },
  140. <span id='Ext-ProgressBar-method-updateProgress'> /**
  141. </span> * Updates the progress bar value, and optionally its text. If the text argument is not specified, any existing text
  142. * value will be unchanged. To blank out existing text, pass ''. Note that even if the progress bar value exceeds 1,
  143. * it will never automatically reset -- you are responsible for determining when the progress is complete and
  144. * calling {@link #reset} to clear and/or hide the control.
  145. * @param {Number} [value=0] A floating point value between 0 and 1 (e.g., .5)
  146. * @param {String} [text=''] The string to display in the progress text element
  147. * @param {Boolean} [animate=false] Whether to animate the transition of the progress bar. If this value is not
  148. * specified, the default for the class is used
  149. * @return {Ext.ProgressBar} this
  150. */
  151. updateProgress: function(value, text, animate) {
  152. var me = this,
  153. oldValue = me.value;
  154. me.value = value || 0;
  155. if (text) {
  156. me.updateText(text);
  157. }
  158. if (me.rendered &amp;&amp; !me.isDestroyed) {
  159. if (animate === true || (animate !== false &amp;&amp; me.animate)) {
  160. me.bar.stopAnimation();
  161. me.bar.animate(Ext.apply({
  162. from: {
  163. width: (oldValue * 100) + '%'
  164. },
  165. to: {
  166. width: (me.value * 100) + '%'
  167. }
  168. }, me.animate));
  169. } else {
  170. me.bar.setStyle('width', (me.value * 100) + '%');
  171. }
  172. }
  173. me.fireEvent('update', me, me.value, text);
  174. return me;
  175. },
  176. <span id='Ext-ProgressBar-method-updateText'> /**
  177. </span> * Updates the progress bar text. If specified, textEl will be updated, otherwise the progress bar itself will
  178. * display the updated text.
  179. * @param {String} [text=''] The string to display in the progress text element
  180. * @return {Ext.ProgressBar} this
  181. */
  182. updateText: function(text) {
  183. var me = this;
  184. me.text = text;
  185. if (me.rendered) {
  186. me.textEl.update(me.text);
  187. }
  188. return me;
  189. },
  190. applyText : function(text) {
  191. this.updateText(text);
  192. },
  193. getText: function(){
  194. return this.text;
  195. },
  196. <span id='Ext-ProgressBar-method-wait'> /**
  197. </span> * Initiates an auto-updating progress bar. A duration can be specified, in which case the progress bar will
  198. * automatically reset after a fixed amount of time and optionally call a callback function if specified. If no
  199. * duration is passed in, then the progress bar will run indefinitely and must be manually cleared by calling
  200. * {@link #reset}.
  201. *
  202. * Example usage:
  203. *
  204. * var p = new Ext.ProgressBar({
  205. * renderTo: 'my-el'
  206. * });
  207. *
  208. * //Wait for 5 seconds, then update the status el (progress bar will auto-reset)
  209. * var p = Ext.create('Ext.ProgressBar', {
  210. * renderTo: Ext.getBody(),
  211. * width: 300
  212. * });
  213. *
  214. * //Wait for 5 seconds, then update the status el (progress bar will auto-reset)
  215. * p.wait({
  216. * interval: 500, //bar will move fast!
  217. * duration: 50000,
  218. * increment: 15,
  219. * text: 'Updating...',
  220. * scope: this,
  221. * fn: function(){
  222. * p.updateText('Done!');
  223. * }
  224. * });
  225. *
  226. * //Or update indefinitely until some async action completes, then reset manually
  227. * p.wait();
  228. * myAction.on('complete', function(){
  229. * p.reset();
  230. * p.updateText('Done!');
  231. * });
  232. *
  233. * @param {Object} config (optional) Configuration options
  234. * @param {Number} config.duration The length of time in milliseconds that the progress bar should
  235. * run before resetting itself (defaults to undefined, in which case it will run indefinitely
  236. * until reset is called)
  237. * @param {Number} config.interval The length of time in milliseconds between each progress update
  238. * (defaults to 1000 ms)
  239. * @param {Boolean} config.animate Whether to animate the transition of the progress bar. If this
  240. * value is not specified, the default for the class is used.
  241. * @param {Number} config.increment The number of progress update segments to display within the
  242. * progress bar (defaults to 10). If the bar reaches the end and is still updating, it will
  243. * automatically wrap back to the beginning.
  244. * @param {String} config.text Optional text to display in the progress bar element (defaults to '').
  245. * @param {Function} config.fn A callback function to execute after the progress bar finishes auto-
  246. * updating. The function will be called with no arguments. This function will be ignored if
  247. * duration is not specified since in that case the progress bar can only be stopped programmatically,
  248. * so any required function should be called by the same code after it resets the progress bar.
  249. * @param {Object} config.scope The scope that is passed to the callback function (only applies when
  250. * duration and fn are both passed).
  251. * @return {Ext.ProgressBar} this
  252. */
  253. wait: function(o) {
  254. var me = this, scope;
  255. if (!me.waitTimer) {
  256. scope = me;
  257. o = o || {};
  258. me.updateText(o.text);
  259. me.waitTimer = Ext.TaskManager.start({
  260. run: function(i){
  261. var inc = o.increment || 10;
  262. i -= 1;
  263. me.updateProgress(((((i+inc)%inc)+1)*(100/inc))*0.01, null, o.animate);
  264. },
  265. interval: o.interval || 1000,
  266. duration: o.duration,
  267. onStop: function(){
  268. if (o.fn) {
  269. o.fn.apply(o.scope || me);
  270. }
  271. me.reset();
  272. },
  273. scope: scope
  274. });
  275. }
  276. return me;
  277. },
  278. <span id='Ext-ProgressBar-method-isWaiting'> /**
  279. </span> * Returns true if the progress bar is currently in a {@link #wait} operation
  280. * @return {Boolean} True if waiting, else false
  281. */
  282. isWaiting: function(){
  283. return this.waitTimer !== null;
  284. },
  285. <span id='Ext-ProgressBar-method-reset'> /**
  286. </span> * Resets the progress bar value to 0 and text to empty string. If hide = true, the progress bar will also be hidden
  287. * (using the {@link #hideMode} property internally).
  288. * @param {Boolean} [hide=false] True to hide the progress bar.
  289. * @return {Ext.ProgressBar} this
  290. */
  291. reset: function(hide){
  292. var me = this;
  293. me.updateProgress(0);
  294. me.clearTimer();
  295. if (hide === true) {
  296. me.hide();
  297. }
  298. return me;
  299. },
  300. // private
  301. clearTimer: function(){
  302. var me = this;
  303. if (me.waitTimer) {
  304. me.waitTimer.onStop = null; //prevent recursion
  305. Ext.TaskManager.stop(me.waitTimer);
  306. me.waitTimer = null;
  307. }
  308. },
  309. onDestroy: function(){
  310. var me = this;
  311. me.clearTimer();
  312. if (me.rendered) {
  313. if (me.textEl.isComposite) {
  314. me.textEl.clear();
  315. }
  316. Ext.destroyMembers(me, 'textEl', 'progressBar');
  317. }
  318. me.callParent();
  319. }
  320. });
  321. </pre>
  322. </body>
  323. </html>