From cbd98c1b284d97287622c45b4c10809fc402eeec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 22 Jun 2023 21:16:11 -0400 Subject: [PATCH] fix: closes #11731, set postIndex on pagination --- public/src/ajaxify.js | 8 ++++---- public/src/client/topic.js | 6 ++---- public/src/modules/navigator.js | 8 ++++++++ src/controllers/topics.js | 8 ++++++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index 17b3405ae2..8111f0ef09 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -303,11 +303,11 @@ ajaxify.widgets = { render: render }; window.scrollTo(0, 0); // if on topic page, scroll to the correct post, // this is here to avoid a flash of the wrong posts at the top of the page - if (ajaxify.data.template.topic && ajaxify.data.postIndex > 1) { - require(['navigator'], function (navigator) { + require(['navigator'], function (navigator) { + if (navigator.shouldScrollToPost(ajaxify.data.postIndex)) { navigator.scrollToPostIndex(ajaxify.data.postIndex - 1, true, 0); - }); - } + } + }); } ajaxify.loadScript(tpl_url, function done() { hooks.fire('action:ajaxify.end', { url: url, tpl_url: tpl_url, title: ajaxify.data.title }); diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 9df1e122d9..7a0b228efd 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -151,10 +151,8 @@ define('forum/topic', [ const bookmark = ajaxify.data.bookmark || storage.getItem('topic:' + tid + ':bookmark'); const postIndex = ajaxify.data.postIndex; updateUserBookmark(postIndex); - if (postIndex > 1) { - if (components.get('post/anchor', postIndex - 1).length) { - return navigator.scrollToPostIndex(postIndex - 1, true, 0); - } + if (navigator.shouldScrollToPost(postIndex)) { + return navigator.scrollToPostIndex(postIndex - 1, true, 0); } else if (bookmark && ( !config.usePagination || (config.usePagination && ajaxify.data.pagination.currentPage === 1) diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.js index 1953e4d009..36b3d61470 100644 --- a/public/src/modules/navigator.js +++ b/public/src/modules/navigator.js @@ -642,6 +642,14 @@ define('navigator', [ } }; + navigator.shouldScrollToPost = function (postIndex) { + if (!ajaxify.data.template.topic || postIndex <= 1) { + return false; + } + const firstPostEl = $('[component="topic"] [component="post"]').first(); + return parseInt(firstPostEl.attr('data-index'), 10) !== postIndex - 1; + }; + navigator.scrollToPostIndex = function (postIndex, highlight, duration) { const scrollTo = components.get('post', 'index', postIndex); navigator.scrollToElement(scrollTo, highlight, duration, postIndex); diff --git a/src/controllers/topics.js b/src/controllers/topics.js index 7840c35277..3fa41c4ee8 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -72,8 +72,12 @@ topicsController.get = async function getTopic(req, res, next) { const sort = req.query.sort || settings.topicPostSort; const set = sort === 'most_votes' ? `tid:${tid}:posts:votes` : `tid:${tid}:posts`; const reverse = sort === 'newest_to_oldest' || sort === 'most_votes'; - if (settings.usePagination && !req.query.page) { - currentPage = calculatePageFromIndex(postIndex, settings); + if (settings.usePagination) { + if (!req.query.page) { + currentPage = calculatePageFromIndex(postIndex, settings); + } else { + postIndex = ((currentPage - 1) * settings.postsPerPage) + 1; + } } const { start, stop } = calculateStartStop(currentPage, postIndex, settings);