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