From 615de8e5d401d553103036c24d8d5c46a795521d Mon Sep 17 00:00:00 2001 From: Kitteh Date: Sun, 16 May 2021 09:59:35 +0100 Subject: [PATCH] Make a PageListType. --- src/PageSystem/PageListType.ts | 6 ++++++ src/PageSystem/PageRouter.ts | 15 ++++++--------- src/allPages.ts | 20 ++++++++++++++++++++ src/main.ts | 4 ++-- 4 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 src/PageSystem/PageListType.ts diff --git a/src/PageSystem/PageListType.ts b/src/PageSystem/PageListType.ts new file mode 100644 index 0000000..7057156 --- /dev/null +++ b/src/PageSystem/PageListType.ts @@ -0,0 +1,6 @@ +import { PageType } from "./PageType"; + +export type PageListType = { + getPageIDs(): Promise; + getPage(pageID: string): Promise; +} \ No newline at end of file diff --git a/src/PageSystem/PageRouter.ts b/src/PageSystem/PageRouter.ts index 24b3a31..b2ab11f 100644 --- a/src/PageSystem/PageRouter.ts +++ b/src/PageSystem/PageRouter.ts @@ -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 { - return getObjectKeys(this.pages); + return await this.pageList.getPageIDs(); } public async getCurrentPage(): Promise { @@ -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); diff --git a/src/allPages.ts b/src/allPages.ts index 175beee..b91c569 100644 --- a/src/allPages.ts +++ b/src/allPages.ts @@ -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 { + return getObjectKeys(this.pages); + } + async getPage(pageID: string): Promise { + return this.pages[pageID]; + } +} + +export const pageList = new PageList(allPages); diff --git a/src/main.ts b/src/main.ts index fd10dae..503c445 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 { window.pageContent = document.querySelector("#pageContent"); const pageRouter = new PageRouter( - allPages, + pageList, pageState, document.getElementById("pageContent"), document.getElementById("pageTitle"),