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

Commit d50e8d2

Browse files
Foxandxsswesleycho
authored andcommitted
fix(pagination): make deprecated controller work with 1.3.x
Closes #4580
1 parent f6c7931 commit d50e8d2

File tree

1 file changed

+74
-7
lines changed

1 file changed

+74
-7
lines changed

src/pagination/pagination.js

+74-7
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,82 @@ angular.module('ui.bootstrap.pagination', [])
234234

235235
angular.module('ui.bootstrap.pagination')
236236
.value('$paginationSuppressWarning', false)
237-
.controller('PaginationController', ['$scope', '$attrs', '$parse', '$controller', '$element', '$log', '$paginationSuppressWarning', function($scope, $attrs, $parse, $controller, $element, $log, $paginationSuppressWarning) {
237+
.controller('PaginationController', ['$scope', '$attrs', '$parse', '$log', '$paginationSuppressWarning', function($scope, $attrs, $parse, $log, $paginationSuppressWarning) {
238238
if (!$paginationSuppressWarning) {
239239
$log.warn('PaginationController is now deprecated. Use UibPaginationController instead.');
240240
}
241-
return $controller('UibPaginationController', {
242-
$scope: $scope,
243-
$element: $element,
244-
$attrs: $attrs
245-
});
241+
242+
var self = this,
243+
ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl
244+
setNumPages = $attrs.numPages ? $parse($attrs.numPages).assign : angular.noop;
245+
246+
this.init = function(ngModelCtrl_, config) {
247+
ngModelCtrl = ngModelCtrl_;
248+
this.config = config;
249+
250+
ngModelCtrl.$render = function() {
251+
self.render();
252+
};
253+
254+
if ($attrs.itemsPerPage) {
255+
$scope.$parent.$watch($parse($attrs.itemsPerPage), function(value) {
256+
self.itemsPerPage = parseInt(value, 10);
257+
$scope.totalPages = self.calculateTotalPages();
258+
});
259+
} else {
260+
this.itemsPerPage = config.itemsPerPage;
261+
}
262+
263+
$scope.$watch('totalItems', function() {
264+
$scope.totalPages = self.calculateTotalPages();
265+
});
266+
267+
$scope.$watch('totalPages', function(value) {
268+
setNumPages($scope.$parent, value); // Readonly variable
269+
270+
if ( $scope.page > value ) {
271+
$scope.selectPage(value);
272+
} else {
273+
ngModelCtrl.$render();
274+
}
275+
});
276+
};
277+
278+
this.calculateTotalPages = function() {
279+
var totalPages = this.itemsPerPage < 1 ? 1 : Math.ceil($scope.totalItems / this.itemsPerPage);
280+
return Math.max(totalPages || 0, 1);
281+
};
282+
283+
this.render = function() {
284+
$scope.page = parseInt(ngModelCtrl.$viewValue, 10) || 1;
285+
};
286+
287+
$scope.selectPage = function(page, evt) {
288+
if (evt) {
289+
evt.preventDefault();
290+
}
291+
292+
var clickAllowed = !$scope.ngDisabled || !evt;
293+
if (clickAllowed && $scope.page !== page && page > 0 && page <= $scope.totalPages) {
294+
if (evt && evt.target) {
295+
evt.target.blur();
296+
}
297+
ngModelCtrl.$setViewValue(page);
298+
ngModelCtrl.$render();
299+
}
300+
};
301+
302+
$scope.getText = function(key) {
303+
return $scope[key + 'Text'] || self.config[key + 'Text'];
304+
};
305+
306+
$scope.noPrevious = function() {
307+
return $scope.page === 1;
308+
};
309+
310+
$scope.noNext = function() {
311+
return $scope.page === $scope.totalPages;
312+
};
246313
}])
247314
.directive('pagination', ['$parse', 'uibPaginationConfig', '$log', '$paginationSuppressWarning', function($parse, paginationConfig, $log, $paginationSuppressWarning) {
248315
return {
@@ -262,7 +329,7 @@ angular.module('ui.bootstrap.pagination')
262329
return attrs.templateUrl || 'template/pagination/pagination.html';
263330
},
264331
replace: true,
265-
link: function(scope, element, attrs, ctrls) {
332+
link: function(scope, element, attrs, ctrls) {
266333
if (!$paginationSuppressWarning) {
267334
$log.warn('pagination is now deprecated. Use uib-pagination instead.');
268335
}

0 commit comments

Comments
 (0)