Roo/bootstrap/PagingToolbar.js
[roojs1] / Roo / bootstrap / 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.bootstrap.PagingToolbar
14  * @extends Roo.bootstrap.NavSimplebar
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  * @param {Roo.data.Store} store
20  */
21 Roo.bootstrap.PagingToolbar = function(config)
22 {
23     // old args format still supported... - xtype is prefered..
24         // created from xtype...
25     
26     this.ds = config.dataSource;
27     
28     if (config.store && !this.ds) {
29         this.store= Roo.factory(config.store, Roo.data);
30         this.ds = this.store;
31         this.ds.xmodule = this.xmodule || false;
32     }
33     
34     this.toolbarItems = [];
35     if (config.items) {
36         this.toolbarItems = config.items;
37     }
38     
39     Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
40     
41     this.cursor = 0;
42     
43     if (this.ds) { 
44         this.bind(this.ds);
45     }
46     
47     this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
48     
49 };
50
51 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
52     /**
53      * @cfg {Roo.data.Store} dataSource
54      * The underlying data store providing the paged data
55      */
56     /**
57      * @cfg {String/HTMLElement/Element} container
58      * container The id or element that will contain the toolbar
59      */
60     /**
61      * @cfg {Boolean} displayInfo
62      * True to display the displayMsg (defaults to false)
63      */
64     /**
65      * @cfg {Number} pageSize
66      * The number of records to display per page (defaults to 20)
67      */
68     pageSize: 20,
69     /**
70      * @cfg {String} displayMsg
71      * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
72      */
73     displayMsg : 'Displaying {0} - {1} of {2}',
74     /**
75      * @cfg {String} emptyMsg
76      * The message to display when no records are found (defaults to "No data to display")
77      */
78     emptyMsg : 'No data to display',
79     /**
80      * Customizable piece of the default paging text (defaults to "Page")
81      * @type String
82      */
83     beforePageText : "Page",
84     /**
85      * Customizable piece of the default paging text (defaults to "of %0")
86      * @type String
87      */
88     afterPageText : "of {0}",
89     /**
90      * Customizable piece of the default paging text (defaults to "First Page")
91      * @type String
92      */
93     firstText : "First Page",
94     /**
95      * Customizable piece of the default paging text (defaults to "Previous Page")
96      * @type String
97      */
98     prevText : "Previous Page",
99     /**
100      * Customizable piece of the default paging text (defaults to "Next Page")
101      * @type String
102      */
103     nextText : "Next Page",
104     /**
105      * Customizable piece of the default paging text (defaults to "Last Page")
106      * @type String
107      */
108     lastText : "Last Page",
109     /**
110      * Customizable piece of the default paging text (defaults to "Refresh")
111      * @type String
112      */
113     refreshText : "Refresh",
114
115     buttons : false,
116     // private
117     onRender : function(ct, position) 
118     {
119         Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
120         this.navgroup.parentId = this.id;
121         this.navgroup.onRender(this.el, null);
122         // add the buttons to the navgroup
123         
124         if(this.displayInfo){
125             this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
126             this.displayEl = this.el.select('.x-paging-info', true).first();
127 //            var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
128 //            this.displayEl = navel.el.select('span',true).first();
129         }
130         
131         var _this = this;
132         
133         if(this.buttons){
134             Roo.each(_this.buttons, function(e){ // this might need to use render????
135                Roo.factory(e).onRender(_this.el, null);
136             });
137         }
138             
139         Roo.each(_this.toolbarItems, function(e) {
140             _this.navgroup.addItem(e);
141         });
142         
143         
144         this.first = this.navgroup.addItem({
145             tooltip: this.firstText,
146             cls: "prev",
147             icon : 'fa fa-backward',
148             disabled: true,
149             preventDefault: true,
150             listeners : { click : this.onClick.createDelegate(this, ["first"]) }
151         });
152         
153         this.prev =  this.navgroup.addItem({
154             tooltip: this.prevText,
155             cls: "prev",
156             icon : 'fa fa-step-backward',
157             disabled: true,
158             preventDefault: true,
159             listeners : { click :  this.onClick.createDelegate(this, ["prev"]) }
160         });
161     //this.addSeparator();
162         
163         
164         var field = this.navgroup.addItem( {
165             tagtype : 'span',
166             cls : 'x-paging-position',
167             
168             html : this.beforePageText  +
169                 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
170                 '<span class="x-paging-after">' +  String.format(this.afterPageText, 1) + '</span>'
171          } ); //?? escaped?
172         
173         this.field = field.el.select('input', true).first();
174         this.field.on("keydown", this.onPagingKeydown, this);
175         this.field.on("focus", function(){this.dom.select();});
176     
177     
178         this.afterTextEl =  field.el.select('.x-paging-after',true).first();
179         //this.field.setHeight(18);
180         //this.addSeparator();
181         this.next = this.navgroup.addItem({
182             tooltip: this.nextText,
183             cls: "next",
184             html : ' <i class="fa fa-step-forward">',
185             disabled: true,
186             preventDefault: true,
187             listeners : { click :  this.onClick.createDelegate(this, ["next"]) }
188         });
189         this.last = this.navgroup.addItem({
190             tooltip: this.lastText,
191             icon : 'fa fa-forward',
192             cls: "next",
193             disabled: true,
194             preventDefault: true,
195             listeners : { click :  this.onClick.createDelegate(this, ["last"]) }
196         });
197     //this.addSeparator();
198         this.loading = this.navgroup.addItem({
199             tooltip: this.refreshText,
200             icon: 'fa fa-refresh',
201             preventDefault: true,
202             listeners : { click : this.onClick.createDelegate(this, ["refresh"]) }
203         });
204         
205     },
206
207     // private
208     updateInfo : function(){
209         if(this.displayEl){
210             var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
211             var msg = count == 0 ?
212                 this.emptyMsg :
213                 String.format(
214                     this.displayMsg,
215                     this.cursor+1, this.cursor+count, this.ds.getTotalCount()    
216                 );
217             this.displayEl.update(msg);
218         }
219     },
220
221     // private
222     onLoad : function(ds, r, o)
223     {
224         this.cursor = o.params ? o.params.start : 0;
225         var d = this.getPageData(),
226             ap = d.activePage,
227             ps = d.pages;
228         
229         
230         this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
231         this.field.dom.value = ap;
232         this.first.setDisabled(ap == 1);
233         this.prev.setDisabled(ap == 1);
234         this.next.setDisabled(ap == ps);
235         this.last.setDisabled(ap == ps);
236         this.loading.enable();
237         this.updateInfo();
238     },
239
240     // private
241     getPageData : function(){
242         var total = this.ds.getTotalCount();
243         return {
244             total : total,
245             activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
246             pages :  total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
247         };
248     },
249
250     // private
251     onLoadError : function(){
252         this.loading.enable();
253     },
254
255     // private
256     onPagingKeydown : function(e){
257         var k = e.getKey();
258         var d = this.getPageData();
259         if(k == e.RETURN){
260             var v = this.field.dom.value, pageNum;
261             if(!v || isNaN(pageNum = parseInt(v, 10))){
262                 this.field.dom.value = d.activePage;
263                 return;
264             }
265             pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
266             this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
267             e.stopEvent();
268         }
269         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))
270         {
271           var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
272           this.field.dom.value = pageNum;
273           this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
274           e.stopEvent();
275         }
276         else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
277         {
278           var v = this.field.dom.value, pageNum; 
279           var increment = (e.shiftKey) ? 10 : 1;
280           if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN) {
281                 increment *= -1;
282           }
283           if(!v || isNaN(pageNum = parseInt(v, 10))) {
284             this.field.dom.value = d.activePage;
285             return;
286           }
287           else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
288           {
289             this.field.dom.value = parseInt(v, 10) + increment;
290             pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
291             this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
292           }
293           e.stopEvent();
294         }
295     },
296
297     // private
298     beforeLoad : function(){
299         if(this.loading){
300             this.loading.disable();
301         }
302     },
303
304     // private
305     onClick : function(which){
306         
307         var ds = this.ds;
308         if (!ds) {
309             return;
310         }
311         
312         switch(which){
313             case "first":
314                 ds.load({params:{start: 0, limit: this.pageSize}});
315             break;
316             case "prev":
317                 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
318             break;
319             case "next":
320                 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
321             break;
322             case "last":
323                 var total = ds.getTotalCount();
324                 var extra = total % this.pageSize;
325                 var lastStart = extra ? (total - extra) : total-this.pageSize;
326                 ds.load({params:{start: lastStart, limit: this.pageSize}});
327             break;
328             case "refresh":
329                 ds.load({params:{start: this.cursor, limit: this.pageSize}});
330             break;
331         }
332     },
333
334     /**
335      * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
336      * @param {Roo.data.Store} store The data store to unbind
337      */
338     unbind : function(ds){
339         ds.un("beforeload", this.beforeLoad, this);
340         ds.un("load", this.onLoad, this);
341         ds.un("loadexception", this.onLoadError, this);
342         ds.un("remove", this.updateInfo, this);
343         ds.un("add", this.updateInfo, this);
344         this.ds = undefined;
345     },
346
347     /**
348      * Binds the paging toolbar to the specified {@link Roo.data.Store}
349      * @param {Roo.data.Store} store The data store to bind
350      */
351     bind : function(ds){
352         ds.on("beforeload", this.beforeLoad, this);
353         ds.on("load", this.onLoad, this);
354         ds.on("loadexception", this.onLoadError, this);
355         ds.on("remove", this.updateInfo, this);
356         ds.on("add", this.updateInfo, this);
357         this.ds = ds;
358     }
359 });