| | |
| | | import { fetchCanonical } from "./util" |
| | | |
| | | const p = new DOMParser() |
| | | let activeAnchor: HTMLAnchorElement | null = null |
| | | |
| | | async function mouseEnterHandler( |
| | | this: HTMLAnchorElement, |
| | | { clientX, clientY }: { clientX: number; clientY: number }, |
| | | ) { |
| | | const link = this |
| | | const link = (activeAnchor = this) |
| | | if (link.dataset.noPopover === "true") { |
| | | return |
| | | } |
| | |
| | | targetUrl.search = "" |
| | | const popoverId = `popover-${link.pathname}` |
| | | const prevPopoverElement = document.getElementById(popoverId) |
| | | const hasAlreadyBeenFetched = () => !!document.getElementById(popoverId) |
| | | |
| | | // dont refetch if there's already a popover |
| | | if (hasAlreadyBeenFetched()) { |
| | | if (!!document.getElementById(popoverId)) { |
| | | showPopover(prevPopoverElement as HTMLElement) |
| | | return |
| | | } |
| | |
| | | console.error(err) |
| | | }) |
| | | |
| | | // bailout if another popover exists |
| | | if (hasAlreadyBeenFetched()) { |
| | | return |
| | | } |
| | | |
| | | if (!response) return |
| | | const [contentType] = response.headers.get("Content-Type")!.split(";") |
| | | const [contentTypeCategory, typeInfo] = contentType.split("/") |
| | |
| | | elts.forEach((elt) => popoverInner.appendChild(elt)) |
| | | } |
| | | |
| | | if (!!document.getElementById(popoverId)) { |
| | | return |
| | | } |
| | | |
| | | document.body.appendChild(popoverElement) |
| | | if (activeAnchor !== this) { |
| | | return |
| | | } |
| | | |
| | | showPopover(popoverElement) |
| | | } |
| | | |
| | | function clearActivePopover() { |
| | | activeAnchor = null |
| | | const allPopoverElements = document.querySelectorAll(".popover") |
| | | allPopoverElements.forEach((popoverElement) => popoverElement.classList.remove("active-popover")) |
| | | } |