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/passbook/static/static/src/pages/RouterOutlet.ts

126 lines
3.1 KiB
TypeScript
Raw Normal View History

import {
2020-11-25 11:41:13 +00:00
css,
customElement,
html,
LitElement,
property,
TemplateResult,
} from "lit-element";
// @ts-ignore
import PF from "@patternfly/patternfly/patternfly.css";
// @ts-ignore
import PFAddons from "@patternfly/patternfly/patternfly-addons.css";
// @ts-ignore
import FA from "@fortawesome/fontawesome-free/css/fontawesome.css";
// @ts-ignore
import PBGlobal from "../passbook.css";
// @ts-ignore
import CodeMirrorStyle from "codemirror/lib/codemirror.css";
// @ts-ignore
import CodeMirrorTheme from "codemirror/theme/monokai.css";
import { ColorStyles } from "../constants";
export class Route {
url: RegExp;
private element?: TemplateResult;
private callback?: () => TemplateResult;
constructor(url: RegExp, element?: TemplateResult) {
this.url = url;
this.element = element;
}
redirect(to: string): Route {
this.callback = () => {
console.log(`passbook/router: redirecting ${to}`);
window.location.hash = `#${to}`;
return html``;
};
return this;
}
render(): TemplateResult {
if (this.callback) {
return this.callback();
}
if (this.element) {
return this.element;
}
throw new Error("Route does not have callback or element");
}
}
export const ROUTES: Route[] = [
// Prevent infinite Shell loops
new Route(new RegExp("^/$")).redirect("/-/overview/"),
new Route(new RegExp("^/applications/$"), html`<h1>test</h1>`),
];
@customElement("pb-router-outlet")
export class RouterOutlet extends LitElement {
@property()
activeRoute?: Route;
2020-11-22 12:13:45 +00:00
@property()
defaultUrl?: string;
static get styles() {
return [
PF,
PFAddons,
FA,
PBGlobal,
CodeMirrorStyle,
CodeMirrorTheme,
ColorStyles,
2020-11-25 11:41:13 +00:00
css`
:host {
height: 100%;
}
`,
];
}
constructor() {
super();
window.addEventListener("hashchange", (e) => this.navigate());
}
2020-11-22 12:13:45 +00:00
firstUpdated() {
this.navigate();
}
navigate() {
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}`;
return;
2020-11-22 12:13:45 +00:00
}
let selectedRoute: Route | null = null;
ROUTES.forEach((route) => {
if (route.url.exec(activeUrl)) {
selectedRoute = route;
return;
}
});
if (!selectedRoute) {
console.log(
`passbook/router: route "${activeUrl}" not defined, defaulting to shell`
);
selectedRoute = new Route(
RegExp(""),
2020-11-25 11:41:13 +00:00
html`<pb-site-shell url=${activeUrl}>
<div slot="body"></div>
</pb-site-shell>`
);
}
this.activeRoute = selectedRoute;
}
render() {
return this.activeRoute?.render();
}
}