forked from 11ty/eleventy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pagination.js
302 lines (243 loc) · 7.77 KB
/
Pagination.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const lodashChunk = require("lodash/chunk");
const lodashGet = require("lodash/get");
const lodashSet = require("lodash/set");
const EleventyBaseError = require("../EleventyBaseError");
const config = require("../Config");
class PaginationError extends EleventyBaseError {}
class Pagination {
constructor(data) {
this.config = config.getConfig();
this.setData(data);
}
static hasPagination(data) {
return "pagination" in data;
}
hasPagination() {
if (!this.data) {
throw new Error("Missing `setData` call for Pagination object.");
}
return Pagination.hasPagination(this.data);
}
circularReferenceCheck(data) {
if (data.eleventyExcludeFromCollections) {
return;
}
let key = data.pagination.data;
let tags = data.tags || [];
for (let tag of tags) {
if (`collections.${tag}` === key) {
throw new PaginationError(
`Pagination circular reference${
this.template ? ` on ${this.template.inputPath}` : ""
}, data:\`${key}\` iterates over both the \`${tag}\` tag and also supplies pages to that tag.`
);
}
}
}
setData(data) {
this.data = data || {};
this.target = [];
if (!this.hasPagination()) {
return;
}
if (!data.pagination) {
throw new Error(
"Misconfigured pagination data in template front matter (YAML front matter precaution: did you use tabs and not spaces for indentation?)."
);
} else if (!("size" in data.pagination)) {
throw new Error("Missing pagination size in front matter data.");
}
this.circularReferenceCheck(data);
this.size = data.pagination.size;
this.alias = data.pagination.alias;
this.target = this._resolveItems();
this.items = this.getPagedItems();
}
setTemplate(tmpl) {
this.template = tmpl;
}
_getDataKey() {
return this.data.pagination.data;
}
doResolveToObjectValues() {
if ("resolve" in this.data.pagination) {
return this.data.pagination.resolve === "values";
}
return false;
}
isFiltered(value) {
if ("filter" in this.data.pagination) {
let filtered = this.data.pagination.filter;
if (Array.isArray(filtered)) {
return filtered.indexOf(value) > -1;
}
return filtered === value;
}
return false;
}
_resolveItems() {
let notFoundValue = "__NOT_FOUND_ERROR__";
let key = this._getDataKey();
let fullDataSet = lodashGet(this.data, key, notFoundValue);
if (fullDataSet === notFoundValue) {
throw new Error(
`Could not resolve pagination key in template data: ${key}`
);
}
let keys;
if (Array.isArray(fullDataSet)) {
keys = fullDataSet;
} else if (this.doResolveToObjectValues()) {
keys = Object.values(fullDataSet);
} else {
keys = Object.keys(fullDataSet);
}
let result = keys.filter(() => true);
if (
this.data.pagination.before &&
typeof this.data.pagination.before === "function"
) {
// we don’t need to make a copy of this because we already .filter() above
result = this.data.pagination.before(result, this.data);
}
if (this.data.pagination.reverse === true) {
result = result.reverse();
}
if (this.data.pagination.filter) {
result = result.filter(value => !this.isFiltered(value));
}
return result;
}
getPagedItems() {
// TODO switch to a getter
if (!this.data) {
throw new Error("Missing `setData` call for Pagination object.");
}
return lodashChunk(this.target, this.size);
}
// TODO this name is not good
// “To cancel” means to not write the original root template
cancel() {
return this.hasPagination();
}
getPageCount() {
if (!this.hasPagination()) {
return 0;
}
return this.items.length;
}
async getPageTemplates() {
if (!this.data) {
throw new Error("Missing `setData` call for Pagination object.");
}
if (!this.hasPagination()) {
return [];
}
if (this.pagesCache) {
return this.pagesCache;
}
let pages = [];
let items = this.items;
let tmpl = this.template;
let templates = [];
let links = [];
let hrefs = [];
let overrides = [];
for (let pageNumber = 0, k = items.length; pageNumber < k; pageNumber++) {
let cloned = tmpl.clone();
// TODO maybe also move this permalink additions up into the pagination class
if (pageNumber > 0 && !this.data[this.config.keys.permalink]) {
cloned.setExtraOutputSubdirectory(pageNumber);
}
templates.push(cloned);
let override = {
pagination: {
data: this.data.pagination.data,
size: this.data.pagination.size,
alias: this.alias,
pages: this.size === 1 ? items.map(entry => entry[0]) : items,
// See Issue #345 for more examples
page: {
previous:
pageNumber > 0
? this.size === 1
? items[pageNumber - 1][0]
: items[pageNumber - 1]
: null,
next:
pageNumber < items.length - 1
? this.size === 1
? items[pageNumber + 1][0]
: items[pageNumber + 1]
: null,
first: items.length
? this.size === 1
? items[0][0]
: items[0]
: null,
last: items.length
? this.size === 1
? items[items.length - 1][0]
: items[items.length - 1]
: null
},
items: items[pageNumber],
pageNumber: pageNumber
}
};
if (this.alias) {
lodashSet(
override,
this.alias,
this.size === 1 ? items[pageNumber][0] : items[pageNumber]
);
}
overrides.push(override);
cloned.setPaginationData(override);
// TO DO subdirectory to links if the site doesn’t live at /
links.push("/" + (await cloned.getOutputLink()));
hrefs.push(await cloned.getOutputHref());
}
// we loop twice to pass in the appropriate prev/next links (already full generated now)
templates.forEach(
function(cloned, pageNumber) {
let pageObj = {};
// links are okay but hrefs are better
pageObj.previousPageLink =
pageNumber > 0 ? links[pageNumber - 1] : null;
pageObj.previous = pageObj.previousPageLink;
pageObj.nextPageLink =
pageNumber < templates.length - 1 ? links[pageNumber + 1] : null;
pageObj.next = pageObj.nextPageLink;
pageObj.firstPageLink = links.length > 0 ? links[0] : null;
pageObj.lastPageLink =
links.length > 0 ? links[links.length - 1] : null;
pageObj.links = links;
// todo deprecated, consistency with collections and use links instead
pageObj.pageLinks = links;
// hrefs are better than links
pageObj.previousPageHref =
pageNumber > 0 ? hrefs[pageNumber - 1] : null;
pageObj.nextPageHref =
pageNumber < templates.length - 1 ? hrefs[pageNumber + 1] : null;
pageObj.firstPageHref = hrefs.length > 0 ? hrefs[0] : null;
pageObj.lastPageHref =
hrefs.length > 0 ? hrefs[hrefs.length - 1] : null;
pageObj.hrefs = hrefs;
// better names
pageObj.href = {
previous: pageObj.previousPageHref,
next: pageObj.nextPageHref,
first: pageObj.firstPageHref,
last: pageObj.lastPageHref
};
Object.assign(overrides[pageNumber].pagination, pageObj);
cloned.setPaginationData(overrides[pageNumber]);
pages.push(cloned);
}.bind(this)
);
this.pagesCache = pages;
return pages;
}
}
module.exports = Pagination;