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

Commit f45815c

Browse files
bekospkozlowski-opensource
authored andcommitted
fix(pagination): use interpolation for text attributes
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>
1 parent 25caf5f commit f45815c

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

src/pagination/pagination.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
angular.module('ui.bootstrap.pagination', [])
22

3-
.controller('PaginationController', ['$scope', function ($scope) {
3+
.controller('PaginationController', ['$scope', '$interpolate', function ($scope, $interpolate) {
44

55
this.currentPage = 1;
66

@@ -31,6 +31,10 @@ angular.module('ui.bootstrap.pagination', [])
3131
$scope.onSelectPage({ page: page });
3232
}
3333
};
34+
35+
this.getAttributeValue = function(attribute, defaultValue, interpolate) {
36+
return angular.isDefined(attribute) ? (interpolate ? $interpolate(attribute)($scope.$parent) : $scope.$parent.$eval(attribute)) : defaultValue;
37+
};
3438
}])
3539

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

46-
.directive('pagination', ['paginationConfig', function(paginationConfig) {
50+
.directive('pagination', ['paginationConfig', function(config) {
4751
return {
4852
restrict: 'EA',
4953
scope: {
@@ -58,13 +62,13 @@ angular.module('ui.bootstrap.pagination', [])
5862
link: function(scope, element, attrs, paginationCtrl) {
5963

6064
// Setup configuration parameters
61-
var boundaryLinks = angular.isDefined(attrs.boundaryLinks) ? scope.$eval(attrs.boundaryLinks) : paginationConfig.boundaryLinks;
62-
var directionLinks = angular.isDefined(attrs.directionLinks) ? scope.$eval(attrs.directionLinks) : paginationConfig.directionLinks;
63-
var firstText = angular.isDefined(attrs.firstText) ? scope.$parent.$eval(attrs.firstText) : paginationConfig.firstText;
64-
var previousText = angular.isDefined(attrs.previousText) ? scope.$parent.$eval(attrs.previousText) : paginationConfig.previousText;
65-
var nextText = angular.isDefined(attrs.nextText) ? scope.$parent.$eval(attrs.nextText) : paginationConfig.nextText;
66-
var lastText = angular.isDefined(attrs.lastText) ? scope.$parent.$eval(attrs.lastText) : paginationConfig.lastText;
67-
var rotate = angular.isDefined(attrs.rotate) ? scope.$eval(attrs.rotate) : paginationConfig.rotate;
65+
var boundaryLinks = paginationCtrl.getAttributeValue(attrs.boundaryLinks, config.boundaryLinks ),
66+
directionLinks = paginationCtrl.getAttributeValue(attrs.directionLinks, config.directionLinks ),
67+
firstText = paginationCtrl.getAttributeValue(attrs.firstText, config.firstText, true),
68+
previousText = paginationCtrl.getAttributeValue(attrs.previousText, config.previousText, true),
69+
nextText = paginationCtrl.getAttributeValue(attrs.nextText, config.nextText, true),
70+
lastText = paginationCtrl.getAttributeValue(attrs.lastText, config.lastText, true),
71+
rotate = paginationCtrl.getAttributeValue(attrs.rotate, config.rotate);
6872

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

167171
// Setup configuration parameters
168-
var previousText = angular.isDefined(attrs.previousText) ? scope.$parent.$eval(attrs.previousText) : config.previousText;
169-
var nextText = angular.isDefined(attrs.nextText) ? scope.$parent.$eval(attrs.nextText) : config.nextText;
170-
var align = angular.isDefined(attrs.align) ? scope.$parent.$eval(attrs.align) : config.align;
172+
var previousText = paginationCtrl.getAttributeValue(attrs.previousText, config.previousText, true),
173+
nextText = paginationCtrl.getAttributeValue(attrs.nextText, config.nextText, true),
174+
align = paginationCtrl.getAttributeValue(attrs.align, config.align);
171175

172176
// Create page object used in template
173177
function makePage(number, text, isDisabled, isPrevious, isNext) {

src/pagination/test/pager.spec.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ describe('setting pagerConfig', function() {
153153

154154
});
155155

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

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

183+
it('changes "previous" & "next" text from interpolated attributes', function() {
184+
$rootScope.previousText = '<<';
185+
$rootScope.nextText = '>>';
186+
element = $compile('<pager align="false" previous-text="{{previousText}}" next-text="{{nextText}}" num-pages="numPages" current-page="currentPage"></pager>')($rootScope);
187+
$rootScope.$digest();
188+
189+
expect(element.find('li').eq(0).text()).toBe('<<');
190+
expect(element.find('li').eq(-1).text()).toBe('>>');
191+
});
192+
183193
});

src/pagination/test/pagination.spec.js

+11-15
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ describe('pagination directive with max size option & no rotate', function () {
222222
$rootScope.numPages = 12;
223223
$rootScope.currentPage = 7;
224224
$rootScope.maxSize = 5;
225-
element = $compile('<pagination num-pages="numPages" current-page="currentPage" max-size="maxSize" rotate="false"></pagination>')($rootScope);
225+
$rootScope.rotate = false;
226+
element = $compile('<pagination num-pages="numPages" current-page="currentPage" max-size="maxSize" rotate="rotate"></pagination>')($rootScope);
226227
$rootScope.$digest();
227228
}));
228229

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

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

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

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

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

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

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

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

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

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

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

464-
465463
it('executes the onSelectPage expression when the current page changes', function() {
466464
$rootScope.selectPageHandler = jasmine.createSpy('selectPageHandler');
467465
element = $compile('<pagination direction-links="false" num-pages="numPages" current-page="currentPage" on-select-page="selectPageHandler(page)"></pagination>')($rootScope);
@@ -541,11 +539,11 @@ describe('pagination directive with first, last & number links', function () {
541539
$rootScope = _$rootScope_;
542540
$rootScope.numPages = 5;
543541
$rootScope.currentPage = 3;
544-
element = $compile('<pagination boundary-links="true" direction-links="false" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
542+
$rootScope.directions = false;
543+
element = $compile('<pagination boundary-links="true" direction-links="directions" num-pages="numPages" current-page="currentPage"></pagination>')($rootScope);
545544
$rootScope.$digest();
546545
}));
547546

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

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

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

0 commit comments

Comments
 (0)