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