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