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