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/pages/router/RouterOutlet.ts

84 lines
2.7 KiB
TypeScript
Raw Normal View History

import { css, CSSResult, customElement, html, LitElement, property, TemplateResult } from "lit-element";
// @ts-ignore
import CodeMirrorStyle from "codemirror/lib/codemirror.css";
// @ts-ignore
import CodeMirrorTheme from "codemirror/theme/monokai.css";
import { ColorStyles } from "../../constants";
import { COMMON_STYLES } from "../../common/styles";
import { Route } from "./Route";
import { ROUTES } from "../../routes";
import { RouteMatch } from "./RouteMatch";
2020-12-02 14:44:40 +00:00
import "../generic/SiteShell";
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 [
CodeMirrorStyle,
CodeMirrorTheme,
ColorStyles,
2020-11-25 11:41:13 +00:00
css`
:host {
height: 100%;
}
`,
].concat(...COMMON_STYLES);
}
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();
}
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}`;
2020-12-05 21:08:42 +00:00
console.debug(`authentik/router: set to ${window.location.hash}`);
return;
2020-11-22 12:13:45 +00:00
}
let matchedRoute: RouteMatch | null = null;
ROUTES.some((route) => {
2020-12-05 21:08:42 +00:00
console.debug(`authentik/router: matching ${activeUrl} against ${route.url}`);
const match = route.url.exec(activeUrl);
if (match != null) {
matchedRoute = new RouteMatch(route);
matchedRoute.arguments = match.groups || {};
matchedRoute.fullUrl = activeUrl;
2020-12-05 21:08:42 +00:00
console.debug(`authentik/router: found match ${matchedRoute}`);
return true;
}
});
if (!matchedRoute) {
2020-12-05 21:08:42 +00:00
console.debug(`authentik/router: route "${activeUrl}" not defined, defaulting to shell`);
const route = new Route(
RegExp(""),
2020-12-05 21:08:42 +00:00
html`<ak-site-shell url=${activeUrl}>
2020-11-25 11:41:13 +00:00
<div slot="body"></div>
2020-12-05 21:08:42 +00:00
</ak-site-shell>`
);
matchedRoute = new RouteMatch(route);
matchedRoute.arguments = route.url.exec(activeUrl)?.groups || {};
matchedRoute.fullUrl = activeUrl;
}
this.current = matchedRoute;
}
render(): TemplateResult | undefined {
// TODO: Render 404 when current Route is empty
return this.current?.render();
}
}