2022-06-25 15:44:17 +00:00
|
|
|
import { ROUTE_SEPARATOR } from "@goauthentik/web/constants";
|
|
|
|
import "@goauthentik/web/elements/router/Router404";
|
|
|
|
|
2021-10-28 07:48:51 +00:00
|
|
|
import { CSSResult, LitElement, TemplateResult, css, html } from "lit";
|
2021-11-04 21:34:48 +00:00
|
|
|
import { customElement, property } from "lit/decorators.js";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2022-06-25 15:44:17 +00:00
|
|
|
import AKGlobal from "@goauthentik/web/authentik.css";
|
2020-11-26 12:58:45 +00:00
|
|
|
|
2021-09-21 09:31:37 +00:00
|
|
|
import { Route } from "./Route";
|
|
|
|
import { RouteMatch } from "./RouteMatch";
|
2021-04-04 21:19:08 +00:00
|
|
|
|
|
|
|
// Poliyfill for hashchange.newURL,
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onhashchange
|
|
|
|
window.addEventListener("load", () => {
|
2021-08-03 15:52:21 +00:00
|
|
|
if (!window.HashChangeEvent)
|
|
|
|
(function () {
|
|
|
|
let lastURL = document.URL;
|
|
|
|
window.addEventListener("hashchange", function (event) {
|
|
|
|
Object.defineProperty(event, "oldURL", {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
value: lastURL,
|
|
|
|
});
|
|
|
|
Object.defineProperty(event, "newURL", {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
value: document.URL,
|
|
|
|
});
|
|
|
|
lastURL = document.URL;
|
|
|
|
});
|
|
|
|
})();
|
2021-04-04 21:19:08 +00:00
|
|
|
});
|
2020-12-02 14:44:40 +00:00
|
|
|
|
2021-12-14 14:23:02 +00:00
|
|
|
export function paramURL(url: string, params?: { [key: string]: unknown }): string {
|
|
|
|
let finalUrl = "#";
|
|
|
|
finalUrl += url;
|
|
|
|
if (params) {
|
|
|
|
finalUrl += ";";
|
|
|
|
finalUrl += encodeURIComponent(JSON.stringify(params));
|
|
|
|
}
|
|
|
|
return finalUrl;
|
|
|
|
}
|
|
|
|
export function navigate(url: string, params?: { [key: string]: unknown }): void {
|
|
|
|
window.location.assign(paramURL(url, params));
|
|
|
|
}
|
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
@customElement("ak-router-outlet")
|
2020-11-21 23:06:25 +00:00
|
|
|
export class RouterOutlet extends LitElement {
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ attribute: false })
|
2020-11-26 12:58:45 +00:00
|
|
|
current?: RouteMatch;
|
2020-11-21 23:06:25 +00:00
|
|
|
|
2020-11-22 12:13:45 +00:00
|
|
|
@property()
|
|
|
|
defaultUrl?: string;
|
|
|
|
|
2021-09-16 15:30:16 +00:00
|
|
|
@property({ attribute: false })
|
|
|
|
routes: Route[] = [];
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2021-08-03 15:52:21 +00:00
|
|
|
return [
|
|
|
|
AKGlobal,
|
2020-11-25 11:41:13 +00:00
|
|
|
css`
|
|
|
|
:host {
|
2021-09-20 10:29:32 +00:00
|
|
|
background-color: transparent !important;
|
2020-11-25 11:41:13 +00:00
|
|
|
}
|
2021-02-19 15:19:44 +00:00
|
|
|
*:first-child {
|
2021-04-10 18:53:57 +00:00
|
|
|
flex-direction: column;
|
2021-02-19 15:19:44 +00:00
|
|
|
}
|
2020-11-25 11:41:13 +00:00
|
|
|
`,
|
2021-03-17 16:11:39 +00:00
|
|
|
];
|
2020-11-21 23:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2021-04-04 21:19:08 +00:00
|
|
|
window.addEventListener("hashchange", (ev: HashChangeEvent) => this.navigate(ev));
|
2020-11-21 23:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 08:15:41 +00:00
|
|
|
firstUpdated(): void {
|
2020-11-22 12:13:45 +00:00
|
|
|
this.navigate();
|
|
|
|
}
|
|
|
|
|
2021-04-04 21:19:08 +00:00
|
|
|
navigate(ev?: HashChangeEvent): void {
|
|
|
|
let activeUrl = window.location.hash.slice(1, Infinity).split(ROUTE_SEPARATOR)[0];
|
|
|
|
if (ev) {
|
|
|
|
// Check if we've actually changed paths
|
|
|
|
const oldPath = new URL(ev.oldURL).hash.slice(1, Infinity).split(ROUTE_SEPARATOR)[0];
|
|
|
|
if (oldPath === activeUrl) return;
|
|
|
|
}
|
2020-11-22 12:13:45 +00:00
|
|
|
if (activeUrl === "") {
|
2020-12-01 16:27:19 +00:00
|
|
|
activeUrl = this.defaultUrl || "/";
|
2020-11-22 22:48:34 +00:00
|
|
|
window.location.hash = `#${activeUrl}`;
|
2021-03-12 11:27:57 +00:00
|
|
|
console.debug(`authentik/router: defaulted URL to ${window.location.hash}`);
|
2020-11-22 22:48:34 +00:00
|
|
|
return;
|
2020-11-22 12:13:45 +00:00
|
|
|
}
|
2020-11-26 12:58:45 +00:00
|
|
|
let matchedRoute: RouteMatch | null = null;
|
2021-09-16 15:30:16 +00:00
|
|
|
this.routes.some((route) => {
|
2020-11-26 12:58:45 +00:00
|
|
|
const match = route.url.exec(activeUrl);
|
|
|
|
if (match != null) {
|
|
|
|
matchedRoute = new RouteMatch(route);
|
2020-12-01 16:27:19 +00:00
|
|
|
matchedRoute.arguments = match.groups || {};
|
2020-11-26 12:58:45 +00:00
|
|
|
matchedRoute.fullUrl = activeUrl;
|
2021-03-12 11:27:57 +00:00
|
|
|
console.debug("authentik/router: found match ", matchedRoute);
|
2020-11-30 11:33:09 +00:00
|
|
|
return true;
|
2020-11-21 23:06:25 +00:00
|
|
|
}
|
|
|
|
});
|
2020-11-26 12:58:45 +00:00
|
|
|
if (!matchedRoute) {
|
2021-02-19 23:27:32 +00:00
|
|
|
console.debug(`authentik/router: route "${activeUrl}" not defined`);
|
2022-06-18 11:36:18 +00:00
|
|
|
const route = new Route(RegExp(""), async () => {
|
|
|
|
return html`<div class="pf-c-page__main">
|
2021-03-17 16:11:39 +00:00
|
|
|
<ak-router-404 url=${activeUrl}></ak-router-404>
|
2022-06-18 11:36:18 +00:00
|
|
|
</div>`;
|
|
|
|
});
|
2020-11-26 12:58:45 +00:00
|
|
|
matchedRoute = new RouteMatch(route);
|
2020-12-01 16:27:19 +00:00
|
|
|
matchedRoute.arguments = route.url.exec(activeUrl)?.groups || {};
|
2020-11-26 12:58:45 +00:00
|
|
|
matchedRoute.fullUrl = activeUrl;
|
2020-11-24 22:02:10 +00:00
|
|
|
}
|
2020-11-26 12:58:45 +00:00
|
|
|
this.current = matchedRoute;
|
2020-11-21 23:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 08:46:59 +00:00
|
|
|
render(): TemplateResult | undefined {
|
2020-11-26 12:58:45 +00:00
|
|
|
return this.current?.render();
|
2020-11-21 23:06:25 +00:00
|
|
|
}
|
|
|
|
}
|