var App = App || {}; App.Presenters = App.Presenters || {}; App.Presenters.PagerPresenter = function( _, jQuery, util, promise, keyboard, router, pager, messagePresenter, browsingSettings, nprogress) { var $target; var $pageList; var $messages; var targetContent; var endlessScroll = browsingSettings.getSettings().endlessScroll; var scrollInterval; 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); promise.wait(util.promiseTemplate('pager')) .then(function(template) { templates.pager = template; render(); loaded(); }).fail(function() { console.log(arguments); loaded(); }); } function reinit(params, loaded) { if (forceClear) { clearContent(); } setQuery(params.query); promise.wait(retrieve()) .then(loaded) .fail(loaded); if (!endlessScroll) { keyboard.keydown('a', function() { pager.prevPage(); syncUrl(); }); keyboard.keydown('d', function() { pager.nextPage(); syncUrl(); }); } } 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); nprogress.start(); return promise.make(function(resolve, reject) { 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(); nprogress.done(); resolve(); }).fail(function(response) { clearContent(); hidePageList(); nprogress.done(); messagePresenter.showError($messages, response.json && response.json.error || response); reject(); }); }); } function clearContent() { detachNextPageLoader(); updateCallback({entities: [], totalRecords: 0}, true); } 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) { pager.nextPage(); syncUrlInplace(); window.clearInterval(myScrollInterval); } }, 100); } function detachNextPageLoader() { window.clearInterval(scrollInterval); } function showPageList() { $pageList.show(); } function hidePageList() { $pageList.hide(); } function refreshPageList() { var pages = pager.getVisiblePages(); $pageList.empty(); var lastPage = 0; _.each(pages, function(page) { if (page - lastPage > 1) { $pageList.append(jQuery('