1
1
angular . module ( 'ui.bootstrap.pagination' , [ ] )
2
2
3
- . controller ( 'PaginationController' , [ '$scope' , '$attrs' , '$parse' , '$interpolate' , function ( $scope , $attrs , $parse , $interpolate ) {
3
+ . controller ( 'PaginationController' , [ '$scope' , '$attrs' , '$parse' , function ( $scope , $attrs , $parse ) {
4
4
var self = this ,
5
5
ngModelCtrl = { $setViewValue : angular . noop } , // nullModelCtrl
6
6
setNumPages = $attrs . numPages ? $parse ( $attrs . numPages ) . assign : angular . noop ;
7
7
8
- this . init = function ( ngModelCtrl_ , defaultItemsPerPage ) {
8
+ this . init = function ( ngModelCtrl_ , config ) {
9
9
ngModelCtrl = ngModelCtrl_ ;
10
+ this . config = config ;
10
11
11
12
ngModelCtrl . $render = function ( ) {
12
13
self . render ( ) ;
@@ -18,52 +19,44 @@ angular.module('ui.bootstrap.pagination', [])
18
19
$scope . totalPages = self . calculateTotalPages ( ) ;
19
20
} ) ;
20
21
} else {
21
- this . itemsPerPage = defaultItemsPerPage ;
22
+ this . itemsPerPage = config . itemsPerPage ;
22
23
}
23
24
} ;
24
25
25
- this . noPrevious = function ( ) {
26
- return this . page === 1 ;
27
- } ;
28
- this . noNext = function ( ) {
29
- return this . page === $scope . totalPages ;
30
- } ;
31
-
32
- this . isActive = function ( page ) {
33
- return this . page === page ;
34
- } ;
35
-
36
26
this . calculateTotalPages = function ( ) {
37
27
var totalPages = this . itemsPerPage < 1 ? 1 : Math . ceil ( $scope . totalItems / this . itemsPerPage ) ;
38
28
return Math . max ( totalPages || 0 , 1 ) ;
39
29
} ;
40
30
41
- this . getAttributeValue = function ( attribute , defaultValue , interpolate ) {
42
- return angular . isDefined ( attribute ) ? ( interpolate ? $interpolate ( attribute ) ( $scope . $parent ) : $scope . $parent . $eval ( attribute ) ) : defaultValue ;
43
- } ;
44
-
45
31
this . render = function ( ) {
46
- this . page = parseInt ( ngModelCtrl . $viewValue , 10 ) || 1 ;
47
- if ( this . page > 0 && this . page <= $scope . totalPages ) {
48
- $scope . pages = this . getPages ( this . page , $scope . totalPages ) ;
49
- }
32
+ $scope . page = parseInt ( ngModelCtrl . $viewValue , 10 ) || 1 ;
50
33
} ;
51
34
52
35
$scope . selectPage = function ( page ) {
53
- if ( ! self . isActive ( page ) && page > 0 && page <= $scope . totalPages ) {
36
+ if ( $scope . page !== page && page > 0 && page <= $scope . totalPages ) {
54
37
ngModelCtrl . $setViewValue ( page ) ;
55
38
ngModelCtrl . $render ( ) ;
56
39
}
57
40
} ;
58
41
42
+ $scope . getText = function ( key ) {
43
+ return $scope [ key + 'Text' ] || self . config [ key + 'Text' ] ;
44
+ } ;
45
+ $scope . noPrevious = function ( ) {
46
+ return $scope . page === 1 ;
47
+ } ;
48
+ $scope . noNext = function ( ) {
49
+ return $scope . page === $scope . totalPages ;
50
+ } ;
51
+
59
52
$scope . $watch ( 'totalItems' , function ( ) {
60
53
$scope . totalPages = self . calculateTotalPages ( ) ;
61
54
} ) ;
62
55
63
56
$scope . $watch ( 'totalPages' , function ( value ) {
64
57
setNumPages ( $scope . $parent , value ) ; // Readonly variable
65
58
66
- if ( self . page > value ) {
59
+ if ( $scope . page > value ) {
67
60
$scope . selectPage ( value ) ;
68
61
} else {
69
62
ngModelCtrl . $render ( ) ;
@@ -82,34 +75,34 @@ angular.module('ui.bootstrap.pagination', [])
82
75
rotate : true
83
76
} )
84
77
85
- . directive ( 'pagination' , [ '$parse' , 'paginationConfig' , function ( $parse , config ) {
78
+ . directive ( 'pagination' , [ '$parse' , 'paginationConfig' , function ( $parse , paginationConfig ) {
86
79
return {
87
80
restrict : 'EA' ,
88
81
scope : {
89
- totalItems : '='
82
+ totalItems : '=' ,
83
+ firstText : '@' ,
84
+ previousText : '@' ,
85
+ nextText : '@' ,
86
+ lastText : '@'
90
87
} ,
91
88
require : [ 'pagination' , '?ngModel' ] ,
92
89
controller : 'PaginationController' ,
93
90
templateUrl : 'template/pagination/pagination.html' ,
94
91
replace : true ,
95
92
link : function ( scope , element , attrs , ctrls ) {
96
- var paginationCtrl = ctrls [ 0 ] , ngModel = ctrls [ 1 ] ;
93
+ var paginationCtrl = ctrls [ 0 ] , ngModelCtrl = ctrls [ 1 ] ;
97
94
98
- if ( ! ngModel ) {
95
+ if ( ! ngModelCtrl ) {
99
96
return ; // do nothing if no ng-model
100
97
}
101
98
102
99
// Setup configuration parameters
103
- var maxSize = paginationCtrl . getAttributeValue ( attrs . maxSize , config . maxSize ) ,
104
- boundaryLinks = paginationCtrl . getAttributeValue ( attrs . boundaryLinks , config . boundaryLinks ) ,
105
- directionLinks = paginationCtrl . getAttributeValue ( attrs . directionLinks , config . directionLinks ) ,
106
- firstText = paginationCtrl . getAttributeValue ( attrs . firstText , config . firstText , true ) ,
107
- previousText = paginationCtrl . getAttributeValue ( attrs . previousText , config . previousText , true ) ,
108
- nextText = paginationCtrl . getAttributeValue ( attrs . nextText , config . nextText , true ) ,
109
- lastText = paginationCtrl . getAttributeValue ( attrs . lastText , config . lastText , true ) ,
110
- rotate = paginationCtrl . getAttributeValue ( attrs . rotate , config . rotate ) ;
100
+ var maxSize = angular . isDefined ( attrs . maxSize ) ? scope . $parent . $eval ( attrs . maxSize ) : paginationConfig . maxSize ,
101
+ rotate = angular . isDefined ( attrs . rotate ) ? scope . $parent . $eval ( attrs . rotate ) : paginationConfig . rotate ;
102
+ scope . boundaryLinks = angular . isDefined ( attrs . boundaryLinks ) ? scope . $parent . $eval ( attrs . boundaryLinks ) : paginationConfig . boundaryLinks ;
103
+ scope . directionLinks = angular . isDefined ( attrs . directionLinks ) ? scope . $parent . $eval ( attrs . directionLinks ) : paginationConfig . directionLinks ;
111
104
112
- paginationCtrl . init ( ngModel , config . itemsPerPage ) ;
105
+ paginationCtrl . init ( ngModelCtrl , paginationConfig ) ;
113
106
114
107
if ( attrs . maxSize ) {
115
108
scope . $parent . $watch ( $parse ( attrs . maxSize ) , function ( value ) {
@@ -119,16 +112,15 @@ angular.module('ui.bootstrap.pagination', [])
119
112
}
120
113
121
114
// Create page object used in template
122
- function makePage ( number , text , isActive , isDisabled ) {
115
+ function makePage ( number , text , isActive ) {
123
116
return {
124
117
number : number ,
125
118
text : text ,
126
- active : isActive ,
127
- disabled : isDisabled
119
+ active : isActive
128
120
} ;
129
121
}
130
122
131
- paginationCtrl . getPages = function ( currentPage , totalPages ) {
123
+ function getPages ( currentPage , totalPages ) {
132
124
var pages = [ ] ;
133
125
134
126
// Default page limits
@@ -158,42 +150,32 @@ angular.module('ui.bootstrap.pagination', [])
158
150
159
151
// Add page number links
160
152
for ( var number = startPage ; number <= endPage ; number ++ ) {
161
- var page = makePage ( number , number , paginationCtrl . isActive ( number ) , false ) ;
153
+ var page = makePage ( number , number , number === currentPage ) ;
162
154
pages . push ( page ) ;
163
155
}
164
156
165
157
// Add links to move between page sets
166
158
if ( isMaxSized && ! rotate ) {
167
159
if ( startPage > 1 ) {
168
- var previousPageSet = makePage ( startPage - 1 , '...' , false , false ) ;
160
+ var previousPageSet = makePage ( startPage - 1 , '...' , false ) ;
169
161
pages . unshift ( previousPageSet ) ;
170
162
}
171
163
172
164
if ( endPage < totalPages ) {
173
- var nextPageSet = makePage ( endPage + 1 , '...' , false , false ) ;
165
+ var nextPageSet = makePage ( endPage + 1 , '...' , false ) ;
174
166
pages . push ( nextPageSet ) ;
175
167
}
176
168
}
177
169
178
- // Add previous & next links
179
- if ( directionLinks ) {
180
- var previousPage = makePage ( currentPage - 1 , previousText , false , paginationCtrl . noPrevious ( ) ) ;
181
- pages . unshift ( previousPage ) ;
182
-
183
- var nextPage = makePage ( currentPage + 1 , nextText , false , paginationCtrl . noNext ( ) ) ;
184
- pages . push ( nextPage ) ;
185
- }
186
-
187
- // Add first & last links
188
- if ( boundaryLinks ) {
189
- var firstPage = makePage ( 1 , firstText , false , paginationCtrl . noPrevious ( ) ) ;
190
- pages . unshift ( firstPage ) ;
170
+ return pages ;
171
+ }
191
172
192
- var lastPage = makePage ( totalPages , lastText , false , paginationCtrl . noNext ( ) ) ;
193
- pages . push ( lastPage ) ;
173
+ var originalRender = paginationCtrl . render ;
174
+ paginationCtrl . render = function ( ) {
175
+ originalRender ( ) ;
176
+ if ( scope . page > 0 && scope . page <= scope . totalPages ) {
177
+ scope . pages = getPages ( scope . page , scope . totalPages ) ;
194
178
}
195
-
196
- return pages ;
197
179
} ;
198
180
}
199
181
} ;
@@ -206,47 +188,27 @@ angular.module('ui.bootstrap.pagination', [])
206
188
align : true
207
189
} )
208
190
209
- . directive ( 'pager' , [ 'pagerConfig' , function ( config ) {
191
+ . directive ( 'pager' , [ 'pagerConfig' , function ( pagerConfig ) {
210
192
return {
211
193
restrict : 'EA' ,
212
194
scope : {
213
- totalItems : '='
195
+ totalItems : '=' ,
196
+ previousText : '@' ,
197
+ nextText : '@'
214
198
} ,
215
199
require : [ 'pager' , '?ngModel' ] ,
216
200
controller : 'PaginationController' ,
217
201
templateUrl : 'template/pagination/pager.html' ,
218
202
replace : true ,
219
203
link : function ( scope , element , attrs , ctrls ) {
220
- var paginationCtrl = ctrls [ 0 ] , ngModel = ctrls [ 1 ] ;
204
+ var paginationCtrl = ctrls [ 0 ] , ngModelCtrl = ctrls [ 1 ] ;
221
205
222
- if ( ! ngModel ) {
206
+ if ( ! ngModelCtrl ) {
223
207
return ; // do nothing if no ng-model
224
208
}
225
209
226
- // Setup configuration parameters
227
- var previousText = paginationCtrl . getAttributeValue ( attrs . previousText , config . previousText , true ) ,
228
- nextText = paginationCtrl . getAttributeValue ( attrs . nextText , config . nextText , true ) ,
229
- align = paginationCtrl . getAttributeValue ( attrs . align , config . align ) ;
230
-
231
- paginationCtrl . init ( ngModel , config . itemsPerPage ) ;
232
-
233
- // Create page object used in template
234
- function makePage ( number , text , isDisabled , isPrevious , isNext ) {
235
- return {
236
- number : number ,
237
- text : text ,
238
- disabled : isDisabled ,
239
- previous : ( align && isPrevious ) ,
240
- next : ( align && isNext )
241
- } ;
242
- }
243
-
244
- paginationCtrl . getPages = function ( currentPage ) {
245
- return [
246
- makePage ( currentPage - 1 , previousText , paginationCtrl . noPrevious ( ) , true , false ) ,
247
- makePage ( currentPage + 1 , nextText , paginationCtrl . noNext ( ) , false , true )
248
- ] ;
249
- } ;
210
+ scope . align = angular . isDefined ( attrs . align ) ? scope . $parent . $eval ( attrs . align ) : pagerConfig . align ;
211
+ paginationCtrl . init ( ngModelCtrl , pagerConfig ) ;
250
212
}
251
213
} ;
252
214
} ] ) ;
0 commit comments