initial import
[roojs1] / Roo / PagingToolbar.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
13  * @class Roo.PagingToolbar
14  * @extends Roo.Toolbar
15  * A specialized toolbar that is bound to a {@link Roo.data.Store} and provides automatic paging controls.
16  * @constructor
17  * Create a new PagingToolbar
18  * @param {Object} config The config object
19  */
20 Roo.PagingToolbar = function(el, ds, config)
21 {
22     // old args format still supported... - xtype is prefered..
23     if (typeof(el) == 'object' && el.xtype) {
24         // created from xtype...
25         config = el;
26         ds = el.dataSource;
27         el = config.container;
28     }
29     
30     
31     Roo.PagingToolbar.superclass.constructor.call(this, el, null, config);
32     this.ds = ds;
33     this.cursor = 0;
34     this.renderButtons(this.el);
35     this.bind(ds);
36 };
37
38 Roo.extend(Roo.PagingToolbar, Roo.Toolbar, {
39     /**
40      * @cfg {Roo.data.Store} dataSource
41      * The underlying data store providing the paged data
42      */
43     /**
44      * @cfg {String/HTMLElement/Element} container
45      * container The id or element that will contain the toolbar
46      */
47     /**
48      * @cfg {Boolean} displayInfo
49      * True to display the displayMsg (defaults to false)
50      */
51     /**
52      * @cfg {Number} pageSize
53      * The number of records to display per page (defaults to 20)
54      */
55     pageSize: 20,
56     /**
57      * @cfg {String} displayMsg
58      * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
59      */
60     displayMsg : 'Displaying {0} - {1} of {2}',
61     /**
62      * @cfg {String} emptyMsg
63      * The message to display when no records are found (defaults to "No data to display")
64      */
65     emptyMsg : 'No data to display',
66     /**
67      * Customizable piece of the default paging text (defaults to "Page")
68      * @type String
69      */
70     beforePageText : "Page",
71     /**
72      * Customizable piece of the default paging text (defaults to "of %0")
73      * @type String
74      */
75     afterPageText : "of {0}",
76     /**
77      * Customizable piece of the default paging text (defaults to "First Page")
78      * @type String
79      */
80     firstText : "First Page",
81     /**
82      * Customizable piece of the default paging text (defaults to "Previous Page")
83      * @type String
84      */
85     prevText : "Previous Page",
86     /**
87      * Customizable piece of the default paging text (defaults to "Next Page")
88      * @type String
89      */
90     nextText : "Next Page",
91     /**
92      * Customizable piece of the default paging text (defaults to "Last Page")
93      * @type String
94      */
95     lastText : "Last Page",
96     /**
97      * Customizable piece of the default paging text (defaults to "Refresh")
98      * @type String
99      */
100     refreshText : "Refresh",
101
102     // private
103     renderButtons : function(el){
104         Roo.PagingToolbar.superclass.render.call(this, el);
105         this.first = this.addButton({
106             tooltip: this.firstText,
107             cls: "x-btn-icon x-grid-page-first",
108             disabled: true,
109             handler: this.onClick.createDelegate(this, ["first"])
110         });
111         this.prev = this.addButton({
112             tooltip: this.prevText,
113             cls: "x-btn-icon x-grid-page-prev",
114             disabled: true,
115             handler: this.onClick.createDelegate(this, ["prev"])
116         });
117         this.addSeparator();
118         this.add(this.beforePageText);
119         this.field = Roo.get(this.addDom({
120            tag: "input",
121            type: "text",
122            size: "3",
123            value: "1",
124            cls: "x-grid-page-number"
125         }).el);
126         this.field.on("keydown", this.onPagingKeydown, this);
127         this.field.on("focus", function(){this.dom.select();});
128         this.afterTextEl = this.addText(String.format(this.afterPageText, 1));
129         this.field.setHeight(18);
130         this.addSeparator();
131         this.next = this.addButton({
132             tooltip: this.nextText,
133             cls: "x-btn-icon x-grid-page-next",
134             disabled: true,
135             handler: this.onClick.createDelegate(this, ["next"])
136         });
137         this.last = this.addButton({
138             tooltip: this.lastText,
139             cls: "x-btn-icon x-grid-page-last",
140             disabled: true,
141             handler: this.onClick.createDelegate(this, ["last"])
142         });
143         this.addSeparator();
144         this.loading = this.addButton({
145             tooltip: this.refreshText,
146             cls: "x-btn-icon x-grid-loading",
147             handler: this.onClick.createDelegate(this, ["refresh"])
148         });
149
150         if(this.displayInfo){
151             this.displayEl = Roo.fly(this.el.dom.firstChild).createChild({cls:'x-paging-info'});
152         }
153     },
154
155     // private
156     updateInfo : function(){
157         if(this.displayEl){
158             var count = this.ds.getCount();
159             var msg = count == 0 ?
160                 this.emptyMsg :
161                 String.format(
162                     this.displayMsg,
163                     this.cursor+1, this.cursor+count, this.ds.getTotalCount()    
164                 );
165             this.displayEl.update(msg);
166         }
167     },
168
169     // private
170     onLoad : function(ds, r, o){
171        this.cursor = o.params ? o.params.start : 0;
172        var d = this.getPageData(), ap = d.activePage, ps = d.pages;
173
174        this.afterTextEl.el.innerHTML = String.format(this.afterPageText, d.pages);
175        this.field.dom.value = ap;
176        this.first.setDisabled(ap == 1);
177        this.prev.setDisabled(ap == 1);
178        this.next.setDisabled(ap == ps);
179        this.last.setDisabled(ap == ps);
180        this.loading.enable();
181        this.updateInfo();
182     },
183
184     // private
185     getPageData : function(){
186         var total = this.ds.getTotalCount();
187         return {
188             total : total,
189             activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
190             pages :  total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
191         };
192     },
193
194     // private
195     onLoadError : function(){
196         this.loading.enable();
197     },
198
199     // private
200     onPagingKeydown : function(e){
201         var k = e.getKey();
202         var d = this.getPageData();
203         if(k == e.RETURN){
204             var v = this.field.dom.value, pageNum;
205             if(!v || isNaN(pageNum = parseInt(v, 10))){
206                 this.field.dom.value = d.activePage;
207                 return;
208             }
209             pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
210             this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
211             e.stopEvent();
212         }
213         else if(k == e.HOME || (k == e.UP && e.ctrlKey) || (k == e.PAGEUP && e.ctrlKey) || (k == e.RIGHT && e.ctrlKey) || k == e.END || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey))
214         {
215           var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
216           this.field.dom.value = pageNum;
217           this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
218           e.stopEvent();
219         }
220         else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
221         {
222           var v = this.field.dom.value, pageNum; 
223           var increment = (e.shiftKey) ? 10 : 1;
224           if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
225             increment *= -1;
226           if(!v || isNaN(pageNum = parseInt(v, 10))) {
227             this.field.dom.value = d.activePage;
228             return;
229           }
230           else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
231           {
232             this.field.dom.value = parseInt(v, 10) + increment;
233             pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
234             this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
235           }
236           e.stopEvent();
237         }
238     },
239
240     // private
241     beforeLoad : function(){
242         if(this.loading){
243             this.loading.disable();
244         }
245     },
246
247     // private
248     onClick : function(which){
249         var ds = this.ds;
250         switch(which){
251             case "first":
252                 ds.load({params:{start: 0, limit: this.pageSize}});
253             break;
254             case "prev":
255                 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
256             break;
257             case "next":
258                 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
259             break;
260             case "last":
261                 var total = ds.getTotalCount();
262                 var extra = total % this.pageSize;
263                 var lastStart = extra ? (total - extra) : total-this.pageSize;
264                 ds.load({params:{start: lastStart, limit: this.pageSize}});
265             break;
266             case "refresh":
267                 ds.load({params:{start: this.cursor, limit: this.pageSize}});
268             break;
269         }
270     },
271
272     /**
273      * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
274      * @param {Roo.data.Store} store The data store to unbind
275      */
276     unbind : function(ds){
277         ds.un("beforeload", this.beforeLoad, this);
278         ds.un("load", this.onLoad, this);
279         ds.un("loadexception", this.onLoadError, this);
280         ds.un("remove", this.updateInfo, this);
281         ds.un("add", this.updateInfo, this);
282         this.ds = undefined;
283     },
284
285     /**
286      * Binds the paging toolbar to the specified {@link Roo.data.Store}
287      * @param {Roo.data.Store} store The data store to bind
288      */
289     bind : function(ds){
290         ds.on("beforeload", this.beforeLoad, this);
291         ds.on("load", this.onLoad, this);
292         ds.on("loadexception", this.onLoadError, this);
293         ds.on("remove", this.updateInfo, this);
294         ds.on("add", this.updateInfo, this);
295         this.ds = ds;
296     }
297 });