Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(paginator): allow custom class name #5001

Merged
merged 1 commit into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 29 additions & 9 deletions lib/plugins/helper/paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ const createLink = (options, ctx) => {

const createPageTag = (options, ctx) => {
const link = createLink(options, ctx);
const { current, escape, transform } = options;
const {
current,
escape,
transform,
page_class: pageClass,
current_class: currentClass
} = options;

return i => {
if (i === current) {
return htmlTag('span', { class: 'page-number current' }, transform ? transform(i) : i, escape);
return htmlTag('span', { class: pageClass + ' ' + currentClass }, transform ? transform(i) : i, escape);
}
return htmlTag('a', { class: 'page-number', href: link(i) }, transform ? transform(i) : i, escape);
return htmlTag('a', { class: pageClass, href: link(i) }, transform ? transform(i) : i, escape);
};
};

Expand All @@ -36,14 +42,15 @@ const pagenasionPartShow = (tags, options, ctx) => {
total,
space,
end_size: endSize,
mid_size: midSize
mid_size: midSize,
space_class: spaceClass
} = options;

const leftEnd = Math.min(endSize, current - 1);
const rightEnd = Math.max(total - endSize + 1, current + 1);
const leftMid = Math.max(leftEnd + 1, current - midSize);
const rightMid = Math.min(rightEnd - 1, current + midSize);
const spaceHtml = htmlTag('span', { class: 'space' }, space, false);
const spaceHtml = htmlTag('span', { class: spaceClass }, space, false);

const pageTag = createPageTag(options, ctx);

Expand Down Expand Up @@ -93,7 +100,13 @@ function paginatorHelper(options = {}) {
next_text: 'Next',
prev_text: 'Prev',
prev_next: true,
escape: true
escape: true,
page_class: 'page-number',
current_class: 'current',
space_class: 'space',
prev_class: 'extend prev',
next_class: 'extend next',
force_prev_next: false
}, options);

const {
Expand All @@ -102,7 +115,10 @@ function paginatorHelper(options = {}) {
prev_text: prevText,
next_text: nextText,
prev_next: prevNext,
escape
escape,
prev_class: prevClass,
next_class: nextClass,
force_prev_next: forcePrevNext
} = options;

if (!current) return '';
Expand All @@ -113,7 +129,9 @@ function paginatorHelper(options = {}) {

// Display the link to the previous page
if (prevNext && current > 1) {
tags.push(htmlTag('a', { class: 'extend prev', rel: 'prev', href: link(current - 1)}, prevText, escape));
tags.push(htmlTag('a', { class: prevClass, rel: 'prev', href: link(current - 1)}, prevText, escape));
} else if (forcePrevNext) {
tags.push(htmlTag('span', { class: prevClass, rel: 'prev' }, prevText, escape));
}

if (options.show_all) {
Expand All @@ -124,7 +142,9 @@ function paginatorHelper(options = {}) {

// Display the link to the next page
if (prevNext && current < total) {
tags.push(htmlTag('a', { class: 'extend next', rel: 'next', href: link(current + 1) }, nextText, escape));
tags.push(htmlTag('a', { class: nextClass, rel: 'next', href: link(current + 1) }, nextText, escape));
} else if (forcePrevNext) {
tags.push(htmlTag('span', { class: nextClass, rel: 'next' }, nextText, escape));
}

return tags.join('');
Expand Down
39 changes: 39 additions & 0 deletions test/scripts/helpers/paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,43 @@ describe('paginator', () => {
'<bar></a>'
].join(''));
});

it('custom_class', () => {
const result = paginator({
current: 2,
current_class: 'current-class',
space_class: 'space-class',
page_class: 'page-class',
prev_class: 'prev-class',
next_class: 'next-class'
});

result.should.eql([
'<a class="prev-class" rel="prev" href="/">Prev</a>',
'<a class="page-class" href="/">1</a>',
'<span class="page-class current-class">2</span>',
'<a class="page-class" href="/page/3/">3</a>',
'<a class="page-class" href="/page/4/">4</a>',
'<span class="space-class">&hellip;</span>',
'<a class="page-class" href="/page/10/">10</a>',
'<a class="next-class" rel="next" href="/page/3/">Next</a>'
].join(''));
});

it('force_prev_next', () => {
const result = paginator({
current: 1,
force_prev_next: true
});

result.should.eql([
'<span class="extend prev" rel="prev">Prev</span>',
'<span class="page-number current">1</span>',
'<a class="page-number" href="/page/2/">2</a>',
'<a class="page-number" href="/page/3/">3</a>',
'<span class="space">&hellip;</span>',
'<a class="page-number" href="/page/10/">10</a>',
'<a class="extend next" rel="next" href="/page/2/">Next</a>'
].join(''));
});
});