mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 03:06:41 +00:00
Extract is navigation click (#3432)
This commit is contained in:
parent
a91bb3cdbb
commit
54ea6176aa
@ -17,6 +17,7 @@ import { mockMediaPlayer } from "./stubs/media_player";
|
|||||||
import { HomeAssistant } from "../../src/types";
|
import { HomeAssistant } from "../../src/types";
|
||||||
import { mockFrontend } from "./stubs/frontend";
|
import { mockFrontend } from "./stubs/frontend";
|
||||||
import { mockPersistentNotification } from "./stubs/persistent_notification";
|
import { mockPersistentNotification } from "./stubs/persistent_notification";
|
||||||
|
import { isNavigationClick } from "../../src/common/dom/is-navigation-click";
|
||||||
|
|
||||||
class HaDemo extends HomeAssistantAppEl {
|
class HaDemo extends HomeAssistantAppEl {
|
||||||
protected async _initialize() {
|
protected async _initialize() {
|
||||||
@ -60,49 +61,14 @@ class HaDemo extends HomeAssistantAppEl {
|
|||||||
document.body.addEventListener(
|
document.body.addEventListener(
|
||||||
"click",
|
"click",
|
||||||
(e) => {
|
(e) => {
|
||||||
if (
|
const href = isNavigationClick(e);
|
||||||
e.defaultPrevented ||
|
|
||||||
e.button !== 0 ||
|
|
||||||
e.metaKey ||
|
|
||||||
e.ctrlKey ||
|
|
||||||
e.shiftKey
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const anchor = e
|
if (!href) {
|
||||||
.composedPath()
|
|
||||||
.filter((n) => (n as HTMLElement).tagName === "A")[0] as
|
|
||||||
| HTMLAnchorElement
|
|
||||||
| undefined;
|
|
||||||
if (
|
|
||||||
!anchor ||
|
|
||||||
anchor.target ||
|
|
||||||
anchor.hasAttribute("download") ||
|
|
||||||
anchor.getAttribute("rel") === "external"
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let href = anchor.href;
|
|
||||||
if (!href || href.indexOf("mailto:") !== -1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const location = window.location;
|
|
||||||
const origin =
|
|
||||||
location.origin || location.protocol + "//" + location.host;
|
|
||||||
if (href.indexOf(origin) !== 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
href = href.substr(origin.length);
|
|
||||||
|
|
||||||
if (href === "#") {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
navigate(this as any, href);
|
navigate(this, href);
|
||||||
},
|
},
|
||||||
{ capture: true }
|
{ capture: true }
|
||||||
);
|
);
|
||||||
|
45
src/common/dom/is-navigation-click.ts
Normal file
45
src/common/dom/is-navigation-click.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
export const isNavigationClick = (e: MouseEvent) => {
|
||||||
|
// Taken from polymer/pwa-helpers. BSD-3 licensed
|
||||||
|
if (
|
||||||
|
e.defaultPrevented ||
|
||||||
|
e.button !== 0 ||
|
||||||
|
e.metaKey ||
|
||||||
|
e.ctrlKey ||
|
||||||
|
e.shiftKey
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const anchor = e
|
||||||
|
.composedPath()
|
||||||
|
.filter((n) => (n as HTMLElement).tagName === "A")[0] as
|
||||||
|
| HTMLAnchorElement
|
||||||
|
| undefined;
|
||||||
|
if (
|
||||||
|
!anchor ||
|
||||||
|
anchor.target ||
|
||||||
|
anchor.hasAttribute("download") ||
|
||||||
|
anchor.getAttribute("rel") === "external"
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let href = anchor.href;
|
||||||
|
if (!href || href.indexOf("mailto:") !== -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const location = window.location;
|
||||||
|
const origin = location.origin || location.protocol + "//" + location.host;
|
||||||
|
if (href.indexOf(origin) !== 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
href = href.substr(origin.length);
|
||||||
|
|
||||||
|
if (href === "#") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
return href;
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user