From e89497ef8224558f673a606f4055c4f24183ac64 Mon Sep 17 00:00:00 2001 From: DarkAvanger Date: Fri, 17 Jul 2015 12:29:41 +0200 Subject: [PATCH 1/4] Added paginationDisabled attribute paginationDisabled attribute allows disabling pagination from outside of directive. Usefull for ajax-based pagination. --- src/pagination/pagination.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/pagination/pagination.js b/src/pagination/pagination.js index 6f0e69eead..86194d5884 100644 --- a/src/pagination/pagination.js +++ b/src/pagination/pagination.js @@ -1,5 +1,3 @@ -angular.module('ui.bootstrap.pagination', []) - .controller('PaginationController', ['$scope', '$attrs', '$parse', function ($scope, $attrs, $parse) { var self = this, ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl @@ -43,11 +41,12 @@ angular.module('ui.bootstrap.pagination', []) }; this.render = function() { - $scope.page = parseInt(ngModelCtrl.$viewValue, 10) || 1; + $scope.page = parseInt(ngModelCtrl.$viewValue, 10) || 1; }; $scope.selectPage = function(page, evt) { - if ( $scope.page !== page && page > 0 && page <= $scope.totalPages) { + var disabled_click = $scope.paginationDisabled && evt !== undefined; + if (!disabled_click && $scope.page !== page && page > 0 && page <= $scope.totalPages) { if (evt && evt.target) { evt.target.blur(); } @@ -75,7 +74,8 @@ angular.module('ui.bootstrap.pagination', []) previousText: 'Previous', nextText: 'Next', lastText: 'Last', - rotate: true + rotate: true, + paginationDisabled:false }) .directive('pagination', ['$parse', 'paginationConfig', function($parse, paginationConfig) { @@ -86,7 +86,8 @@ angular.module('ui.bootstrap.pagination', []) firstText: '@', previousText: '@', nextText: '@', - lastText: '@' + lastText: '@', + paginationDisabled:'=' }, require: ['pagination', '?ngModel'], controller: 'PaginationController', From 0c8a8934a0adb82b0262749a4269fd4ec3624232 Mon Sep 17 00:00:00 2001 From: DarkAvanger Date: Fri, 17 Jul 2015 12:31:45 +0200 Subject: [PATCH 2/4] Added support for paginationDisabled attribute --- template/pagination/pagination.html | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/template/pagination/pagination.html b/template/pagination/pagination.html index acd101a995..e058c290bf 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')}}
  • + From c430010c52feccfdfa6f7c26287885674c3bebc1 Mon Sep 17 00:00:00 2001 From: DarkAvanger Date: Fri, 17 Jul 2015 13:25:54 +0200 Subject: [PATCH 3/4] Added missing line --- src/pagination/pagination.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pagination/pagination.js b/src/pagination/pagination.js index 86194d5884..7148def147 100644 --- a/src/pagination/pagination.js +++ b/src/pagination/pagination.js @@ -1,3 +1,4 @@ +angular.module('ui.bootstrap.pagination', []) .controller('PaginationController', ['$scope', '$attrs', '$parse', function ($scope, $attrs, $parse) { var self = this, ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl From 3c894e9fb6fff6c50dacecef169f9c7699507e0e Mon Sep 17 00:00:00 2001 From: DarkAvanger Date: Sat, 18 Jul 2015 00:27:00 +0200 Subject: [PATCH 4/4] Changed to ngDisabled attribute, improved coding and added some tests. --- src/pagination/pagination.js | 11 ++++----- src/pagination/test/pagination.spec.js | 32 ++++++++++++++++++++++++++ template/pagination/pagination.html | 10 ++++---- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/pagination/pagination.js b/src/pagination/pagination.js index 7148def147..7a5da1b67b 100644 --- a/src/pagination/pagination.js +++ b/src/pagination/pagination.js @@ -42,12 +42,12 @@ angular.module('ui.bootstrap.pagination', []) }; this.render = function() { - $scope.page = parseInt(ngModelCtrl.$viewValue, 10) || 1; + $scope.page = parseInt(ngModelCtrl.$viewValue, 10) || 1; }; $scope.selectPage = function(page, evt) { - var disabled_click = $scope.paginationDisabled && evt !== undefined; - if (!disabled_click && $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(); } @@ -75,8 +75,7 @@ angular.module('ui.bootstrap.pagination', []) previousText: 'Previous', nextText: 'Next', lastText: 'Last', - rotate: true, - paginationDisabled:false + rotate: true }) .directive('pagination', ['$parse', 'paginationConfig', function($parse, paginationConfig) { @@ -88,7 +87,7 @@ angular.module('ui.bootstrap.pagination', []) previousText: '@', nextText: '@', lastText: '@', - paginationDisabled:'=' + 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 e058c290bf..d207e74b8d 100644 --- a/template/pagination/pagination.html +++ b/template/pagination/pagination.html @@ -1,7 +1,7 @@