Fixed clicking "search" twice on endless scroll

This commit is contained in:
Marcin Kurczewski 2014-10-25 10:36:07 +02:00
parent 54f07f5a90
commit 55dbb67940

View file

@ -20,7 +20,7 @@ App.Presenters.PagerPresenter = function(
var endlessScroll = browsingSettings.getSettings().endlessScroll; var endlessScroll = browsingSettings.getSettings().endlessScroll;
var scrollInterval; var scrollInterval;
var templates = {}; var templates = {};
var forceClear = false; var forceClear = !endlessScroll;
var baseUri; var baseUri;
var updateCallback; var updateCallback;
@ -34,11 +34,11 @@ App.Presenters.PagerPresenter = function(
$target = params.$target; $target = params.$target;
targetContent = jQuery(params.$target).html(); targetContent = jQuery(params.$target).html();
pager.init({url: params.backendUri});
setQuery(params.query);
if (forceClear) { if (forceClear) {
clearContent(); clearContent();
} }
pager.init({url: params.backendUri});
setQuery(params.query);
promise.wait(util.promiseTemplate('pager')) promise.wait(util.promiseTemplate('pager'))
.then(function(template) { .then(function(template) {
@ -52,18 +52,18 @@ App.Presenters.PagerPresenter = function(
} }
function reinit(params, loaded) { function reinit(params, loaded) {
setQuery(params.query);
if (forceClear) { if (forceClear) {
clearContent(); clearContent();
} }
setQuery(params.query);
promise.wait(retrieve()) promise.wait(retrieve())
.then(loaded) .then(loaded)
.fail(loaded); .fail(loaded);
if (!endlessScroll) { if (!endlessScroll) {
keyboard.keydown('a', function() { pager.prevPage(); syncUrl(); }); keyboard.keydown('a', function() { syncUrl({page: pager.getPage() - 1}); });
keyboard.keydown('d', function() { pager.nextPage(); syncUrl(); }); keyboard.keydown('d', function() { syncUrl({page: pager.getPage() + 1}); });
} }
} }
@ -71,16 +71,22 @@ App.Presenters.PagerPresenter = function(
detachNextPageLoader(); detachNextPageLoader();
} }
function getUrl() { function getUrl(options) {
return util.appendComplexRouteParam(baseUri, _.extend({}, pager.getSearchParams(), {page: pager.getPage()})); return util.appendComplexRouteParam(
baseUri,
_.extend(
{},
pager.getSearchParams(),
{page: pager.getPage()},
options));
} }
function syncUrl() { function syncUrl(options) {
router.navigate(getUrl()); router.navigate(getUrl(options));
} }
function syncUrlInplace() { function syncUrlInplace(options) {
router.navigateInplace(getUrl()); router.navigateInplace(getUrl(options));
} }
function retrieve() { function retrieve() {
@ -92,7 +98,7 @@ App.Presenters.PagerPresenter = function(
.then(function(response) { .then(function(response) {
progress.done(); progress.done();
if (forceClear || !endlessScroll) { if (forceClear) {
clearContent(); clearContent();
window.scrollTo(0, 0); window.scrollTo(0, 0);
} }
@ -104,7 +110,7 @@ App.Presenters.PagerPresenter = function(
$target.find('.pagination-content').append($page); $target.find('.pagination-content').append($page);
updateCallback($page, response); updateCallback($page, response);
forceClear = false; refreshPageList();
if (!response.entities.length) { if (!response.entities.length) {
messagePresenter.showInfo($messages, 'No data to show'); messagePresenter.showInfo($messages, 'No data to show');
if (pager.getVisiblePages().length === 1) { if (pager.getVisiblePages().length === 1) {
@ -119,7 +125,7 @@ App.Presenters.PagerPresenter = function(
if (pager.getPage() < response.totalPages) { if (pager.getPage() < response.totalPages) {
attachNextPageLoader(); attachNextPageLoader();
} }
refreshPageList();
resolve(); resolve();
}).fail(function(response) { }).fail(function(response) {
progress.done(); progress.done();
@ -148,8 +154,7 @@ App.Presenters.PagerPresenter = function(
var baseLine = $target.offset().top + $target.innerHeight(); var baseLine = $target.offset().top + $target.innerHeight();
var scrollY = jQuery(window).scrollTop() + jQuery(window).height(); var scrollY = jQuery(window).scrollTop() + jQuery(window).height();
if (scrollY > baseLine) { if (scrollY > baseLine) {
pager.nextPage(); syncUrlInplace({page: pager.getPage() + 1});
syncUrlInplace();
window.clearInterval(myScrollInterval); window.clearInterval(myScrollInterval);
} }
}, 100); }, 100);
@ -180,8 +185,7 @@ App.Presenters.PagerPresenter = function(
var $a = jQuery('<a href="#"/>'); var $a = jQuery('<a href="#"/>');
$a.click(function(e) { $a.click(function(e) {
e.preventDefault(); e.preventDefault();
pager.setPage(page); syncUrl({page: page});
syncUrl();
}); });
$a.addClass('big-button'); $a.addClass('big-button');
$a.text(page); $a.text(page);
@ -215,7 +219,8 @@ App.Presenters.PagerPresenter = function(
forceClear = forceClear =
query.query !== pager.getSearchParams().query || query.query !== pager.getSearchParams().query ||
query.order !== pager.getSearchParams().order || query.order !== pager.getSearchParams().order ||
parseInt(page) !== pager.getPage(); parseInt(page) !== pager.getPage() + 1 ||
!endlessScroll;
pager.setSearchParams(query); pager.setSearchParams(query);
pager.setPage(page); pager.setPage(page);
} }