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