1
0
Fork 0

Make a PageListType.

This commit is contained in:
Kitteh 2021-05-16 09:59:35 +01:00
parent 5c6e21f349
commit 615de8e5d4
4 changed files with 34 additions and 11 deletions

View file

@ -0,0 +1,6 @@
import { PageType } from "./PageType";
export type PageListType = {
getPageIDs(): Promise<string[]>;
getPage(pageID: string): Promise<PageType>;
}

View file

@ -1,28 +1,25 @@
import { PageType } from "./PageType";
import { getObjectKeys } from "../utils";
type pageList = {
[key: string]: PageType;
};
import { PageListType } from "./PageListType";
const PageDoesNotExistError = new Error("Page does not exist.");
const PageHasNotBeenSetError = new Error("Page has not been set.");
export class PageRouter extends EventTarget {
constructor(
pages: pageList,
pageList: PageListType,
state: unknown,
pageContentElement: HTMLElement,
pageTitleElement: HTMLElement,
) {
super();
this.pages = pages;
this.pageList = pageList;
this.state = state;
this.pageContentElement = pageContentElement;
this.pageTitleElement = pageTitleElement;
}
private pages: pageList;
private pageList: PageListType;
private currentPageID: string;
private currentPage: PageType;
@ -31,7 +28,7 @@ export class PageRouter extends EventTarget {
public pageTitleElement: HTMLElement;
public async getPageIDs(): Promise<string[]> {
return getObjectKeys(this.pages);
return await this.pageList.getPageIDs();
}
public async getCurrentPage(): Promise<PageType> {
@ -61,7 +58,7 @@ export class PageRouter extends EventTarget {
// Set the current page to the new page
this.currentPageID = pageID;
this.currentPage = this.pages[pageID];
this.currentPage = await this.pageList.getPage(pageID);
await this.currentPage.setRouterAndState(this, this.state);

View file

@ -24,6 +24,8 @@ import { TransitRewrapPage } from "./pages/Transit/TransitRewrap";
import { TransitViewPage } from "./pages/Transit/TransitView";
import { TransitViewSecretPage } from "./pages/Transit/TransitViewSecret";
import { UnsealPage } from "./pages/Unseal";
import { getObjectKeys } from "./utils";
import { PageType } from "./PageSystem/PageType";
type pagesList = {
[key: string]: Page;
@ -56,3 +58,21 @@ export const allPages: pagesList = {
NEW_TOTP_ENGINE: new NewTOTPEnginePage(),
NEW_TRANSIT_ENGINE: new NewTransitEnginePage(),
};
// This should implement all o PageListType
class PageList {
constructor(pages: pagesList) {
this.pages = pages;
}
private pages: pagesList;
async getPageIDs(): Promise<string[]> {
return getObjectKeys(this.pages);
}
async getPage(pageID: string): Promise<PageType> {
return this.pages[pageID];
}
}
export const pageList = new PageList(allPages);

View file

@ -21,7 +21,7 @@ Prism.highlightAll();
import { NavBar } from "./elements/NavBar";
import { PageRouter } from "./PageSystem/PageRouter";
import { allPages } from "./allPages";
import { pageList } from "./allPages";
import { formatDistance } from "./formatDistance";
import { getSealStatus } from "./api/sys/getSealStatus";
import { makeElement } from "./htmlUtils";
@ -67,7 +67,7 @@ async function onLoad(): Promise<void> {
window.pageContent = document.querySelector("#pageContent");
const pageRouter = new PageRouter(
allPages,
pageList,
pageState,
document.getElementById("pageContent"),
document.getElementById("pageTitle"),