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