From e1c11bd2060a8aa125346a15415b54e951acb960 Mon Sep 17 00:00:00 2001 From: Shannan Finley Date: Tue, 11 Oct 2016 09:42:01 +1100 Subject: [PATCH] Allow ellipsis "pages" to be used as buttons by logically finding the previous or next page required for the current page set. --- dirPagination.js | 25 +++++++++++++++++++++---- dirPagination.tpl.html | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/dirPagination.js b/dirPagination.js index a1a7265..1253cf7 100644 --- a/dirPagination.js +++ b/dirPagination.js @@ -326,10 +326,27 @@ } }); - scope.setCurrent = function(num) { - if (paginationService.isRegistered(paginationId) && isValidPageNumber(num)) { - num = parseInt(num, 10); - paginationService.setCurrentPage(paginationId, num); + scope.setCurrent = function (num, index) { + if (paginationService.isRegistered(paginationId)) { + if (num === '...') { + /* Ellipsis entry */ + // setCurrent is being called on an ellipsis, so assume that it's being used as a button and we need to work out where it should go. + if (index === 1) { // Opening Ellipsis + // Get the first page after the opening ellipsis to find which page comes before it. For example, given the page set 1 ... 15, 16, 17, selecting the opening ellipsis, + // would result in page 14 (scope.pages[index + 1] = 15 - 1 = 14) becoming selected. + num = scope.pages[index + 1] - 1; // Verbose for clarity. + } + else { // Closing Ellipsis + // Get the last page before the closing ellipsis to find which page should come next. + num = scope.pages[index - 1] + 1; + } + } + + if (isValidPageNumber(num)) { + num = parseInt(num, 10); + + paginationService.setCurrentPage(paginationId, num); + } } }; diff --git a/dirPagination.tpl.html b/dirPagination.tpl.html index db98d4c..0219d82 100644 --- a/dirPagination.tpl.html +++ b/dirPagination.tpl.html @@ -6,7 +6,7 @@
  • - {{ pageNumber }} + {{ pageNumber }}