Skip to content

Commit 759009c

Browse files
Add support for template strings.
Closes #88
1 parent 8153b23 commit 759009c

File tree

3 files changed

+113
-7
lines changed

3 files changed

+113
-7
lines changed

src/directives/pagination/README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,43 @@ expects pagination lists to have a particular structure different from the defau
110110
If you plan to use a custom template, take a look at the default as demonstrated in dirPagination.tpl.html to get
111111
an idea of how it interacts with the directive.
112112

113-
There are two ways to specify the template of the pagination controls directive:
113+
There are three ways to specify the template of the pagination controls directive:
114114

115-
**1. Use the `paginationTemplateProvider` in your app's config block to set a global template for your app:**
115+
**1. Use the `paginationTemplateProvider` in your app's config block to set a global templateUrl for your app:**
116116

117117
```JavaScript
118118
myApp.config(function(paginationTemplateProvider) {
119119
paginationTemplateProvider.setPath('path/to/dirPagination.tpl.html');
120120
});
121121
```
122122

123-
**2. Use the `template-url` attribute on each pagination controls directive:**
123+
**2. Use the `paginationTemplateProvider` in your app's config block to set a global template string for your app:**
124+
125+
```JavaScript
126+
myApp.config(function(paginationTemplateProvider) {
127+
paginationTemplateProvider.setString('<div class="my-page-links">...</div>');
128+
129+
// or with e.g. Webpack you might do
130+
paginationTemplateProvider.setString(require('/path/to/customPagination.tpl.html'));
131+
});
132+
```
133+
134+
**3. Use the `template-url` attribute on each pagination controls directive:**
124135

125136
```HTML
126137
<dir-pagination-controls template-url="path/to/dirPagination.tpl.html"></dir-pagination-controls>
127138
```
128139

140+
#### Template Priority
141+
142+
If you use more than one method for specifying the template, the actual template to use will be decided based on
143+
the following order of precedence (highest priority first):
144+
145+
1. `paginationTemplate.getString()`
146+
2. `template-url`
147+
3. `paginationTemplate.getPath()`
148+
4. (default built-in template)
149+
129150
## Directives API
130151

131152
The following attributes form the API for the pagination and pagination-controls directives. Optional attributes are marked as such,

src/directives/pagination/dirPagination.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,8 @@
224224

225225
var numberRegex = /^\d+$/;
226226

227-
return {
227+
var DDO = {
228228
restrict: 'AE',
229-
templateUrl: function(elem, attrs) {
230-
return attrs.templateUrl || paginationTemplate.getPath();
231-
},
232229
scope: {
233230
maxSize: '=?',
234231
onPageChange: '&?',
@@ -238,6 +235,23 @@
238235
link: dirPaginationControlsLinkFn
239236
};
240237

238+
// We need to check the paginationTemplate service to see whether a template path or
239+
// string has been specified, and add the `template` or `templateUrl` property to
240+
// the DDO as appropriate. The order of priority to decide which template to use is
241+
// (highest priority first):
242+
// 1. paginationTemplate.getString()
243+
// 2. attrs.templateUrl
244+
// 3. paginationTemplate.getPath()
245+
var templateString = paginationTemplate.getString();
246+
if (templateString !== undefined) {
247+
DDO.template = templateString;
248+
} else {
249+
DDO.templateUrl = function(elem, attrs) {
250+
return attrs.templateUrl || paginationTemplate.getPath();
251+
};
252+
}
253+
return DDO;
254+
241255
function dirPaginationControlsLinkFn(scope, element, attrs) {
242256

243257
// rawId is the un-interpolated value of the pagination-id attribute. This is only important when the corresponding dir-paginate directive has
@@ -586,15 +600,33 @@
586600
function paginationTemplateProvider() {
587601

588602
var templatePath = 'angularUtils.directives.dirPagination.template';
603+
var templateString;
589604

605+
/**
606+
* Set a templateUrl to be used by all instances of <dir-pagination-controls>
607+
* @param {String} path
608+
*/
590609
this.setPath = function(path) {
591610
templatePath = path;
592611
};
593612

613+
/**
614+
* Set a string of HTML to be used as a template by all instances
615+
* of <dir-pagination-controls>. If both a path *and* a string have been set,
616+
* the string takes precedence.
617+
* @param {String} str
618+
*/
619+
this.setString = function(str) {
620+
templateString = str;
621+
};
622+
594623
this.$get = function() {
595624
return {
596625
getPath: function() {
597626
return templatePath;
627+
},
628+
getString: function() {
629+
return templateString;
598630
}
599631
};
600632
};

src/directives/pagination/dirPagination.spec.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ describe('dirPagination directive', function() {
1414
beforeEach(module('angularUtils.directives.dirPagination'));
1515
beforeEach(module('templates-main'));
1616

17+
// used to test the paginationTemplateProvider (see end of file)
18+
var templateProvider;
19+
angular.module('customTemplateTestApp', []);
20+
beforeEach(module('customTemplateTestApp', function(paginationTemplateProvider) {
21+
templateProvider = paginationTemplateProvider;
22+
}));
23+
1724
beforeEach(inject(function($rootScope, _$compile_, _$timeout_) {
1825

1926
$compile = _$compile_;
@@ -1141,6 +1148,52 @@ describe('dirPagination directive', function() {
11411148
expect(getListItems($list2)).toEqual([ 'a', 'b', 'c' ]);
11421149
});
11431150

1151+
});
1152+
1153+
describe('paginationTemplateProvider', function() {
1154+
1155+
beforeEach(inject(function($templateCache) {
1156+
$templateCache.put('setPath_template', '<div class="set-path-template"><span>Test Template</span>{{ pages.length }}</div>');
1157+
$templateCache.put('templateUrl_template', '<div class="template-url-template"><span>Test TemplateUrl Template</span>{{ pages.length }}</div>');
1158+
}));
1159+
1160+
it('should use the custom template specified by setPath()', function() {
1161+
templateProvider.setPath('setPath_template');
1162+
compileElement(myCollection, 10);
1163+
1164+
expect(containingElement.find('.set-path-template').html()).toContain('Test Template');
1165+
});
1166+
1167+
it('should use the custom template specified by setString()', function() {
1168+
templateProvider.setString('<div class="set-string-template"><span>Test Template String</span>{{ pages.length }}</div>');
1169+
compileElement(myCollection, 10);
1170+
1171+
expect(containingElement.find('.set-string-template').html()).toContain('Test Template String');
1172+
});
1173+
1174+
it('should prioritize setString() if both path and string have been set', function() {
1175+
templateProvider.setString('<div class="set-string-template"><span>Test Template String</span>{{ pages.length }}</div>');
1176+
templateProvider.setPath('setPath_template');
1177+
compileElement(myCollection, 10);
1178+
1179+
expect(containingElement.find('.set-path-template').html()).toBeUndefined();
1180+
expect(containingElement.find('.set-string-template').html()).toContain('Test Template String');
1181+
});
1182+
1183+
it('should prioritize setString() over path and template-url attribute.', function() {
1184+
templateProvider.setString('<div class="set-string-template"><span>Test Template String</span>{{ pages.length }}</div>');
1185+
templateProvider.setPath('setPath_template');
1186+
1187+
var html = '<ul class="list"><li dir-paginate="item in collection | itemsPerPage: itemsPerPage" current-page="currentPage">{{ item }}</li></ul> ' +
1188+
'<dir-pagination-controls template-url="templateUrl_template"></dir-pagination-controls>';
1189+
containingElement.append($compile(html)($scope));
1190+
$scope.$apply();
1191+
1192+
expect(containingElement.find('.set-path-template').html()).toBeUndefined();
1193+
expect(containingElement.find('.template-url-template').html()).toBeUndefined();
1194+
expect(containingElement.find('.set-string-template').html()).toContain('Test Template String');
1195+
});
11441196

11451197
});
1198+
11461199
});

0 commit comments

Comments
 (0)