2014-09-03 19:07:53 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Presenters = App.Presenters || {};
|
|
|
|
|
2014-10-01 23:22:20 +02:00
|
|
|
App.Presenters.PagerPresenter = function(
|
2014-09-12 22:58:07 +02:00
|
|
|
_,
|
|
|
|
jQuery,
|
|
|
|
util,
|
|
|
|
promise,
|
|
|
|
api,
|
2014-09-19 17:37:10 +02:00
|
|
|
keyboard,
|
2014-09-12 22:58:07 +02:00
|
|
|
router,
|
2014-10-01 22:18:50 +02:00
|
|
|
pager,
|
2014-09-18 11:29:19 +02:00
|
|
|
presenterManager,
|
2014-09-12 22:58:07 +02:00
|
|
|
browsingSettings) {
|
|
|
|
|
2014-09-18 11:29:19 +02:00
|
|
|
var $target;
|
2014-09-18 17:54:31 +02:00
|
|
|
var $pageList;
|
2014-09-18 11:29:19 +02:00
|
|
|
var targetContent;
|
2014-09-12 22:58:07 +02:00
|
|
|
var endlessScroll = browsingSettings.getSettings().endlessScroll;
|
|
|
|
var scrollInterval;
|
|
|
|
var template;
|
2014-09-18 11:29:19 +02:00
|
|
|
var forceClear = false;
|
2014-09-03 19:07:53 +02:00
|
|
|
|
|
|
|
var baseUri;
|
2014-09-12 22:58:07 +02:00
|
|
|
var updateCallback;
|
2014-09-04 19:57:06 +02:00
|
|
|
var failCallback;
|
2014-09-03 19:07:53 +02:00
|
|
|
|
2014-09-17 22:37:49 +02:00
|
|
|
function init(args, loaded) {
|
2014-09-03 19:07:53 +02:00
|
|
|
baseUri = args.baseUri;
|
2014-09-12 22:58:07 +02:00
|
|
|
updateCallback = args.updateCallback;
|
2014-09-04 19:57:06 +02:00
|
|
|
failCallback = args.failCallback;
|
2014-09-03 19:07:53 +02:00
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
$target = args.$target;
|
|
|
|
targetContent = jQuery(args.$target).html();
|
|
|
|
|
|
|
|
pager.init({url: args.backendUri});
|
|
|
|
pager.setSearchParams(args.searchParams);
|
|
|
|
|
2014-09-17 22:37:49 +02:00
|
|
|
promise.wait(util.promiseTemplate('pager'))
|
|
|
|
.then(function(html) {
|
|
|
|
template = _.template(html);
|
2014-09-18 11:29:19 +02:00
|
|
|
render();
|
|
|
|
loaded();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function reinit(args, loaded) {
|
2014-10-01 22:18:50 +02:00
|
|
|
forceClear = !_.isEqual(args.searchParams, pager.getSearchParams()) || parseInt(args.page) !== pager.getPage();
|
2014-09-18 11:29:19 +02:00
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
pager.setSearchParams(args.searchParams);
|
|
|
|
pager.setPage(args.page || 1);
|
2014-09-18 11:29:19 +02:00
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
retrieve()
|
2014-09-18 11:29:19 +02:00
|
|
|
.then(loaded)
|
|
|
|
.fail(loaded);
|
|
|
|
|
|
|
|
if (!endlessScroll) {
|
2014-09-19 17:37:10 +02:00
|
|
|
keyboard.keydown('a', prevPage);
|
|
|
|
keyboard.keydown('d', nextPage);
|
2014-09-18 11:29:19 +02:00
|
|
|
}
|
2014-09-03 19:07:53 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 22:58:07 +02:00
|
|
|
function prevPage() {
|
2014-10-01 22:18:50 +02:00
|
|
|
pager.prevPage();
|
|
|
|
syncUrl();
|
2014-09-12 19:33:27 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 22:58:07 +02:00
|
|
|
function nextPage() {
|
2014-10-01 22:18:50 +02:00
|
|
|
pager.nextPage();
|
|
|
|
syncUrl();
|
2014-09-12 22:58:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function nextPageInplace() {
|
2014-10-01 22:18:50 +02:00
|
|
|
pager.nextPage();
|
|
|
|
syncUrlInplace();
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPage(newPage) {
|
|
|
|
pager.setPage(newPage);
|
|
|
|
syncUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
function setSearchParams(newSearchParams) {
|
|
|
|
clearContent();
|
|
|
|
pager.setSearchParams(newSearchParams);
|
|
|
|
syncUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUrl() {
|
|
|
|
return util.compileComplexRouteArgs(
|
|
|
|
baseUri,
|
|
|
|
_.extend(
|
|
|
|
{},
|
|
|
|
pager.getSearchParams(),
|
|
|
|
{page: pager.getPage()}));
|
2014-09-12 19:33:27 +02:00
|
|
|
}
|
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
function syncUrl() {
|
|
|
|
router.navigate(getUrl());
|
2014-09-12 22:58:07 +02:00
|
|
|
}
|
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
function syncUrlInplace() {
|
|
|
|
router.navigateInplace(getUrl());
|
2014-09-12 22:58:07 +02:00
|
|
|
}
|
|
|
|
|
2014-09-18 11:29:19 +02:00
|
|
|
function showSpinner() {
|
|
|
|
if (endlessScroll) {
|
|
|
|
$target.find('.spinner').show();
|
|
|
|
} else {
|
|
|
|
presenterManager.showContentSpinner();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function hideSpinner() {
|
|
|
|
if (endlessScroll) {
|
|
|
|
$target.find('.spinner').hide();
|
|
|
|
} else {
|
|
|
|
presenterManager.hideContentSpinner();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
function retrieve() {
|
2014-09-18 11:29:19 +02:00
|
|
|
showSpinner();
|
|
|
|
|
2014-09-17 22:37:49 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
2014-10-01 22:18:50 +02:00
|
|
|
pager.retrieve()
|
|
|
|
.then(function(response) {
|
|
|
|
updateCallback(response, forceClear || !endlessScroll);
|
|
|
|
forceClear = false;
|
|
|
|
|
|
|
|
refreshPageList();
|
|
|
|
hideSpinner();
|
|
|
|
attachNextPageLoader();
|
|
|
|
resolve();
|
|
|
|
}).fail(function(response) {
|
|
|
|
if (typeof(failCallback) !== 'undefined') {
|
|
|
|
failCallback(response);
|
|
|
|
} else {
|
|
|
|
console.log(new Error(response.json && response.json.error || response));
|
|
|
|
}
|
|
|
|
reject();
|
|
|
|
});
|
2014-09-17 22:37:49 +02:00
|
|
|
});
|
2014-09-03 19:07:53 +02:00
|
|
|
}
|
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
function clearContent() {
|
|
|
|
updateCallback({entities: [], totalRecords: 0}, true);
|
|
|
|
}
|
|
|
|
|
2014-09-18 11:29:19 +02:00
|
|
|
function attachNextPageLoader() {
|
|
|
|
if (!endlessScroll) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-18 13:06:43 +02:00
|
|
|
|
2014-09-18 11:29:19 +02:00
|
|
|
window.clearInterval(scrollInterval);
|
|
|
|
scrollInterval = window.setInterval(function() {
|
2014-09-18 13:06:43 +02:00
|
|
|
var baseLine = $target.offset().top + $target.innerHeight();
|
|
|
|
var scrollY = jQuery(window).scrollTop() + jQuery(window).height();
|
|
|
|
if (scrollY > baseLine) {
|
2014-09-18 11:29:19 +02:00
|
|
|
nextPageInplace();
|
|
|
|
window.clearInterval(scrollInterval);
|
|
|
|
}
|
2014-09-18 13:06:43 +02:00
|
|
|
}, 100);
|
2014-09-18 11:29:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function refreshPageList() {
|
2014-10-01 22:18:50 +02:00
|
|
|
var pages = pager.getVisiblePages();
|
2014-09-18 17:54:31 +02:00
|
|
|
$pageList.empty();
|
|
|
|
var lastPage = 0;
|
2014-09-18 11:29:19 +02:00
|
|
|
_.each(pages, function(page) {
|
2014-09-18 17:54:31 +02:00
|
|
|
if (page - lastPage > 1) {
|
|
|
|
$pageList.append(jQuery('<li><a>…</a></li>'));
|
|
|
|
}
|
|
|
|
lastPage = page;
|
|
|
|
|
2014-09-18 11:29:19 +02:00
|
|
|
var $a = jQuery('<a/>');
|
2014-10-01 22:18:50 +02:00
|
|
|
$a.click(function() {
|
|
|
|
setPage(page);
|
|
|
|
});
|
2014-09-18 11:29:19 +02:00
|
|
|
$a.addClass('big-button');
|
2014-10-01 22:18:50 +02:00
|
|
|
$a.attr('href', '#');
|
2014-09-18 11:29:19 +02:00
|
|
|
$a.text(page);
|
2014-10-01 22:18:50 +02:00
|
|
|
if (page === pager.getPage()) {
|
2014-09-18 11:29:19 +02:00
|
|
|
$a.addClass('active');
|
|
|
|
}
|
|
|
|
var $li = jQuery('<li/>');
|
|
|
|
$li.append($a);
|
2014-09-18 17:54:31 +02:00
|
|
|
$pageList.append($li);
|
2014-09-18 11:29:19 +02:00
|
|
|
});
|
|
|
|
}
|
2014-09-12 22:58:07 +02:00
|
|
|
|
2014-09-18 11:29:19 +02:00
|
|
|
function render() {
|
|
|
|
$target.html(template({originalHtml: targetContent}));
|
2014-09-18 17:54:31 +02:00
|
|
|
$pageList = $target.find('.page-list');
|
2014-09-18 11:29:19 +02:00
|
|
|
if (endlessScroll) {
|
2014-09-18 17:54:31 +02:00
|
|
|
$pageList.remove();
|
2014-09-12 22:58:07 +02:00
|
|
|
} else {
|
2014-09-18 11:29:19 +02:00
|
|
|
refreshPageList();
|
2014-09-12 22:58:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
return {
|
|
|
|
init: init,
|
2014-09-18 11:29:19 +02:00
|
|
|
reinit: reinit,
|
2014-10-01 22:18:50 +02:00
|
|
|
setPage: setPage,
|
|
|
|
setSearchParams: setSearchParams,
|
2014-09-03 19:07:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-10-01 23:22:20 +02:00
|
|
|
App.DI.register('pagerPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'keyboard', 'router', 'pager', 'presenterManager', 'browsingSettings'], App.Presenters.PagerPresenter);
|