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