2020-11-21 23:06:25 +00:00
|
|
|
import {
|
2020-11-25 11:41:13 +00:00
|
|
|
css,
|
2020-11-21 23:06:25 +00:00
|
|
|
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";
|
2020-11-22 19:54:05 +00:00
|
|
|
// @ts-ignore
|
2020-11-23 15:46:28 +00:00
|
|
|
import FA from "@fortawesome/fontawesome-free/css/fontawesome.css";
|
|
|
|
// @ts-ignore
|
2020-11-23 15:42:03 +00:00
|
|
|
import PBGlobal from "../passbook.css";
|
2020-11-22 20:25:58 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import CodeMirrorStyle from "codemirror/lib/codemirror.css";
|
|
|
|
// @ts-ignore
|
|
|
|
import CodeMirrorTheme from "codemirror/theme/monokai.css";
|
2020-11-22 21:05:11 +00:00
|
|
|
import { ColorStyles } from "../constants";
|
2020-11-21 23:06:25 +00:00
|
|
|
|
2020-11-24 22:02:10 +00:00
|
|
|
export class Route {
|
2020-11-21 23:06:25 +00:00
|
|
|
url: RegExp;
|
2020-11-24 22:02:10 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2020-11-21 23:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const ROUTES: Route[] = [
|
2020-11-24 22:02:10 +00:00
|
|
|
// Prevent infinite Shell loops
|
|
|
|
new Route(new RegExp("^/$")).redirect("/-/overview/"),
|
|
|
|
new Route(new RegExp("^/applications/$"), html`<h1>test</h1>`),
|
2020-11-21 23:06:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
@customElement("pb-router-outlet")
|
|
|
|
export class RouterOutlet extends LitElement {
|
|
|
|
@property()
|
|
|
|
activeRoute?: Route;
|
|
|
|
|
2020-11-22 12:13:45 +00:00
|
|
|
@property()
|
|
|
|
defaultUrl?: string;
|
|
|
|
|
2020-11-21 23:06:25 +00:00
|
|
|
static get styles() {
|
2020-11-22 21:05:11 +00:00
|
|
|
return [
|
|
|
|
PF,
|
|
|
|
PFAddons,
|
2020-11-23 15:46:28 +00:00
|
|
|
FA,
|
2020-11-22 21:05:11 +00:00
|
|
|
PBGlobal,
|
|
|
|
CodeMirrorStyle,
|
|
|
|
CodeMirrorTheme,
|
|
|
|
ColorStyles,
|
2020-11-25 11:41:13 +00:00
|
|
|
css`
|
|
|
|
:host {
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
`,
|
2020-11-22 21:05:11 +00:00
|
|
|
];
|
2020-11-21 23:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
window.addEventListener("hashchange", (e) => this.navigate());
|
|
|
|
}
|
|
|
|
|
2020-11-22 12:13:45 +00:00
|
|
|
firstUpdated() {
|
|
|
|
this.navigate();
|
|
|
|
}
|
|
|
|
|
2020-11-21 23:06:25 +00:00
|
|
|
navigate() {
|
2020-11-22 12:13:45 +00:00
|
|
|
let activeUrl = window.location.hash.slice(1, Infinity);
|
|
|
|
if (activeUrl === "") {
|
|
|
|
activeUrl = this.defaultUrl!;
|
2020-11-22 22:48:34 +00:00
|
|
|
window.location.hash = `#${activeUrl}`;
|
|
|
|
return;
|
2020-11-22 12:13:45 +00:00
|
|
|
}
|
2020-11-24 22:02:10 +00:00
|
|
|
let selectedRoute: Route | null = null;
|
2020-11-21 23:06:25 +00:00
|
|
|
ROUTES.forEach((route) => {
|
|
|
|
if (route.url.exec(activeUrl)) {
|
|
|
|
selectedRoute = route;
|
2020-11-24 22:02:10 +00:00
|
|
|
return;
|
2020-11-21 23:06:25 +00:00
|
|
|
}
|
|
|
|
});
|
2020-11-24 22:02:10 +00:00
|
|
|
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>`
|
2020-11-24 22:02:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
this.activeRoute = selectedRoute;
|
2020-11-21 23:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-11-24 22:02:10 +00:00
|
|
|
return this.activeRoute?.render();
|
2020-11-21 23:06:25 +00:00
|
|
|
}
|
|
|
|
}
|