This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/web/src/elements/router/RouterOutlet.ts

94 lines
3 KiB
TypeScript
Raw Normal View History

import { css, CSSResult, customElement, html, LitElement, property, TemplateResult } from "lit-element";
import { Route } from "./Route";
import { ROUTES } from "../../routes";
import { RouteMatch } from "./RouteMatch";
import AKGlobal from "../../authentik.css";
import "./Router404";
import { Page } from "../Page";
import { TITLE_SUFFIX } from "../../constants";
2020-12-02 14:44:40 +00:00
2020-12-05 21:08:42 +00:00
@customElement("ak-router-outlet")
export class RouterOutlet extends LitElement {
2020-12-02 14:44:40 +00:00
@property({attribute: false})
current?: RouteMatch;
2020-11-22 12:13:45 +00:00
@property()
defaultUrl?: string;
static get styles(): CSSResult[] {
return [AKGlobal,
2020-11-25 11:41:13 +00:00
css`
:host {
height: 100vh;
background-color: var(--ak-dark-background, var(--pf-c-page--BackgroundColor)) !important;
2020-11-25 11:41:13 +00:00
}
2021-02-19 15:19:44 +00:00
*:first-child {
height: 100%;
display: flex;
flex-direction: column;
}
2020-11-25 11:41:13 +00:00
`,
];
}
constructor() {
super();
2020-12-01 08:15:41 +00:00
window.addEventListener("hashchange", () => this.navigate());
}
2020-12-01 08:15:41 +00:00
firstUpdated(): void {
2020-11-22 12:13:45 +00:00
this.navigate();
}
updated(): void {
if (!this.shadowRoot) return;
Array.from(this.shadowRoot?.children).forEach((el) => {
if ("pageTitle" in el) {
const title = (el as Page).pageTitle();
document.title = `${title} - ${TITLE_SUFFIX}`;
} else {
document.title = TITLE_SUFFIX;
}
});
}
2020-12-01 08:15:41 +00:00
navigate(): void {
2020-11-22 12:13:45 +00:00
let activeUrl = window.location.hash.slice(1, Infinity);
if (activeUrl === "") {
activeUrl = this.defaultUrl || "/";
window.location.hash = `#${activeUrl}`;
2021-03-12 11:27:57 +00:00
console.debug(`authentik/router: defaulted URL to ${window.location.hash}`);
return;
2020-11-22 12:13:45 +00:00
}
let matchedRoute: RouteMatch | null = null;
ROUTES.some((route) => {
const match = route.url.exec(activeUrl);
if (match != null) {
matchedRoute = new RouteMatch(route);
matchedRoute.arguments = match.groups || {};
matchedRoute.fullUrl = activeUrl;
2021-03-12 11:27:57 +00:00
console.debug("authentik/router: found match ", matchedRoute);
return true;
}
});
if (!matchedRoute) {
console.debug(`authentik/router: route "${activeUrl}" not defined`);
const route = new Route(
RegExp(""),
html`<div class="pf-c-page__main">
<ak-router-404 url=${activeUrl}></ak-router-404>
</div>`
);
matchedRoute = new RouteMatch(route);
matchedRoute.arguments = route.url.exec(activeUrl)?.groups || {};
matchedRoute.fullUrl = activeUrl;
}
this.current = matchedRoute;
}
render(): TemplateResult | undefined {
return this.current?.render();
}
}