ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
export interface Page {
|
|
|
|
name: string;
|
|
|
|
title: string;
|
|
|
|
url: string;
|
|
|
|
show: () => boolean;
|
|
|
|
hide: () => boolean;
|
|
|
|
shouldSkip: () => boolean;
|
|
|
|
index?: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface PageConfig {
|
|
|
|
hideOthersOnPageShow: boolean;
|
|
|
|
defaultName: string;
|
|
|
|
defaultTitle: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PageManager {
|
|
|
|
pages: Map<string, Page>;
|
|
|
|
pageList: string[];
|
|
|
|
hideOthers: boolean;
|
|
|
|
defaultName: string = "";
|
|
|
|
defaultTitle: string = "";
|
|
|
|
|
|
|
|
private _overridePushState = () => {
|
|
|
|
const pushState = window.history.pushState;
|
|
|
|
window.history.pushState = function (data: any, __: string, _: string | URL) {
|
|
|
|
pushState.apply(window.history, arguments);
|
|
|
|
let ev = { state: data as string } as PopStateEvent;
|
|
|
|
window.onpopstate(ev);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private _onpopstate = (event: PopStateEvent) => {
|
|
|
|
let name = event.state;
|
2024-08-28 13:18:52 +00:00
|
|
|
if (!this.pages.has(event.state)) {
|
ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
name = this.pageList[0]
|
|
|
|
}
|
2024-08-28 13:18:52 +00:00
|
|
|
let success = this.pages.get(name).show();
|
ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
if (!success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(this.hideOthers)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (let k of this.pageList) {
|
|
|
|
if (name != k) {
|
2024-08-28 13:18:52 +00:00
|
|
|
this.pages.get(k).hide();
|
ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(c: PageConfig) {
|
|
|
|
this.pages = new Map<string, Page>;
|
|
|
|
this.pageList = [];
|
|
|
|
this.hideOthers = c.hideOthersOnPageShow;
|
|
|
|
this.defaultName = c.defaultName;
|
|
|
|
this.defaultTitle = c.defaultTitle;
|
|
|
|
|
|
|
|
this._overridePushState();
|
|
|
|
window.onpopstate = this._onpopstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
setPage(p: Page) {
|
|
|
|
p.index = this.pageList.length;
|
2024-08-28 13:18:52 +00:00
|
|
|
this.pages.set(p.name, p);
|
ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
this.pageList.push(p.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
load(name: string = "") {
|
2024-08-28 13:18:52 +00:00
|
|
|
if (!this.pages.has(name)) return window.history.pushState(name || this.defaultName, this.defaultTitle, "")
|
|
|
|
const p = this.pages.get(name);
|
ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
this.loadPage(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadPage (p: Page) {
|
2024-08-28 13:18:52 +00:00
|
|
|
window.history.pushState(p.name || this.defaultName, p.title, p.url + window.location.search);
|
ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prev(name: string = "") {
|
2024-08-28 13:18:52 +00:00
|
|
|
if (!this.pages.has(name)) return console.error(`previous page ${name} not found`);
|
|
|
|
let p = this.pages.get(name);
|
ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
let shouldSkip = true;
|
|
|
|
while (shouldSkip && p.index > 0) {
|
2024-08-28 13:18:52 +00:00
|
|
|
p = this.pages.get(this.pageList[p.index-1]);
|
ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
shouldSkip = p.shouldSkip();
|
|
|
|
}
|
|
|
|
this.loadPage(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
next(name: string = "") {
|
2024-08-28 13:18:52 +00:00
|
|
|
if (!this.pages.has(name)) return console.error(`previous page ${name} not found`);
|
|
|
|
let p = this.pages.get(name);
|
ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
let shouldSkip = true;
|
|
|
|
while (shouldSkip && p.index < this.pageList.length) {
|
2024-08-28 13:18:52 +00:00
|
|
|
p = this.pages.get(this.pageList[p.index+1]);
|
ts: move "page" stuff to module
not the happiest with it, but it works alright. PageManager is
instantiated, you pass is Page{} objects, which have a (code)name, page
title, and url, and a show, hide, and shouldSkip function, each
returning a bool. The first two are self explanatory, the last tells you
if the page is disabled for some reason (like on setup some are disabled
if messages are). You can then call load(<(code)name>), or
prev/next(<name>).
2024-08-27 17:55:28 +00:00
|
|
|
shouldSkip = p.shouldSkip();
|
|
|
|
}
|
|
|
|
this.loadPage(p);
|
|
|
|
}
|
|
|
|
};
|