Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / examples / ux / ProgressBarPager.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
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.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16 * @class Ext.ux.ProgressBarPager
17 * @extends Object
18 * Plugin for displaying a progressbar inside of a paging toolbar instead of plain text
19 * @constructor
20 * Create a new ItemSelector
21 * @param {Object} config Configuration options
22 */
23 Ext.define('Ext.ux.ProgressBarPager', {
24     extend: 'Object',
25
26     requires: ['Ext.ProgressBar'],
27     /**
28      * @cfg {Integer} width
29      * <p>The default progress bar width.  Default is 225.</p>
30     */
31     width   : 225,
32     /**
33      * @cfg {String} defaultText
34     * <p>The text to display while the store is loading.  Default is 'Loading...'</p>
35      */
36     defaultText    : 'Loading...',
37     /**
38      * @cfg {Object} defaultAnimCfg
39      * <p>A {@link Ext.fx.Anim Ext.fx.Anim} configuration object.</p>
40      */
41     defaultAnimCfg : {
42                 duration: 1000,
43                 easing: 'bounceOut'     
44         },      
45     
46     constructor : function(config) {
47         if (config) {
48             Ext.apply(this, config);
49         }
50     },
51     //public
52     init : function (parent) {
53         var displayItem;
54         if(parent.displayInfo) {
55             this.parent = parent;
56
57             displayItem = parent.child("#displayItem");
58             if (displayItem) {
59                 parent.remove(displayItem, true);
60             }
61
62             this.progressBar = Ext.create('Ext.ProgressBar', {
63                 text    : this.defaultText,
64                 width   : this.width,
65                 animate : this.defaultAnimCfg
66             });
67
68             parent.displayItem = this.progressBar;
69
70             parent.add(parent.displayItem);
71             parent.doLayout();
72             Ext.apply(parent, this.parentOverrides);
73
74             this.progressBar.on('render', function(pb) {
75                 pb.mon(pb.getEl().applyStyles('cursor:pointer'), 'click', this.handleProgressBarClick, this);
76             }, this, {single: true});
77         }
78     },
79     // private
80     // This method handles the click for the progress bar
81     handleProgressBarClick : function(e){
82         var parent = this.parent,
83             displayItem = parent.displayItem,
84             box = this.progressBar.getBox(),
85             xy = e.getXY(),
86             position = xy[0]- box.x,
87             pages = Math.ceil(parent.store.getTotalCount()/parent.pageSize),
88             newpage = Math.ceil(position/(displayItem.width/pages));
89
90         parent.store.loadPage(newpage);
91     },
92
93     // private, overriddes
94     parentOverrides  : {
95         // private
96         // This method updates the information via the progress bar.
97         updateInfo : function(){
98             if(this.displayItem){
99                 var count = this.store.getCount(),
100                     pageData = this.getPageData(),
101                     message = count === 0 ?
102                     this.emptyMsg :
103                     Ext.String.format(
104                         this.displayMsg,
105                         pageData.fromRecord, pageData.toRecord, this.store.getTotalCount()
106                     ),
107                     percentage = pageData.pageCount > 0 ? (pageData.currentPage / pageData.pageCount) : 0;
108
109                 this.displayItem.updateProgress(percentage, message, this.animate || this.defaultAnimConfig);
110             }
111         }
112     }
113 });
114
115