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