From 8437130ec2bd18d4e94db2980a50610c98844620 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 30 Sep 2020 09:02:54 -0400 Subject: [PATCH] fix: early button/anchor clicks do nothing This reverts commit a395324b52a921b9e58cac11cb1237db48b2cb03, and fixes the issue where the ACP became unusable because of the earlier variant of this code. Thanks @psychobunny for spotting. --- public/src/app.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/public/src/app.js b/public/src/app.js index ed25c592d9..f0fb98af8f 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -26,6 +26,40 @@ app.cacheBuster = null; app.load(); }); + app.handleEarlyClicks = function () { + /** + * Occasionally, a button or anchor (not meant to be ajaxified) is clicked before + * ajaxify is ready. Capture that event and re-click it once NodeBB is ready. + * + * e.g. New Topic/Reply, post tools + */ + if (document.body) { + var earlyQueue = []; // once we can ES6, use Set instead + var earlyClick = function (ev) { + var btnEl = ev.target.closest('button'); + var anchorEl = ev.target.closest('a'); + if (!btnEl && anchorEl && (anchorEl.getAttribute('data-ajaxify') === 'false' || anchorEl.href === '#')) { + btnEl = anchorEl; + } + if (btnEl && !earlyQueue.includes(btnEl)) { + earlyQueue.push(btnEl); + ev.stopImmediatePropagation(); + ev.preventDefault(); + } + }; + document.body.addEventListener('click', earlyClick); + $(window).on('action:ajaxify.end', function () { + document.body.removeEventListener('click', earlyClick); + earlyQueue.forEach(function (el) { + el.click(); + }); + }); + } else { + setTimeout(app.handleEarlyClicks, 50); + } + }; + app.handleEarlyClicks(); + app.load = function () { overrides.overrideTimeago();