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