1
1
angular . module ( 'ui.bootstrap.pagination' , [ ] )
2
- . controller ( 'PaginationController ' , [ '$scope' , '$attrs' , '$parse' , function ( $scope , $attrs , $parse ) {
2
+ . controller ( 'UibPaginationController ' , [ '$scope' , '$attrs' , '$parse' , function ( $scope , $attrs , $parse ) {
3
3
var self = this ,
4
4
ngModelCtrl = { $setViewValue : angular . noop } , // nullModelCtrl
5
5
setNumPages = $attrs . numPages ? $parse ( $attrs . numPages ) . assign : angular . noop ;
@@ -73,7 +73,7 @@ angular.module('ui.bootstrap.pagination', [])
73
73
} ;
74
74
} ] )
75
75
76
- . constant ( 'paginationConfig ' , {
76
+ . constant ( 'uibPaginationConfig ' , {
77
77
itemsPerPage : 10 ,
78
78
boundaryLinks : false ,
79
79
directionLinks : true ,
@@ -84,7 +84,7 @@ angular.module('ui.bootstrap.pagination', [])
84
84
rotate : true
85
85
} )
86
86
87
- . directive ( 'pagination ' , [ '$parse' , 'paginationConfig ' , function ( $parse , paginationConfig ) {
87
+ . directive ( 'uibPagination ' , [ '$parse' , 'uibPaginationConfig ' , function ( $parse , paginationConfig ) {
88
88
return {
89
89
restrict : 'EA' ,
90
90
scope : {
@@ -95,8 +95,8 @@ angular.module('ui.bootstrap.pagination', [])
95
95
lastText : '@' ,
96
96
ngDisabled :'='
97
97
} ,
98
- require : [ 'pagination ' , '?ngModel' ] ,
99
- controller : 'PaginationController ' ,
98
+ require : [ 'uibPagination ' , '?ngModel' ] ,
99
+ controller : 'UibPaginationController ' ,
100
100
controllerAs : 'pagination' ,
101
101
templateUrl : function ( element , attrs ) {
102
102
return attrs . templateUrl || 'template/pagination/pagination.html' ;
@@ -194,14 +194,233 @@ angular.module('ui.bootstrap.pagination', [])
194
194
} ;
195
195
} ] )
196
196
197
- . constant ( 'pagerConfig ' , {
197
+ . constant ( 'uibPagerConfig ' , {
198
198
itemsPerPage : 10 ,
199
199
previousText : '« Previous' ,
200
200
nextText : 'Next »' ,
201
201
align : true
202
202
} )
203
203
204
- . directive ( 'pager' , [ 'pagerConfig' , function ( pagerConfig ) {
204
+ . directive ( 'uibPager' , [ 'uibPagerConfig' , function ( pagerConfig ) {
205
+ return {
206
+ restrict : 'EA' ,
207
+ scope : {
208
+ totalItems : '=' ,
209
+ previousText : '@' ,
210
+ nextText : '@' ,
211
+ ngDisabled : '='
212
+ } ,
213
+ require : [ 'uibPager' , '?ngModel' ] ,
214
+ controller : 'UibPaginationController' ,
215
+ controllerAs : 'pagination' ,
216
+ templateUrl : function ( element , attrs ) {
217
+ return attrs . templateUrl || 'template/pagination/pager.html' ;
218
+ } ,
219
+ replace : true ,
220
+ link : function ( scope , element , attrs , ctrls ) {
221
+ var paginationCtrl = ctrls [ 0 ] , ngModelCtrl = ctrls [ 1 ] ;
222
+
223
+ if ( ! ngModelCtrl ) {
224
+ return ; // do nothing if no ng-model
225
+ }
226
+
227
+ scope . align = angular . isDefined ( attrs . align ) ? scope . $parent . $eval ( attrs . align ) : pagerConfig . align ;
228
+ paginationCtrl . init ( ngModelCtrl , pagerConfig ) ;
229
+ }
230
+ } ;
231
+ } ] ) ;
232
+
233
+ /* Deprecated Pagination Below */
234
+
235
+ angular . module ( 'ui.bootstrap.pagination' )
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
+ } ] )
310
+ . directive ( 'pagination' , [ '$parse' , 'uibPaginationConfig' , '$log' , '$paginationSuppressWarning' , function ( $parse , paginationConfig , $log , $paginationSuppressWarning ) {
311
+ return {
312
+ restrict : 'EA' ,
313
+ scope : {
314
+ totalItems : '=' ,
315
+ firstText : '@' ,
316
+ previousText : '@' ,
317
+ nextText : '@' ,
318
+ lastText : '@' ,
319
+ ngDisabled :'='
320
+ } ,
321
+ require : [ 'pagination' , '?ngModel' ] ,
322
+ controller : 'PaginationController' ,
323
+ controllerAs : 'pagination' ,
324
+ templateUrl : function ( element , attrs ) {
325
+ return attrs . templateUrl || 'template/pagination/pagination.html' ;
326
+ } ,
327
+ replace : true ,
328
+ link : function ( scope , element , attrs , ctrls ) {
329
+ if ( ! $paginationSuppressWarning ) {
330
+ $log . warn ( 'pagination is now deprecated. Use uib-pagination instead.' ) ;
331
+ }
332
+ var paginationCtrl = ctrls [ 0 ] , ngModelCtrl = ctrls [ 1 ] ;
333
+
334
+ if ( ! ngModelCtrl ) {
335
+ return ; // do nothing if no ng-model
336
+ }
337
+
338
+ // Setup configuration parameters
339
+ var maxSize = angular . isDefined ( attrs . maxSize ) ? scope . $parent . $eval ( attrs . maxSize ) : paginationConfig . maxSize ,
340
+ rotate = angular . isDefined ( attrs . rotate ) ? scope . $parent . $eval ( attrs . rotate ) : paginationConfig . rotate ;
341
+ scope . boundaryLinks = angular . isDefined ( attrs . boundaryLinks ) ? scope . $parent . $eval ( attrs . boundaryLinks ) : paginationConfig . boundaryLinks ;
342
+ scope . directionLinks = angular . isDefined ( attrs . directionLinks ) ? scope . $parent . $eval ( attrs . directionLinks ) : paginationConfig . directionLinks ;
343
+
344
+ paginationCtrl . init ( ngModelCtrl , paginationConfig ) ;
345
+
346
+ if ( attrs . maxSize ) {
347
+ scope . $parent . $watch ( $parse ( attrs . maxSize ) , function ( value ) {
348
+ maxSize = parseInt ( value , 10 ) ;
349
+ paginationCtrl . render ( ) ;
350
+ } ) ;
351
+ }
352
+
353
+ // Create page object used in template
354
+ function makePage ( number , text , isActive ) {
355
+ return {
356
+ number : number ,
357
+ text : text ,
358
+ active : isActive
359
+ } ;
360
+ }
361
+
362
+ function getPages ( currentPage , totalPages ) {
363
+ var pages = [ ] ;
364
+
365
+ // Default page limits
366
+ var startPage = 1 , endPage = totalPages ;
367
+ var isMaxSized = angular . isDefined ( maxSize ) && maxSize < totalPages ;
368
+
369
+ // recompute if maxSize
370
+ if ( isMaxSized ) {
371
+ if ( rotate ) {
372
+ // Current page is displayed in the middle of the visible ones
373
+ startPage = Math . max ( currentPage - Math . floor ( maxSize / 2 ) , 1 ) ;
374
+ endPage = startPage + maxSize - 1 ;
375
+
376
+ // Adjust if limit is exceeded
377
+ if ( endPage > totalPages ) {
378
+ endPage = totalPages ;
379
+ startPage = endPage - maxSize + 1 ;
380
+ }
381
+ } else {
382
+ // Visible pages are paginated with maxSize
383
+ startPage = ( ( Math . ceil ( currentPage / maxSize ) - 1 ) * maxSize ) + 1 ;
384
+
385
+ // Adjust last page if limit is exceeded
386
+ endPage = Math . min ( startPage + maxSize - 1 , totalPages ) ;
387
+ }
388
+ }
389
+
390
+ // Add page number links
391
+ for ( var number = startPage ; number <= endPage ; number ++ ) {
392
+ var page = makePage ( number , number , number === currentPage ) ;
393
+ pages . push ( page ) ;
394
+ }
395
+
396
+ // Add links to move between page sets
397
+ if ( isMaxSized && ! rotate ) {
398
+ if ( startPage > 1 ) {
399
+ var previousPageSet = makePage ( startPage - 1 , '...' , false ) ;
400
+ pages . unshift ( previousPageSet ) ;
401
+ }
402
+
403
+ if ( endPage < totalPages ) {
404
+ var nextPageSet = makePage ( endPage + 1 , '...' , false ) ;
405
+ pages . push ( nextPageSet ) ;
406
+ }
407
+ }
408
+
409
+ return pages ;
410
+ }
411
+
412
+ var originalRender = paginationCtrl . render ;
413
+ paginationCtrl . render = function ( ) {
414
+ originalRender ( ) ;
415
+ if ( scope . page > 0 && scope . page <= scope . totalPages ) {
416
+ scope . pages = getPages ( scope . page , scope . totalPages ) ;
417
+ }
418
+ } ;
419
+ }
420
+ } ;
421
+ } ] )
422
+
423
+ . directive ( 'pager' , [ 'uibPagerConfig' , '$log' , '$paginationSuppressWarning' , function ( pagerConfig , $log , $paginationSuppressWarning ) {
205
424
return {
206
425
restrict : 'EA' ,
207
426
scope : {
@@ -218,6 +437,9 @@ angular.module('ui.bootstrap.pagination', [])
218
437
} ,
219
438
replace : true ,
220
439
link : function ( scope , element , attrs , ctrls ) {
440
+ if ( ! $paginationSuppressWarning ) {
441
+ $log . warn ( 'pager is now deprecated. Use uib-pager instead.' ) ;
442
+ }
221
443
var paginationCtrl = ctrls [ 0 ] , ngModelCtrl = ctrls [ 1 ] ;
222
444
223
445
if ( ! ngModelCtrl ) {
0 commit comments