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