DEV: De-jQ click-interceptor (#26515)
Co-authored-by: David Taylor <david@taylorhq.com>
This commit is contained in:
parent
6c2c1a43ae
commit
32e0a0a0d3
|
@ -1,11 +1,12 @@
|
||||||
import $ from "jquery";
|
|
||||||
import interceptClick from "discourse/lib/intercept-click";
|
import interceptClick from "discourse/lib/intercept-click";
|
||||||
import DiscourseURL from "discourse/lib/url";
|
import DiscourseURL from "discourse/lib/url";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
initialize(owner) {
|
initialize(owner) {
|
||||||
this.selector = owner.rootElement;
|
this.selector = owner.rootElement;
|
||||||
$(this.selector).on("click.discourse", "a", interceptClick);
|
document
|
||||||
|
.querySelector(this.selector)
|
||||||
|
.addEventListener("click", interceptClick);
|
||||||
window.addEventListener("hashchange", this.hashChanged);
|
window.addEventListener("hashchange", this.hashChanged);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -14,7 +15,9 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
teardown() {
|
teardown() {
|
||||||
$(this.selector).off("click.discourse", "a", interceptClick);
|
document
|
||||||
|
.querySelector(this.selector)
|
||||||
|
.removeEventListener("click", interceptClick);
|
||||||
window.removeEventListener("hashchange", this.hashChanged);
|
window.removeEventListener("hashchange", this.hashChanged);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
import DiscourseURL from "discourse/lib/url";
|
import DiscourseURL from "discourse/lib/url";
|
||||||
|
|
||||||
|
const MOUSE_EVENT_PRIMARY_BUTTON_ID = 0;
|
||||||
|
|
||||||
export function wantsNewWindow(e) {
|
export function wantsNewWindow(e) {
|
||||||
return (
|
return (
|
||||||
e.defaultPrevented ||
|
e.defaultPrevented ||
|
||||||
(e.isDefaultPrevented && e.isDefaultPrevented()) ||
|
e.isDefaultPrevented?.() ||
|
||||||
e.shiftKey ||
|
e.shiftKey ||
|
||||||
e.metaKey ||
|
e.metaKey ||
|
||||||
e.ctrlKey ||
|
e.ctrlKey ||
|
||||||
(e.button && e.button !== 0) ||
|
(e.button && e.button !== MOUSE_EVENT_PRIMARY_BUTTON_ID) ||
|
||||||
(e.currentTarget && e.currentTarget.target === "_blank")
|
e.currentTarget?.target === "_blank"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,29 +20,33 @@ export function wantsNewWindow(e) {
|
||||||
This jQuery code intercepts clicks on those links and routes them properly.
|
This jQuery code intercepts clicks on those links and routes them properly.
|
||||||
**/
|
**/
|
||||||
export default function interceptClick(e) {
|
export default function interceptClick(e) {
|
||||||
if (wantsNewWindow(e)) {
|
const target = e.target.closest("a");
|
||||||
|
|
||||||
|
if (!target) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentTarget = e.currentTarget;
|
if (wantsNewWindow(e, target) || target.target === "_blank") {
|
||||||
const href = currentTarget.getAttribute("href");
|
return;
|
||||||
const linkTarget = currentTarget.getAttribute("target");
|
}
|
||||||
const targettingOtherFrame = linkTarget && linkTarget !== "_self";
|
|
||||||
|
const href = target.getAttribute("href");
|
||||||
|
const linkTarget = target.getAttribute("target");
|
||||||
|
const targetingOtherFrame = linkTarget && linkTarget !== "_self";
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!href ||
|
!href ||
|
||||||
href.startsWith("#") ||
|
href.startsWith("#") ||
|
||||||
targettingOtherFrame ||
|
targetingOtherFrame ||
|
||||||
currentTarget.dataset.emberAction ||
|
target.dataset.autoRoute ||
|
||||||
currentTarget.dataset.autoRoute ||
|
target.dataset.shareUrl ||
|
||||||
currentTarget.dataset.shareUrl ||
|
target.classList.contains("widget-link") ||
|
||||||
currentTarget.classList.contains("widget-link") ||
|
target.classList.contains("raw-link") ||
|
||||||
currentTarget.classList.contains("raw-link") ||
|
target.classList.contains("mention") ||
|
||||||
currentTarget.classList.contains("mention") ||
|
(!target.classList.contains("d-link") &&
|
||||||
(!currentTarget.classList.contains("d-link") &&
|
!target.dataset.userCard &&
|
||||||
!currentTarget.dataset.userCard &&
|
target.classList.contains("ember-view")) ||
|
||||||
currentTarget.classList.contains("ember-view")) ||
|
target.classList.contains("lightbox") ||
|
||||||
currentTarget.classList.contains("lightbox") ||
|
|
||||||
href.startsWith("mailto:") ||
|
href.startsWith("mailto:") ||
|
||||||
(href.match(/^http[s]?:\/\//i) &&
|
(href.match(/^http[s]?:\/\//i) &&
|
||||||
!href.match(new RegExp("^https?:\\/\\/" + window.location.hostname, "i")))
|
!href.match(new RegExp("^https?:\\/\\/" + window.location.hostname, "i")))
|
||||||
|
@ -50,5 +56,4 @@ export default function interceptClick(e) {
|
||||||
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
DiscourseURL.routeTo(href);
|
DiscourseURL.routeTo(href);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue