| @@ 4193-4279 (lines=87) @@ | ||
| 4190 | * Helper internal service for generating common controller code between the |
|
| 4191 | * pager and pagination components |
|
| 4192 | */ |
|
| 4193 | .factory('uibPaging', ['$parse', function($parse) { |
|
| 4194 | return { |
|
| 4195 | create: function(ctrl, $scope, $attrs) { |
|
| 4196 | ctrl.setNumPages = $attrs.numPages ? $parse($attrs.numPages).assign : angular.noop; |
|
| 4197 | ctrl.ngModelCtrl = { $setViewValue: angular.noop }; // nullModelCtrl |
|
| 4198 | ctrl._watchers = []; |
|
| 4199 | ||
| 4200 | ctrl.init = function(ngModelCtrl, config) { |
|
| 4201 | ctrl.ngModelCtrl = ngModelCtrl; |
|
| 4202 | ctrl.config = config; |
|
| 4203 | ||
| 4204 | ngModelCtrl.$render = function() { |
|
| 4205 | ctrl.render(); |
|
| 4206 | }; |
|
| 4207 | ||
| 4208 | if ($attrs.itemsPerPage) { |
|
| 4209 | ctrl._watchers.push($scope.$parent.$watch($attrs.itemsPerPage, function(value) { |
|
| 4210 | ctrl.itemsPerPage = parseInt(value, 10); |
|
| 4211 | $scope.totalPages = ctrl.calculateTotalPages(); |
|
| 4212 | ctrl.updatePage(); |
|
| 4213 | })); |
|
| 4214 | } else { |
|
| 4215 | ctrl.itemsPerPage = config.itemsPerPage; |
|
| 4216 | } |
|
| 4217 | ||
| 4218 | $scope.$watch('totalItems', function(newTotal, oldTotal) { |
|
| 4219 | if (angular.isDefined(newTotal) || newTotal !== oldTotal) { |
|
| 4220 | $scope.totalPages = ctrl.calculateTotalPages(); |
|
| 4221 | ctrl.updatePage(); |
|
| 4222 | } |
|
| 4223 | }); |
|
| 4224 | }; |
|
| 4225 | ||
| 4226 | ctrl.calculateTotalPages = function() { |
|
| 4227 | var totalPages = ctrl.itemsPerPage < 1 ? 1 : Math.ceil($scope.totalItems / ctrl.itemsPerPage); |
|
| 4228 | return Math.max(totalPages || 0, 1); |
|
| 4229 | }; |
|
| 4230 | ||
| 4231 | ctrl.render = function() { |
|
| 4232 | $scope.page = parseInt(ctrl.ngModelCtrl.$viewValue, 10) || 1; |
|
| 4233 | }; |
|
| 4234 | ||
| 4235 | $scope.selectPage = function(page, evt) { |
|
| 4236 | if (evt) { |
|
| 4237 | evt.preventDefault(); |
|
| 4238 | } |
|
| 4239 | ||
| 4240 | var clickAllowed = !$scope.ngDisabled || !evt; |
|
| 4241 | if (clickAllowed && $scope.page !== page && page > 0 && page <= $scope.totalPages) { |
|
| 4242 | if (evt && evt.target) { |
|
| 4243 | evt.target.blur(); |
|
| 4244 | } |
|
| 4245 | ctrl.ngModelCtrl.$setViewValue(page); |
|
| 4246 | ctrl.ngModelCtrl.$render(); |
|
| 4247 | } |
|
| 4248 | }; |
|
| 4249 | ||
| 4250 | $scope.getText = function(key) { |
|
| 4251 | return $scope[key + 'Text'] || ctrl.config[key + 'Text']; |
|
| 4252 | }; |
|
| 4253 | ||
| 4254 | $scope.noPrevious = function() { |
|
| 4255 | return $scope.page === 1; |
|
| 4256 | }; |
|
| 4257 | ||
| 4258 | $scope.noNext = function() { |
|
| 4259 | return $scope.page === $scope.totalPages; |
|
| 4260 | }; |
|
| 4261 | ||
| 4262 | ctrl.updatePage = function() { |
|
| 4263 | ctrl.setNumPages($scope.$parent, $scope.totalPages); // Readonly variable |
|
| 4264 | ||
| 4265 | if ($scope.page > $scope.totalPages) { |
|
| 4266 | $scope.selectPage($scope.totalPages); |
|
| 4267 | } else { |
|
| 4268 | ctrl.ngModelCtrl.$render(); |
|
| 4269 | } |
|
| 4270 | }; |
|
| 4271 | ||
| 4272 | $scope.$on('$destroy', function() { |
|
| 4273 | while (ctrl._watchers.length) { |
|
| 4274 | ctrl._watchers.shift()(); |
|
| 4275 | } |
|
| 4276 | }); |
|
| 4277 | } |
|
| 4278 | }; |
|
| 4279 | }]); |
|
| 4280 | ||
| 4281 | angular.module('ui.bootstrap.pager', ['ui.bootstrap.paging']) |
|
| 4282 | ||
| @@ 4192-4278 (lines=87) @@ | ||
| 4189 | * Helper internal service for generating common controller code between the |
|
| 4190 | * pager and pagination components |
|
| 4191 | */ |
|
| 4192 | .factory('uibPaging', ['$parse', function($parse) { |
|
| 4193 | return { |
|
| 4194 | create: function(ctrl, $scope, $attrs) { |
|
| 4195 | ctrl.setNumPages = $attrs.numPages ? $parse($attrs.numPages).assign : angular.noop; |
|
| 4196 | ctrl.ngModelCtrl = { $setViewValue: angular.noop }; // nullModelCtrl |
|
| 4197 | ctrl._watchers = []; |
|
| 4198 | ||
| 4199 | ctrl.init = function(ngModelCtrl, config) { |
|
| 4200 | ctrl.ngModelCtrl = ngModelCtrl; |
|
| 4201 | ctrl.config = config; |
|
| 4202 | ||
| 4203 | ngModelCtrl.$render = function() { |
|
| 4204 | ctrl.render(); |
|
| 4205 | }; |
|
| 4206 | ||
| 4207 | if ($attrs.itemsPerPage) { |
|
| 4208 | ctrl._watchers.push($scope.$parent.$watch($attrs.itemsPerPage, function(value) { |
|
| 4209 | ctrl.itemsPerPage = parseInt(value, 10); |
|
| 4210 | $scope.totalPages = ctrl.calculateTotalPages(); |
|
| 4211 | ctrl.updatePage(); |
|
| 4212 | })); |
|
| 4213 | } else { |
|
| 4214 | ctrl.itemsPerPage = config.itemsPerPage; |
|
| 4215 | } |
|
| 4216 | ||
| 4217 | $scope.$watch('totalItems', function(newTotal, oldTotal) { |
|
| 4218 | if (angular.isDefined(newTotal) || newTotal !== oldTotal) { |
|
| 4219 | $scope.totalPages = ctrl.calculateTotalPages(); |
|
| 4220 | ctrl.updatePage(); |
|
| 4221 | } |
|
| 4222 | }); |
|
| 4223 | }; |
|
| 4224 | ||
| 4225 | ctrl.calculateTotalPages = function() { |
|
| 4226 | var totalPages = ctrl.itemsPerPage < 1 ? 1 : Math.ceil($scope.totalItems / ctrl.itemsPerPage); |
|
| 4227 | return Math.max(totalPages || 0, 1); |
|
| 4228 | }; |
|
| 4229 | ||
| 4230 | ctrl.render = function() { |
|
| 4231 | $scope.page = parseInt(ctrl.ngModelCtrl.$viewValue, 10) || 1; |
|
| 4232 | }; |
|
| 4233 | ||
| 4234 | $scope.selectPage = function(page, evt) { |
|
| 4235 | if (evt) { |
|
| 4236 | evt.preventDefault(); |
|
| 4237 | } |
|
| 4238 | ||
| 4239 | var clickAllowed = !$scope.ngDisabled || !evt; |
|
| 4240 | if (clickAllowed && $scope.page !== page && page > 0 && page <= $scope.totalPages) { |
|
| 4241 | if (evt && evt.target) { |
|
| 4242 | evt.target.blur(); |
|
| 4243 | } |
|
| 4244 | ctrl.ngModelCtrl.$setViewValue(page); |
|
| 4245 | ctrl.ngModelCtrl.$render(); |
|
| 4246 | } |
|
| 4247 | }; |
|
| 4248 | ||
| 4249 | $scope.getText = function(key) { |
|
| 4250 | return $scope[key + 'Text'] || ctrl.config[key + 'Text']; |
|
| 4251 | }; |
|
| 4252 | ||
| 4253 | $scope.noPrevious = function() { |
|
| 4254 | return $scope.page === 1; |
|
| 4255 | }; |
|
| 4256 | ||
| 4257 | $scope.noNext = function() { |
|
| 4258 | return $scope.page === $scope.totalPages; |
|
| 4259 | }; |
|
| 4260 | ||
| 4261 | ctrl.updatePage = function() { |
|
| 4262 | ctrl.setNumPages($scope.$parent, $scope.totalPages); // Readonly variable |
|
| 4263 | ||
| 4264 | if ($scope.page > $scope.totalPages) { |
|
| 4265 | $scope.selectPage($scope.totalPages); |
|
| 4266 | } else { |
|
| 4267 | ctrl.ngModelCtrl.$render(); |
|
| 4268 | } |
|
| 4269 | }; |
|
| 4270 | ||
| 4271 | $scope.$on('$destroy', function() { |
|
| 4272 | while (ctrl._watchers.length) { |
|
| 4273 | ctrl._watchers.shift()(); |
|
| 4274 | } |
|
| 4275 | }); |
|
| 4276 | } |
|
| 4277 | }; |
|
| 4278 | }]); |
|
| 4279 | ||
| 4280 | angular.module('ui.bootstrap.pager', ['ui.bootstrap.paging']) |
|
| 4281 | ||