var App = App || {}; App.Presenters = App.Presenters || {}; App.Presenters.PagedCollectionPresenter = function( _, jQuery, util, promise, api, keyboard, router, presenterManager, browsingSettings) { var $target; var $pageList; var targetContent; var endlessScroll = browsingSettings.getSettings().endlessScroll; var scrollInterval; var template; var totalPages; var forceClear = false; var pageNumber; var searchParams; var baseUri; var backendUri; var updateCallback; var failCallback; function init(args, loaded) { $target = args.$target; targetContent = jQuery(args.$target).html(); baseUri = args.baseUri; backendUri = args.backendUri; updateCallback = args.updateCallback; failCallback = args.failCallback; promise.wait(util.promiseTemplate('pager')) .then(function(html) { template = _.template(html); render(); loaded(); }); } function reinit(args, loaded) { forceClear = !_.isEqual(args.searchParams, searchParams) || parseInt(args.page) !== pageNumber + 1; searchParams = args.searchParams; pageNumber = parseInt(args.page) || 1; softChangePage(pageNumber) .then(loaded) .fail(loaded); if (!endlessScroll) { keyboard.keydown('a', prevPage); keyboard.keydown('d', nextPage); } } function prevPage() { if (pageNumber > 1) { changePage(pageNumber - 1); } } function nextPage() { if (pageNumber < totalPages) { changePage(pageNumber + 1); } } function nextPageInplace() { if (pageNumber < totalPages) { changePageInplace(pageNumber + 1); } } function changePageInplace(newPageNumber) { router.navigateInplace(getPageChangeLink(newPageNumber)); } function changePage(newPageNumber) { router.navigate(getPageChangeLink(newPageNumber)); } function showSpinner() { if (endlessScroll) { $target.find('.spinner').show(); } else { presenterManager.showContentSpinner(); } } function hideSpinner() { if (endlessScroll) { $target.find('.spinner').hide(); } else { presenterManager.hideContentSpinner(); } } function softChangePage(newPageNumber) { pageNumber = newPageNumber; showSpinner(); return promise.make(function(resolve, reject) { promise.wait( api.get(backendUri, _.extend({}, searchParams, {page: pageNumber}))) .then(function(response) { resolve(response); var pageSize = response.json.pageSize; var totalRecords = response.json.totalRecords; totalPages = Math.ceil(totalRecords / pageSize); updateCallback({ entities: response.json.data, totalRecords: totalRecords}, forceClear || !endlessScroll); forceClear = false; refreshPageList(); hideSpinner(); attachNextPageLoader(); }).fail(function(response) { reject(response); if (typeof(failCallback) !== 'undefined') { failCallback(response); } else { console.log(new Error(response.json && response.json.error || response)); } }); }); } 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) { nextPageInplace(); window.clearInterval(scrollInterval); } }, 100); } function refreshPageList() { if (typeof(totalPages) === 'undefined') { return; } var pages = getVisiblePages(); $pageList.empty(); var lastPage = 0; _.each(pages, function(page) { if (page - lastPage > 1) { $pageList.append(jQuery('