2016-04-12 23:49:46 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-12 20:11:43 +02:00
|
|
|
const router = require('../router.js');
|
2016-04-12 23:49:46 +02:00
|
|
|
const events = require('../events.js');
|
|
|
|
const views = require('../util/views.js');
|
|
|
|
|
2016-05-20 23:59:24 +02:00
|
|
|
function _formatUrl(url, page) {
|
|
|
|
return url.replace('{page}', page);
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:49:46 +02:00
|
|
|
class EndlessPageView {
|
|
|
|
constructor() {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._holderTemplate = views.getTemplate('endless-pager');
|
|
|
|
this._pageTemplate = views.getTemplate('endless-pager-page');
|
2016-04-12 23:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render(ctx) {
|
|
|
|
const target = document.getElementById('content-holder');
|
2016-05-20 21:35:12 +02:00
|
|
|
const source = this._holderTemplate();
|
2016-04-14 12:11:49 +02:00
|
|
|
const pageHeaderHolder = source.querySelector('.page-header-holder');
|
2016-06-12 21:01:18 +02:00
|
|
|
this._pagesHolder = source.querySelector('.pages-holder');
|
2016-05-11 21:29:57 +02:00
|
|
|
views.listenToMessages(source);
|
2016-04-12 23:49:46 +02:00
|
|
|
views.showView(target, source);
|
2016-05-20 21:35:12 +02:00
|
|
|
this._active = true;
|
|
|
|
this._working = 0;
|
2016-06-12 21:01:18 +02:00
|
|
|
this._init = true;
|
2016-04-12 23:49:46 +02:00
|
|
|
|
2016-06-11 09:59:29 +02:00
|
|
|
ctx.headerContext.target = pageHeaderHolder;
|
2016-06-12 14:04:42 +02:00
|
|
|
if (ctx.headerRenderer) {
|
|
|
|
ctx.headerRenderer.render(ctx.headerContext);
|
|
|
|
}
|
2016-04-14 12:11:49 +02:00
|
|
|
|
2016-06-12 21:01:18 +02:00
|
|
|
this.threshold = window.innerHeight / 3;
|
2016-05-09 11:17:04 +02:00
|
|
|
this.minPageShown = null;
|
|
|
|
this.maxPageShown = null;
|
|
|
|
this.totalPages = null;
|
|
|
|
this.currentPage = null;
|
2016-04-12 23:49:46 +02:00
|
|
|
|
2016-06-12 21:01:18 +02:00
|
|
|
this._loadPage(ctx, ctx.searchQuery.page, true);
|
2016-05-20 21:35:12 +02:00
|
|
|
window.addEventListener('unload', this._scrollToTop, true);
|
2016-06-12 21:01:18 +02:00
|
|
|
this._probePageLoad(ctx);
|
2016-04-12 23:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unrender() {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._active = false;
|
|
|
|
window.removeEventListener('unload', this._scrollToTop, true);
|
2016-04-12 23:49:46 +02:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
_scrollToTop() {
|
2016-05-09 11:17:04 +02:00
|
|
|
window.scroll(0, 0);
|
|
|
|
}
|
2016-04-12 23:49:46 +02:00
|
|
|
|
2016-06-12 21:01:18 +02:00
|
|
|
_probePageLoad(ctx) {
|
|
|
|
if (this._active) {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
this._probePageLoad(ctx);
|
|
|
|
});
|
|
|
|
}, 250);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._working) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let topPageNode = null;
|
|
|
|
let element = document.elementFromPoint(
|
|
|
|
window.innerWidth / 2,
|
|
|
|
window.innerHeight / 2);
|
|
|
|
while (element.parentNode !== null) {
|
|
|
|
if (element.classList.contains('page')) {
|
|
|
|
topPageNode = element;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
element = element.parentNode;
|
|
|
|
}
|
|
|
|
if (!topPageNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let topPageNumber = parseInt(topPageNode.getAttribute('data-page'));
|
|
|
|
if (topPageNumber !== this.currentPage) {
|
|
|
|
router.replace(
|
|
|
|
_formatUrl(ctx.clientUrl, topPageNumber),
|
|
|
|
{},
|
|
|
|
false);
|
|
|
|
this.currentPage = topPageNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.totalPages === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let scrollHeight =
|
|
|
|
document.documentElement.scrollHeight -
|
|
|
|
document.documentElement.clientHeight;
|
|
|
|
|
|
|
|
if (this.minPageShown > 1 && window.scrollY < this.threshold) {
|
|
|
|
this._loadPage(ctx, this.minPageShown - 1, false);
|
|
|
|
} else if (this.maxPageShown < this.totalPages &&
|
|
|
|
window.scrollY + this.threshold > scrollHeight) {
|
|
|
|
this._loadPage(ctx, this.maxPageShown + 1, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadPage(ctx, pageNumber, append) {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._working++;
|
2016-06-12 21:01:18 +02:00
|
|
|
ctx.requestPage(pageNumber).then(response => {
|
2016-05-20 21:35:12 +02:00
|
|
|
if (!this._active) {
|
|
|
|
this._working--;
|
2016-05-09 11:17:04 +02:00
|
|
|
return Promise.reject();
|
2016-04-17 09:24:05 +02:00
|
|
|
}
|
2016-04-12 23:49:46 +02:00
|
|
|
this.totalPages = Math.ceil(response.total / response.pageSize);
|
2016-06-12 21:01:18 +02:00
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
this._renderPage(
|
|
|
|
ctx, pageNumber, append, response);
|
2016-05-20 21:35:12 +02:00
|
|
|
this._working--;
|
2016-06-12 21:01:18 +02:00
|
|
|
});
|
2016-04-12 23:49:46 +02:00
|
|
|
}, response => {
|
|
|
|
events.notify(events.Error, response.description);
|
2016-05-20 21:35:12 +02:00
|
|
|
this._working--;
|
2016-04-12 23:49:46 +02:00
|
|
|
});
|
|
|
|
}
|
2016-06-12 21:01:18 +02:00
|
|
|
|
|
|
|
_renderPage(ctx, pageNumber, append, response) {
|
|
|
|
if (response.total) {
|
|
|
|
const pageNode = this._pageTemplate({
|
|
|
|
page: pageNumber,
|
|
|
|
totalPages: this.totalPages,
|
|
|
|
});
|
|
|
|
pageNode.setAttribute('data-page', pageNumber);
|
|
|
|
|
|
|
|
Object.assign(ctx.pageContext, response);
|
|
|
|
ctx.pageContext.target = pageNode.querySelector(
|
|
|
|
'.page-content-holder');
|
|
|
|
ctx.pageRenderer.render(ctx.pageContext);
|
|
|
|
|
|
|
|
if (pageNumber < this.minPageShown ||
|
|
|
|
this.minPageShown === null) {
|
|
|
|
this.minPageShown = pageNumber;
|
|
|
|
}
|
|
|
|
if (pageNumber > this.maxPageShown ||
|
|
|
|
this.maxPageShown === null) {
|
|
|
|
this.maxPageShown = pageNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (append) {
|
|
|
|
this._pagesHolder.appendChild(pageNode);
|
|
|
|
/*if (this._init && pageNumber !== 1) {
|
|
|
|
window.scroll(0, pageNode.getBoundingClientRect().top);
|
|
|
|
}*/
|
|
|
|
} else {
|
|
|
|
this._pagesHolder.prependChild(pageNode);
|
|
|
|
|
|
|
|
window.scroll(
|
|
|
|
window.scrollX,
|
|
|
|
window.scrollY + pageNode.offsetHeight);
|
|
|
|
}
|
|
|
|
} else if (response.total <= (pageNumber - 1) * response.pageSize) {
|
|
|
|
events.notify(events.Info, 'No data to show');
|
|
|
|
}
|
|
|
|
this._init = false;
|
|
|
|
}
|
2016-04-12 23:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EndlessPageView;
|