Files
grafana/public/app/core/navigation/patch/interceptLinkClicks.ts
Dominik Prokop deccc97985 DataLinks: Ensure window origin to be stripped from url when using absolute urls (#32634)
* Ensure window origin to be stripped from url when using absolute urls

* Update test

* Make sure link interception works for external links

* Apply suggestions from code review

* Review
2021-04-08 17:38:56 +02:00

51 lines
1.5 KiB
TypeScript

import { locationUtil } from '@grafana/data';
import { locationService, navigationLogger } from '@grafana/runtime';
export function interceptLinkClicks(e: MouseEvent) {
const anchor = getParentAnchor(e.target as HTMLElement);
// Ignore if opening new tab or already default prevented
if (e.ctrlKey || e.metaKey || e.defaultPrevented) {
return;
}
if (anchor) {
let href = anchor.getAttribute('href');
const target = anchor.getAttribute('target');
if (href && !target) {
navigationLogger('utils', false, 'intercepting link click', e);
e.preventDefault();
href = locationUtil.stripBaseFromUrl(href);
// Ensure old angular urls with no starting '/' are handled the same as before
// Make sure external links are handled correctly
// That is they where seen as being absolute from app root
if (href[0] !== '/') {
try {
const external = new URL(href);
if (external.origin !== window.location.origin) {
window.location.href = external.toString();
return;
}
} catch (e) {
console.warn(e);
}
href = `/${href}`;
}
locationService.push(href);
}
}
}
function getParentAnchor(element: HTMLElement | null): HTMLElement | null {
while (element !== null && element.tagName) {
if (element.tagName.toUpperCase() === 'A') {
return element;
}
element = element.parentNode as HTMLElement;
}
return null;
}