diff --git a/src/pagination/pagination.js b/src/pagination/pagination.js index 82732ac9d1..8b31ee46ec 100644 --- a/src/pagination/pagination.js +++ b/src/pagination/pagination.js @@ -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; @@ -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; }; @@ -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; @@ -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); } diff --git a/src/pagination/test/pager.spec.js b/src/pagination/test/pager.spec.js index 76cbf8ad51..ce6496be90 100644 --- a/src/pagination/test/pager.spec.js +++ b/src/pagination/test/pager.spec.js @@ -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_) { @@ -9,6 +9,7 @@ describe('pager directive', function () { $rootScope.currentPage = 3; $document = _$document_; $templateCache = _$templateCache_; + body = $document.find('body'); element = $compile('')($rootScope); $rootScope.$digest(); })); @@ -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(); @@ -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(); @@ -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('')($rootScope); @@ -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'); @@ -186,7 +187,7 @@ describe('pager directive', function () { }); }); - describe('`num-pages`', function () { + describe('`num-pages`', function() { beforeEach(function() { $rootScope.numpg = null; element = $compile('')($rootScope); @@ -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('')($rootScope); $rootScope.$digest(); @@ -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'); }); diff --git a/src/pagination/test/pagination.spec.js b/src/pagination/test/pagination.spec.js index 54881b4f69..ee748e46bf 100644 --- a/src/pagination/test/pagination.spec.js +++ b/src/pagination/test/pagination.spec.js @@ -1,5 +1,5 @@ -describe('pagination directive', function () { - var $compile, $rootScope, $document, $templateCache, element; +describe('pagination directive', function() { + var $compile, $rootScope, $document, $templateCache, body, element; beforeEach(module('ui.bootstrap.pagination')); beforeEach(module('template/pagination/pagination.html')); beforeEach(inject(function(_$compile_, _$rootScope_, _$document_, _$templateCache_) { @@ -10,6 +10,7 @@ describe('pagination directive', function () { $rootScope.disabled = false; $document = _$document_; $templateCache = _$templateCache_; + body = $document.find('body'); element = $compile('')($rootScope); $rootScope.$digest(); })); @@ -25,7 +26,7 @@ describe('pagination directive', function () { function clickPaginationEl(index) { getPaginationEl(index).find('a').click(); } - + function getPaginationLinkEl(elem, index) { return elem.find('li').eq(index).find('a'); } @@ -35,8 +36,7 @@ describe('pagination directive', function () { $rootScope.$digest(); } - function setDisabled(value) - { + function setDisabled(value) { $rootScope.disabled = value; $rootScope.$digest(); } @@ -162,46 +162,46 @@ describe('pagination directive', function () { expect($rootScope.currentPage).toBe(1); }); - it('should blur a page link after it has been clicked', function () { - $document.find('body').append(element); + it('should blur a page link after it has been clicked', function() { + body.append(element); var linkEl = getPaginationLinkEl(element, 2); - + linkEl.focus(); expect(linkEl).toHaveFocus(); - + linkEl.click(); expect(linkEl).not.toHaveFocus(); - + element.remove(); }); - - 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(); expect(linkEl).toHaveFocus(); - + linkEl.click(); expect(linkEl).not.toHaveFocus(); - + 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, 0); - + linkEl.focus(); expect(linkEl).toHaveFocus(); - + linkEl.click(); expect(linkEl).not.toHaveFocus(); - + element.remove(); }); - - describe('`items-per-page`', function () { + + describe('`items-per-page`', function() { beforeEach(function() { $rootScope.perpage = 5; element = $compile('')($rootScope); @@ -244,7 +244,7 @@ describe('pagination directive', function () { }); }); - describe('executes `ng-change` expression', function () { + describe('executes `ng-change` expression', function() { beforeEach(function() { $rootScope.selectPageHandler = jasmine.createSpy('selectPageHandler'); element = $compile('')($rootScope); @@ -257,7 +257,7 @@ describe('pagination directive', function () { }); }); - describe('when `page` is not a number', function () { + describe('when `page` is not a number', function() { it('handles numerical string', function() { updateCurrentPage('2'); expect(getPaginationEl(2)).toHaveClass('active'); @@ -272,7 +272,7 @@ describe('pagination directive', function () { }); }); - describe('with `max-size` option', function () { + describe('with `max-size` option', function() { beforeEach(function() { $rootScope.total = 98; // 10 pages $rootScope.currentPage = 3; @@ -338,22 +338,22 @@ describe('pagination directive', function () { expect(getPaginationEl(0).text()).toBe('Previous'); expect(getPaginationEl(-1).text()).toBe('Next'); }); - + it('should blur page link when visible range changes', function () { - $document.find('body').append(element); + body.append(element); var linkEl = getPaginationLinkEl(element, 4); - + linkEl.focus(); expect(linkEl).toHaveFocus(); - + linkEl.click(); expect(linkEl).not.toHaveFocus(); - + element.remove(); }); }); - describe('with `max-size` option & no `rotate`', function () { + describe('with `max-size` option & no `rotate`', function() { beforeEach(function() { $rootScope.total = 115; // 12 pages $rootScope.currentPage = 7; @@ -416,7 +416,7 @@ describe('pagination directive', function () { }); }); - describe('pagination directive with `boundary-links`', function () { + describe('pagination directive with `boundary-links`', function() { beforeEach(function() { element = $compile('')($rootScope); $rootScope.$digest(); @@ -507,35 +507,35 @@ describe('pagination directive', function () { expect(getPaginationEl(1).text()).toBe('<<'); expect(getPaginationEl(-2).text()).toBe('>>'); }); - - it('should blur the "first" link after it has been clicked', function () { - $document.find('body').append(element); + + it('should blur the "first" link after it has been clicked', function() { + body.append(element); var linkEl = getPaginationLinkEl(element, 0); - + linkEl.focus(); expect(linkEl).toHaveFocus(); - + linkEl.click(); expect(linkEl).not.toHaveFocus(); - + element.remove(); }); - - it('should blur the "last" link after it has been clicked', function () { - $document.find('body').append(element); + + it('should blur the "last" link after it has been clicked', function() { + body.append(element); var linkEl = getPaginationLinkEl(element, -1); - + linkEl.focus(); expect(linkEl).toHaveFocus(); - + linkEl.click(); expect(linkEl).not.toHaveFocus(); - + element.remove(); }); }); - describe('pagination directive with just number links', function () { + describe('pagination directive with just number links', function() { beforeEach(function() { element = $compile('')($rootScope); $rootScope.$digest(); @@ -586,7 +586,7 @@ describe('pagination directive', function () { }); }); - describe('with just boundary & number links', function () { + describe('with just boundary & number links', function() { beforeEach(function() { $rootScope.directions = false; element = $compile('')($rootScope); @@ -657,7 +657,7 @@ describe('pagination directive', function () { angular.copy(originalConfig, paginationConfig); })); - it('should change paging text', function () { + it('should change paging text', function() { paginationConfig.boundaryLinks = true; paginationConfig.directionLinks = true; paginationConfig.firstText = 'FI'; @@ -681,7 +681,7 @@ describe('pagination directive', function () { expect(getPaginationBarSize()).toBe(12); }); - it('should take maxSize defaults into account', function () { + it('should take maxSize defaults into account', function() { paginationConfig.maxSize = 2; element = $compile('')($rootScope); $rootScope.$digest(); @@ -690,7 +690,7 @@ describe('pagination directive', function () { }); }); - describe('override configuration from attributes', function () { + describe('override configuration from attributes', function() { beforeEach(function() { element = $compile('')($rootScope); $rootScope.$digest(); @@ -700,7 +700,7 @@ describe('pagination directive', function () { expect(getPaginationBarSize()).toBe(9); }); - it('should change paging text from attribute', function () { + it('should change paging text from attribute', function() { expect(getPaginationEl(0).text()).toBe('<<'); expect(getPaginationEl(1).text()).toBe('<'); expect(getPaginationEl(-2).text()).toBe('>'); @@ -708,7 +708,7 @@ describe('pagination directive', function () { }); }); - describe('disabled with ngDisable', function () { + describe('disabled with ngDisable', function() { beforeEach(function() { element = $compile('')($rootScope); $rootScope.currentPage = 3; @@ -724,7 +724,7 @@ describe('pagination directive', function () { expect($rootScope.currentPage).toBe(2); }); - it('should change the class of all buttons except selected one', function () { + it('should change the class of all buttons except selected one', function() { setDisabled(false); expect(getPaginationEl(3).hasClass('active')).toBe(true); expect(getPaginationEl(4).hasClass('active')).toBe(false);