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