From e8c17feedba203ce1a93017661c4c0587dc8c22b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 28 Oct 2021 12:00:51 -0400 Subject: [PATCH] refactor: use utils.debounce --- public/src/client/topic.js | 55 +++++++++++++------------------------- 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 944bd9ad2a..6fe0310dd6 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -7,7 +7,6 @@ define('forum/topic', [ 'forum/topic/postTools', 'forum/topic/events', 'forum/topic/posts', - 'forum/topic/images', 'navigator', 'sort', 'components', @@ -15,17 +14,13 @@ define('forum/topic', [ 'hooks', ], function ( infinitescroll, threadTools, postTools, - events, posts, images, navigator, sort, + events, posts, navigator, sort, components, storage, hooks ) { - const Topic = {}; + const Topic = {}; let currentUrl = ''; $(window).on('action:ajaxify.start', function (ev, data) { - if (Topic.replaceURLTimeout) { - clearTimeout(Topic.replaceURLTimeout); - Topic.replaceURLTimeout = 0; - } events.removeListeners(); if (!String(data.url).startsWith('topic/')) { @@ -44,7 +39,7 @@ define('forum/topic', [ posts.onTopicPageLoad(components.get('post')); - navigator.init('[component="post"]', ajaxify.data.postcount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback); + navigator.init('[component="post"]', ajaxify.data.postcount, Topic.toTop, Topic.toBottom, utils.debounce(Topic.navigatorCallback, 500)); postTools.init(tid); threadTools.init(tid, $('.topic')); @@ -61,11 +56,9 @@ define('forum/topic', [ addDropupHandler(); addRepliesHandler(); - - handleBookmark(tid); - $(window).on('scroll', updateTopicTitle); + $(window).on('scroll', utils.debounce(updateTopicTitle, 250)); handleTopicSearch(); @@ -192,41 +185,31 @@ define('forum/topic', [ } Topic.navigatorCallback = function (index, elementCount) { - const path = ajaxify.removeRelativePath(window.location.pathname.slice(1)); - if (!path.startsWith('topic')) { - return; - } - - if (navigator.scrollActive) { + if (!ajaxify.data.template.topic || navigator.scrollActive) { return; } const newUrl = 'topic/' + ajaxify.data.slug + (index > 1 ? ('/' + index) : ''); if (newUrl !== currentUrl) { - if (Topic.replaceURLTimeout) { - clearTimeout(Topic.replaceURLTimeout); - Topic.replaceURLTimeout = 0; - } currentUrl = newUrl; - Topic.replaceURLTimeout = setTimeout(function () { - if (index >= elementCount && app.user.uid) { - socket.emit('topics.markAsRead', [ajaxify.data.tid]); - } - updateUserBookmark(index); + if (index >= elementCount && app.user.uid) { + socket.emit('topics.markAsRead', [ajaxify.data.tid]); + } - Topic.replaceURLTimeout = 0; - if (ajaxify.data.updateUrlWithPostIndex && history.replaceState) { - let search = window.location.search || ''; - if (!config.usePagination) { - search = (search && !/^\?page=\d+$/.test(search) ? search : ''); - } + updateUserBookmark(index); - history.replaceState({ - url: newUrl + search, - }, null, window.location.protocol + '//' + window.location.host + config.relative_path + '/' + newUrl + search); + Topic.replaceURLTimeout = 0; + if (ajaxify.data.updateUrlWithPostIndex && history.replaceState) { + let search = window.location.search || ''; + if (!config.usePagination) { + search = (search && !/^\?page=\d+$/.test(search) ? search : ''); } - }, 500); + + history.replaceState({ + url: newUrl + search, + }, null, window.location.protocol + '//' + window.location.host + config.relative_path + '/' + newUrl + search); + } } };