2022-10-10 12:50:21 +00:00
|
|
|
import { safeRun } from "../common/util.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
function encodePageUrl(name: string): string {
|
2023-05-23 18:53:53 +00:00
|
|
|
return name;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function decodePageUrl(url: string): string {
|
2023-05-23 18:53:53 +00:00
|
|
|
return url;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:25:05 +00:00
|
|
|
export class PathPageNavigator {
|
|
|
|
navigationResolve?: () => void;
|
|
|
|
|
2022-08-02 10:43:39 +00:00
|
|
|
constructor(readonly indexPage: string, readonly root: string = "") {}
|
2022-07-22 11:44:28 +00:00
|
|
|
|
2022-08-30 08:44:20 +00:00
|
|
|
async navigate(page: string, pos?: number | string, replaceState = false) {
|
2022-08-02 10:43:39 +00:00
|
|
|
let encodedPage = encodePageUrl(page);
|
|
|
|
if (page === this.indexPage) {
|
|
|
|
encodedPage = "";
|
|
|
|
}
|
2022-08-01 13:06:32 +00:00
|
|
|
if (replaceState) {
|
|
|
|
window.history.replaceState(
|
|
|
|
{ page, pos },
|
|
|
|
page,
|
2022-10-10 12:50:21 +00:00
|
|
|
`${this.root}/${encodedPage}`,
|
2022-08-01 13:06:32 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
window.history.pushState(
|
|
|
|
{ page, pos },
|
|
|
|
page,
|
2022-10-10 12:50:21 +00:00
|
|
|
`${this.root}/${encodedPage}`,
|
2022-08-01 13:06:32 +00:00
|
|
|
);
|
|
|
|
}
|
2023-06-13 18:47:05 +00:00
|
|
|
globalThis.dispatchEvent(
|
2022-04-01 13:02:35 +00:00
|
|
|
new PopStateEvent("popstate", {
|
|
|
|
state: { page, pos },
|
2022-10-10 12:50:21 +00:00
|
|
|
}),
|
2022-03-28 13:25:05 +00:00
|
|
|
);
|
|
|
|
await new Promise<void>((resolve) => {
|
2022-03-20 08:56:28 +00:00
|
|
|
this.navigationResolve = resolve;
|
|
|
|
});
|
|
|
|
this.navigationResolve = undefined;
|
|
|
|
}
|
2022-03-28 13:25:05 +00:00
|
|
|
|
|
|
|
subscribe(
|
2022-10-10 12:50:21 +00:00
|
|
|
pageLoadCallback: (pageName: string, pos: number | string) => Promise<void>,
|
2022-03-28 13:25:05 +00:00
|
|
|
): void {
|
2022-04-01 13:03:12 +00:00
|
|
|
const cb = (event?: PopStateEvent) => {
|
|
|
|
const gotoPage = this.getCurrentPage();
|
|
|
|
if (!gotoPage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
safeRun(async () => {
|
2022-04-04 13:25:07 +00:00
|
|
|
await pageLoadCallback(
|
|
|
|
this.getCurrentPage(),
|
2022-10-10 12:50:21 +00:00
|
|
|
event?.state?.pos || this.getCurrentPos(),
|
2022-04-04 13:25:07 +00:00
|
|
|
);
|
2022-04-01 13:03:12 +00:00
|
|
|
if (this.navigationResolve) {
|
|
|
|
this.navigationResolve();
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
2022-04-01 13:03:12 +00:00
|
|
|
});
|
|
|
|
};
|
2023-06-13 18:47:05 +00:00
|
|
|
globalThis.addEventListener("popstate", cb);
|
2022-03-20 08:56:28 +00:00
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:44:20 +00:00
|
|
|
decodeURI(): [string, number | string] {
|
2023-06-13 18:47:05 +00:00
|
|
|
const [page, pos] = decodeURI(
|
2022-10-10 12:50:21 +00:00
|
|
|
location.pathname.substring(this.root.length + 1),
|
2022-07-22 11:44:28 +00:00
|
|
|
).split("@");
|
2022-08-30 08:44:20 +00:00
|
|
|
if (pos) {
|
|
|
|
if (pos.match(/^\d+$/)) {
|
|
|
|
return [page, +pos];
|
|
|
|
} else {
|
|
|
|
return [page, pos];
|
|
|
|
}
|
2022-04-24 16:06:34 +00:00
|
|
|
} else {
|
2022-08-30 08:44:20 +00:00
|
|
|
return [page, 0];
|
2022-04-24 16:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
getCurrentPage(): string {
|
2022-08-02 10:43:39 +00:00
|
|
|
return decodePageUrl(this.decodeURI()[0]) || this.indexPage;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 08:44:20 +00:00
|
|
|
getCurrentPos(): number | string {
|
2022-07-22 11:44:28 +00:00
|
|
|
// console.log("Pos", this.decodeURI()[1]);
|
2022-04-24 16:06:34 +00:00
|
|
|
return this.decodeURI()[1];
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
}
|