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-03-18 13:47:13 +00:00
|
|
|
import { Page } from "../Page";
|
2021-03-29 13:53:56 +00:00
|
|
|
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")
|
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%;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
}
|
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();
|
2020-12-01 08:15:41 +00:00
|
|
|
window.addEventListener("hashchange", () => this.navigate());
|
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-03-18 13:47:13 +00:00
|
|
|
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 === "") {
|
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
|
|
|
}
|
|
|
|
}
|