@@ 4324-4431 (lines=108) @@ | ||
4321 | }]); |
|
4322 | ||
4323 | angular.module('ui.bootstrap.pagination', ['ui.bootstrap.paging']) |
|
4324 | .controller('UibPaginationController', ['$scope', '$attrs', '$parse', 'uibPaging', 'uibPaginationConfig', function($scope, $attrs, $parse, uibPaging, uibPaginationConfig) { |
|
4325 | var ctrl = this; |
|
4326 | // Setup configuration parameters |
|
4327 | var maxSize = angular.isDefined($attrs.maxSize) ? $scope.$parent.$eval($attrs.maxSize) : uibPaginationConfig.maxSize, |
|
4328 | rotate = angular.isDefined($attrs.rotate) ? $scope.$parent.$eval($attrs.rotate) : uibPaginationConfig.rotate, |
|
4329 | forceEllipses = angular.isDefined($attrs.forceEllipses) ? $scope.$parent.$eval($attrs.forceEllipses) : uibPaginationConfig.forceEllipses, |
|
4330 | boundaryLinkNumbers = angular.isDefined($attrs.boundaryLinkNumbers) ? $scope.$parent.$eval($attrs.boundaryLinkNumbers) : uibPaginationConfig.boundaryLinkNumbers, |
|
4331 | pageLabel = angular.isDefined($attrs.pageLabel) ? function(idx) { return $scope.$parent.$eval($attrs.pageLabel, {$page: idx}); } : angular.identity; |
|
4332 | $scope.boundaryLinks = angular.isDefined($attrs.boundaryLinks) ? $scope.$parent.$eval($attrs.boundaryLinks) : uibPaginationConfig.boundaryLinks; |
|
4333 | $scope.directionLinks = angular.isDefined($attrs.directionLinks) ? $scope.$parent.$eval($attrs.directionLinks) : uibPaginationConfig.directionLinks; |
|
4334 | ||
4335 | uibPaging.create(this, $scope, $attrs); |
|
4336 | ||
4337 | if ($attrs.maxSize) { |
|
4338 | ctrl._watchers.push($scope.$parent.$watch($parse($attrs.maxSize), function(value) { |
|
4339 | maxSize = parseInt(value, 10); |
|
4340 | ctrl.render(); |
|
4341 | })); |
|
4342 | } |
|
4343 | ||
4344 | // Create page object used in template |
|
4345 | function makePage(number, text, isActive) { |
|
4346 | return { |
|
4347 | number: number, |
|
4348 | text: text, |
|
4349 | active: isActive |
|
4350 | }; |
|
4351 | } |
|
4352 | ||
4353 | function getPages(currentPage, totalPages) { |
|
4354 | var pages = []; |
|
4355 | ||
4356 | // Default page limits |
|
4357 | var startPage = 1, endPage = totalPages; |
|
4358 | var isMaxSized = angular.isDefined(maxSize) && maxSize < totalPages; |
|
4359 | ||
4360 | // recompute if maxSize |
|
4361 | if (isMaxSized) { |
|
4362 | if (rotate) { |
|
4363 | // Current page is displayed in the middle of the visible ones |
|
4364 | startPage = Math.max(currentPage - Math.floor(maxSize / 2), 1); |
|
4365 | endPage = startPage + maxSize - 1; |
|
4366 | ||
4367 | // Adjust if limit is exceeded |
|
4368 | if (endPage > totalPages) { |
|
4369 | endPage = totalPages; |
|
4370 | startPage = endPage - maxSize + 1; |
|
4371 | } |
|
4372 | } else { |
|
4373 | // Visible pages are paginated with maxSize |
|
4374 | startPage = (Math.ceil(currentPage / maxSize) - 1) * maxSize + 1; |
|
4375 | ||
4376 | // Adjust last page if limit is exceeded |
|
4377 | endPage = Math.min(startPage + maxSize - 1, totalPages); |
|
4378 | } |
|
4379 | } |
|
4380 | ||
4381 | // Add page number links |
|
4382 | for (var number = startPage; number <= endPage; number++) { |
|
4383 | var page = makePage(number, pageLabel(number), number === currentPage); |
|
4384 | pages.push(page); |
|
4385 | } |
|
4386 | ||
4387 | // Add links to move between page sets |
|
4388 | if (isMaxSized && maxSize > 0 && (!rotate || forceEllipses || boundaryLinkNumbers)) { |
|
4389 | if (startPage > 1) { |
|
4390 | if (!boundaryLinkNumbers || startPage > 3) { //need ellipsis for all options unless range is too close to beginning |
|
4391 | var previousPageSet = makePage(startPage - 1, '...', false); |
|
4392 | pages.unshift(previousPageSet); |
|
4393 | } |
|
4394 | if (boundaryLinkNumbers) { |
|
4395 | if (startPage === 3) { //need to replace ellipsis when the buttons would be sequential |
|
4396 | var secondPageLink = makePage(2, '2', false); |
|
4397 | pages.unshift(secondPageLink); |
|
4398 | } |
|
4399 | //add the first page |
|
4400 | var firstPageLink = makePage(1, '1', false); |
|
4401 | pages.unshift(firstPageLink); |
|
4402 | } |
|
4403 | } |
|
4404 | ||
4405 | if (endPage < totalPages) { |
|
4406 | if (!boundaryLinkNumbers || endPage < totalPages - 2) { //need ellipsis for all options unless range is too close to end |
|
4407 | var nextPageSet = makePage(endPage + 1, '...', false); |
|
4408 | pages.push(nextPageSet); |
|
4409 | } |
|
4410 | if (boundaryLinkNumbers) { |
|
4411 | if (endPage === totalPages - 2) { //need to replace ellipsis when the buttons would be sequential |
|
4412 | var secondToLastPageLink = makePage(totalPages - 1, totalPages - 1, false); |
|
4413 | pages.push(secondToLastPageLink); |
|
4414 | } |
|
4415 | //add the last page |
|
4416 | var lastPageLink = makePage(totalPages, totalPages, false); |
|
4417 | pages.push(lastPageLink); |
|
4418 | } |
|
4419 | } |
|
4420 | } |
|
4421 | return pages; |
|
4422 | } |
|
4423 | ||
4424 | var originalRender = this.render; |
|
4425 | this.render = function() { |
|
4426 | originalRender(); |
|
4427 | if ($scope.page > 0 && $scope.page <= $scope.totalPages) { |
|
4428 | $scope.pages = getPages($scope.page, $scope.totalPages); |
|
4429 | } |
|
4430 | }; |
|
4431 | }]) |
|
4432 | ||
4433 | .constant('uibPaginationConfig', { |
|
4434 | itemsPerPage: 10, |
@@ 4323-4430 (lines=108) @@ | ||
4320 | }]); |
|
4321 | ||
4322 | angular.module('ui.bootstrap.pagination', ['ui.bootstrap.paging']) |
|
4323 | .controller('UibPaginationController', ['$scope', '$attrs', '$parse', 'uibPaging', 'uibPaginationConfig', function($scope, $attrs, $parse, uibPaging, uibPaginationConfig) { |
|
4324 | var ctrl = this; |
|
4325 | // Setup configuration parameters |
|
4326 | var maxSize = angular.isDefined($attrs.maxSize) ? $scope.$parent.$eval($attrs.maxSize) : uibPaginationConfig.maxSize, |
|
4327 | rotate = angular.isDefined($attrs.rotate) ? $scope.$parent.$eval($attrs.rotate) : uibPaginationConfig.rotate, |
|
4328 | forceEllipses = angular.isDefined($attrs.forceEllipses) ? $scope.$parent.$eval($attrs.forceEllipses) : uibPaginationConfig.forceEllipses, |
|
4329 | boundaryLinkNumbers = angular.isDefined($attrs.boundaryLinkNumbers) ? $scope.$parent.$eval($attrs.boundaryLinkNumbers) : uibPaginationConfig.boundaryLinkNumbers, |
|
4330 | pageLabel = angular.isDefined($attrs.pageLabel) ? function(idx) { return $scope.$parent.$eval($attrs.pageLabel, {$page: idx}); } : angular.identity; |
|
4331 | $scope.boundaryLinks = angular.isDefined($attrs.boundaryLinks) ? $scope.$parent.$eval($attrs.boundaryLinks) : uibPaginationConfig.boundaryLinks; |
|
4332 | $scope.directionLinks = angular.isDefined($attrs.directionLinks) ? $scope.$parent.$eval($attrs.directionLinks) : uibPaginationConfig.directionLinks; |
|
4333 | ||
4334 | uibPaging.create(this, $scope, $attrs); |
|
4335 | ||
4336 | if ($attrs.maxSize) { |
|
4337 | ctrl._watchers.push($scope.$parent.$watch($parse($attrs.maxSize), function(value) { |
|
4338 | maxSize = parseInt(value, 10); |
|
4339 | ctrl.render(); |
|
4340 | })); |
|
4341 | } |
|
4342 | ||
4343 | // Create page object used in template |
|
4344 | function makePage(number, text, isActive) { |
|
4345 | return { |
|
4346 | number: number, |
|
4347 | text: text, |
|
4348 | active: isActive |
|
4349 | }; |
|
4350 | } |
|
4351 | ||
4352 | function getPages(currentPage, totalPages) { |
|
4353 | var pages = []; |
|
4354 | ||
4355 | // Default page limits |
|
4356 | var startPage = 1, endPage = totalPages; |
|
4357 | var isMaxSized = angular.isDefined(maxSize) && maxSize < totalPages; |
|
4358 | ||
4359 | // recompute if maxSize |
|
4360 | if (isMaxSized) { |
|
4361 | if (rotate) { |
|
4362 | // Current page is displayed in the middle of the visible ones |
|
4363 | startPage = Math.max(currentPage - Math.floor(maxSize / 2), 1); |
|
4364 | endPage = startPage + maxSize - 1; |
|
4365 | ||
4366 | // Adjust if limit is exceeded |
|
4367 | if (endPage > totalPages) { |
|
4368 | endPage = totalPages; |
|
4369 | startPage = endPage - maxSize + 1; |
|
4370 | } |
|
4371 | } else { |
|
4372 | // Visible pages are paginated with maxSize |
|
4373 | startPage = (Math.ceil(currentPage / maxSize) - 1) * maxSize + 1; |
|
4374 | ||
4375 | // Adjust last page if limit is exceeded |
|
4376 | endPage = Math.min(startPage + maxSize - 1, totalPages); |
|
4377 | } |
|
4378 | } |
|
4379 | ||
4380 | // Add page number links |
|
4381 | for (var number = startPage; number <= endPage; number++) { |
|
4382 | var page = makePage(number, pageLabel(number), number === currentPage); |
|
4383 | pages.push(page); |
|
4384 | } |
|
4385 | ||
4386 | // Add links to move between page sets |
|
4387 | if (isMaxSized && maxSize > 0 && (!rotate || forceEllipses || boundaryLinkNumbers)) { |
|
4388 | if (startPage > 1) { |
|
4389 | if (!boundaryLinkNumbers || startPage > 3) { //need ellipsis for all options unless range is too close to beginning |
|
4390 | var previousPageSet = makePage(startPage - 1, '...', false); |
|
4391 | pages.unshift(previousPageSet); |
|
4392 | } |
|
4393 | if (boundaryLinkNumbers) { |
|
4394 | if (startPage === 3) { //need to replace ellipsis when the buttons would be sequential |
|
4395 | var secondPageLink = makePage(2, '2', false); |
|
4396 | pages.unshift(secondPageLink); |
|
4397 | } |
|
4398 | //add the first page |
|
4399 | var firstPageLink = makePage(1, '1', false); |
|
4400 | pages.unshift(firstPageLink); |
|
4401 | } |
|
4402 | } |
|
4403 | ||
4404 | if (endPage < totalPages) { |
|
4405 | if (!boundaryLinkNumbers || endPage < totalPages - 2) { //need ellipsis for all options unless range is too close to end |
|
4406 | var nextPageSet = makePage(endPage + 1, '...', false); |
|
4407 | pages.push(nextPageSet); |
|
4408 | } |
|
4409 | if (boundaryLinkNumbers) { |
|
4410 | if (endPage === totalPages - 2) { //need to replace ellipsis when the buttons would be sequential |
|
4411 | var secondToLastPageLink = makePage(totalPages - 1, totalPages - 1, false); |
|
4412 | pages.push(secondToLastPageLink); |
|
4413 | } |
|
4414 | //add the last page |
|
4415 | var lastPageLink = makePage(totalPages, totalPages, false); |
|
4416 | pages.push(lastPageLink); |
|
4417 | } |
|
4418 | } |
|
4419 | } |
|
4420 | return pages; |
|
4421 | } |
|
4422 | ||
4423 | var originalRender = this.render; |
|
4424 | this.render = function() { |
|
4425 | originalRender(); |
|
4426 | if ($scope.page > 0 && $scope.page <= $scope.totalPages) { |
|
4427 | $scope.pages = getPages($scope.page, $scope.totalPages); |
|
4428 | } |
|
4429 | }; |
|
4430 | }]) |
|
4431 | ||
4432 | .constant('uibPaginationConfig', { |
|
4433 | itemsPerPage: 10, |