static: improve router, add redirect support
This commit is contained in:
parent
3f67da8f54
commit
7c73d2c2fb
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -96,11 +96,15 @@ export class ModalButton extends LitElement {
|
||||||
this.querySelector(
|
this.querySelector(
|
||||||
"[slot=modal]"
|
"[slot=modal]"
|
||||||
)!.innerHTML = data;
|
)!.innerHTML = data;
|
||||||
console.log(`passbook/modalbutton: re-showing form`);
|
console.log(
|
||||||
|
`passbook/modalbutton: re-showing form`
|
||||||
|
);
|
||||||
this.updateHandlers();
|
this.updateHandlers();
|
||||||
} else {
|
} else {
|
||||||
this.open = false;
|
this.open = false;
|
||||||
console.log(`passbook/modalbutton: successful submit`);
|
console.log(
|
||||||
|
`passbook/modalbutton: successful submit`
|
||||||
|
);
|
||||||
this.dispatchEvent(
|
this.dispatchEvent(
|
||||||
new CustomEvent("hashchange", {
|
new CustomEvent("hashchange", {
|
||||||
bubbles: true,
|
bubbles: true,
|
||||||
|
|
|
@ -19,22 +19,41 @@ import CodeMirrorStyle from "codemirror/lib/codemirror.css";
|
||||||
import CodeMirrorTheme from "codemirror/theme/monokai.css";
|
import CodeMirrorTheme from "codemirror/theme/monokai.css";
|
||||||
import { ColorStyles } from "../constants";
|
import { ColorStyles } from "../constants";
|
||||||
|
|
||||||
export interface Route {
|
export class Route {
|
||||||
url: RegExp;
|
url: RegExp;
|
||||||
element: TemplateResult;
|
|
||||||
|
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[] = [
|
export const ROUTES: Route[] = [
|
||||||
{
|
// Prevent infinite Shell loops
|
||||||
url: new RegExp("^overview$"),
|
new Route(new RegExp("^/$")).redirect("/-/overview/"),
|
||||||
element: html`<pb-site-shell url="/overview/"
|
new Route(new RegExp("^/applications/$"), html`<h1>test</h1>`),
|
||||||
><div slot="body"></div
|
|
||||||
></pb-site-shell>`,
|
|
||||||
},
|
|
||||||
// {
|
|
||||||
// url: new RegExp("^applications$"),
|
|
||||||
// element: html`<h1>test2</h1>`,
|
|
||||||
// },
|
|
||||||
];
|
];
|
||||||
|
|
||||||
@customElement("pb-router-outlet")
|
@customElement("pb-router-outlet")
|
||||||
|
@ -73,27 +92,28 @@ export class RouterOutlet extends LitElement {
|
||||||
window.location.hash = `#${activeUrl}`;
|
window.location.hash = `#${activeUrl}`;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let selectedRoute: Route | null = null;
|
||||||
ROUTES.forEach((route) => {
|
ROUTES.forEach((route) => {
|
||||||
let selectedRoute: Route | null = null;
|
|
||||||
if (route.url.exec(activeUrl)) {
|
if (route.url.exec(activeUrl)) {
|
||||||
selectedRoute = route;
|
selectedRoute = route;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (!selectedRoute) {
|
|
||||||
console.log(
|
|
||||||
`passbook/router: route "${activeUrl}" not defined, defaulting to shell`
|
|
||||||
);
|
|
||||||
selectedRoute = {
|
|
||||||
url: RegExp(""),
|
|
||||||
element: html`<pb-site-shell url=${activeUrl}
|
|
||||||
><div slot="body"></div
|
|
||||||
></pb-site-shell>`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
this.activeRoute = selectedRoute;
|
|
||||||
});
|
});
|
||||||
|
if (!selectedRoute) {
|
||||||
|
console.log(
|
||||||
|
`passbook/router: route "${activeUrl}" not defined, defaulting to shell`
|
||||||
|
);
|
||||||
|
selectedRoute = new Route(
|
||||||
|
RegExp(""),
|
||||||
|
html`<pb-site-shell url=${activeUrl}
|
||||||
|
><div slot="body"></div
|
||||||
|
></pb-site-shell>`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
this.activeRoute = selectedRoute;
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return this.activeRoute?.element;
|
return this.activeRoute?.render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue