import { Component } from "../component/component"; import { Destination, RouterEnv } from "./Router"; export const LINK_TEMPLATE_NAME = "__owl__-router-link"; export const LINK_TEMPLATE = ` `; type Props = Destination; export class Link extends Component { template = LINK_TEMPLATE_NAME; href: string = this.env.router.destToPath(this.props); async willUpdateProps(nextProps) { this.href = this.env.router.destToPath(nextProps); } get isActive() { if (this.env.router.mode === "hash") { return (document.location).hash === this.href; } return (document.location).pathname === this.href; } navigate(ev) { // don't redirect with control keys if (ev.metaKey || ev.altKey || ev.ctrlKey || ev.shiftKey) { return; } // don't redirect on right click if (ev.button !== undefined && ev.button !== 0) { return; } // don't redirect if `target="_blank"` if (ev.currentTarget && ev.currentTarget.getAttribute) { const target = ev.currentTarget.getAttribute("target"); if (/\b_blank\b/i.test(target)) { return; } } ev.preventDefault(); this.env.router.navigate(this.props); } }