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

Commit

Permalink
feat(pagination): add uib- prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieBarker committed Oct 4, 2015
1 parent 18c8210 commit 3514f01
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions src/pagination/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,79 @@ angular.module('ui.bootstrap.pagination', [])

angular.module('ui.bootstrap.pagination')
.value('$paginationSuppressWarning', false)
.controller('PaginationController', ['$scope', '$attrs', '$parse', function($scope, $attrs, $parse) {
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 {
restrict: 'EA',
Expand All @@ -246,7 +319,7 @@ angular.module('ui.bootstrap.pagination')
ngDisabled:'='
},
require: ['pagination', '?ngModel'],
controller: 'UibPaginationController',
controller: 'PaginationController',
controllerAs: 'pagination',
templateUrl: function(element, attrs) {
return attrs.templateUrl || 'template/pagination/pagination.html';
Expand Down Expand Up @@ -357,7 +430,7 @@ angular.module('ui.bootstrap.pagination')
ngDisabled: '='
},
require: ['pager', '?ngModel'],
controller: 'UibPaginationController',
controller: 'PaginationController',
controllerAs: 'pagination',
templateUrl: function(element, attrs) {
return attrs.templateUrl || 'template/pagination/pager.html';
Expand Down

0 comments on commit 3514f01

Please sign in to comment.