00434004330f9e13dfb461dc12cdd5eeae661103
[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-refresh"></i>',
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 });