From d50e8d22ac5294b488777945cffe65a6b44e480f Mon Sep 17 00:00:00 2001 From: Foxandxss Date: Sun, 11 Oct 2015 18:26:57 +0200 Subject: [PATCH] fix(pagination): make deprecated controller work with 1.3.x Closes #4580 --- src/pagination/pagination.js | 81 ++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 7 deletions(-) diff --git a/src/pagination/pagination.js b/src/pagination/pagination.js index ed7678d5e2..1e75dcfd5e 100644 --- a/src/pagination/pagination.js +++ b/src/pagination/pagination.js @@ -234,15 +234,82 @@ angular.module('ui.bootstrap.pagination', []) angular.module('ui.bootstrap.pagination') .value('$paginationSuppressWarning', false) -.controller('PaginationController', ['$scope', '$attrs', '$parse', '$controller', '$element', '$log', '$paginationSuppressWarning', function($scope, $attrs, $parse, $controller, $element, $log, $paginationSuppressWarning) { +.controller('PaginationController', ['$scope', '$attrs', '$parse', '$log', '$paginationSuppressWarning', function($scope, $attrs, $parse, $log, $paginationSuppressWarning) { if (!$paginationSuppressWarning) { $log.warn('PaginationController is now deprecated. Use UibPaginationController instead.'); } - return $controller('UibPaginationController', { - $scope: $scope, - $element: $element, - $attrs: $attrs - }); + + var self = this, + ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl + setNumPages = $attrs.numPages ? $parse($attrs.numPages).assign : angular.noop; + + this.init = function(ngModelCtrl_, config) { + ngModelCtrl = ngModelCtrl_; + this.config = config; + + ngModelCtrl.$render = function() { + self.render(); + }; + + if ($attrs.itemsPerPage) { + $scope.$parent.$watch($parse($attrs.itemsPerPage), function(value) { + self.itemsPerPage = parseInt(value, 10); + $scope.totalPages = self.calculateTotalPages(); + }); + } else { + this.itemsPerPage = config.itemsPerPage; + } + + $scope.$watch('totalItems', function() { + $scope.totalPages = self.calculateTotalPages(); + }); + + $scope.$watch('totalPages', function(value) { + setNumPages($scope.$parent, value); // Readonly variable + + if ( $scope.page > value ) { + $scope.selectPage(value); + } else { + ngModelCtrl.$render(); + } + }); + }; + + this.calculateTotalPages = function() { + var totalPages = this.itemsPerPage < 1 ? 1 : Math.ceil($scope.totalItems / this.itemsPerPage); + return Math.max(totalPages || 0, 1); + }; + + this.render = function() { + $scope.page = parseInt(ngModelCtrl.$viewValue, 10) || 1; + }; + + $scope.selectPage = function(page, evt) { + if (evt) { + evt.preventDefault(); + } + + var clickAllowed = !$scope.ngDisabled || !evt; + if (clickAllowed && $scope.page !== page && page > 0 && page <= $scope.totalPages) { + if (evt && evt.target) { + evt.target.blur(); + } + ngModelCtrl.$setViewValue(page); + ngModelCtrl.$render(); + } + }; + + $scope.getText = function(key) { + return $scope[key + 'Text'] || self.config[key + 'Text']; + }; + + $scope.noPrevious = function() { + return $scope.page === 1; + }; + + $scope.noNext = function() { + return $scope.page === $scope.totalPages; + }; }]) .directive('pagination', ['$parse', 'uibPaginationConfig', '$log', '$paginationSuppressWarning', function($parse, paginationConfig, $log, $paginationSuppressWarning) { return { @@ -262,7 +329,7 @@ angular.module('ui.bootstrap.pagination') return attrs.templateUrl || 'template/pagination/pagination.html'; }, replace: true, - link: function(scope, element, attrs, ctrls) { + link: function(scope, element, attrs, ctrls) { if (!$paginationSuppressWarning) { $log.warn('pagination is now deprecated. Use uib-pagination instead.'); }