From f6edfa5ddf3a6d30fba8bf7e1d93a48392f48b5a Mon Sep 17 00:00:00 2001 From: DarkAvanger Date: Fri, 17 Jul 2015 12:29:41 +0200 Subject: [PATCH] feat(pagination): add support for `ng-disabled` - Add support for ng-disabled Closes #3956 --- src/pagination/docs/readme.md | 4 ++++ src/pagination/pagination.js | 7 +++--- src/pagination/test/pagination.spec.js | 32 ++++++++++++++++++++++++++ template/pagination/pagination.html | 12 +++++----- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/pagination/docs/readme.md b/src/pagination/docs/readme.md index e2b2ee0b69..66733bff16 100644 --- a/src/pagination/docs/readme.md +++ b/src/pagination/docs/readme.md @@ -13,6 +13,10 @@ Settings can be provided as attributes in the `` or globally configu : Current page number. First page is 1. + * `ng-disabled` + : + Used to disable the pagination component + * `total-items` : Total number of items in all pages. diff --git a/src/pagination/pagination.js b/src/pagination/pagination.js index 6f0e69eead..7a5da1b67b 100644 --- a/src/pagination/pagination.js +++ b/src/pagination/pagination.js @@ -1,5 +1,4 @@ angular.module('ui.bootstrap.pagination', []) - .controller('PaginationController', ['$scope', '$attrs', '$parse', function ($scope, $attrs, $parse) { var self = this, ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl @@ -47,7 +46,8 @@ angular.module('ui.bootstrap.pagination', []) }; $scope.selectPage = function(page, evt) { - if ( $scope.page !== page && page > 0 && page <= $scope.totalPages) { + var clickAllowed = !$scope.ngDisabled || !evt; + if (clickAllowed && $scope.page !== page && page > 0 && page <= $scope.totalPages) { if (evt && evt.target) { evt.target.blur(); } @@ -86,7 +86,8 @@ angular.module('ui.bootstrap.pagination', []) firstText: '@', previousText: '@', nextText: '@', - lastText: '@' + lastText: '@', + ngDisabled:'=' }, require: ['pagination', '?ngModel'], controller: 'PaginationController', diff --git a/src/pagination/test/pagination.spec.js b/src/pagination/test/pagination.spec.js index 19a9618c28..968df358bf 100644 --- a/src/pagination/test/pagination.spec.js +++ b/src/pagination/test/pagination.spec.js @@ -7,6 +7,7 @@ describe('pagination directive', function () { $rootScope = _$rootScope_; $rootScope.total = 47; // 5 pages $rootScope.currentPage = 3; + $rootScope.disabled = false; $document = _$document_; element = $compile('')($rootScope); $rootScope.$digest(); @@ -33,6 +34,12 @@ describe('pagination directive', function () { $rootScope.$digest(); } + function setDisabled(value) + { + $rootScope.disabled = value; + $rootScope.$digest(); + } + it('has a "pagination" css class', function() { expect(element.hasClass('pagination')).toBe(true); }); @@ -673,4 +680,29 @@ describe('pagination directive', function () { }); }); + describe('disabled with ngDisable', function () { + beforeEach(function() { + element = $compile('')($rootScope); + $rootScope.currentPage = 3; + $rootScope.$digest(); + }); + + it('should not respond to clicking', function() { + setDisabled(true); + clickPaginationEl(2); + expect($rootScope.currentPage).toBe(3); + setDisabled(false); + clickPaginationEl(2); + expect($rootScope.currentPage).toBe(2); + }); + + 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); + setDisabled(true); + expect(getPaginationEl(3).hasClass('disabled')).toBe(false); + expect(getPaginationEl(4).hasClass('disabled')).toBe(true); + }); + }); }); diff --git a/template/pagination/pagination.html b/template/pagination/pagination.html index acd101a995..d207e74b8d 100644 --- a/template/pagination/pagination.html +++ b/template/pagination/pagination.html @@ -1,7 +1,7 @@ \ No newline at end of file +
  • {{getText('first')}}
  • +
  • {{getText('previous')}}
  • +
  • {{page.text}}
  • +
  • {{getText('next')}}
  • +
  • {{getText('last')}}
  • +