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

Commit

Permalink
fix(pagination): use interpolation for text attributes
Browse files Browse the repository at this point in the history
Closes #696

BREAKING CHANGE: The 'first-text', 'previous-text', 'next-text' and 'last-text'
  attributes are now interpolated.

  To migrate your code, remove quotes for constant attributes and/or
  interpolate scope variables.

  Before:

  <pagination first-text="'<<'" ...></pagination>

  and/or

  $scope.var1 = '<<';
  <pagination first-text="var1" ...></pagination>

  After:

  <pagination first-text="<<" ...></pagination>

  and/or

  $scope.var1 = '<<';
  <pagination first-text="{{var1}}" ...></pagination>
  • Loading branch information
bekos authored and pkozlowski-opensource committed Aug 1, 2013
1 parent 25caf5f commit f45815c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
28 changes: 16 additions & 12 deletions src/pagination/pagination.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('ui.bootstrap.pagination', [])

.controller('PaginationController', ['$scope', function ($scope) {
.controller('PaginationController', ['$scope', '$interpolate', function ($scope, $interpolate) {

this.currentPage = 1;

Expand Down Expand Up @@ -31,6 +31,10 @@ angular.module('ui.bootstrap.pagination', [])
$scope.onSelectPage({ page: page });
}
};

this.getAttributeValue = function(attribute, defaultValue, interpolate) {
return angular.isDefined(attribute) ? (interpolate ? $interpolate(attribute)($scope.$parent) : $scope.$parent.$eval(attribute)) : defaultValue;
};
}])

.constant('paginationConfig', {
Expand All @@ -43,7 +47,7 @@ angular.module('ui.bootstrap.pagination', [])
rotate: true
})

.directive('pagination', ['paginationConfig', function(paginationConfig) {
.directive('pagination', ['paginationConfig', function(config) {
return {
restrict: 'EA',
scope: {
Expand All @@ -58,13 +62,13 @@ angular.module('ui.bootstrap.pagination', [])
link: function(scope, element, attrs, paginationCtrl) {

// Setup configuration parameters
var boundaryLinks = angular.isDefined(attrs.boundaryLinks) ? scope.$eval(attrs.boundaryLinks) : paginationConfig.boundaryLinks;
var directionLinks = angular.isDefined(attrs.directionLinks) ? scope.$eval(attrs.directionLinks) : paginationConfig.directionLinks;
var firstText = angular.isDefined(attrs.firstText) ? scope.$parent.$eval(attrs.firstText) : paginationConfig.firstText;
var previousText = angular.isDefined(attrs.previousText) ? scope.$parent.$eval(attrs.previousText) : paginationConfig.previousText;
var nextText = angular.isDefined(attrs.nextText) ? scope.$parent.$eval(attrs.nextText) : paginationConfig.nextText;
var lastText = angular.isDefined(attrs.lastText) ? scope.$parent.$eval(attrs.lastText) : paginationConfig.lastText;
var rotate = angular.isDefined(attrs.rotate) ? scope.$eval(attrs.rotate) : paginationConfig.rotate;
var boundaryLinks = paginationCtrl.getAttributeValue(attrs.boundaryLinks, config.boundaryLinks ),
directionLinks = paginationCtrl.getAttributeValue(attrs.directionLinks, config.directionLinks ),
firstText = paginationCtrl.getAttributeValue(attrs.firstText, config.firstText, true),
previousText = paginationCtrl.getAttributeValue(attrs.previousText, config.previousText, true),
nextText = paginationCtrl.getAttributeValue(attrs.nextText, config.nextText, true),
lastText = paginationCtrl.getAttributeValue(attrs.lastText, config.lastText, true),
rotate = paginationCtrl.getAttributeValue(attrs.rotate, config.rotate);

// Create page object used in template
function makePage(number, text, isActive, isDisabled) {
Expand Down Expand Up @@ -165,9 +169,9 @@ angular.module('ui.bootstrap.pagination', [])
link: function(scope, element, attrs, paginationCtrl) {

// Setup configuration parameters
var previousText = angular.isDefined(attrs.previousText) ? scope.$parent.$eval(attrs.previousText) : config.previousText;
var nextText = angular.isDefined(attrs.nextText) ? scope.$parent.$eval(attrs.nextText) : config.nextText;
var align = angular.isDefined(attrs.align) ? scope.$parent.$eval(attrs.align) : config.align;
var previousText = paginationCtrl.getAttributeValue(attrs.previousText, config.previousText, true),
nextText = paginationCtrl.getAttributeValue(attrs.nextText, config.nextText, true),
align = paginationCtrl.getAttributeValue(attrs.align, config.align);

// Create page object used in template
function makePage(number, text, isDisabled, isPrevious, isNext) {
Expand Down
14 changes: 12 additions & 2 deletions src/pagination/test/pager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('setting pagerConfig', function() {

});

describe('pagination bypass configuration from attributes', function () {
describe('pager bypass configuration from attributes', function () {
var $rootScope, element;
beforeEach(module('ui.bootstrap.pagination'));
beforeEach(module('template/pagination/pager.html'));
Expand All @@ -162,7 +162,7 @@ describe('pagination bypass configuration from attributes', function () {
$rootScope = _$rootScope_;
$rootScope.numPages = 5;
$rootScope.currentPage = 3;
element = $compile('<pager align="false" previous-text="\'<\'" next-text="\'>\'" num-pages="numPages" current-page="currentPage"></pager>')($rootScope);
element = $compile('<pager align="false" previous-text="<" next-text=">" num-pages="numPages" current-page="currentPage"></pager>')($rootScope);
$rootScope.$digest();
}));

Expand All @@ -180,4 +180,14 @@ describe('pagination bypass configuration from attributes', function () {
expect(element.find('li').eq(-1).hasClass('next')).toBe(false);
});

it('changes "previous" & "next" text from interpolated attributes', function() {
$rootScope.previousText = '<<';
$rootScope.nextText = '>>';
element = $compile('<pager align="false" previous-text="{{previousText}}" next-text="{{nextText}}" num-pages="numPages" current-page="currentPage"></pager>')($rootScope);
$rootScope.$digest();

expect(element.find('li').eq(0).text()).toBe('<<');
expect(element.find('li').eq(-1).text()).toBe('>>');
});

});
26 changes: 11 additions & 15 deletions src/pagination/test/pagination.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ describe('pagination directive with max size option & no rotate', function () {
$rootScope.numPages = 12;
$rootScope.currentPage = 7;
$rootScope.maxSize = 5;
element = $compile('<pagination num-pages="numPages" current-page="currentPage" max-size="maxSize" rotate="false"></pagination>')($rootScope);
$rootScope.rotate = false;
element = $compile('<pagination num-pages="numPages" current-page="currentPage" max-size="maxSize" rotate="rotate"></pagination>')($rootScope);
$rootScope.$digest();
}));

Expand Down Expand Up @@ -296,7 +297,6 @@ describe('pagination directive with added first & last links', function () {
$rootScope.$digest();
}));


it('contains one ul and num-pages + 4 li elements', function() {
expect(element.find('ul').length).toBe(1);
expect(element.find('li').length).toBe(9);
Expand Down Expand Up @@ -331,7 +331,6 @@ describe('pagination directive with added first & last links', function () {
expect(element.find('li').eq(-1).hasClass('disabled')).toBe(true);
});


it('changes currentPage if the "first" link is clicked', function() {
var first = element.find('li').eq(0).find('a').eq(0);
first.click();
Expand Down Expand Up @@ -365,35 +364,35 @@ describe('pagination directive with added first & last links', function () {
});

it('changes "first" & "last" text from attributes', function() {
element = $compile('<pagination boundary-links="true" first-text="\'<<<\'" last-text="\'>>>\'" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
element = $compile('<pagination boundary-links="true" first-text="<<<" last-text=">>>" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
$rootScope.$digest();

expect(element.find('li').eq(0).text()).toBe('<<<');
expect(element.find('li').eq(-1).text()).toBe('>>>');
});

it('changes "previous" & "next" text from attributes', function() {
element = $compile('<pagination boundary-links="true" previous-text="\'<<\'" next-text="\'>>\'" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
element = $compile('<pagination boundary-links="true" previous-text="<<" next-text=">>" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
$rootScope.$digest();

expect(element.find('li').eq(1).text()).toBe('<<');
expect(element.find('li').eq(-2).text()).toBe('>>');
});

it('changes "first" & "last" text from attribute variables', function() {
it('changes "first" & "last" text from interpolated attributes', function() {
$rootScope.myfirstText = '<<<';
$rootScope.mylastText = '>>>';
element = $compile('<pagination boundary-links="true" first-text="myfirstText" last-text="mylastText" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
element = $compile('<pagination boundary-links="true" first-text="{{myfirstText}}" last-text="{{mylastText}}" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
$rootScope.$digest();

expect(element.find('li').eq(0).text()).toBe('<<<');
expect(element.find('li').eq(-1).text()).toBe('>>>');
});

it('changes "previous" & "next" text from attribute variables', function() {
it('changes "previous" & "next" text from interpolated attributes', function() {
$rootScope.previousText = '<<';
$rootScope.nextText = '>>';
element = $compile('<pagination boundary-links="true" previous-text="previousText" next-text="nextText" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
element = $compile('<pagination boundary-links="true" previous-text="{{previousText}}" next-text="{{nextText}}" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
$rootScope.$digest();

expect(element.find('li').eq(1).text()).toBe('<<');
Expand Down Expand Up @@ -461,7 +460,6 @@ describe('pagination directive with just number links', function () {
expect($rootScope.currentPage).toBe(2);
});


it('executes the onSelectPage expression when the current page changes', function() {
$rootScope.selectPageHandler = jasmine.createSpy('selectPageHandler');
element = $compile('<pagination direction-links="false" num-pages="numPages" current-page="currentPage" on-select-page="selectPageHandler(page)"></pagination>')($rootScope);
Expand Down Expand Up @@ -541,11 +539,11 @@ describe('pagination directive with first, last & number links', function () {
$rootScope = _$rootScope_;
$rootScope.numPages = 5;
$rootScope.currentPage = 3;
element = $compile('<pagination boundary-links="true" direction-links="false" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
$rootScope.directions = false;
element = $compile('<pagination boundary-links="true" direction-links="directions" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
$rootScope.$digest();
}));


it('contains one ul and num-pages + 2 li elements', function() {
expect(element.find('ul').length).toBe(1);
expect(element.find('li').length).toBe(7);
Expand All @@ -555,7 +553,6 @@ describe('pagination directive with first, last & number links', function () {
expect(element.find('li').eq(-1).text()).toBe('Last');
});


it('disables the "first" & activates "1" link if current-page is 1', function() {
$rootScope.currentPage = 1;
$rootScope.$digest();
Expand All @@ -572,7 +569,6 @@ describe('pagination directive with first, last & number links', function () {
expect(element.find('li').eq(-1).hasClass('disabled')).toBe(true);
});


it('changes currentPage if the "first" link is clicked', function() {
var first = element.find('li').eq(0).find('a').eq(0);
first.click();
Expand All @@ -598,7 +594,7 @@ describe('pagination bypass configuration from attributes', function () {
$rootScope = _$rootScope_;
$rootScope.numPages = 5;
$rootScope.currentPage = 3;
element = $compile('<pagination boundary-links="true" first-text="\'<<\'" previous-text="\'<\'" next-text="\'>\'" last-text="\'>>\'" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
element = $compile('<pagination boundary-links="true" first-text="<<" previous-text="<" next-text=">" last-text=">>" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
$rootScope.$digest();
}));

Expand Down

0 comments on commit f45815c

Please sign in to comment.