mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-19 04:57:54 +01:00
add pagination to admin searches
This commit is contained in:
@@ -15,6 +15,7 @@ const user = require('../../user');
|
||||
const topics = require('../../topics');
|
||||
const utils = require('../../utils');
|
||||
const emailer = require('../../emailer');
|
||||
const pagination = require('../../pagination');
|
||||
|
||||
const dashboardController = module.exports;
|
||||
|
||||
@@ -341,24 +342,30 @@ dashboardController.getTopics = async (req, res) => {
|
||||
};
|
||||
|
||||
dashboardController.getSearches = async (req, res) => {
|
||||
let start = 0;
|
||||
let end = 0;
|
||||
const page = parseInt(req.query.page, 10) || 1;
|
||||
const perPage = 25;
|
||||
const start = Math.max(0, (page - 1) * perPage);
|
||||
const stop = start + perPage - 1;
|
||||
|
||||
let startDate = 0;
|
||||
let endDate = 0;
|
||||
if (req.query.start) {
|
||||
start = new Date(req.query.start);
|
||||
start.setHours(24, 0, 0, 0);
|
||||
end = new Date();
|
||||
end.setHours(24, 0, 0, 0);
|
||||
startDate = new Date(req.query.start);
|
||||
startDate.setHours(24, 0, 0, 0);
|
||||
endDate = new Date();
|
||||
endDate.setHours(24, 0, 0, 0);
|
||||
}
|
||||
if (req.query.end) {
|
||||
end = new Date(req.query.end);
|
||||
end.setHours(24, 0, 0, 0);
|
||||
endDate = new Date(req.query.end);
|
||||
endDate.setHours(24, 0, 0, 0);
|
||||
}
|
||||
|
||||
let searches;
|
||||
if (start && end && start <= end) {
|
||||
const daysArr = [start];
|
||||
const nextDay = new Date(start.getTime());
|
||||
while (nextDay < end) {
|
||||
let itemCount;
|
||||
if (startDate && endDate && startDate <= endDate) {
|
||||
const daysArr = [startDate];
|
||||
const nextDay = new Date(startDate.getTime());
|
||||
while (nextDay < endDate) {
|
||||
nextDay.setDate(nextDay.getDate() + 1);
|
||||
nextDay.setHours(0, 0, 0, 0);
|
||||
daysArr.push(new Date(nextDay.getTime()));
|
||||
@@ -382,13 +389,21 @@ dashboardController.getSearches = async (req, res) => {
|
||||
searches = Object.keys(map)
|
||||
.map(key => ({ value: key, score: map[key] }))
|
||||
.sort((a, b) => b.score - a.score);
|
||||
itemCount = searches.length;
|
||||
searches = searches.slice(start, stop + 1);
|
||||
} else {
|
||||
searches = await db.getSortedSetRevRangeWithScores('searches:all', 0, 99);
|
||||
[itemCount, searches] = await Promise.all([
|
||||
db.sortedSetCard('searches:all'),
|
||||
db.getSortedSetRevRangeWithScores('searches:all', start, stop),
|
||||
]);
|
||||
}
|
||||
|
||||
const pageCount = Math.ceil(itemCount / perPage);
|
||||
|
||||
res.render('admin/dashboard/searches', {
|
||||
searches: searches.map(s => ({ value: validator.escape(String(s.value)), score: s.score })),
|
||||
startDate: req.query.start ? validator.escape(String(req.query.start)) : null,
|
||||
endDate: req.query.end ? validator.escape(String(req.query.end)) : null,
|
||||
pagination: pagination.create(page, pageCount, req.query),
|
||||
});
|
||||
};
|
||||
|
||||
@@ -36,5 +36,6 @@
|
||||
{{{ end }}}
|
||||
</tbody>
|
||||
</table>
|
||||
<!-- IMPORT admin/partials/paginator.tpl -->
|
||||
</div>
|
||||
</div>
|
||||
@@ -1,47 +1,47 @@
|
||||
<div component="pagination" class="pagination-container mt-3{{{ if !pagination.pages.length }}} hidden{{{ end }}}">
|
||||
<nav component="pagination" class="pagination-container mt-3{{{ if !pagination.pages.length }}} hidden{{{ end }}}" aria-label="[[global:pagination]]">
|
||||
<ul class="pagination pagination-sm gap-1 hidden-xs hidden-sm justify-content-center">
|
||||
<li class="page-item previous float-start{{{ if !pagination.prev.active }}} disabled{{{ end }}}">
|
||||
<a class="page-link rounded fw-secondary px-3" href="?{pagination.prev.qs}" data-page="{pagination.prev.page}"><i class="fa fa-chevron-left"></i> </a>
|
||||
<li class="page-item previous {{{ if !pagination.prev.active }}} opacity-50 pe-none{{{ end }}}">
|
||||
<a class="page-link rounded fw-secondary px-3 border-0" href="?{pagination.prev.qs}" data-page="{pagination.prev.page}" aria-label="[[global:pagination.previouspage]]"><i class="fa fa-chevron-left"></i> </a>
|
||||
</li>
|
||||
|
||||
{{{each pagination.pages}}}
|
||||
{{{ if pagination.pages.separator }}}
|
||||
{{{ if ./separator }}}
|
||||
<li component="pagination/select-page" class="page-item page select-page">
|
||||
<a class="page-link rounded fw-secondary px-3" href="#"><i class="fa fa-ellipsis-h"></i></a>
|
||||
<a class="page-link rounded fw-secondary px-3 border-0" href="#" aria-label="[[global:pagination.go-to-page]]"><i class="fa fa-ellipsis-h"></i></a>
|
||||
</li>
|
||||
{{{ else }}}
|
||||
<li class="page-item page{{{ if pagination.pages.active }}} active{{{ end }}}" >
|
||||
<a class="page-link rounded fw-secondary px-3" href="?{pagination.pages.qs}" data-page="{pagination.pages.page}">{pagination.pages.page}</a>
|
||||
<li class="page-item page{{{ if ./active }}} active{{{ end }}}" >
|
||||
<a class="page-link rounded fw-secondary px-3 border-0" href="?{./qs}" data-page="{./page}" aria-label="[[global:pagination.page-x, {./page}]]">{./page}</a>
|
||||
</li>
|
||||
{{{ end }}}
|
||||
{{{end}}}
|
||||
|
||||
<li class="page-item next float-end {{{ if !pagination.next.active }}} disabled{{{ end }}}">
|
||||
<a class="page-link rounded fw-secondary px-3" href="?{pagination.next.qs}" data-page="{pagination.next.page}"> <i class="fa fa-chevron-right"></i></a>
|
||||
<li class="page-item next {{{ if !pagination.next.active }}} opacity-50 pe-none{{{ end }}}">
|
||||
<a class="page-link rounded fw-secondary px-3 border-0" href="?{pagination.next.qs}" data-page="{pagination.next.page}" aria-label="[[global:pagination.nextpage]]"> <i class="fa fa-chevron-right"></i></a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
{{{ if !template.topic }}}
|
||||
<ul class="pagination pagination-sm hidden-md hidden-lg justify-content-center">
|
||||
<li class="page-item first{{{ if !pagination.prev.active }}} disabled{{{ end }}}">
|
||||
<a class="page-link fw-secondary" href="?{pagination.first.qs}" data-page="1"><i class="fa fa-fast-backward"></i> </a>
|
||||
<ul class="pagination hidden-md hidden-lg justify-content-center gap-3">
|
||||
<li class="page-item first{{{ if !pagination.prev.active }}} opacity-50 pe-none{{{ end }}}">
|
||||
<a class="page-link fw-secondary border-0" href="?{pagination.first.qs}" data-page="1" aria-label="[[global:pagination.firstpage]]"><i class="fa fa-fast-backward"></i> </a>
|
||||
</li>
|
||||
|
||||
<li class="page-item previous{{{ if !pagination.prev.active }}} disabled{{{ end }}}">
|
||||
<a class="page-link fw-secondary" href="?{pagination.prev.qs}" data-page="{pagination.prev.page}"><i class="fa fa-chevron-left"></i> </a>
|
||||
<li class="page-item previous{{{ if !pagination.prev.active }}} opacity-50 pe-none{{{ end }}}">
|
||||
<a class="page-link fw-secondary border-0" href="?{pagination.prev.qs}" data-page="{pagination.prev.page}" aria-label="[[global:pagination.previouspage]]"><i class="fa fa-chevron-left"></i> </a>
|
||||
</li>
|
||||
|
||||
<li component="pagination/select-page" class="page-item page select-page">
|
||||
<a class="page-link fw-secondary" href="#">{pagination.currentPage} / {pagination.pageCount}</a>
|
||||
<a class="page-link fw-secondary border-0" href="#" aria-label="[[global:pagination.go-to-page]]">{pagination.currentPage} / {pagination.pageCount}</a>
|
||||
</li>
|
||||
|
||||
<li class="page-item next{{{ if !pagination.next.active }}} disabled{{{ end }}}">
|
||||
<a class="page-link fw-secondary" href="?{pagination.next.qs}" data-page="{pagination.next.page}"> <i class="fa fa-chevron-right"></i></a>
|
||||
<li class="page-item next{{{ if !pagination.next.active }}} opacity-50 pe-none{{{ end }}}">
|
||||
<a class="page-link fw-secondary border-0" href="?{pagination.next.qs}" data-page="{pagination.next.page}" aria-label="[[global:pagination.nextpage]]"> <i class="fa fa-chevron-right"></i></a>
|
||||
</li>
|
||||
|
||||
<li class="page-item last{{{ if !pagination.next.active }}} disabled{{{ end }}}">
|
||||
<a class="page-link fw-secondary" href="?{pagination.last.qs}" data-page="{pagination.pageCount}"><i class="fa fa-fast-forward"></i> </a>
|
||||
<li class="page-item last{{{ if !pagination.next.active }}} opacity-50 pe-none{{{ end }}}">
|
||||
<a class="page-link fw-secondary border-0" href="?{pagination.last.qs}" data-page="{pagination.pageCount}" aria-label="[[global:pagination.lastpage]]"><i class="fa fa-fast-forward"></i> </a>
|
||||
</li>
|
||||
</ul>
|
||||
{{{ end }}}
|
||||
</div>
|
||||
</nav>
|
||||
Reference in New Issue
Block a user