|
| 1 | +angular.module('ui.bootstrap.pagination', ['ui.bootstrap.paging']) |
| 2 | +.controller('UibPaginationController', ['$scope', '$attrs', '$parse', 'uibPaging', 'uibPaginationConfig', function($scope, $attrs, $parse, uibPaging, uibPaginationConfig) { |
| 3 | + var ctrl = this; |
| 4 | + // Setup configuration parameters |
| 5 | + var maxSize = angular.isDefined($attrs.maxSize) ? $scope.$parent.$eval($attrs.maxSize) : uibPaginationConfig.maxSize, |
| 6 | + rotate = angular.isDefined($attrs.rotate) ? $scope.$parent.$eval($attrs.rotate) : uibPaginationConfig.rotate, |
| 7 | + forceEllipses = angular.isDefined($attrs.forceEllipses) ? $scope.$parent.$eval($attrs.forceEllipses) : uibPaginationConfig.forceEllipses, |
| 8 | + boundaryLinkNumbers = angular.isDefined($attrs.boundaryLinkNumbers) ? $scope.$parent.$eval($attrs.boundaryLinkNumbers) : uibPaginationConfig.boundaryLinkNumbers; |
| 9 | + $scope.boundaryLinks = angular.isDefined($attrs.boundaryLinks) ? $scope.$parent.$eval($attrs.boundaryLinks) : uibPaginationConfig.boundaryLinks; |
| 10 | + $scope.directionLinks = angular.isDefined($attrs.directionLinks) ? $scope.$parent.$eval($attrs.directionLinks) : uibPaginationConfig.directionLinks; |
| 11 | + |
| 12 | + uibPaging.create(this, $scope, $attrs); |
| 13 | + |
| 14 | + if ($attrs.maxSize) { |
| 15 | + $scope.$parent.$watch($parse($attrs.maxSize), function(value) { |
| 16 | + maxSize = parseInt(value, 10); |
| 17 | + ctrl.render(); |
| 18 | + }); |
| 19 | + } |
| 20 | + |
| 21 | + // Create page object used in template |
| 22 | + function makePage(number, text, isActive) { |
| 23 | + return { |
| 24 | + number: number, |
| 25 | + text: text, |
| 26 | + active: isActive |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + function getPages(currentPage, totalPages) { |
| 31 | + var pages = []; |
| 32 | + |
| 33 | + // Default page limits |
| 34 | + var startPage = 1, endPage = totalPages; |
| 35 | + var isMaxSized = angular.isDefined(maxSize) && maxSize < totalPages; |
| 36 | + |
| 37 | + // recompute if maxSize |
| 38 | + if (isMaxSized) { |
| 39 | + if (rotate) { |
| 40 | + // Current page is displayed in the middle of the visible ones |
| 41 | + startPage = Math.max(currentPage - Math.floor(maxSize / 2), 1); |
| 42 | + endPage = startPage + maxSize - 1; |
| 43 | + |
| 44 | + // Adjust if limit is exceeded |
| 45 | + if (endPage > totalPages) { |
| 46 | + endPage = totalPages; |
| 47 | + startPage = endPage - maxSize + 1; |
| 48 | + } |
| 49 | + } else { |
| 50 | + // Visible pages are paginated with maxSize |
| 51 | + startPage = (Math.ceil(currentPage / maxSize) - 1) * maxSize + 1; |
| 52 | + |
| 53 | + // Adjust last page if limit is exceeded |
| 54 | + endPage = Math.min(startPage + maxSize - 1, totalPages); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Add page number links |
| 59 | + for (var number = startPage; number <= endPage; number++) { |
| 60 | + var page = makePage(number, number, number === currentPage); |
| 61 | + pages.push(page); |
| 62 | + } |
| 63 | + |
| 64 | + // Add links to move between page sets |
| 65 | + if (isMaxSized && maxSize > 0 && (!rotate || forceEllipses || boundaryLinkNumbers)) { |
| 66 | + if (startPage > 1) { |
| 67 | + if (!boundaryLinkNumbers || startPage > 3) { //need ellipsis for all options unless range is too close to beginning |
| 68 | + var previousPageSet = makePage(startPage - 1, '...', false); |
| 69 | + pages.unshift(previousPageSet); |
| 70 | + } |
| 71 | + if (boundaryLinkNumbers) { |
| 72 | + if (startPage === 3) { //need to replace ellipsis when the buttons would be sequential |
| 73 | + var secondPageLink = makePage(2, '2', false); |
| 74 | + pages.unshift(secondPageLink); |
| 75 | + } |
| 76 | + //add the first page |
| 77 | + var firstPageLink = makePage(1, '1', false); |
| 78 | + pages.unshift(firstPageLink); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (endPage < totalPages) { |
| 83 | + if (!boundaryLinkNumbers || endPage < totalPages - 2) { //need ellipsis for all options unless range is too close to end |
| 84 | + var nextPageSet = makePage(endPage + 1, '...', false); |
| 85 | + pages.push(nextPageSet); |
| 86 | + } |
| 87 | + if (boundaryLinkNumbers) { |
| 88 | + if (endPage === totalPages - 2) { //need to replace ellipsis when the buttons would be sequential |
| 89 | + var secondToLastPageLink = makePage(totalPages - 1, totalPages - 1, false); |
| 90 | + pages.push(secondToLastPageLink); |
| 91 | + } |
| 92 | + //add the last page |
| 93 | + var lastPageLink = makePage(totalPages, totalPages, false); |
| 94 | + pages.push(lastPageLink); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return pages; |
| 99 | + } |
| 100 | + |
| 101 | + var originalRender = this.render; |
| 102 | + this.render = function() { |
| 103 | + originalRender(); |
| 104 | + if ($scope.page > 0 && $scope.page <= $scope.totalPages) { |
| 105 | + $scope.pages = getPages($scope.page, $scope.totalPages); |
| 106 | + } |
| 107 | + }; |
| 108 | +}]) |
| 109 | + |
| 110 | +.constant('uibPaginationConfig', { |
| 111 | + itemsPerPage: 10, |
| 112 | + boundaryLinks: false, |
| 113 | + boundaryLinkNumbers: false, |
| 114 | + directionLinks: true, |
| 115 | + firstText: 'First', |
| 116 | + previousText: 'Previous', |
| 117 | + nextText: 'Next', |
| 118 | + lastText: 'Last', |
| 119 | + rotate: true, |
| 120 | + forceEllipses: false |
| 121 | +}) |
| 122 | + |
| 123 | +.directive('uibPagination', ['$parse', 'uibPaginationConfig', function($parse, uibPaginationConfig) { |
| 124 | + return { |
| 125 | + scope: { |
| 126 | + totalItems: '=', |
| 127 | + firstText: '@', |
| 128 | + previousText: '@', |
| 129 | + nextText: '@', |
| 130 | + lastText: '@', |
| 131 | + ngDisabled:'=' |
| 132 | + }, |
| 133 | + require: ['uibPagination', '?ngModel'], |
| 134 | + controller: 'UibPaginationController', |
| 135 | + controllerAs: 'pagination', |
| 136 | + templateUrl: function(element, attrs) { |
| 137 | + return attrs.templateUrl || 'src/template/pagination/pagination.html'; |
| 138 | + }, |
| 139 | + replace: true, |
| 140 | + link: function(scope, element, attrs, ctrls) { |
| 141 | + var paginationCtrl = ctrls[0], ngModelCtrl = ctrls[1]; |
| 142 | + |
| 143 | + if (!ngModelCtrl) { |
| 144 | + return; // do nothing if no ng-model |
| 145 | + } |
| 146 | + |
| 147 | + paginationCtrl.init(ngModelCtrl, uibPaginationConfig); |
| 148 | + } |
| 149 | + }; |
| 150 | +}]); |
0 commit comments