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

chore(pagination): unify code style #4176

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/pagination/pagination.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
angular.module('ui.bootstrap.pagination', [])
.controller('PaginationController', ['$scope', '$attrs', '$parse', function ($scope, $attrs, $parse) {
.controller('PaginationController', ['$scope', '$attrs', '$parse', function($scope, $attrs, $parse) {
var self = this,
ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl
setNumPages = $attrs.numPages ? $parse($attrs.numPages).assign : angular.noop;
Expand Down Expand Up @@ -60,12 +60,14 @@ angular.module('ui.bootstrap.pagination', [])
}
};

$scope.getText = function( key ) {
$scope.getText = function(key) {
return $scope[key + 'Text'] || self.config[key + 'Text'];
};

$scope.noPrevious = function() {
return $scope.page === 1;
};

$scope.noNext = function() {
return $scope.page === $scope.totalPages;
};
Expand Down Expand Up @@ -136,11 +138,11 @@ angular.module('ui.bootstrap.pagination', [])

// Default page limits
var startPage = 1, endPage = totalPages;
var isMaxSized = ( angular.isDefined(maxSize) && maxSize < totalPages );
var isMaxSized = angular.isDefined(maxSize) && maxSize < totalPages;

// recompute if maxSize
if ( isMaxSized ) {
if ( rotate ) {
if (isMaxSized) {
if (rotate) {
// Current page is displayed in the middle of the visible ones
startPage = Math.max(currentPage - Math.floor(maxSize/2), 1);
endPage = startPage + maxSize - 1;
Expand All @@ -166,13 +168,13 @@ angular.module('ui.bootstrap.pagination', [])
}

// Add links to move between page sets
if ( isMaxSized && ! rotate ) {
if ( startPage > 1 ) {
if (isMaxSized && ! rotate) {
if (startPage > 1) {
var previousPageSet = makePage(startPage - 1, '...', false);
pages.unshift(previousPageSet);
}

if ( endPage < totalPages ) {
if (endPage < totalPages) {
var nextPageSet = makePage(endPage + 1, '...', false);
pages.push(nextPageSet);
}
Expand Down
29 changes: 15 additions & 14 deletions src/pagination/test/pager.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('pager directive', function () {
var $compile, $rootScope, $document, $templateCache, element;
describe('pager directive', function() {
var $compile, $rootScope, $document, $templateCache, body, element;
beforeEach(module('ui.bootstrap.pagination'));
beforeEach(module('template/pagination/pager.html'));
beforeEach(inject(function(_$compile_, _$rootScope_, _$document_, _$templateCache_) {
Expand All @@ -9,6 +9,7 @@ describe('pager directive', function () {
$rootScope.currentPage = 3;
$document = _$document_;
$templateCache = _$templateCache_;
body = $document.find('body');
element = $compile('<pager total-items="total" ng-model="currentPage"></pager>')($rootScope);
$rootScope.$digest();
}));
Expand Down Expand Up @@ -117,8 +118,8 @@ describe('pager directive', function () {
expect(getPaginationEl(-1).text()).toBe('Next »');
});

it('should blur the "next" link after it has been clicked', function () {
$document.find('body').append(element);
it('should blur the "next" link after it has been clicked', function() {
body.append(element);
var linkEl = getPaginationLinkEl(element, -1);

linkEl.focus();
Expand All @@ -130,8 +131,8 @@ describe('pager directive', function () {
element.remove();
});

it('should blur the "prev" link after it has been clicked', function () {
$document.find('body').append(element);
it('should blur the "prev" link after it has been clicked', function() {
body.append(element);
var linkEl = getPaginationLinkEl(element, -1);

linkEl.focus();
Expand All @@ -152,7 +153,7 @@ describe('pager directive', function () {
expect(element.html()).toBe('baz');
});

describe('`items-per-page`', function () {
describe('`items-per-page`', function() {
beforeEach(function() {
$rootScope.perpage = 5;
element = $compile('<pager total-items="total" items-per-page="perpage" ng-model="currentPage"></pager>')($rootScope);
Expand All @@ -176,7 +177,7 @@ describe('pager directive', function () {
});
});

describe('when `page` is not a number', function () {
describe('when `page` is not a number', function() {
it('handles string', function() {
updateCurrentPage('1');
expect(getPaginationEl(0)).toHaveClass('disabled');
Expand All @@ -186,7 +187,7 @@ describe('pager directive', function () {
});
});

describe('`num-pages`', function () {
describe('`num-pages`', function() {
beforeEach(function() {
$rootScope.numpg = null;
element = $compile('<pager total-items="total" ng-model="currentPage" num-pages="numpg"></pager>')($rootScope);
Expand All @@ -213,18 +214,18 @@ describe('pager directive', function () {
angular.extend(pagerConfig, originalConfig);
}));

it('should change paging text', function () {
it('should change paging text', function() {
expect(getPaginationEl(0).text()).toBe('PR');
expect(getPaginationEl(-1).text()).toBe('NE');
});

it('should not align previous & next page link', function () {
it('should not align previous & next page link', function() {
expect(getPaginationEl(0)).not.toHaveClass('previous');
expect(getPaginationEl(-1)).not.toHaveClass('next');
});
});

describe('override configuration from attributes', function () {
describe('override configuration from attributes', function() {
beforeEach(function() {
element = $compile('<pager align="false" previous-text="<" next-text=">" total-items="total" ng-model="currentPage"></pager>')($rootScope);
$rootScope.$digest();
Expand All @@ -234,12 +235,12 @@ describe('pager directive', function () {
expect(getPaginationBarSize()).toBe(2);
});

it('should change paging text from attributes', function () {
it('should change paging text from attributes', function() {
expect(getPaginationEl(0).text()).toBe('<');
expect(getPaginationEl(-1).text()).toBe('>');
});

it('should not align previous & next page link', function () {
it('should not align previous & next page link', function() {
expect(getPaginationEl(0)).not.toHaveClass('previous');
expect(getPaginationEl(-1)).not.toHaveClass('next');
});
Expand Down
Loading