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

Commit ff7e23c

Browse files
committed
feat(pagination): add uib- prefix
1 parent 6b4267b commit ff7e23c

File tree

4 files changed

+276
-57
lines changed

4 files changed

+276
-57
lines changed

Diff for: src/pagination/docs/demo.html

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<div ng-controller="PaginationDemoCtrl">
22
<h4>Default</h4>
3-
<pagination total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()"></pagination>
4-
<pagination boundary-links="true" total-items="totalItems" ng-model="currentPage" class="pagination-sm" previous-text="&lsaquo;" next-text="&rsaquo;" first-text="&laquo;" last-text="&raquo;"></pagination>
5-
<pagination direction-links="false" boundary-links="true" total-items="totalItems" ng-model="currentPage"></pagination>
6-
<pagination direction-links="false" total-items="totalItems" ng-model="currentPage" num-pages="smallnumPages"></pagination>
3+
<uib-pagination total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()"></uib-pagination>
4+
<uib-pagination boundary-links="true" total-items="totalItems" ng-model="currentPage" class="pagination-sm" previous-text="&lsaquo;" next-text="&rsaquo;" first-text="&laquo;" last-text="&raquo;"></uib-pagination>
5+
<uib-pagination direction-links="false" boundary-links="true" total-items="totalItems" ng-model="currentPage"></uib-pagination>
6+
<uib-pagination direction-links="false" total-items="totalItems" ng-model="currentPage" num-pages="smallnumPages"></uib-pagination>
77
<pre>The selected page no: {{currentPage}}</pre>
88
<button type="button" class="btn btn-info" ng-click="setPage(3)">Set current page to: 3</button>
99

1010
<hr />
1111
<h4>Pager</h4>
12-
<pager total-items="totalItems" ng-model="currentPage"></pager>
12+
<uib-pager total-items="totalItems" ng-model="currentPage"></uib-pager>
1313

1414
<hr />
1515
<h4>Limit the maximum visible buttons</h4>
16-
<pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-links="true"></pagination>
17-
<pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-links="true" rotate="false" num-pages="numPages"></pagination>
16+
<uib-pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-links="true"></uib-pagination>
17+
<uib-pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-links="true" rotate="false" num-pages="numPages"></uib-pagination>
1818
<pre>Page: {{bigCurrentPage}} / {{numPages}}</pre>
1919
</div>

Diff for: src/pagination/pagination.js

+166-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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) {
33
var self = this,
44
ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl
55
setNumPages = $attrs.numPages ? $parse($attrs.numPages).assign : angular.noop;
@@ -73,7 +73,7 @@ angular.module('ui.bootstrap.pagination', [])
7373
};
7474
}])
7575

76-
.constant('paginationConfig', {
76+
.constant('uibPaginationConfig', {
7777
itemsPerPage: 10,
7878
boundaryLinks: false,
7979
directionLinks: true,
@@ -84,7 +84,7 @@ angular.module('ui.bootstrap.pagination', [])
8484
rotate: true
8585
})
8686

87-
.directive('pagination', ['$parse', 'paginationConfig', function($parse, paginationConfig) {
87+
.directive('uibPagination', ['$parse', 'uibPaginationConfig', function($parse, paginationConfig) {
8888
return {
8989
restrict: 'EA',
9090
scope: {
@@ -95,8 +95,8 @@ angular.module('ui.bootstrap.pagination', [])
9595
lastText: '@',
9696
ngDisabled:'='
9797
},
98-
require: ['pagination', '?ngModel'],
99-
controller: 'PaginationController',
98+
require: ['uibPagination', '?ngModel'],
99+
controller: 'UibPaginationController',
100100
controllerAs: 'pagination',
101101
templateUrl: function(element, attrs) {
102102
return attrs.templateUrl || 'template/pagination/pagination.html';
@@ -194,14 +194,170 @@ angular.module('ui.bootstrap.pagination', [])
194194
};
195195
}])
196196

197-
.constant('pagerConfig', {
197+
.constant('uibPagerConfig', {
198198
itemsPerPage: 10,
199199
previousText: '« Previous',
200200
nextText: 'Next »',
201201
align: true
202202
})
203203

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', '$controller', '$element', '$log', '$paginationSuppressWarning', function($scope, $attrs, $parse, $controller, $element, $log, $paginationSuppressWarning) {
238+
if (!$paginationSuppressWarning) {
239+
$log.warn('PaginationController is now deprecated. Use UibPaginationController instead.');
240+
}
241+
return $controller('UibPaginationController', {
242+
$scope: $scope,
243+
$element: $element,
244+
$attrs: $attrs
245+
});
246+
}])
247+
.directive('pagination', ['$parse', 'uibPaginationConfig', '$log', '$paginationSuppressWarning', function($parse, paginationConfig, $log, $paginationSuppressWarning) {
248+
return {
249+
restrict: 'EA',
250+
scope: {
251+
totalItems: '=',
252+
firstText: '@',
253+
previousText: '@',
254+
nextText: '@',
255+
lastText: '@',
256+
ngDisabled:'='
257+
},
258+
require: ['pagination', '?ngModel'],
259+
controller: 'PaginationController',
260+
controllerAs: 'pagination',
261+
templateUrl: function(element, attrs) {
262+
return attrs.templateUrl || 'template/pagination/pagination.html';
263+
},
264+
replace: true,
265+
link: function(scope, element, attrs, ctrls) {
266+
if (!$paginationSuppressWarning) {
267+
$log.warn('pagination is now deprecated. Use uib-pagination instead.');
268+
}
269+
var paginationCtrl = ctrls[0], ngModelCtrl = ctrls[1];
270+
271+
if (!ngModelCtrl) {
272+
return; // do nothing if no ng-model
273+
}
274+
275+
// Setup configuration parameters
276+
var maxSize = angular.isDefined(attrs.maxSize) ? scope.$parent.$eval(attrs.maxSize) : paginationConfig.maxSize,
277+
rotate = angular.isDefined(attrs.rotate) ? scope.$parent.$eval(attrs.rotate) : paginationConfig.rotate;
278+
scope.boundaryLinks = angular.isDefined(attrs.boundaryLinks) ? scope.$parent.$eval(attrs.boundaryLinks) : paginationConfig.boundaryLinks;
279+
scope.directionLinks = angular.isDefined(attrs.directionLinks) ? scope.$parent.$eval(attrs.directionLinks) : paginationConfig.directionLinks;
280+
281+
paginationCtrl.init(ngModelCtrl, paginationConfig);
282+
283+
if (attrs.maxSize) {
284+
scope.$parent.$watch($parse(attrs.maxSize), function(value) {
285+
maxSize = parseInt(value, 10);
286+
paginationCtrl.render();
287+
});
288+
}
289+
290+
// Create page object used in template
291+
function makePage(number, text, isActive) {
292+
return {
293+
number: number,
294+
text: text,
295+
active: isActive
296+
};
297+
}
298+
299+
function getPages(currentPage, totalPages) {
300+
var pages = [];
301+
302+
// Default page limits
303+
var startPage = 1, endPage = totalPages;
304+
var isMaxSized = angular.isDefined(maxSize) && maxSize < totalPages;
305+
306+
// recompute if maxSize
307+
if (isMaxSized) {
308+
if (rotate) {
309+
// Current page is displayed in the middle of the visible ones
310+
startPage = Math.max(currentPage - Math.floor(maxSize/2), 1);
311+
endPage = startPage + maxSize - 1;
312+
313+
// Adjust if limit is exceeded
314+
if (endPage > totalPages) {
315+
endPage = totalPages;
316+
startPage = endPage - maxSize + 1;
317+
}
318+
} else {
319+
// Visible pages are paginated with maxSize
320+
startPage = ((Math.ceil(currentPage / maxSize) - 1) * maxSize) + 1;
321+
322+
// Adjust last page if limit is exceeded
323+
endPage = Math.min(startPage + maxSize - 1, totalPages);
324+
}
325+
}
326+
327+
// Add page number links
328+
for (var number = startPage; number <= endPage; number++) {
329+
var page = makePage(number, number, number === currentPage);
330+
pages.push(page);
331+
}
332+
333+
// Add links to move between page sets
334+
if (isMaxSized && ! rotate) {
335+
if (startPage > 1) {
336+
var previousPageSet = makePage(startPage - 1, '...', false);
337+
pages.unshift(previousPageSet);
338+
}
339+
340+
if (endPage < totalPages) {
341+
var nextPageSet = makePage(endPage + 1, '...', false);
342+
pages.push(nextPageSet);
343+
}
344+
}
345+
346+
return pages;
347+
}
348+
349+
var originalRender = paginationCtrl.render;
350+
paginationCtrl.render = function() {
351+
originalRender();
352+
if (scope.page > 0 && scope.page <= scope.totalPages) {
353+
scope.pages = getPages(scope.page, scope.totalPages);
354+
}
355+
};
356+
}
357+
};
358+
}])
359+
360+
.directive('pager', ['uibPagerConfig', '$log', '$paginationSuppressWarning', function(pagerConfig, $log, $paginationSuppressWarning) {
205361
return {
206362
restrict: 'EA',
207363
scope: {
@@ -218,6 +374,9 @@ angular.module('ui.bootstrap.pagination', [])
218374
},
219375
replace: true,
220376
link: function(scope, element, attrs, ctrls) {
377+
if (!$paginationSuppressWarning) {
378+
$log.warn('pager is now deprecated. Use uib-pager instead.');
379+
}
221380
var paginationCtrl = ctrls[0], ngModelCtrl = ctrls[1];
222381

223382
if (!ngModelCtrl) {

Diff for: src/pagination/test/pager.spec.js

+47-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('pager directive', function() {
1010
$document = _$document_;
1111
$templateCache = _$templateCache_;
1212
body = $document.find('body');
13-
element = $compile('<pager total-items="total" ng-model="currentPage"></pager>')($rootScope);
13+
element = $compile('<uib-pager total-items="total" ng-model="currentPage"></uib-pager>')($rootScope);
1414
$rootScope.$digest();
1515
}));
1616

@@ -56,10 +56,10 @@ describe('pager directive', function() {
5656
it('exposes the controller on the template', function() {
5757
$templateCache.put('template/pagination/pager.html', '<div>{{pagination.text}}</div>');
5858

59-
element = $compile('<pager></pager>')($rootScope);
59+
element = $compile('<uib-pager></uib-pager>')($rootScope);
6060
$rootScope.$digest();
6161

62-
var ctrl = element.controller('pager');
62+
var ctrl = element.controller('uibPager');
6363
expect(ctrl).toBeDefined();
6464

6565
ctrl.text = 'foo';
@@ -102,7 +102,7 @@ describe('pager directive', function() {
102102

103103
it('executes the `ng-change` expression when an element is clicked', function() {
104104
$rootScope.selectPageHandler = jasmine.createSpy('selectPageHandler');
105-
element = $compile('<pager total-items="total" ng-model="currentPage" ng-change="selectPageHandler()"></pager>')($rootScope);
105+
element = $compile('<uib-pager total-items="total" ng-model="currentPage" ng-change="selectPageHandler()"></uib-pager>')($rootScope);
106106
$rootScope.$digest();
107107

108108
clickPaginationEl(-1);
@@ -156,7 +156,7 @@ describe('pager directive', function() {
156156
describe('`items-per-page`', function() {
157157
beforeEach(function() {
158158
$rootScope.perpage = 5;
159-
element = $compile('<pager total-items="total" items-per-page="perpage" ng-model="currentPage"></pager>')($rootScope);
159+
element = $compile('<uib-pager total-items="total" items-per-page="perpage" ng-model="currentPage"></uib-pager>')($rootScope);
160160
$rootScope.$digest();
161161
});
162162

@@ -190,7 +190,7 @@ describe('pager directive', function() {
190190
describe('`num-pages`', function() {
191191
beforeEach(function() {
192192
$rootScope.numpg = null;
193-
element = $compile('<pager total-items="total" ng-model="currentPage" num-pages="numpg"></pager>')($rootScope);
193+
element = $compile('<uib-pager total-items="total" ng-model="currentPage" num-pages="numpg"></uib-pager>')($rootScope);
194194
$rootScope.$digest();
195195
});
196196

@@ -201,17 +201,17 @@ describe('pager directive', function() {
201201

202202
describe('setting `pagerConfig`', function() {
203203
var originalConfig = {};
204-
beforeEach(inject(function(pagerConfig) {
205-
angular.extend(originalConfig, pagerConfig);
206-
pagerConfig.previousText = 'PR';
207-
pagerConfig.nextText = 'NE';
208-
pagerConfig.align = false;
209-
element = $compile('<pager total-items="total" ng-model="currentPage"></pager>')($rootScope);
204+
beforeEach(inject(function(uibPagerConfig) {
205+
angular.extend(originalConfig, uibPagerConfig);
206+
uibPagerConfig.previousText = 'PR';
207+
uibPagerConfig.nextText = 'NE';
208+
uibPagerConfig.align = false;
209+
element = $compile('<uib-pager total-items="total" ng-model="currentPage"></uib-pager>')($rootScope);
210210
$rootScope.$digest();
211211
}));
212-
afterEach(inject(function(pagerConfig) {
212+
afterEach(inject(function(uibPagerConfig) {
213213
// return it to the original state
214-
angular.extend(pagerConfig, originalConfig);
214+
angular.extend(uibPagerConfig, originalConfig);
215215
}));
216216

217217
it('should change paging text', function() {
@@ -227,7 +227,7 @@ describe('pager directive', function() {
227227

228228
describe('override configuration from attributes', function() {
229229
beforeEach(function() {
230-
element = $compile('<pager align="false" previous-text="<" next-text=">" total-items="total" ng-model="currentPage"></pager>')($rootScope);
230+
element = $compile('<uib-pager align="false" previous-text="<" next-text=">" total-items="total" ng-model="currentPage"></uib-pager>')($rootScope);
231231
$rootScope.$digest();
232232
});
233233

@@ -248,7 +248,7 @@ describe('pager directive', function() {
248248
it('changes "previous" & "next" text from interpolated attributes', function() {
249249
$rootScope.previousText = '<<';
250250
$rootScope.nextText = '>>';
251-
element = $compile('<pager align="false" previous-text="{{previousText}}" next-text="{{nextText}}" total-items="total" ng-model="currentPage"></pager>')($rootScope);
251+
element = $compile('<uib-pager align="false" previous-text="{{previousText}}" next-text="{{nextText}}" total-items="total" ng-model="currentPage"></uib-pager>')($rootScope);
252252
$rootScope.$digest();
253253

254254
expect(getPaginationEl(0).text()).toBe('<<');
@@ -259,7 +259,7 @@ describe('pager directive', function() {
259259
it('disables the component when ng-disabled is true', function() {
260260
$rootScope.disable = true;
261261

262-
element = $compile('<pager total-items="total" ng-disabled="disable" ng-model="currentPage"></pager>')($rootScope);
262+
element = $compile('<uib-pager total-items="total" ng-disabled="disable" ng-model="currentPage"></uib-pager>')($rootScope);
263263
$rootScope.$digest();
264264
updateCurrentPage(2);
265265

@@ -279,3 +279,33 @@ describe('pager directive', function() {
279279
expect(getPaginationEl(-1)).toHaveClass('disabled');
280280
});
281281
});
282+
283+
describe('pager deprecation', function() {
284+
beforeEach(module('ui.bootstrap.pagination'));
285+
beforeEach(module('template/pagination/pager.html'));
286+
287+
it('should suppress warning', function() {
288+
module(function($provide) {
289+
$provide.value('$paginationSuppressWarning', true);
290+
});
291+
292+
inject(function($compile, $log, $rootScope) {
293+
spyOn($log, 'warn');
294+
295+
var element = $compile('<pager total-items="total" ng-model="currentPage"></pager>')($rootScope);
296+
$rootScope.$digest();
297+
expect($log.warn.calls.count()).toBe(0);
298+
});
299+
});
300+
301+
it('should give warning by default', inject(function($compile, $log, $rootScope) {
302+
spyOn($log, 'warn');
303+
304+
var element = $compile('<pager total-items="total" ng-model="currentPage"></pager>')($rootScope);
305+
$rootScope.$digest();
306+
307+
expect($log.warn.calls.count()).toBe(2);
308+
expect($log.warn.calls.argsFor(0)).toEqual(['PaginationController is now deprecated. Use UibPaginationController instead.']);
309+
expect($log.warn.calls.argsFor(1)).toEqual(['pager is now deprecated. Use uib-pager instead.']);
310+
}));
311+
});

0 commit comments

Comments
 (0)