diff --git a/package.json b/package.json index 0444045..0e39cad 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "uikit": "^3.6.21", "webpack": "^5.36.2", "webpack-cli": "^4.7.0", - "webpack-dev-server": "^3.11.2" + "webpack-dev-server": "^3.11.2", + "z-pagerouter": "^1.0.1" } } diff --git a/src/PageSystem/PageListType.ts b/src/PageSystem/PageListType.ts deleted file mode 100644 index 7057156..0000000 --- a/src/PageSystem/PageListType.ts +++ /dev/null @@ -1,6 +0,0 @@ -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 deleted file mode 100644 index b2ab11f..0000000 --- a/src/PageSystem/PageRouter.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { PageType } from "./PageType"; -import { getObjectKeys } from "../utils"; -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( - pageList: PageListType, - state: unknown, - pageContentElement: HTMLElement, - pageTitleElement: HTMLElement, - ) { - super(); - this.pageList = pageList; - this.state = state; - this.pageContentElement = pageContentElement; - this.pageTitleElement = pageTitleElement; - } - - private pageList: PageListType; - private currentPageID: string; - private currentPage: PageType; - - public state: unknown; - public pageContentElement: HTMLElement; - public pageTitleElement: HTMLElement; - - public async getPageIDs(): Promise { - return await this.pageList.getPageIDs(); - } - - public async getCurrentPage(): Promise { - return this.currentPage; - } - - public async getCurrentPageID(): Promise { - return this.currentPageID; - } - - public async setPageContent(content: string | HTMLElement): Promise { - if (typeof content === "string") { - this.pageContentElement.innerHTML = content; - } else { - this.pageContentElement.innerHTML = ""; - this.pageContentElement.appendChild(content); - } - } - - public async changePage(pageID: string): Promise { - if (!(await this.getPageIDs()).includes(pageID)) throw PageDoesNotExistError; - - // Do cleanup on current page. - if (this.currentPage) { - await this.currentPage.cleanup(); - } - - // Set the current page to the new page - this.currentPageID = pageID; - this.currentPage = await this.pageList.getPage(pageID); - - await this.currentPage.setRouterAndState(this, this.state); - - // Dispatch an event saying the page has been changed. - this.dispatchEvent(new CustomEvent("pageChanged")); - - // Render the page. - await this.renderPage(); - } - - public async renderPage(): Promise { - if (!this.currentPage) throw PageHasNotBeenSetError; - - // Reset back to blank. - this.pageContentElement.innerHTML = ""; - this.pageTitleElement.innerHTML = ""; - - const pageTitle = await this.currentPage.getPageTitle(); - if (typeof pageTitle === "string") { - this.pageTitleElement.innerText = pageTitle; - } else { - this.pageTitleElement.appendChild(pageTitle); - } - - await this.currentPage.render(); - } - - public async refresh(): Promise { - await this.renderPage(); - } - - public async goBack(): Promise { - if (!this.currentPage) throw PageHasNotBeenSetError; - await this.currentPage.goBack(); - } -} diff --git a/src/PageSystem/PageType.ts b/src/PageSystem/PageType.ts deleted file mode 100644 index c8004fd..0000000 --- a/src/PageSystem/PageType.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { PageRouter } from "./PageRouter"; - -export type PageType = { - render(): Promise; - getPageTitle(): Promise; - goBack(): Promise; - cleanup(): Promise; - setRouterAndState(router: PageRouter, state: unknown): Promise; -} \ No newline at end of file diff --git a/src/allPages.ts b/src/allPages.ts index b91c569..286f7fa 100644 --- a/src/allPages.ts +++ b/src/allPages.ts @@ -25,7 +25,7 @@ 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"; +import { PageType } from "z-pagerouter"; type pagesList = { [key: string]: Page; diff --git a/src/elements/NavBar.ts b/src/elements/NavBar.ts index ebf57b3..28eccae 100644 --- a/src/elements/NavBar.ts +++ b/src/elements/NavBar.ts @@ -1,5 +1,5 @@ import { ListItem } from "./ListItem"; -import { PageRouter } from "../PageSystem/PageRouter"; +import { PageRouter } from "z-pagerouter"; import { makeElement } from "../htmlUtils"; import i18next from "i18next"; diff --git a/src/elements/SecretTitleElement.ts b/src/elements/SecretTitleElement.ts index 502bf0f..9c78188 100644 --- a/src/elements/SecretTitleElement.ts +++ b/src/elements/SecretTitleElement.ts @@ -1,4 +1,4 @@ -import { PageRouter } from "../PageSystem/PageRouter"; +import { PageRouter } from "z-pagerouter"; import { makeElement } from "../htmlUtils"; import { PageState } from "../PageState"; diff --git a/src/main.ts b/src/main.ts index 503c445..d6d7286 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,7 +20,7 @@ Prism.highlightAll(); // Actual Imports import { NavBar } from "./elements/NavBar"; -import { PageRouter } from "./PageSystem/PageRouter"; +import { PageRouter } from "z-pagerouter"; import { pageList } from "./allPages"; import { formatDistance } from "./formatDistance"; import { getSealStatus } from "./api/sys/getSealStatus"; diff --git a/src/pageUtils.ts b/src/pageUtils.ts index 6fe94ab..c232798 100644 --- a/src/pageUtils.ts +++ b/src/pageUtils.ts @@ -1,4 +1,4 @@ -import { PageRouter } from "./PageSystem/PageRouter"; +import { PageRouter } from "z-pagerouter"; import { getSealStatus } from "./api/sys/getSealStatus"; import { lookupSelf } from "./api/sys/lookupSelf"; import ClipboardJS from "clipboard"; diff --git a/src/playground.ts b/src/playground.ts index c9bd175..f5dfa18 100644 --- a/src/playground.ts +++ b/src/playground.ts @@ -1,4 +1,4 @@ -import { PageRouter } from "./PageSystem/PageRouter"; +import { PageRouter } from "z-pagerouter"; import { PageState } from "./PageState"; import i18next from "i18next"; diff --git a/src/types/Page.ts b/src/types/Page.ts index a9e895e..2a745f9 100644 --- a/src/types/Page.ts +++ b/src/types/Page.ts @@ -1,4 +1,4 @@ -import { PageRouter } from "../PageSystem/PageRouter"; +import { PageRouter } from "z-pagerouter"; import { PageState } from "../PageState"; export class Page {