diff --git a/public/src/client/infinitescroll.js b/public/src/client/infinitescroll.js index b4836f9a81..25607d5d6d 100644 --- a/public/src/client/infinitescroll.js +++ b/public/src/client/infinitescroll.js @@ -103,21 +103,21 @@ define('forum/infinitescroll', ['hooks', 'alerts'], function (hooks, alerts) { }; scroll.removeExtra = function (els, direction, count) { + let removedEls = $(); if (els.length <= count) { - return; + return removedEls; } const removeCount = els.length - count; if (direction > 0) { const height = $(document).height(); const scrollTop = $(window).scrollTop(); - - els.slice(0, removeCount).remove(); - + removedEls = els.slice(0, removeCount).remove(); $(window).scrollTop(scrollTop + ($(document).height() - height)); } else { - els.slice(els.length - removeCount).remove(); + removedEls = els.slice(els.length - removeCount).remove(); } + return removedEls; }; return scroll; diff --git a/public/src/client/topic/posts.js b/public/src/client/topic/posts.js index 124ccdd3e5..4dfe7c181f 100644 --- a/public/src/client/topic/posts.js +++ b/public/src/client/topic/posts.js @@ -230,7 +230,8 @@ define('forum/topic/posts', [ components.get('topic').append(html); } - infinitescroll.removeExtra($('[component="post"]'), direction, Math.max(20, config.postsPerPage * 2)); + const removedEls = infinitescroll.removeExtra($('[component="post"]'), direction, Math.max(20, config.postsPerPage * 2)); + removeNecroPostMessages(removedEls); hooks.fire('action:posts.loaded', { posts: data.posts }); @@ -309,14 +310,14 @@ define('forum/topic/posts', [ }); }; - function addNecroPostMessage(callback) { + function addNecroPostMessage() { const necroThreshold = ajaxify.data.necroThreshold * 24 * 60 * 60 * 1000; if (!necroThreshold || (config.topicPostSort !== 'newest_to_oldest' && config.topicPostSort !== 'oldest_to_newest')) { - return callback && callback(); + return; } const postEls = $('[component="post"]').toArray(); - Promise.all(postEls.map(function (post) { + postEls.forEach(function (post) { post = $(post); const prev = post.prev('[component="post"]'); if (post.is(':has(.necro-post)') || !prev.length) { @@ -327,39 +328,37 @@ define('forum/topic/posts', [ } const diff = post.attr('data-timestamp') - prev.attr('data-timestamp'); - return new Promise(function (resolve) { - if (Math.abs(diff) >= necroThreshold) { - const suffixAgo = $.timeago.settings.strings.suffixAgo; - const prefixAgo = $.timeago.settings.strings.prefixAgo; - const suffixFromNow = $.timeago.settings.strings.suffixFromNow; - const prefixFromNow = $.timeago.settings.strings.prefixFromNow; - - $.timeago.settings.strings.suffixAgo = ''; - $.timeago.settings.strings.prefixAgo = ''; - $.timeago.settings.strings.suffixFromNow = ''; - $.timeago.settings.strings.prefixFromNow = ''; - - const translationText = (diff > 0 ? '[[topic:timeago_later,' : '[[topic:timeago_earlier,') + $.timeago.inWords(diff) + ']]'; - - $.timeago.settings.strings.suffixAgo = suffixAgo; - $.timeago.settings.strings.prefixAgo = prefixAgo; - $.timeago.settings.strings.suffixFromNow = suffixFromNow; - $.timeago.settings.strings.prefixFromNow = prefixFromNow; - app.parseAndTranslate('partials/topic/necro-post', { text: translationText }, function (html) { - html.insertBefore(post); - resolve(); - }); - } else { - resolve(); - } - }); - })).then(function () { - if (typeof callback === 'function') { - callback(); + if (Math.abs(diff) >= necroThreshold) { + const suffixAgo = $.timeago.settings.strings.suffixAgo; + const prefixAgo = $.timeago.settings.strings.prefixAgo; + const suffixFromNow = $.timeago.settings.strings.suffixFromNow; + const prefixFromNow = $.timeago.settings.strings.prefixFromNow; + + $.timeago.settings.strings.suffixAgo = ''; + $.timeago.settings.strings.prefixAgo = ''; + $.timeago.settings.strings.suffixFromNow = ''; + $.timeago.settings.strings.prefixFromNow = ''; + + const translationText = (diff > 0 ? '[[topic:timeago_later,' : '[[topic:timeago_earlier,') + $.timeago.inWords(diff) + ']]'; + + $.timeago.settings.strings.suffixAgo = suffixAgo; + $.timeago.settings.strings.prefixAgo = prefixAgo; + $.timeago.settings.strings.suffixFromNow = suffixFromNow; + $.timeago.settings.strings.prefixFromNow = prefixFromNow; + app.parseAndTranslate('partials/topic/necro-post', { text: translationText }, function (html) { + html.attr('data-necro-post-index', prev.attr('data-index')); + html.insertBefore(post); + }); } }); } + function removeNecroPostMessages(removedPostEls) { + removedPostEls.each((index, el) => { + $(`[data-necro-post-index="${$(el).attr('data-index')}"]`).remove(); + }); + } + function handlePrivateUploads(posts) { if (app.user.uid || !ajaxify.data.privateUploads) { return;