Skip to content

Commit

Permalink
feat(paginator): custom class name (#5001)
Browse files Browse the repository at this point in the history
  • Loading branch information
renbaoshuo committed Jun 22, 2022
1 parent aa6c3c5 commit 8193550
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
38 changes: 29 additions & 9 deletions lib/plugins/helper/paginator.js
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
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(''));
});
});

0 comments on commit 8193550

Please sign in to comment.