szurubooru/client/js/views/manual_page_view.js

90 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-04-10 22:13:01 +02:00
'use strict';
const events = require('../events.js');
const misc = require('../util/misc.js');
const views = require('../util/views.js');
function removeConsecutiveDuplicates(a) {
return a.filter((item, pos, ary) => {
return !pos || item != ary[pos - 1];
});
}
class ManualPageView {
constructor() {
this.holderTemplate = views.getTemplate('manual-pager');
this.navTemplate = views.getTemplate('manual-pager-nav');
}
render(ctx) {
const target = document.getElementById('content-holder');
const source = this.holderTemplate();
const pageContentHolder = source.querySelector('.page-content-holder');
const pageNav = source.querySelector('.page-nav');
const currentPage = ctx.initialPage;
2016-04-10 22:13:01 +02:00
ctx.requestPage(currentPage).then(response => {
2016-04-10 22:13:01 +02:00
let pageRendererCtx = response;
pageRendererCtx.target = pageContentHolder;
ctx.pageRenderer.render(pageRendererCtx);
const totalPages = Math.ceil(response.total / response.pageSize);
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;
2016-04-10 22:13:01 +02:00
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);
const pages = [];
let lastPage = 0;
for (let page of pagesVisible) {
if (page !== lastPage + 1) {
pages.push({ellipsis: true});
}
pages.push({
number: page,
link: ctx.clientUrl.format({page: page}),
active: currentPage === page,
2016-04-10 22:13:01 +02:00
});
lastPage = page;
}
if (response.total) {
views.showView(pageNav, this.navTemplate({
prevLink: ctx.clientUrl.format({page: currentPage - 1}),
nextLink: ctx.clientUrl.format({page: currentPage + 1}),
prevLinkActive: currentPage > 1,
nextLinkActive: currentPage < totalPages,
pages: pages,
}));
}
2016-04-10 22:13:01 +02:00
views.listenToMessages(target);
views.showView(target, source);
if (response.total < (currentPage - 1) * response.pageSize) {
events.notify(events.Info, 'No data to show');
}
2016-04-10 22:13:01 +02:00
}, response => {
views.listenToMessages(target);
views.showView(target, source);
events.notify(events.Error, response.description);
});
}
}
module.exports = ManualPageView;