Make a PageListType.
This commit is contained in:
parent
5c6e21f349
commit
615de8e5d4
6
src/PageSystem/PageListType.ts
Normal file
6
src/PageSystem/PageListType.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { PageType } from "./PageType";
|
||||||
|
|
||||||
|
export type PageListType = {
|
||||||
|
getPageIDs(): Promise<string[]>;
|
||||||
|
getPage(pageID: string): Promise<PageType>;
|
||||||
|
}
|
|
@ -1,28 +1,25 @@
|
||||||
import { PageType } from "./PageType";
|
import { PageType } from "./PageType";
|
||||||
import { getObjectKeys } from "../utils";
|
import { getObjectKeys } from "../utils";
|
||||||
|
import { PageListType } from "./PageListType";
|
||||||
type pageList = {
|
|
||||||
[key: string]: PageType;
|
|
||||||
};
|
|
||||||
|
|
||||||
const PageDoesNotExistError = new Error("Page does not exist.");
|
const PageDoesNotExistError = new Error("Page does not exist.");
|
||||||
const PageHasNotBeenSetError = new Error("Page has not been set.");
|
const PageHasNotBeenSetError = new Error("Page has not been set.");
|
||||||
|
|
||||||
export class PageRouter extends EventTarget {
|
export class PageRouter extends EventTarget {
|
||||||
constructor(
|
constructor(
|
||||||
pages: pageList,
|
pageList: PageListType,
|
||||||
state: unknown,
|
state: unknown,
|
||||||
pageContentElement: HTMLElement,
|
pageContentElement: HTMLElement,
|
||||||
pageTitleElement: HTMLElement,
|
pageTitleElement: HTMLElement,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
this.pages = pages;
|
this.pageList = pageList;
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.pageContentElement = pageContentElement;
|
this.pageContentElement = pageContentElement;
|
||||||
this.pageTitleElement = pageTitleElement;
|
this.pageTitleElement = pageTitleElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
private pages: pageList;
|
private pageList: PageListType;
|
||||||
private currentPageID: string;
|
private currentPageID: string;
|
||||||
private currentPage: PageType;
|
private currentPage: PageType;
|
||||||
|
|
||||||
|
@ -31,7 +28,7 @@ export class PageRouter extends EventTarget {
|
||||||
public pageTitleElement: HTMLElement;
|
public pageTitleElement: HTMLElement;
|
||||||
|
|
||||||
public async getPageIDs(): Promise<string[]> {
|
public async getPageIDs(): Promise<string[]> {
|
||||||
return getObjectKeys(this.pages);
|
return await this.pageList.getPageIDs();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getCurrentPage(): Promise<PageType> {
|
public async getCurrentPage(): Promise<PageType> {
|
||||||
|
@ -61,7 +58,7 @@ export class PageRouter extends EventTarget {
|
||||||
|
|
||||||
// Set the current page to the new page
|
// Set the current page to the new page
|
||||||
this.currentPageID = pageID;
|
this.currentPageID = pageID;
|
||||||
this.currentPage = this.pages[pageID];
|
this.currentPage = await this.pageList.getPage(pageID);
|
||||||
|
|
||||||
await this.currentPage.setRouterAndState(this, this.state);
|
await this.currentPage.setRouterAndState(this, this.state);
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,8 @@ import { TransitRewrapPage } from "./pages/Transit/TransitRewrap";
|
||||||
import { TransitViewPage } from "./pages/Transit/TransitView";
|
import { TransitViewPage } from "./pages/Transit/TransitView";
|
||||||
import { TransitViewSecretPage } from "./pages/Transit/TransitViewSecret";
|
import { TransitViewSecretPage } from "./pages/Transit/TransitViewSecret";
|
||||||
import { UnsealPage } from "./pages/Unseal";
|
import { UnsealPage } from "./pages/Unseal";
|
||||||
|
import { getObjectKeys } from "./utils";
|
||||||
|
import { PageType } from "./PageSystem/PageType";
|
||||||
|
|
||||||
type pagesList = {
|
type pagesList = {
|
||||||
[key: string]: Page;
|
[key: string]: Page;
|
||||||
|
@ -56,3 +58,21 @@ export const allPages: pagesList = {
|
||||||
NEW_TOTP_ENGINE: new NewTOTPEnginePage(),
|
NEW_TOTP_ENGINE: new NewTOTPEnginePage(),
|
||||||
NEW_TRANSIT_ENGINE: new NewTransitEnginePage(),
|
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);
|
||||||
|
|
|
@ -21,7 +21,7 @@ Prism.highlightAll();
|
||||||
|
|
||||||
import { NavBar } from "./elements/NavBar";
|
import { NavBar } from "./elements/NavBar";
|
||||||
import { PageRouter } from "./PageSystem/PageRouter";
|
import { PageRouter } from "./PageSystem/PageRouter";
|
||||||
import { allPages } from "./allPages";
|
import { pageList } from "./allPages";
|
||||||
import { formatDistance } from "./formatDistance";
|
import { formatDistance } from "./formatDistance";
|
||||||
import { getSealStatus } from "./api/sys/getSealStatus";
|
import { getSealStatus } from "./api/sys/getSealStatus";
|
||||||
import { makeElement } from "./htmlUtils";
|
import { makeElement } from "./htmlUtils";
|
||||||
|
@ -67,7 +67,7 @@ async function onLoad(): Promise<void> {
|
||||||
window.pageContent = document.querySelector("#pageContent");
|
window.pageContent = document.querySelector("#pageContent");
|
||||||
|
|
||||||
const pageRouter = new PageRouter(
|
const pageRouter = new PageRouter(
|
||||||
allPages,
|
pageList,
|
||||||
pageState,
|
pageState,
|
||||||
document.getElementById("pageContent"),
|
document.getElementById("pageContent"),
|
||||||
document.getElementById("pageTitle"),
|
document.getElementById("pageTitle"),
|
||||||
|
|
Loading…
Reference in a new issue