2016-04-10 22:13:01 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-12 20:11:43 +02:00
|
|
|
const router = require('../router.js');
|
2016-04-14 21:01:33 +02:00
|
|
|
const keyboard = require('../util/keyboard.js');
|
2016-04-10 22:13:01 +02:00
|
|
|
const views = require('../util/views.js');
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
const holderTemplate = views.getTemplate('manual-pager');
|
|
|
|
const navTemplate = views.getTemplate('manual-pager-nav');
|
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
function _removeConsecutiveDuplicates(a) {
|
2016-04-10 22:13:01 +02:00
|
|
|
return a.filter((item, pos, ary) => {
|
|
|
|
return !pos || item != ary[pos - 1];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
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; });
|
2016-05-20 21:35:12 +02:00
|
|
|
pagesVisible = _removeConsecutiveDuplicates(pagesVisible);
|
2016-04-14 20:54:00 +02:00
|
|
|
return pagesVisible;
|
|
|
|
}
|
|
|
|
|
2016-07-05 21:20:28 +02:00
|
|
|
function _getPages(currentPage, pageNumbers, ctx) {
|
2016-04-14 20:54:00 +02:00
|
|
|
const pages = [];
|
|
|
|
let lastPage = 0;
|
|
|
|
for (let page of pageNumbers) {
|
|
|
|
if (page !== lastPage + 1) {
|
|
|
|
pages.push({ellipsis: true});
|
|
|
|
}
|
|
|
|
pages.push({
|
|
|
|
number: page,
|
2016-07-05 21:20:28 +02:00
|
|
|
link: ctx.getClientUrlForPage(page),
|
2016-04-14 20:54:00 +02:00
|
|
|
active: currentPage === page,
|
|
|
|
});
|
|
|
|
lastPage = page;
|
|
|
|
}
|
|
|
|
return pages;
|
|
|
|
}
|
|
|
|
|
2016-04-10 22:13:01 +02:00
|
|
|
class ManualPageView {
|
2016-06-14 10:31:48 +02:00
|
|
|
constructor(ctx) {
|
|
|
|
this._hostNode = document.getElementById('content-holder');
|
2016-08-28 18:53:06 +02:00
|
|
|
views.replaceContent(this._hostNode, holderTemplate());
|
|
|
|
}
|
2016-04-10 22:13:01 +02:00
|
|
|
|
2016-08-28 18:53:06 +02:00
|
|
|
run(ctx) {
|
2016-07-07 21:18:35 +02:00
|
|
|
const currentPage = ctx.parameters.page;
|
2016-08-28 18:53:06 +02:00
|
|
|
this.clearMessages();
|
|
|
|
views.emptyContent(this._pageNavNode);
|
2016-06-14 10:31:48 +02:00
|
|
|
|
2016-04-12 18:17:46 +02:00
|
|
|
ctx.requestPage(currentPage).then(response => {
|
2016-06-11 09:59:29 +02:00
|
|
|
Object.assign(ctx.pageContext, response);
|
2016-08-28 18:53:06 +02:00
|
|
|
ctx.pageContext.hostNode = this._pageContentHolderNode;
|
2016-06-14 10:31:48 +02:00
|
|
|
ctx.pageRenderer(ctx.pageContext);
|
2016-04-10 22:13:01 +02:00
|
|
|
|
|
|
|
const totalPages = Math.ceil(response.total / response.pageSize);
|
2016-05-20 21:35:12 +02:00
|
|
|
const pageNumbers = _getVisiblePageNumbers(currentPage, totalPages);
|
2016-07-05 21:20:28 +02:00
|
|
|
const pages = _getPages(currentPage, pageNumbers, ctx);
|
2016-04-12 18:17:46 +02:00
|
|
|
|
2016-04-14 21:01:33 +02:00
|
|
|
keyboard.bind(['a', 'left'], () => {
|
|
|
|
if (currentPage > 1) {
|
2016-07-05 21:20:28 +02:00
|
|
|
router.show(ctx.getClientUrlForPage(currentPage - 1));
|
2016-04-14 21:01:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
keyboard.bind(['d', 'right'], () => {
|
|
|
|
if (currentPage < totalPages) {
|
2016-07-05 21:20:28 +02:00
|
|
|
router.show(ctx.getClientUrlForPage(currentPage + 1));
|
2016-04-14 21:01:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-12 18:17:46 +02:00
|
|
|
if (response.total) {
|
2016-06-14 10:31:48 +02:00
|
|
|
views.replaceContent(
|
2016-08-28 18:53:06 +02:00
|
|
|
this._pageNavNode,
|
2016-06-14 10:31:48 +02:00
|
|
|
navTemplate({
|
2016-07-05 21:20:28 +02:00
|
|
|
prevLink: ctx.getClientUrlForPage(currentPage - 1),
|
|
|
|
nextLink: ctx.getClientUrlForPage(currentPage + 1),
|
2016-06-14 10:31:48 +02:00
|
|
|
prevLinkActive: currentPage > 1,
|
|
|
|
nextLinkActive: currentPage < totalPages,
|
|
|
|
pages: pages,
|
|
|
|
}));
|
2016-04-12 18:17:46 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 23:49:46 +02:00
|
|
|
if (response.total <= (currentPage - 1) * response.pageSize) {
|
2016-06-14 10:31:48 +02:00
|
|
|
this.showInfo('No data to show');
|
2016-04-12 18:17:46 +02:00
|
|
|
}
|
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
|
|
|
|
2016-08-28 18:53:06 +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);
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
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
|
|
|
}
|
2016-04-10 22:13:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ManualPageView;
|