66 lines
3.4 KiB
JavaScript
66 lines
3.4 KiB
JavaScript
hexo.extend.helper.register("uk_paginator", function(option) {
|
|
const options = Object.assign({
|
|
base: "/",
|
|
pagePath: "page/%d/",
|
|
total: 1,
|
|
current: 1,
|
|
space: "...",
|
|
sideSize: 5,
|
|
midSize: 10,
|
|
mainClasses: [],
|
|
itemClasses: [],
|
|
}, option);
|
|
const pagePathReplaced = options.pagePath.replace('/', '\/').replace('%d', '\\d+');
|
|
const regPagePath = new RegExp(pagePathReplaced);
|
|
options.base = options.base.replace(regPagePath, '');
|
|
|
|
let pageStructure = [];
|
|
pageStructure.push(`<ul class="uk-pagination ${options.mainClasses.join(' ')}">`);
|
|
|
|
if (options.total > 0 && options.current > 2) {
|
|
pageStructure.push(`<li class="${options.itemClasses.join(' ')}"><a href="${options.base}${options.pagePath.replace('%d', options.current - 1)}" title="上一页"><span uk-pagination-previous></span></a></li>`);
|
|
} else if (options.total > 0 && options.current === 2) {
|
|
pageStructure.push(`<li class="${options.itemClasses.join(' ')}"><a href="${options.base}" title="上一页"><span uk-pagination-previous></span></a></li>`);
|
|
} else {
|
|
pageStructure.push(`<li class="uk-disabled ${options.itemClasses.join(' ')}"><a href="${options.base}" title="上一页"><span uk-pagination-previous></span></a></li>`);
|
|
}
|
|
|
|
let pageGroups = [];
|
|
// 生成起始页范围
|
|
for (let p = 1; p <= Math.min(options.sideSize, options.total); p++) {
|
|
pageGroups.push(p);
|
|
}
|
|
// 生成中间页范围
|
|
if (options.current > options.sideSize && options.current < (options.total - options.sideSize + 1)) {
|
|
for (let p = Math.max(pageGroups[pageGroups.length - 1] + 1, options.current - options.midSize / 2); p <= Math.min(options.total, options.current + options.midSize / 2); p++) {
|
|
pageGroups.push(p);
|
|
}
|
|
}
|
|
// 生成结束页范围
|
|
for (let p = Math.max(pageGroups[pageGroups.length - 1] + 1, options.total - options.sideSize + 1); p <= options.total; p++) {
|
|
pageGroups.push(p);
|
|
}
|
|
|
|
pageGroups.forEach(function(page, index) {
|
|
if (index > 0 && page - pageGroups[index - 1] > 1) {
|
|
pageStructure.push(`<li class="uk-disabled ${options.itemClasses.join(' ')}"><span>${options.space}</span></li>`);
|
|
}
|
|
if (page === options.current) {
|
|
pageStructure.push(`<li class="uk-active ${options.itemClasses.join(' ')}"><span>${page}</span></li>`)
|
|
} else if (page === 1) {
|
|
pageStructure.push(`<li class="${options.itemClasses.join(' ')}"><a href="${options.base}" title="第${page}页">${page}</a></li>`)
|
|
} else {
|
|
pageStructure.push(`<li class="${options.itemClasses.join(' ')}"><a href="${options.base}${options.pagePath.replace('%d', page)}" title="第${page}页">${page}</a></li>`)
|
|
}
|
|
});
|
|
|
|
if (options.total > 0 && options.current + 1 <= options.total) {
|
|
pageStructure.push(`<li class="${options.itemClasses.join(' ')}"><a href="${options.base}${options.pagePath.replace('%d', options.current + 1)}" title="下一页"><span uk-pagination-next></span></a></li>`)
|
|
} else {
|
|
pageStructure.push(`<li class="uk-disabled ${options.itemClasses.join(' ')}"><a href="${options.base}${options.pagePath.replace('%d', options.current + 1)}" title="下一页"><span uk-pagination-next></span></a></li>`)
|
|
}
|
|
|
|
pageStructure.push(`</ul>`);
|
|
|
|
return pageStructure.join("\n");
|
|
}); |