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-10-01 23:43:39 +02:00
|
|
|
messagePresenter,
|
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-10-01 23:43:39 +02:00
|
|
|
var $messages;
|
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;
|
2014-10-05 10:41:12 +02:00
|
|
|
var templates = {};
|
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-03 19:07:53 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
function init(params, loaded) {
|
|
|
|
baseUri = params.baseUri;
|
|
|
|
updateCallback = params.updateCallback;
|
2014-10-01 23:43:39 +02:00
|
|
|
|
|
|
|
messagePresenter.instant = true;
|
2014-09-03 19:07:53 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
$target = params.$target;
|
|
|
|
targetContent = jQuery(params.$target).html();
|
2014-10-01 22:18:50 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
if (forceClear) {
|
|
|
|
clearContent();
|
|
|
|
}
|
|
|
|
pager.init({url: params.backendUri});
|
|
|
|
setQuery(params.query);
|
2014-10-01 22:18:50 +02:00
|
|
|
|
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;
|
2014-09-18 11:29:19 +02:00
|
|
|
render();
|
|
|
|
loaded();
|
2014-10-10 18:13:46 +02:00
|
|
|
}).fail(function() {
|
|
|
|
console.log(arguments);
|
|
|
|
loaded();
|
2014-09-18 11:29:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
function reinit(params, loaded) {
|
2014-10-18 09:58:06 +02:00
|
|
|
window.clearInterval(scrollInterval);
|
2014-10-09 21:41:46 +02:00
|
|
|
if (forceClear) {
|
|
|
|
clearContent();
|
|
|
|
}
|
|
|
|
setQuery(params.query);
|
2014-09-18 11:29:19 +02:00
|
|
|
|
2014-10-02 00:30:25 +02:00
|
|
|
promise.wait(retrieve())
|
2014-09-18 11:29:19 +02:00
|
|
|
.then(loaded)
|
|
|
|
.fail(loaded);
|
|
|
|
|
|
|
|
if (!endlessScroll) {
|
2014-10-09 21:41:46 +02:00
|
|
|
keyboard.keydown('a', function() { pager.prevPage(); syncUrl(); });
|
|
|
|
keyboard.keydown('d', function() { pager.nextPage(); syncUrl(); });
|
2014-09-18 11:29:19 +02:00
|
|
|
}
|
2014-09-03 19:07:53 +02:00
|
|
|
}
|
|
|
|
|
2014-10-02 00:30:25 +02:00
|
|
|
function deinit() {
|
|
|
|
detachNextPageLoader();
|
|
|
|
}
|
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
function getUrl() {
|
2014-10-09 21:41:46 +02:00
|
|
|
return util.appendComplexRouteParam(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-10-01 23:43:39 +02:00
|
|
|
messagePresenter.hideMessages($messages);
|
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-02 00:30:25 +02:00
|
|
|
promise.wait(pager.retrieve())
|
2014-10-01 22:18:50 +02:00
|
|
|
.then(function(response) {
|
|
|
|
updateCallback(response, forceClear || !endlessScroll);
|
|
|
|
forceClear = false;
|
2014-10-01 23:50:20 +02:00
|
|
|
if (!response.entities.length) {
|
|
|
|
messagePresenter.showInfo($messages, 'No data to show');
|
|
|
|
if (pager.getVisiblePages().length === 1) {
|
|
|
|
hidePageList();
|
|
|
|
} else {
|
|
|
|
showPageList();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
showPageList();
|
|
|
|
}
|
2014-10-01 22:18:50 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
if (pager.getPage() < response.totalPages) {
|
|
|
|
attachNextPageLoader();
|
|
|
|
}
|
2014-10-01 22:18:50 +02:00
|
|
|
refreshPageList();
|
|
|
|
hideSpinner();
|
|
|
|
resolve();
|
|
|
|
}).fail(function(response) {
|
2014-10-01 23:43:39 +02:00
|
|
|
clearContent();
|
|
|
|
hidePageList();
|
|
|
|
hideSpinner();
|
|
|
|
messagePresenter.showError($messages, response.json && response.json.error || response);
|
|
|
|
|
2014-10-01 22:18:50 +02:00
|
|
|
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-10-09 21:41:46 +02:00
|
|
|
pager.nextPage();
|
|
|
|
syncUrlInplace();
|
2014-09-18 11:29:19 +02:00
|
|
|
window.clearInterval(scrollInterval);
|
|
|
|
}
|
2014-09-18 13:06:43 +02:00
|
|
|
}, 100);
|
2014-09-18 11:29:19 +02:00
|
|
|
}
|
|
|
|
|
2014-10-02 00:30:25 +02:00
|
|
|
function detachNextPageLoader() {
|
|
|
|
window.clearInterval(scrollInterval);
|
|
|
|
}
|
|
|
|
|
2014-10-01 23:43:39 +02:00
|
|
|
function showPageList() {
|
|
|
|
$pageList.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
function hidePageList() {
|
|
|
|
$pageList.hide();
|
|
|
|
}
|
|
|
|
|
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-10-11 23:24:03 +02:00
|
|
|
var $a = jQuery('<a href="#"/>');
|
|
|
|
$a.click(function(e) {
|
|
|
|
e.preventDefault();
|
2014-10-09 21:41:46 +02:00
|
|
|
pager.setPage(page);
|
|
|
|
syncUrl();
|
2014-10-01 22:18:50 +02:00
|
|
|
});
|
2014-09-18 11:29:19 +02:00
|
|
|
$a.addClass('big-button');
|
|
|
|
$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() {
|
2014-10-05 10:41:12 +02:00
|
|
|
$target.html(templates.pager({originalHtml: targetContent}));
|
2014-10-01 23:43:39 +02:00
|
|
|
$messages = $target.find('.pagination-content');
|
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-10-09 21:41:46 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
return {
|
|
|
|
init: init,
|
2014-09-18 11:29:19 +02:00
|
|
|
reinit: reinit,
|
2014-10-02 00:30:25 +02:00
|
|
|
deinit: deinit,
|
2014-10-09 21:41:46 +02:00
|
|
|
syncUrl: syncUrl,
|
|
|
|
setQuery: setQueryAndSyncUrl,
|
2014-09-03 19:07:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-10-01 23:43:39 +02:00
|
|
|
App.DI.register('pagerPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'keyboard', 'router', 'pager', 'presenterManager', 'messagePresenter', 'browsingSettings'], App.Presenters.PagerPresenter);
|