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