2021-09-21 09:19:26 +00:00
|
|
|
import { html, TemplateResult } from "lit";
|
2020-12-01 16:41:27 +00:00
|
|
|
|
|
|
|
export const SLUG_REGEX = "[-a-zA-Z0-9_]+";
|
2021-02-04 09:22:14 +00:00
|
|
|
export const ID_REGEX = "\\d+";
|
2021-02-04 20:10:13 +00:00
|
|
|
export const UUID_REGEX = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
|
2020-12-01 16:41:27 +00:00
|
|
|
|
2021-02-16 19:00:34 +00:00
|
|
|
export interface RouteArgs {
|
|
|
|
[key: string]: string;
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:41:27 +00:00
|
|
|
export class Route {
|
|
|
|
url: RegExp;
|
|
|
|
|
|
|
|
private element?: TemplateResult;
|
2021-02-16 19:00:34 +00:00
|
|
|
private callback?: (args: RouteArgs) => TemplateResult;
|
2020-12-01 16:41:27 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-16 20:17:05 +00:00
|
|
|
redirectRaw(to: string): Route {
|
|
|
|
this.callback = () => {
|
|
|
|
console.debug(`authentik/router: redirecting ${to}`);
|
|
|
|
window.location.hash = `${to}`;
|
|
|
|
return html``;
|
|
|
|
};
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-02-16 19:00:34 +00:00
|
|
|
then(render: (args: RouteArgs) => TemplateResult): Route {
|
2020-12-01 16:41:27 +00:00
|
|
|
this.callback = render;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-02-16 19:00:34 +00:00
|
|
|
render(args: RouteArgs): TemplateResult {
|
2020-12-01 16:41:27 +00:00
|
|
|
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"}>`;
|
|
|
|
}
|
|
|
|
}
|