~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/ext/source/widgets/ProgressBar.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

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