szurubooru/client/js/views/endless_page_view.js

161 lines
5.6 KiB
JavaScript
Raw Normal View History

2016-04-12 23:49:46 +02:00
'use strict';
const page = require('page');
2016-04-12 23:49:46 +02:00
const events = require('../events.js');
const views = require('../util/views.js');
function _formatUrl(url, page) {
return url.replace('{page}', page);
}
2016-04-12 23:49:46 +02:00
class EndlessPageView {
constructor() {
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');
const source = this._holderTemplate();
2016-04-14 12:11:49 +02:00
const pageHeaderHolder = source.querySelector('.page-header-holder');
2016-04-12 23:49:46 +02:00
const pagesHolder = source.querySelector('.pages-holder');
views.listenToMessages(source);
2016-04-12 23:49:46 +02:00
views.showView(target, source);
this._active = true;
this._working = 0;
2016-04-12 23:49:46 +02:00
2016-04-14 12:11:49 +02:00
let headerRendererCtx = ctx;
headerRendererCtx.target = pageHeaderHolder;
ctx.headerRenderer.render(headerRendererCtx);
2016-04-12 23:49:46 +02:00
const threshold = window.innerHeight / 3;
this.minPageShown = null;
this.maxPageShown = null;
this.totalPages = null;
this.currentPage = null;
2016-04-12 23:49:46 +02:00
this._updater = () => {
if (this._working) {
return;
}
let topPageNode = null;
let element = document.elementFromPoint(window.innerWidth / 2, 1);
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) {
page.replace(
_formatUrl(ctx.clientUrl, topPageNumber),
null,
false,
false);
this.currentPage = topPageNumber;
}
if (this.totalPages === null) {
2016-04-12 23:49:46 +02:00
return;
}
let scrollHeight =
document.documentElement.scrollHeight -
document.documentElement.clientHeight;
if (this.minPageShown > 1 && window.scrollY - threshold < 0) {
this._loadPage(pagesHolder, ctx, this.minPageShown - 1, false)
.then(() => this._updater());
2016-04-12 23:49:46 +02:00
} else if (this.maxPageShown < this.totalPages &&
window.scrollY + threshold > scrollHeight) {
this._loadPage(pagesHolder, ctx, this.maxPageShown + 1, true)
.then(() => this._updater());
2016-04-12 23:49:46 +02:00
}
};
this._loadPage(pagesHolder, ctx, ctx.searchQuery.page, true)
.then(pageNode => {
if (ctx.searchQuery.page > 1) {
window.scroll(0, pageNode.getBoundingClientRect().top);
}
this._updater();
});
window.addEventListener('scroll', this._updater, true);
window.addEventListener('unload', this._scrollToTop, true);
2016-04-12 23:49:46 +02:00
}
unrender() {
this._active = false;
window.removeEventListener('scroll', this._updater, true);
window.removeEventListener('unload', this._scrollToTop, true);
2016-04-12 23:49:46 +02:00
}
_scrollToTop() {
window.scroll(0, 0);
}
2016-04-12 23:49:46 +02:00
_loadPage(pagesHolder, ctx, pageNumber, append) {
this._working++;
return ctx.requestPage(pageNumber).then(response => {
if (!this._active) {
this._working--;
return Promise.reject();
}
2016-04-12 23:49:46 +02:00
this.totalPages = Math.ceil(response.total / response.pageSize);
if (response.total) {
const pageNode = this._pageTemplate({
page: pageNumber,
2016-04-12 23:49:46 +02:00
totalPages: this.totalPages,
});
pageNode.setAttribute('data-page', pageNumber);
2016-04-12 23:49:46 +02:00
let pageRendererCtx = response;
pageRendererCtx.target = pageNode.querySelector(
2016-04-12 23:49:46 +02:00
'.page-content-holder');
ctx.pageRenderer.render(pageRendererCtx);
2016-05-10 14:13:24 +02:00
if (pageNumber < this.minPageShown ||
this.minPageShown === null) {
this.minPageShown = pageNumber;
}
2016-05-10 14:13:24 +02:00
if (pageNumber > this.maxPageShown ||
this.maxPageShown === null) {
this.maxPageShown = pageNumber;
}
2016-04-12 23:49:46 +02:00
if (append) {
pagesHolder.appendChild(pageNode);
2016-04-12 23:49:46 +02:00
} else {
pagesHolder.prependChild(pageNode);
// note: with probability of 75%, if the user has scrolled
// with a mouse wheel, chrome 49 doesn't give a slightest
// fuck about this and loads all the way to page 1 at once
2016-04-12 23:49:46 +02:00
window.scroll(
window.scrollX,
window.scrollY + pageNode.offsetHeight);
2016-04-12 23:49:46 +02:00
}
this._working--;
return Promise.resolve(pageNode);
2016-04-12 23:49:46 +02:00
}
if (response.total <= (pageNumber - 1) * response.pageSize) {
2016-04-12 23:49:46 +02:00
events.notify(events.Info, 'No data to show');
}
this._working--;
return Promise.reject();
2016-04-12 23:49:46 +02:00
}, response => {
events.notify(events.Error, response.description);
this._working--;
return Promise.reject();
2016-04-12 23:49:46 +02:00
});
}
}
module.exports = EndlessPageView;