3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * An updateable progress bar component. The progress bar supports two different modes: manual and automatic.
18 * In manual mode, you are responsible for showing, updating (via {@link #updateProgress}) and clearing the progress bar
19 * as needed from your own code. This method is most appropriate when you want to show progress throughout an operation
20 * that has predictable points of interest at which you can update the control.
22 * In automatic mode, you simply call {@link #wait} and let the progress bar run indefinitely, only clearing it once the
23 * operation is complete. You can optionally have the progress bar wait for a specific amount of time and then clear
24 * itself. Automatic mode is most appropriate for timed operations or asynchronous operations in which you have no need
25 * for indicating intermediate progress.
28 * var p = Ext.create('Ext.ProgressBar', {
29 * renderTo: Ext.getBody(),
33 * // Wait for 5 seconds, then update the status el (progress bar will auto-reset)
35 * interval: 500, //bar will move fast!
38 * text: 'Updating...',
41 * p.updateText('Done!');
45 Ext.define('Ext.ProgressBar', {
46 extend: 'Ext.Component',
47 alias: 'widget.progressbar',
51 'Ext.CompositeElement',
53 'Ext.layout.component.ProgressBar'
56 uses: ['Ext.fx.Anim'],
59 * @cfg {Number} [value=0]
60 * A floating point value between 0 and 1 (e.g., .5)
64 * @cfg {String} [text='']
65 * The progress bar text (defaults to '')
69 * @cfg {String/HTMLElement/Ext.Element} textEl
70 * The element to render the progress text to (defaults to the progress bar's internal text element)
75 * The progress bar element's id (defaults to an auto-generated id)
79 * @cfg {String} [baseCls='x-progress']
80 * The base CSS class to apply to the progress bar's wrapper element.
82 baseCls: Ext.baseCSSPrefix + 'progress',
86 * @cfg {Boolean} animate
87 * True to animate the progress bar during transitions
93 * The text shown in the progress bar
102 '<div class="{baseCls}-text {baseCls}-text-back">',
105 '<div id="{id}-bar" class="{baseCls}-bar">',
106 '<div class="{baseCls}-text">',
112 componentLayout: 'progressbar',
115 initComponent: function() {
118 this.addChildEls('bar');
123 * Fires after each update interval
124 * @param {Ext.ProgressBar} this
125 * @param {Number} value The current progress value
126 * @param {String} text The current progress text
132 afterRender : function() {
135 // This produces a composite w/2 el's (which is why we cannot use childEls or
137 me.textEl = me.textEl ? Ext.get(me.textEl) : me.el.select('.' + me.baseCls + '-text');
139 me.callParent(arguments);
142 me.updateProgress(me.value, me.text);
145 me.updateText(me.text);
150 * Updates the progress bar value, and optionally its text. If the text argument is not specified, any existing text
151 * value will be unchanged. To blank out existing text, pass ''. Note that even if the progress bar value exceeds 1,
152 * it will never automatically reset -- you are responsible for determining when the progress is complete and
153 * calling {@link #reset} to clear and/or hide the control.
154 * @param {Number} [value=0] A floating point value between 0 and 1 (e.g., .5)
155 * @param {String} [text=''] The string to display in the progress text element
156 * @param {Boolean} [animate=false] Whether to animate the transition of the progress bar. If this value is not
157 * specified, the default for the class is used
158 * @return {Ext.ProgressBar} this
160 updateProgress: function(value, text, animate) {
164 me.value = value || 0;
168 if (me.rendered && !me.isDestroyed) {
169 if (me.isVisible(true)) {
170 newWidth = Math.floor(me.value * me.el.getWidth(true));
171 if (Ext.isForcedBorderBox) {
172 newWidth += me.bar.getBorderWidth("lr");
174 if (animate === true || (animate !== false && me.animate)) {
175 me.bar.stopAnimation();
176 me.bar.animate(Ext.apply({
178 width: newWidth + 'px'
182 me.bar.setWidth(newWidth);
185 // force a layout when we're visible again
186 me.doComponentLayout();
189 me.fireEvent('update', me, me.value, text);
194 * Updates the progress bar text. If specified, textEl will be updated, otherwise the progress bar itself will
195 * display the updated text.
196 * @param {String} [text=''] The string to display in the progress text element
197 * @return {Ext.ProgressBar} this
199 updateText: function(text) {
204 me.textEl.update(me.text);
209 applyText : function(text) {
210 this.updateText(text);
214 * Initiates an auto-updating progress bar. A duration can be specified, in which case the progress bar will
215 * automatically reset after a fixed amount of time and optionally call a callback function if specified. If no
216 * duration is passed in, then the progress bar will run indefinitely and must be manually cleared by calling
221 * var p = new Ext.ProgressBar({
225 * //Wait for 5 seconds, then update the status el (progress bar will auto-reset)
226 * var p = Ext.create('Ext.ProgressBar', {
227 * renderTo: Ext.getBody(),
231 * //Wait for 5 seconds, then update the status el (progress bar will auto-reset)
233 * interval: 500, //bar will move fast!
236 * text: 'Updating...',
239 * p.updateText('Done!');
243 * //Or update indefinitely until some async action completes, then reset manually
245 * myAction.on('complete', function(){
247 * p.updateText('Done!');
250 * @param {Object} config (optional) Configuration options
251 * @param {Number} config.duration The length of time in milliseconds that the progress bar should
252 * run before resetting itself (defaults to undefined, in which case it will run indefinitely
253 * until reset is called)
254 * @param {Number} config.interval The length of time in milliseconds between each progress update
255 * (defaults to 1000 ms)
256 * @param {Boolean} config.animate Whether to animate the transition of the progress bar. If this
257 * value is not specified, the default for the class is used.
258 * @param {Number} config.increment The number of progress update segments to display within the
259 * progress bar (defaults to 10). If the bar reaches the end and is still updating, it will
260 * automatically wrap back to the beginning.
261 * @param {String} config.text Optional text to display in the progress bar element (defaults to '').
262 * @param {Function} config.fn A callback function to execute after the progress bar finishes auto-
263 * updating. The function will be called with no arguments. This function will be ignored if
264 * duration is not specified since in that case the progress bar can only be stopped programmatically,
265 * so any required function should be called by the same code after it resets the progress bar.
266 * @param {Object} config.scope The scope that is passed to the callback function (only applies when
267 * duration and fn are both passed).
268 * @return {Ext.ProgressBar} this
276 me.updateText(o.text);
277 me.waitTimer = Ext.TaskManager.start({
279 var inc = o.increment || 10;
281 me.updateProgress(((((i+inc)%inc)+1)*(100/inc))*0.01, null, o.animate);
283 interval: o.interval || 1000,
284 duration: o.duration,
287 o.fn.apply(o.scope || me);
298 * Returns true if the progress bar is currently in a {@link #wait} operation
299 * @return {Boolean} True if waiting, else false
301 isWaiting: function(){
302 return this.waitTimer !== null;
306 * Resets the progress bar value to 0 and text to empty string. If hide = true, the progress bar will also be hidden
307 * (using the {@link #hideMode} property internally).
308 * @param {Boolean} [hide=false] True to hide the progress bar.
309 * @return {Ext.ProgressBar} this
311 reset: function(hide){
314 me.updateProgress(0);
323 clearTimer: function(){
327 me.waitTimer.onStop = null; //prevent recursion
328 Ext.TaskManager.stop(me.waitTimer);
333 onDestroy: function(){
338 if (me.textEl.isComposite) {
341 Ext.destroyMembers(me, 'textEl', 'progressBar');