Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix(pagination): make deprecated controller work with 1.3.x
Browse files Browse the repository at this point in the history
Closes #4580
  • Loading branch information
Foxandxss authored and wesleycho committed Oct 11, 2015
1 parent f6c7931 commit d50e8d2
Showing 1 changed file with 74 additions and 7 deletions.
81 changes: 74 additions & 7 deletions src/pagination/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.');
}
Expand Down

0 comments on commit d50e8d2

Please sign in to comment.