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