Upgrade to ExtJS 4.0.2 - Released 06/09/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     constructor : function(config) {
42         if (config) {
43             Ext.apply(this, config);
44         }
45     },
46     //public
47     init : function (parent) {
48         var displayItem;
49         if(parent.displayInfo) {
50             this.parent = parent;
51
52             displayItem = parent.child("#displayItem");
53             if (displayItem) {
54                 parent.remove(displayItem, true);
55             }
56
57             this.progressBar = Ext.create('Ext.ProgressBar', {
58                 text    : this.defaultText,
59                 width   : this.width,
60                 animate : this.defaultAnimCfg
61             });
62
63             parent.displayItem = this.progressBar;
64
65             parent.add(parent.displayItem);
66             parent.doLayout();
67             Ext.apply(parent, this.parentOverrides);
68
69             this.progressBar.on('render', function(pb) {
70                 pb.mon(pb.getEl().applyStyles('cursor:pointer'), 'click', this.handleProgressBarClick, this);
71             }, this, {single: true});
72         }
73     },
74     // private
75     // This method handles the click for the progress bar
76     handleProgressBarClick : function(e){
77         var parent = this.parent,
78             displayItem = parent.displayItem,
79             box = this.progressBar.getBox(),
80             xy = e.getXY(),
81             position = xy[0]- box.x,
82             pages = Math.ceil(parent.store.getTotalCount()/parent.pageSize),
83             newpage = Math.ceil(position/(displayItem.width/pages));
84
85         parent.store.loadPage(newpage);
86     },
87
88     // private, overriddes
89     parentOverrides  : {
90         // private
91         // This method updates the information via the progress bar.
92         updateInfo : function(){
93             if(this.displayItem){
94                 var count = this.store.getCount(),
95                     pageData = this.getPageData(),
96                     message = count === 0 ?
97                     this.emptyMsg :
98                     Ext.String.format(
99                         this.displayMsg,
100                         pageData.fromRecord, pageData.toRecord, this.store.getTotalCount()
101                     ),
102                     percentage = pageData.pageCount > 0 ? (pageData.currentPage / pageData.pageCount) : 0;
103
104                 this.displayItem.updateProgress(percentage, message, this.animate || this.defaultAnimConfig);
105             }
106         }
107     }
108 });
109
110