From bd927a7319fc3b107f13a534722bf3b2f47bd03c Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Sun, 22 May 2011 20:25:34 -0700 Subject: [PATCH 1/3] Add oneIndexed option, allowing page counting to optionally start at 1 --- src/jquery.pagination.js | 66 ++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/src/jquery.pagination.js b/src/jquery.pagination.js index db2da39..82fcfca 100644 --- a/src/jquery.pagination.js +++ b/src/jquery.pagination.js @@ -27,6 +27,22 @@ numPages:function() { return Math.ceil(this.maxentries/this.opts.items_per_page); }, + /** + * Return 1 if page numbers are 1 indexed and 0 otherwise + * @method + * @returns {Number} + */ + firstPage:function() { + return this.opts.oneIndexed ? 1 : 0; + }, + /** + * Calculate the index of the last page + * @method + * @returns {Number} + */ + lastPage:function() { + return this.opts.oneIndexed ? this.numPages() : this.numPages()-1; + }, /** * Calculate start and end point of pagination links depending on * current_page and num_display_entries. @@ -35,9 +51,11 @@ getInterval:function(current_page) { var ne_half = Math.floor(this.opts.num_display_entries/2); var np = this.numPages(); + var fp = this.firstPage(); + var lp = this.lastPage(); var upper_limit = np - this.opts.num_display_entries; - var start = current_page > ne_half ? Math.max( Math.min(current_page - ne_half, upper_limit), 0 ) : 0; - var end = current_page > ne_half?Math.min(current_page+ne_half + (this.opts.num_display_entries % 2), np):Math.min(this.opts.num_display_entries, np); + var start = current_page > ne_half ? Math.max( Math.min(current_page - ne_half, upper_limit + fp), fp ) : fp; + var end = current_page > ne_half?Math.min(current_page+ne_half + (this.opts.num_display_entries % 2), lp):Math.min(this.opts.num_display_entries, lp) + fp; return {start:start, end:end}; } }); @@ -62,9 +80,15 @@ * @returns {jQuery} jQuery object containing the link */ createLink:function(page_id, current_page, appendopts){ - var lnk, np = this.pc.numPages(); - page_id = page_id<0?0:(page_id" + appendopts.text + ""); } @@ -88,18 +112,20 @@ var begin, end, interval = this.pc.getInterval(current_page), np = this.pc.numPages(), + lp = this.pc.lastPage(), + fp = this.pc.firstPage(), fragment = $(""); // Generate "Previous"-Link - if(this.opts.prev_text && (current_page > 0 || this.opts.prev_show_always)){ + if(this.opts.prev_text && (current_page > fp || this.opts.prev_show_always)){ fragment.append(this.createLink(current_page-1, current_page, {text:this.opts.prev_text, classes:"prev"})); } // Generate starting points - if (interval.start > 0 && this.opts.num_edge_entries > 0) + if (interval.start > fp && this.opts.num_edge_entries > 0) { end = Math.min(this.opts.num_edge_entries, interval.start); this.appendRange(fragment, current_page, 0, end, {classes:'sp'}); - if(this.opts.num_edge_entries < interval.start && this.opts.ellipse_text) + if(this.opts.num_edge_entries < interval.start - fp && this.opts.ellipse_text) { jQuery(""+this.opts.ellipse_text+"").appendTo(fragment); } @@ -107,18 +133,18 @@ // Generate interval links this.appendRange(fragment, current_page, interval.start, interval.end); // Generate ending points - if (interval.end < np && this.opts.num_edge_entries > 0) + if (interval.end <= lp && this.opts.num_edge_entries > 0) { - if(np-this.opts.num_edge_entries > interval.end && this.opts.ellipse_text) + if(lp+1-this.opts.num_edge_entries > interval.end && this.opts.ellipse_text) { jQuery(""+this.opts.ellipse_text+"").appendTo(fragment); } - begin = Math.max(np-this.opts.num_edge_entries, interval.end); - this.appendRange(fragment, current_page, begin, np, {classes:'ep'}); + begin = Math.max(lp+1-this.opts.num_edge_entries, interval.end); + this.appendRange(fragment, current_page, begin, lp+1, {classes:'ep'}); } // Generate "Next"-Link - if(this.opts.next_text && (current_page < np-1 || this.opts.next_show_always)){ + if(this.opts.next_text && (current_page < lp || this.opts.next_show_always)){ fragment.append(this.createLink(current_page+1, current_page, {text:this.opts.next_text, classes:"next"})); } $('a', fragment).click(eventHandler); @@ -142,6 +168,7 @@ prev_show_always:true, next_show_always:true, renderer:"defaultRenderer", + oneIndexed: false, callback:function(){return false;} },opts||{}); @@ -196,22 +223,23 @@ // Attach control events to the DOM elements var pc = new $.PaginationCalculator(maxentries, opts); - var np = pc.numPages(); - containers.bind('setPage', {numPages:np}, function(evt, page_id) { - if(page_id >= 0 && page_id < evt.data.numPages) { + var lp = pc.lastPage(); + var fp = pc.firstPage(); + containers.bind('setPage', {lastPage:lp}, function(evt, page_id) { + if(page_id >= fp && page_id <= evt.data.lastPage) { selectPage(page_id); return false; } }); containers.bind('prevPage', function(evt){ var current_page = $(this).data('current_page'); - if (current_page > 0) { + if (current_page > fp) { selectPage(current_page - 1); } return false; }); - containers.bind('nextPage', {numPages:np}, function(evt){ + containers.bind('nextPage', {lastPage:lp}, function(evt){ var current_page = $(this).data('current_page'); - if(current_page < evt.data.numPages - 1) { + if(current_page <= evt.data.lastPage - 1) { selectPage(current_page + 1); } return false; From 123537c6806505d304a7d3c551366531a037d89a Mon Sep 17 00:00:00 2001 From: Trey Hunner Date: Wed, 25 May 2011 19:44:46 -0700 Subject: [PATCH 2/3] Change one-index option name to conform to current naming convention --- src/jquery.pagination.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/jquery.pagination.js b/src/jquery.pagination.js index 82fcfca..01fff88 100644 --- a/src/jquery.pagination.js +++ b/src/jquery.pagination.js @@ -33,7 +33,7 @@ * @returns {Number} */ firstPage:function() { - return this.opts.oneIndexed ? 1 : 0; + return this.opts.one_indexed ? 1 : 0; }, /** * Calculate the index of the last page @@ -41,7 +41,7 @@ * @returns {Number} */ lastPage:function() { - return this.opts.oneIndexed ? this.numPages() : this.numPages()-1; + return this.opts.one_indexed ? this.numPages() : this.numPages()-1; }, /** * Calculate start and end point of pagination links depending on @@ -82,7 +82,7 @@ createLink:function(page_id, current_page, appendopts){ var lnk, lp = this.pc.lastPage(), fp = this.pc.firstPage(); page_id = page_id Date: Thu, 26 May 2011 17:24:58 -0700 Subject: [PATCH 3/3] Add one-indexed option to README --- README.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.rst b/README.rst index 53b8646..c91d494 100644 --- a/README.rst +++ b/README.rst @@ -112,6 +112,10 @@ ellipse_text displayed number interval, this text will be inserted into the gap (inside a span tag). Can be left blank to avoid the additional tag. Default: ``...`` +one_indexed + Start page numbering at 1 instead of 0 for referencing page numbers + internally. For example when using ``__id__`` in the `link_to` option. + Triggering pagination with custom events ----------------------------------------