Files
NodeBB/src/pagination.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-01-28 13:46:07 -05:00
'use strict';
2021-02-04 00:06:15 -07:00
const qs = require('querystring');
const _ = require('lodash');
2015-01-28 13:46:07 -05:00
2021-02-04 00:06:15 -07:00
const pagination = module.exports;
2015-01-28 13:46:07 -05:00
pagination.create = function (currentPage, pageCount, queryObj) {
2015-01-28 13:46:07 -05:00
if (pageCount <= 1) {
return {
2017-02-18 12:30:49 -07:00
prev: { page: 1, active: currentPage > 1 },
next: { page: 1, active: currentPage < pageCount },
2019-03-19 11:29:16 -04:00
first: { page: 1, active: currentPage === 1 },
last: { page: 1, active: currentPage === pageCount },
2015-01-28 13:46:07 -05:00
rel: [],
2015-10-07 16:13:37 -04:00
pages: [],
currentPage: 1,
2017-02-17 19:31:21 -07:00
pageCount: 1,
2015-01-28 13:46:07 -05:00
};
}
2015-04-22 13:47:41 -04:00
pageCount = parseInt(pageCount, 10);
2021-02-04 00:06:15 -07:00
let pagesToShow = [1, 2, pageCount - 1, pageCount];
2015-01-28 13:46:07 -05:00
currentPage = parseInt(currentPage, 10) || 1;
2021-02-04 00:06:15 -07:00
const previous = Math.max(1, currentPage - 1);
const next = Math.min(pageCount, currentPage + 1);
2015-01-28 13:46:07 -05:00
2021-02-04 00:06:15 -07:00
let startPage = Math.max(1, currentPage - 2);
2016-11-24 22:38:30 +03:00
if (startPage > pageCount - 5) {
startPage -= 2 - (pageCount - currentPage);
}
2021-02-04 00:06:15 -07:00
let i;
for (i = 0; i < 5; i += 1) {
2015-04-17 23:07:18 -04:00
pagesToShow.push(startPage + i);
2015-01-28 13:46:07 -05:00
}
2021-02-04 00:01:39 -07:00
pagesToShow = _.uniq(pagesToShow).filter(page => page > 0 && page <= pageCount).sort((a, b) => a - b);
2015-01-28 13:46:07 -05:00
queryObj = { ...(queryObj || {}) };
2017-03-07 20:26:49 +03:00
2017-03-07 20:58:43 +03:00
delete queryObj._;
2021-02-04 00:06:15 -07:00
const pages = pagesToShow.map((page) => {
queryObj.page = page;
2017-02-18 12:30:49 -07:00
return { page: page, active: page === currentPage, qs: qs.stringify(queryObj) };
2015-01-28 13:46:07 -05:00
});
for (i = pages.length - 1; i > 0; i -= 1) {
if (pages[i].page - 2 === pages[i - 1].page) {
2017-02-18 12:30:49 -07:00
pages.splice(i, 0, { page: pages[i].page - 1, active: false, qs: qs.stringify(queryObj) });
} else if (pages[i].page - 1 !== pages[i - 1].page) {
2017-02-18 12:30:49 -07:00
pages.splice(i, 0, { separator: true });
2015-04-17 23:07:18 -04:00
}
}
2021-02-04 00:06:15 -07:00
const data = { rel: [], pages: pages, currentPage: currentPage, pageCount: pageCount };
2015-04-19 18:45:16 -04:00
queryObj.page = previous;
2017-02-18 12:30:49 -07:00
data.prev = { page: previous, active: currentPage > 1, qs: qs.stringify(queryObj) };
2015-04-19 18:45:16 -04:00
queryObj.page = next;
2017-02-18 12:30:49 -07:00
data.next = { page: next, active: currentPage < pageCount, qs: qs.stringify(queryObj) };
2015-01-28 13:46:07 -05:00
2019-03-19 11:29:16 -04:00
queryObj.page = 1;
data.first = { page: 1, active: currentPage === 1, qs: qs.stringify(queryObj) };
queryObj.page = pageCount;
data.last = { page: pageCount, active: currentPage === pageCount, qs: qs.stringify(queryObj) };
2015-01-28 13:46:07 -05:00
if (currentPage < pageCount) {
data.rel.push({
2015-01-28 13:46:07 -05:00
rel: 'next',
2021-02-03 23:59:08 -07:00
href: `?${qs.stringify({ ...queryObj, page: next })}`,
2015-01-28 13:46:07 -05:00
});
}
if (currentPage > 1) {
data.rel.push({
2015-01-28 13:46:07 -05:00
rel: 'prev',
2021-02-03 23:59:08 -07:00
href: `?${qs.stringify({ ...queryObj, page: previous })}`,
2015-01-28 13:46:07 -05:00
});
}
return data;
2015-01-28 13:46:07 -05:00
};