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