This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2020-12-01 16:41:27 +00:00
|
|
|
import { html, TemplateResult } from "lit-html";
|
|
|
|
|
|
|
|
export const SLUG_REGEX = "[-a-zA-Z0-9_]+";
|
2021-02-04 09:22:14 +00:00
|
|
|
export const ID_REGEX = "\\d+";
|
2020-12-01 16:41:27 +00:00
|
|
|
|
|
|
|
export class Route {
|
|
|
|
url: RegExp;
|
|
|
|
|
|
|
|
private element?: TemplateResult;
|
|
|
|
private callback?: (args: { [key: string]: string }) => TemplateResult;
|
|
|
|
|
|
|
|
constructor(url: RegExp, element?: TemplateResult) {
|
|
|
|
this.url = url;
|
|
|
|
this.element = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
redirect(to: string): Route {
|
|
|
|
this.callback = () => {
|
2020-12-05 21:08:42 +00:00
|
|
|
console.debug(`authentik/router: redirecting ${to}`);
|
2020-12-01 16:41:27 +00:00
|
|
|
window.location.hash = `#${to}`;
|
|
|
|
return html``;
|
|
|
|
};
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
then(render: (args: { [key: string]: string }) => TemplateResult): Route {
|
|
|
|
this.callback = render;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
render(args: { [key: string]: string }): TemplateResult {
|
|
|
|
if (this.callback) {
|
|
|
|
return this.callback(args);
|
|
|
|
}
|
|
|
|
if (this.element) {
|
|
|
|
return this.element;
|
|
|
|
}
|
|
|
|
throw new Error("Route does not have callback or element");
|
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return `<Route url=${this.url} callback=${this.callback ? "true" : "false"}>`;
|
|
|
|
}
|
|
|
|
}
|