-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
paginator.js
138 lines (110 loc) · 3.4 KB
/
paginator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
const { htmlTag, url_for } = require('hexo-util');
const createLink = (options, ctx) => {
const { base, format } = options;
return i => url_for.call(ctx, i === 1 ? base : base + format.replace('%d', i));
};
const createPageTag = (options, ctx) => {
const link = createLink(options, ctx);
const { current, escape, transform } = options;
return i => {
if (i === current) {
return htmlTag('span', { class: 'page-number current' }, transform ? transform(i) : i, escape);
}
return htmlTag('a', { class: 'page-number', href: link(i) }, transform ? transform(i) : i, escape);
};
};
const showAll = (tags, options, ctx) => {
const { total } = options;
const pageLink = createPageTag(options, ctx);
for (let i = 1; i <= total; i++) {
tags.push(pageLink(i));
}
};
// It's too complicated. May need refactor.
const pagenasionPartShow = (tags, options, ctx) => {
const {
current,
total,
space,
end_size: endSize,
mid_size: midSize
} = options;
const leftEnd = current <= endSize ? current - 1 : endSize;
const rightEnd = total - current <= endSize ? current + 1 : total - endSize + 1;
const leftMid = current - midSize <= endSize ? leftEnd + 1 : current - midSize;
const rightMid = current + midSize + endSize > total ? rightEnd - 1 : current + midSize;
const spaceHtml = htmlTag('span', { class: 'space' }, space, false);
const pageTag = createPageTag(options, ctx);
// Display pages on the left edge
for (let i = 1; i <= leftEnd; i++) {
tags.push(pageTag(i));
}
// Display spaces between edges and middle pages
if (space && current - endSize - midSize > 1) {
tags.push(spaceHtml);
}
// Display left middle pages
if (leftMid > leftEnd) {
for (let i = leftMid; i < current; i++) {
tags.push(pageTag(i));
}
}
// Display the current page
tags.push(pageTag(current));
// Display right middle pages
if (rightMid < rightEnd) {
for (let i = current + 1; i <= rightMid; i++) {
tags.push(pageTag(i));
}
}
// Display spaces between edges and middle pages
if (space && total - endSize - midSize > current) {
tags.push(spaceHtml);
}
// Display pages on the right edge
for (let i = rightEnd; i <= total; i++) {
tags.push(pageTag(i));
}
};
function paginatorHelper(options = {}) {
options = Object.assign({
base: this.page.base || '',
current: this.page.current || 0,
format: `${this.config.pagination_dir}/%d/`,
total: this.page.total || 1,
end_size: 1,
mid_size: 2,
space: '…',
next_text: 'Next',
prev_text: 'Prev',
prev_next: true,
escape: true
}, options);
const {
current,
total,
prev_text: prevText,
next_text: nextText,
prev_next: prevNext,
escape
} = options;
if (!current) return '';
const link = createLink(options, this);
const tags = [];
// 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));
}
if (options.show_all) {
showAll(tags, options, this);
} else {
pagenasionPartShow(tags, options, this);
}
// 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));
}
return tags.join('');
}
module.exports = paginatorHelper;