szurubooru/public_html/js/Presenters/PagerPresenter.js

226 lines
4.8 KiB
JavaScript
Raw Normal View History

var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.PagerPresenter = function(
_,
jQuery,
util,
promise,
2014-09-19 17:37:10 +02:00
keyboard,
router,
pager,
messagePresenter,
2014-10-18 18:06:22 +02:00
browsingSettings,
nprogress) {
var $target;
2014-09-18 17:54:31 +02:00
var $pageList;
var $messages;
var targetContent;
var endlessScroll = browsingSettings.getSettings().endlessScroll;
var scrollInterval;
2014-10-05 10:41:12 +02:00
var templates = {};
var forceClear = false;
var baseUri;
var updateCallback;
function init(params, loaded) {
baseUri = params.baseUri;
updateCallback = params.updateCallback;
messagePresenter.instant = true;
$target = params.$target;
targetContent = jQuery(params.$target).html();
if (forceClear) {
clearContent();
}
pager.init({url: params.backendUri});
setQuery(params.query);
2014-09-17 22:37:49 +02:00
promise.wait(util.promiseTemplate('pager'))
2014-10-05 10:41:12 +02:00
.then(function(template) {
templates.pager = template;
render();
loaded();
}).fail(function() {
console.log(arguments);
loaded();
});
}
function reinit(params, loaded) {
window.clearInterval(scrollInterval);
if (forceClear) {
clearContent();
}
setQuery(params.query);
2014-10-02 00:30:25 +02:00
promise.wait(retrieve())
.then(loaded)
.fail(loaded);
if (!endlessScroll) {
keyboard.keydown('a', function() { pager.prevPage(); syncUrl(); });
keyboard.keydown('d', function() { pager.nextPage(); syncUrl(); });
}
}
2014-10-02 00:30:25 +02:00
function deinit() {
detachNextPageLoader();
}
function getUrl() {
return util.appendComplexRouteParam(baseUri, _.extend({}, pager.getSearchParams(), {page: pager.getPage()}));
}
function syncUrl() {
router.navigate(getUrl());
}
function syncUrlInplace() {
router.navigateInplace(getUrl());
}
function retrieve() {
messagePresenter.hideMessages($messages);
2014-10-18 18:06:22 +02:00
nprogress.start();
2014-09-17 22:37:49 +02:00
return promise.make(function(resolve, reject) {
2014-10-02 00:30:25 +02:00
promise.wait(pager.retrieve())
.then(function(response) {
updateCallback(response, forceClear || !endlessScroll);
forceClear = false;
if (!response.entities.length) {
messagePresenter.showInfo($messages, 'No data to show');
if (pager.getVisiblePages().length === 1) {
hidePageList();
} else {
showPageList();
}
} else {
showPageList();
}
if (pager.getPage() < response.totalPages) {
attachNextPageLoader();
}
refreshPageList();
2014-10-18 18:06:22 +02:00
nprogress.done();
resolve();
}).fail(function(response) {
clearContent();
hidePageList();
2014-10-18 18:06:22 +02:00
nprogress.done();
messagePresenter.showError($messages, response.json && response.json.error || response);
reject();
});
2014-09-17 22:37:49 +02:00
});
}
function clearContent() {
updateCallback({entities: [], totalRecords: 0}, true);
}
function attachNextPageLoader() {
if (!endlessScroll) {
return;
}
window.clearInterval(scrollInterval);
scrollInterval = window.setInterval(function() {
var baseLine = $target.offset().top + $target.innerHeight();
var scrollY = jQuery(window).scrollTop() + jQuery(window).height();
if (scrollY > baseLine) {
pager.nextPage();
syncUrlInplace();
window.clearInterval(scrollInterval);
}
}, 100);
}
2014-10-02 00:30:25 +02:00
function detachNextPageLoader() {
window.clearInterval(scrollInterval);
}
function showPageList() {
$pageList.show();
}
function hidePageList() {
$pageList.hide();
}
function refreshPageList() {
var pages = pager.getVisiblePages();
2014-09-18 17:54:31 +02:00
$pageList.empty();
var lastPage = 0;
_.each(pages, function(page) {
2014-09-18 17:54:31 +02:00
if (page - lastPage > 1) {
$pageList.append(jQuery('<li><a>&hellip;</a></li>'));
}
lastPage = page;
var $a = jQuery('<a href="#"/>');
$a.click(function(e) {
e.preventDefault();
pager.setPage(page);
syncUrl();
});
$a.addClass('big-button');
$a.text(page);
if (page === pager.getPage()) {
$a.addClass('active');
}
var $li = jQuery('<li/>');
$li.append($a);
2014-09-18 17:54:31 +02:00
$pageList.append($li);
});
}
function render() {
2014-10-05 10:41:12 +02:00
$target.html(templates.pager({originalHtml: targetContent}));
$messages = $target.find('.pagination-content');
2014-09-18 17:54:31 +02:00
$pageList = $target.find('.page-list');
if (endlessScroll) {
2014-09-18 17:54:31 +02:00
$pageList.remove();
} else {
refreshPageList();
}
}
function setQuery(query) {
if (!query) {
return;
}
query.page = parseInt(query.page) || 1;
var page = query.page;
delete query.page;
forceClear =
query.query !== pager.getSearchParams().query ||
query.order !== pager.getSearchParams().order ||
parseInt(page) !== pager.getPage();
pager.setSearchParams(query);
pager.setPage(page);
}
function setQueryAndSyncUrl(query) {
setQuery(query);
syncUrl();
}
return {
init: init,
reinit: reinit,
2014-10-02 00:30:25 +02:00
deinit: deinit,
syncUrl: syncUrl,
setQuery: setQueryAndSyncUrl,
};
};
2014-10-18 18:06:22 +02:00
App.DI.register('pagerPresenter', ['_', 'jQuery', 'util', 'promise', 'keyboard', 'router', 'pager', 'messagePresenter', 'browsingSettings', 'nprogress'], App.Presenters.PagerPresenter);