2020-12-01 16:27:19 +00:00
|
|
|
import { css, CSSResult, customElement, html, LitElement, property, TemplateResult } from "lit-element";
|
2020-12-01 16:41:27 +00:00
|
|
|
import { Route } from "./Route";
|
|
|
|
import { ROUTES } from "../../routes";
|
|
|
|
import { RouteMatch } from "./RouteMatch";
|
2021-03-17 17:49:55 +00:00
|
|
|
import AKGlobal from "../../authentik.css";
|
2020-11-26 12:58:45 +00:00
|
|
|
|
2021-02-19 23:27:32 +00:00
|
|
|
import "./Router404";
|
2021-04-10 15:06:54 +00:00
|
|
|
import { ROUTE_SEPARATOR } from "../../constants";
|
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", () => {
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}());
|
|
|
|
});
|
2020-12-02 14:44:40 +00:00
|
|
|
|
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 {
|
2020-12-02 14:44:40 +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;
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2021-03-17 17:49:55 +00:00
|
|
|
return [AKGlobal,
|
2020-11-25 11:41:13 +00:00
|
|
|
css`
|
|
|
|
:host {
|
2021-01-30 11:38:33 +00:00
|
|
|
height: 100vh;
|
2021-03-17 18:18:15 +00:00
|
|
|
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%;
|
|
|
|
}
|
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;
|
2020-11-30 11:33:09 +00:00
|
|
|
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`);
|
2020-11-26 12:58:45 +00:00
|
|
|
const route = new Route(
|
2020-11-24 22:02:10 +00:00
|
|
|
RegExp(""),
|
2021-03-17 16:11:39 +00:00
|
|
|
html`<div class="pf-c-page__main">
|
|
|
|
<ak-router-404 url=${activeUrl}></ak-router-404>
|
|
|
|
</div>`
|
2020-11-24 22:02:10 +00:00
|
|
|
);
|
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
|
|
|
}
|
|
|
|
}
|