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