source: trunk/web/addons/job_monarch/lib/extjs/source/widgets/ProgressBar.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

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