From 32e0a0a0d3f74bd7a433ad4fe4562a52411a906f Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Mon, 8 Apr 2024 13:38:46 +0200 Subject: [PATCH] DEV: De-jQ click-interceptor (#26515) Co-authored-by: David Taylor --- .../click-interceptor.js | 9 ++-- .../discourse/app/lib/intercept-click.js | 45 ++++++++++--------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/discourse/app/instance-initializers/click-interceptor.js b/app/assets/javascripts/discourse/app/instance-initializers/click-interceptor.js index bcf7325a717..c4b5f6b9b2a 100644 --- a/app/assets/javascripts/discourse/app/instance-initializers/click-interceptor.js +++ b/app/assets/javascripts/discourse/app/instance-initializers/click-interceptor.js @@ -1,11 +1,12 @@ -import $ from "jquery"; import interceptClick from "discourse/lib/intercept-click"; import DiscourseURL from "discourse/lib/url"; export default { initialize(owner) { this.selector = owner.rootElement; - $(this.selector).on("click.discourse", "a", interceptClick); + document + .querySelector(this.selector) + .addEventListener("click", interceptClick); window.addEventListener("hashchange", this.hashChanged); }, @@ -14,7 +15,9 @@ export default { }, teardown() { - $(this.selector).off("click.discourse", "a", interceptClick); + document + .querySelector(this.selector) + .removeEventListener("click", interceptClick); window.removeEventListener("hashchange", this.hashChanged); }, }; diff --git a/app/assets/javascripts/discourse/app/lib/intercept-click.js b/app/assets/javascripts/discourse/app/lib/intercept-click.js index fe81d6d5254..da15dfd60b3 100644 --- a/app/assets/javascripts/discourse/app/lib/intercept-click.js +++ b/app/assets/javascripts/discourse/app/lib/intercept-click.js @@ -1,14 +1,16 @@ import DiscourseURL from "discourse/lib/url"; +const MOUSE_EVENT_PRIMARY_BUTTON_ID = 0; + export function wantsNewWindow(e) { return ( e.defaultPrevented || - (e.isDefaultPrevented && e.isDefaultPrevented()) || + e.isDefaultPrevented?.() || e.shiftKey || e.metaKey || e.ctrlKey || - (e.button && e.button !== 0) || - (e.currentTarget && e.currentTarget.target === "_blank") + (e.button && e.button !== MOUSE_EVENT_PRIMARY_BUTTON_ID) || + e.currentTarget?.target === "_blank" ); } @@ -18,29 +20,33 @@ export function wantsNewWindow(e) { This jQuery code intercepts clicks on those links and routes them properly. **/ export default function interceptClick(e) { - if (wantsNewWindow(e)) { + const target = e.target.closest("a"); + + if (!target) { return; } - const currentTarget = e.currentTarget; - const href = currentTarget.getAttribute("href"); - const linkTarget = currentTarget.getAttribute("target"); - const targettingOtherFrame = linkTarget && linkTarget !== "_self"; + if (wantsNewWindow(e, target) || target.target === "_blank") { + return; + } + + const href = target.getAttribute("href"); + const linkTarget = target.getAttribute("target"); + const targetingOtherFrame = linkTarget && linkTarget !== "_self"; if ( !href || href.startsWith("#") || - targettingOtherFrame || - currentTarget.dataset.emberAction || - currentTarget.dataset.autoRoute || - currentTarget.dataset.shareUrl || - currentTarget.classList.contains("widget-link") || - currentTarget.classList.contains("raw-link") || - currentTarget.classList.contains("mention") || - (!currentTarget.classList.contains("d-link") && - !currentTarget.dataset.userCard && - currentTarget.classList.contains("ember-view")) || - currentTarget.classList.contains("lightbox") || + targetingOtherFrame || + target.dataset.autoRoute || + target.dataset.shareUrl || + target.classList.contains("widget-link") || + target.classList.contains("raw-link") || + target.classList.contains("mention") || + (!target.classList.contains("d-link") && + !target.dataset.userCard && + target.classList.contains("ember-view")) || + target.classList.contains("lightbox") || href.startsWith("mailto:") || (href.match(/^http[s]?:\/\//i) && !href.match(new RegExp("^https?:\\/\\/" + window.location.hostname, "i"))) @@ -50,5 +56,4 @@ export default function interceptClick(e) { e.preventDefault(); DiscourseURL.routeTo(href); - return false; }