1
0
Fork 0

Move to z-pagerouter library!

This commit is contained in:
Kitteh 2021-05-16 10:27:21 +01:00
parent 615de8e5d4
commit 3b251ee46f
11 changed files with 9 additions and 120 deletions

View file

@ -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"
}
}

View file

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

View file

@ -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<string[]> {
return await this.pageList.getPageIDs();
}
public async getCurrentPage(): Promise<PageType> {
return this.currentPage;
}
public async getCurrentPageID(): Promise<string> {
return this.currentPageID;
}
public async setPageContent(content: string | HTMLElement): Promise<void> {
if (typeof content === "string") {
this.pageContentElement.innerHTML = content;
} else {
this.pageContentElement.innerHTML = "";
this.pageContentElement.appendChild(content);
}
}
public async changePage(pageID: string): Promise<void> {
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<void> {
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<void> {
await this.renderPage();
}
public async goBack(): Promise<void> {
if (!this.currentPage) throw PageHasNotBeenSetError;
await this.currentPage.goBack();
}
}

View file

@ -1,9 +0,0 @@
import { PageRouter } from "./PageRouter";
export type PageType = {
render(): Promise<void>;
getPageTitle(): Promise<Element | string>;
goBack(): Promise<void>;
cleanup(): Promise<void>;
setRouterAndState(router: PageRouter, state: unknown): Promise<void>;
}

View file

@ -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;

View file

@ -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";

View file

@ -1,4 +1,4 @@
import { PageRouter } from "../PageSystem/PageRouter";
import { PageRouter } from "z-pagerouter";
import { makeElement } from "../htmlUtils";
import { PageState } from "../PageState";

View file

@ -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";

View file

@ -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";

View file

@ -1,4 +1,4 @@
import { PageRouter } from "./PageSystem/PageRouter";
import { PageRouter } from "z-pagerouter";
import { PageState } from "./PageState";
import i18next from "i18next";

View file

@ -1,4 +1,4 @@
import { PageRouter } from "../PageSystem/PageRouter";
import { PageRouter } from "z-pagerouter";
import { PageState } from "../PageState";
export class Page {