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

Commit 3514f01

Browse files
committed
feat(pagination): add uib- prefix
1 parent 18c8210 commit 3514f01

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

src/pagination/pagination.js

+75-2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,79 @@ angular.module('ui.bootstrap.pagination', [])
234234

235235
angular.module('ui.bootstrap.pagination')
236236
.value('$paginationSuppressWarning', false)
237+
.controller('PaginationController', ['$scope', '$attrs', '$parse', function($scope, $attrs, $parse) {
238+
var self = this,
239+
ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl
240+
setNumPages = $attrs.numPages ? $parse($attrs.numPages).assign : angular.noop;
241+
242+
this.init = function(ngModelCtrl_, config) {
243+
ngModelCtrl = ngModelCtrl_;
244+
this.config = config;
245+
246+
ngModelCtrl.$render = function() {
247+
self.render();
248+
};
249+
250+
if ($attrs.itemsPerPage) {
251+
$scope.$parent.$watch($parse($attrs.itemsPerPage), function(value) {
252+
self.itemsPerPage = parseInt(value, 10);
253+
$scope.totalPages = self.calculateTotalPages();
254+
});
255+
} else {
256+
this.itemsPerPage = config.itemsPerPage;
257+
}
258+
259+
$scope.$watch('totalItems', function() {
260+
$scope.totalPages = self.calculateTotalPages();
261+
});
262+
263+
$scope.$watch('totalPages', function(value) {
264+
setNumPages($scope.$parent, value); // Readonly variable
265+
266+
if ( $scope.page > value ) {
267+
$scope.selectPage(value);
268+
} else {
269+
ngModelCtrl.$render();
270+
}
271+
});
272+
};
273+
274+
this.calculateTotalPages = function() {
275+
var totalPages = this.itemsPerPage < 1 ? 1 : Math.ceil($scope.totalItems / this.itemsPerPage);
276+
return Math.max(totalPages || 0, 1);
277+
};
278+
279+
this.render = function() {
280+
$scope.page = parseInt(ngModelCtrl.$viewValue, 10) || 1;
281+
};
282+
283+
$scope.selectPage = function(page, evt) {
284+
if (evt) {
285+
evt.preventDefault();
286+
}
287+
288+
var clickAllowed = !$scope.ngDisabled || !evt;
289+
if (clickAllowed && $scope.page !== page && page > 0 && page <= $scope.totalPages) {
290+
if (evt && evt.target) {
291+
evt.target.blur();
292+
}
293+
ngModelCtrl.$setViewValue(page);
294+
ngModelCtrl.$render();
295+
}
296+
};
297+
298+
$scope.getText = function(key) {
299+
return $scope[key + 'Text'] || self.config[key + 'Text'];
300+
};
301+
302+
$scope.noPrevious = function() {
303+
return $scope.page === 1;
304+
};
305+
306+
$scope.noNext = function() {
307+
return $scope.page === $scope.totalPages;
308+
};
309+
}])
237310
.directive('pagination', ['$parse', 'uibPaginationConfig', '$log', '$paginationSuppressWarning', function($parse, paginationConfig, $log, $paginationSuppressWarning) {
238311
return {
239312
restrict: 'EA',
@@ -246,7 +319,7 @@ angular.module('ui.bootstrap.pagination')
246319
ngDisabled:'='
247320
},
248321
require: ['pagination', '?ngModel'],
249-
controller: 'UibPaginationController',
322+
controller: 'PaginationController',
250323
controllerAs: 'pagination',
251324
templateUrl: function(element, attrs) {
252325
return attrs.templateUrl || 'template/pagination/pagination.html';
@@ -357,7 +430,7 @@ angular.module('ui.bootstrap.pagination')
357430
ngDisabled: '='
358431
},
359432
require: ['pager', '?ngModel'],
360-
controller: 'UibPaginationController',
433+
controller: 'PaginationController',
361434
controllerAs: 'pagination',
362435
templateUrl: function(element, attrs) {
363436
return attrs.templateUrl || 'template/pagination/pager.html';

0 commit comments

Comments
 (0)