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