diff --git a/public/src/client/topic.js b/public/src/client/topic.js index d85b079632..2ea69a9dfd 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -61,7 +61,7 @@ define('forum/topic', [ handleKeys(); - navigator.init('[component="post"]', ajaxify.data.postcount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback, Topic.calculateIndex); + navigator.init('[component="post/anchor"]', ajaxify.data.postcount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback, Topic.calculateIndex); $(window).on('scroll', updateTopicTitle); @@ -219,43 +219,30 @@ define('forum/topic', [ return index; }; - Topic.navigatorCallback = function(topPostIndex, bottomPostIndex, elementCount) { + Topic.navigatorCallback = function(index, elementCount) { var path = ajaxify.removeRelativePath(window.location.pathname.slice(1)); if (!path.startsWith('topic')) { return 1; } - var postIndex = topPostIndex; - var index = bottomPostIndex; - if (config.topicPostSort !== 'oldest_to_newest') { - if (bottomPostIndex === 0) { - index = 1; - } else { - index = Math.max(elementCount - bottomPostIndex + 2, 1); - } - } var bookmarkKey = 'topic:' + ajaxify.data.tid + ':bookmark'; var currentBookmark = ajaxify.data.bookmark || localStorage.getItem(bookmarkKey); - if (!currentBookmark || parseInt(postIndex, 10) > parseInt(currentBookmark, 10)) { + if (!currentBookmark || parseInt(index, 10) > parseInt(currentBookmark, 10)) { if (app.user.uid) { - var payload = { + socket.emit('topics.bookmark', { 'tid': ajaxify.data.tid, - 'index': postIndex - }; - socket.emit('topics.bookmark', payload, function(err) { - if (err) { - console.warn('Error saving bookmark:', err); - } - ajaxify.data.bookmark = postIndex; + 'index': index + }, function(err) { + ajaxify.data.bookmark = index; }); } else { - localStorage.setItem(bookmarkKey, postIndex); + localStorage.setItem(bookmarkKey, index); } } // removes the bookmark alert when we get to / past the bookmark - if (!currentBookmark || parseInt(postIndex, 10) >= parseInt(currentBookmark, 10)) { + if (!currentBookmark || parseInt(index, 10) >= parseInt(currentBookmark, 10)) { app.removeAlert('bookmark'); } @@ -264,14 +251,15 @@ define('forum/topic', [ var topicId = parts[1], slug = parts[2]; var newUrl = 'topic/' + topicId + '/' + (slug ? slug : ''); - if (postIndex > 1) { - newUrl += '/' + postIndex; + if (index > 1) { + newUrl += '/' + index; } if (newUrl !== currentUrl) { if (Topic.replaceURLTimeout) { clearTimeout(Topic.replaceURLTimeout); } + Topic.replaceURLTimeout = setTimeout(function() { Topic.replaceURLTimeout = 0; if (history.replaceState) { @@ -284,7 +272,6 @@ define('forum/topic', [ }, 500); } } - return index; }; diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.js index 5cc1092f37..174e4838c1 100644 --- a/public/src/modules/navigator.js +++ b/public/src/modules/navigator.js @@ -90,28 +90,21 @@ define('navigator', ['forum/pagination', 'components'], function(pagination, com navigator.update = function() { toggle(!!count); - var topIndex = 0; - var bottomIndex = 0; + var middleOfViewport = $(window).scrollTop() + $(window).height() / 2; + + index = 0; + $(navigator.selector).each(function() { var el = $(this); - if (elementInView(el)) { - if (!topIndex) { - topIndex = parseInt(el.attr('data-index'), 10) + 1; - } else { - bottomIndex = parseInt(el.attr('data-index'), 10) + 1; - } - } else if (topIndex && bottomIndex) { + index++; + if (el.offset().top > middleOfViewport) { return false; } }); - if (topIndex && !bottomIndex) { - bottomIndex = topIndex; - } - - if (typeof navigator.callback === 'function' && topIndex && bottomIndex) { - index = navigator.callback(topIndex, bottomIndex, count); + if (typeof navigator.callback === 'function') { + navigator.callback(index, count); navigator.updateTextAndProgressBar(); } }; @@ -155,16 +148,6 @@ define('navigator', ['forum/pagination', 'components'], function(pagination, com } }; - function elementInView(el) { - var scrollTop = $(window).scrollTop() + $('#header-menu').height(); - var scrollBottom = scrollTop + $(window).height(); - - var elTop = el.offset().top; - var elBottom = elTop + Math.floor(el.height()); - - return (elTop >= scrollTop && elBottom < scrollBottom) || (elTop < scrollTop && elBottom > scrollTop); - } - navigator.scrollToPost = function(postIndex, highlight, duration, offset) { if (!utils.isNumber(postIndex) || !components.get('topic').length) { return;