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