From 6810b529c9b76f1da73bbb0adc25c3cc2b4d2bb2 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 27 Aug 2015 15:32:27 -0400 Subject: [PATCH] prevent infinite loop on topic scroll to bottom --- public/src/client/topic.js | 18 ++++++++++++++---- public/src/modules/navigator.js | 18 +++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 50a8ebe705..c1bd11c26c 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -121,7 +121,9 @@ define('forum/topic', [ var postIndex = getPostIndex(); if (postIndex && window.location.search.indexOf('page=') === -1) { - navigator.scrollToPost(postIndex - 1, true); + if (components.get('post/anchor', postIndex).length) { + return navigator.scrollToPostIndex(postIndex - 1, true); + } } else if (bookmark && (!config.usePagination || (config.usePagination && pagination.currentPage === 1)) && ajaxify.data.postcount > 1) { app.alert({ alert_id: 'bookmark', @@ -140,10 +142,18 @@ define('forum/topic', [ function getPostIndex() { var parts = window.location.pathname.split('/'); - if (parts[parts.length - 1] && utils.isNumber(parts[parts.length - 1])) { - return parseInt(parts[parts.length - 1], 10); + var lastPart = parts[parts.length - 1]; + if (lastPart && utils.isNumber(lastPart)) { + lastPart = parseInt(lastPart, 10); + } else { + return 0; } - return 0; + + while (lastPart > 0 && !components.get('post/anchor', lastPart).length) { + lastPart --; + } + + return lastPart; } function addBlockQuoteHandler() { diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.js index c1a0963f18..2d07b35e5d 100644 --- a/public/src/modules/navigator.js +++ b/public/src/modules/navigator.js @@ -175,18 +175,18 @@ define('navigator', ['forum/pagination', 'components'], function(pagination, com navigator.scrollActive = true; if (components.get('post/anchor', postIndex).length) { - return scrollToPid(postIndex, highlight, duration, offset); + return navigator.scrollToPostIndex(postIndex, highlight, duration, offset); } if (config.usePagination) { var page = Math.max(1, Math.ceil(postIndex / config.postsPerPage)); - if(parseInt(page, 10) !== pagination.currentPage) { + if (parseInt(page, 10) !== pagination.currentPage) { pagination.loadPage(page, function() { - scrollToPid(postIndex, highlight, duration, offset); + navigator.scrollToPostIndex(postIndex, highlight, duration, offset); }); } else { - scrollToPid(postIndex, highlight, duration, offset); + navigator.scrollToPostIndex(postIndex, highlight, duration, offset); } } else { navigator.scrollActive = false; @@ -195,14 +195,16 @@ define('navigator', ['forum/pagination', 'components'], function(pagination, com } }; - function scrollToPid(postIndex, highlight, duration, offset) { + navigator.scrollToPostIndex = function(postIndex, highlight, duration, offset) { var scrollTo = components.get('post/anchor', postIndex); if (!scrollTo.length) { navigator.scrollActive = false; return; } - + offset = offset || 0; + duration = duration !== undefined ? duration : 400; + navigator.scrollActive = true; var done = false; function animateScroll() { $('html, body').animate({ @@ -232,8 +234,10 @@ define('navigator', ['forum/pagination', 'components'], function(pagination, com if (components.get('topic').length) { animateScroll(); + } else { + navigator.scrollActive = false; } - } + }; return navigator;