szurubooru/client/js/views/manual_page_view.js

153 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-04-10 22:13:01 +02:00
'use strict';
const router = require('../router.js');
const keyboard = require('../util/keyboard.js');
2016-04-10 22:13:01 +02:00
const views = require('../util/views.js');
const holderTemplate = views.getTemplate('manual-pager');
const navTemplate = views.getTemplate('manual-pager-nav');
function _removeConsecutiveDuplicates(a) {
2016-04-10 22:13:01 +02:00
return a.filter((item, pos, ary) => {
return !pos || item != ary[pos - 1];
});
}
function _getVisiblePageNumbers(currentPage, totalPages) {
2016-04-14 20:54:00 +02:00
const threshold = 2;
let pagesVisible = [];
for (let i = 1; i <= threshold; i++) {
pagesVisible.push(i);
}
for (let i = totalPages - threshold; i <= totalPages; i++) {
pagesVisible.push(i);
}
for (let i = currentPage - threshold;
i <= currentPage + threshold;
i++) {
pagesVisible.push(i);
}
pagesVisible = pagesVisible.filter((item, pos, ary) => {
return item >= 1 && item <= totalPages;
});
pagesVisible = pagesVisible.sort((a, b) => { return a - b; });
pagesVisible = _removeConsecutiveDuplicates(pagesVisible);
2016-04-14 20:54:00 +02:00
return pagesVisible;
}
2017-02-09 00:48:06 +01:00
function _getPages(currentPage, pageNumbers, ctx, limit) {
const pages = new Map();
let prevPage = 0;
2016-04-14 20:54:00 +02:00
for (let page of pageNumbers) {
2017-02-09 00:48:06 +01:00
if (page !== prevPage + 1) {
pages.set(page - 1, {ellipsis: true});
2016-04-14 20:54:00 +02:00
}
2017-02-09 00:48:06 +01:00
pages.set(page, {
2016-04-14 20:54:00 +02:00
number: page,
2017-02-09 00:48:06 +01:00
offset: (page - 1) * limit,
limit: limit === ctx.defaultLimit ? null : limit,
2016-04-14 20:54:00 +02:00
active: currentPage === page,
});
2017-02-09 00:48:06 +01:00
prevPage = page;
2016-04-14 20:54:00 +02:00
}
return pages;
}
2016-04-10 22:13:01 +02:00
class ManualPageView {
constructor(ctx) {
this._hostNode = document.getElementById('content-holder');
views.replaceContent(this._hostNode, holderTemplate());
}
2016-04-10 22:13:01 +02:00
run(ctx) {
2017-02-09 00:48:06 +01:00
const offset = parseInt(ctx.parameters.offset || 0);
const limit = parseInt(ctx.parameters.limit || ctx.defaultLimit);
this.clearMessages();
views.emptyContent(this._pageNavNode);
2017-02-09 00:48:06 +01:00
ctx.requestPage(offset, limit).then(response => {
ctx.pageRenderer({
parameters: ctx.parameters,
response: response,
hostNode: this._pageContentHolderNode,
});
keyboard.bind(['a', 'left'], () => {
2017-02-09 00:48:06 +01:00
this._navigateToPrevNextPage('prev');
});
keyboard.bind(['d', 'right'], () => {
2017-02-09 00:48:06 +01:00
this._navigateToPrevNextPage('next');
});
if (response.total) {
2017-02-09 00:48:06 +01:00
this._refreshNav(response, ctx);
}
2017-02-09 00:48:06 +01:00
if (!response.results.length) {
this.showInfo('No data to show');
}
2016-07-13 17:18:28 +02:00
views.syncScrollPosition();
2016-04-10 22:13:01 +02:00
}, response => {
2017-01-15 21:09:08 +01:00
this.showError(response.message);
2016-04-10 22:13:01 +02:00
});
}
2016-04-12 23:49:46 +02:00
get pageHeaderHolderNode() {
return this._hostNode.querySelector('.page-header-holder');
}
get _pageContentHolderNode() {
return this._hostNode.querySelector('.page-content-holder');
}
get _pageNavNode() {
return this._hostNode.querySelector('.page-nav');
}
clearMessages() {
views.clearMessages(this._hostNode);
}
showSuccess(message) {
views.showSuccess(this._hostNode, message);
}
showError(message) {
views.showError(this._hostNode, message);
}
showInfo(message) {
views.showInfo(this._hostNode, message);
2016-04-12 23:49:46 +02:00
}
2017-02-09 00:48:06 +01:00
_navigateToPrevNextPage(className) {
const linkNode = this._hostNode.querySelector('a.' + className);
if (linkNode.classList.contains('disabled')) {
return;
}
router.show(linkNode.getAttribute('href'));
}
_refreshNav(response, ctx) {
const currentPage = Math.ceil(
(response.offset + response.limit) / response.limit);
const totalPages = Math.ceil(response.total / response.limit);
const pageNumbers = _getVisiblePageNumbers(currentPage, totalPages);
const pages = _getPages(currentPage, pageNumbers, ctx, response.limit);
views.replaceContent(
this._pageNavNode,
navTemplate({
getClientUrlForPage: ctx.getClientUrlForPage,
prevPage: Math.min(totalPages, Math.max(1, currentPage - 1)),
nextPage: Math.min(totalPages, Math.max(1, currentPage + 1)),
currentPage: currentPage,
totalPages: totalPages,
pages: pages,
}));
}
2016-04-10 22:13:01 +02:00
}
module.exports = ManualPageView;