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