szurubooru/public_html/js/Presenters/PagerPresenter.js

269 lines
5.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,
2014-10-20 20:01:37 +02:00
progress) {
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 = !endlessScroll;
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();
pager.init({url: params.backendUri});
setQuery(params.query);
if (forceClear) {
clearContent();
}
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) {
setQuery(params.query);
if (forceClear) {
clearContent();
}
2014-10-02 00:30:25 +02:00
promise.wait(retrieve())
.then(loaded)
.fail(loaded);
if (!endlessScroll) {
keyboard.keydown('a', function() {
if (pager.prevPage()) {
syncUrl({page: pager.getPage()});
}
});
keyboard.keydown('d', function() {
if (pager.nextPage()) {
syncUrl({page: pager.getPage()});
}
});
}
}
2014-10-02 00:30:25 +02:00
function deinit() {
detachNextPageLoader();
}
function getUrl(options) {
return util.appendComplexRouteParam(
baseUri,
util.simplifySearchQuery(
_.extend(
{},
pager.getSearchParams(),
{page: pager.getPage()},
options)));
}
function syncUrl(options) {
router.navigate(getUrl(options));
}
function syncUrlInplace(options) {
router.navigateInplace(getUrl(options));
}
function retrieve() {
messagePresenter.hideMessages($messages);
2014-10-20 20:01:37 +02:00
progress.start();
2014-09-17 22:37:49 +02:00
return promise.make(function(resolve, reject) {
hidePageList();
2014-10-02 00:30:25 +02:00
promise.wait(pager.retrieve())
.then(function(response) {
2014-10-20 20:01:37 +02:00
progress.done();
if (forceClear) {
clearContent();
window.scrollTo(0, 0);
}
var $page = jQuery('<div class="page">');
if (endlessScroll && pager.getTotalPages() > 1) {
$page.append('<p>Page ' + pager.getPage() + ' of ' + pager.getTotalPages() + '</p>');
}
$page.append(targetContent);
$target.find('.pagination-content').append($page);
updateCallback($page, response);
refreshPageList();
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();
}
resolve();
}).fail(function(response) {
2014-10-20 20:01:37 +02:00
progress.done();
clearContent();
hidePageList();
messagePresenter.showError($messages, response.json && response.json.error || response);
reject();
});
2014-09-17 22:37:49 +02:00
});
}
function clearContent() {
detachNextPageLoader();
$target.find('.pagination-content').empty();
}
function attachNextPageLoader() {
if (!endlessScroll) {
return;
}
detachNextPageLoader();
scrollInterval = window.setInterval(function() {
var myScrollInterval = scrollInterval;
var baseLine = $target.offset().top + $target.innerHeight();
var scrollY = jQuery(window).scrollTop() + jQuery(window).height();
if (scrollY > baseLine) {
syncUrlInplace({page: pager.getPage() + 1});
window.clearInterval(myScrollInterval);
}
}, 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 $lastItem = $pageList.find('li:last-child');
var currentPage = pager.getPage();
var pages = pager.getVisiblePages();
$pageList.find('li.page').remove();
2014-09-18 17:54:31 +02:00
var lastPage = 0;
_.each(pages, function(page) {
2014-09-18 17:54:31 +02:00
if (page - lastPage > 1) {
jQuery('<li class="page ellipsis"><a>&hellip;</a></li>').insertBefore($lastItem);
2014-09-18 17:54:31 +02:00
}
lastPage = page;
var $a = jQuery('<a href="#"/>');
$a.click(function(e) {
e.preventDefault();
syncUrl({page: page});
});
$a.addClass('big-button');
$a.text(page);
if (page === currentPage) {
$a.addClass('active');
}
jQuery('<li class="page"/>').append($a).insertBefore($lastItem);
});
$pageList.find('li.next a').click(function(e) {
e.preventDefault();
if (currentPage + 1 < pages.length) {
syncUrl({page: currentPage + 1});
}
});
$pageList.find('li.prev a').click(function(e) {
e.preventDefault();
if (currentPage - 1 >= 1) {
syncUrl({page: currentPage - 1});
}
});
}
function render() {
$target.html(templates.pager());
$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;
2014-11-03 11:58:28 +01:00
query = _.extend({}, query);
delete query.page;
forceClear =
query.query !== pager.getSearchParams().query ||
query.order !== pager.getSearchParams().order ||
parseInt(page) !== pager.getPage() + 1 ||
!endlessScroll;
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-20 20:01:37 +02:00
App.DI.register('pagerPresenter', ['_', 'jQuery', 'util', 'promise', 'keyboard', 'router', 'pager', 'messagePresenter', 'browsingSettings', 'progress'], App.Presenters.PagerPresenter);