@@ -234,6 +234,79 @@ angular.module('ui.bootstrap.pagination', [])
234
234
235
235
angular . module ( 'ui.bootstrap.pagination' )
236
236
. 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
+ } ] )
237
310
. directive ( 'pagination' , [ '$parse' , 'uibPaginationConfig' , '$log' , '$paginationSuppressWarning' , function ( $parse , paginationConfig , $log , $paginationSuppressWarning ) {
238
311
return {
239
312
restrict : 'EA' ,
@@ -246,7 +319,7 @@ angular.module('ui.bootstrap.pagination')
246
319
ngDisabled :'='
247
320
} ,
248
321
require : [ 'pagination' , '?ngModel' ] ,
249
- controller : 'UibPaginationController ' ,
322
+ controller : 'PaginationController ' ,
250
323
controllerAs : 'pagination' ,
251
324
templateUrl : function ( element , attrs ) {
252
325
return attrs . templateUrl || 'template/pagination/pagination.html' ;
@@ -357,7 +430,7 @@ angular.module('ui.bootstrap.pagination')
357
430
ngDisabled : '='
358
431
} ,
359
432
require : [ 'pager' , '?ngModel' ] ,
360
- controller : 'UibPaginationController ' ,
433
+ controller : 'PaginationController ' ,
361
434
controllerAs : 'pagination' ,
362
435
templateUrl : function ( element , attrs ) {
363
436
return attrs . templateUrl || 'template/pagination/pager.html' ;
0 commit comments