From 859717e482ca4f233dea4f02456d2b859c08f51f Mon Sep 17 00:00:00 2001 From: Kitteh Date: Fri, 7 May 2021 23:21:38 +0100 Subject: [PATCH] Add typing to pageUtils.ts. --- src/elements/CopyableInputBox.js | 2 +- src/elements/CopyableModal.js | 2 +- src/main.js | 2 +- src/{pageUtils.js => pageUtils.ts} | 59 ++++++++++++----------- src/pages/Home.js | 2 +- src/pages/KeyValue/KeyValueDelete.js | 2 +- src/pages/KeyValue/KeyValueNew.js | 2 +- src/pages/KeyValue/KeyValueSecret.js | 2 +- src/pages/KeyValue/KeyValueSecretsEdit.js | 2 +- src/pages/KeyValue/KeyValueVersions.js | 2 +- src/pages/KeyValue/KeyValueView.js | 2 +- src/pages/Login.js | 2 +- src/pages/Me.js | 2 +- src/pages/PwGen.js | 2 +- src/pages/SetLanguage.js | 2 +- src/pages/SetVaultURL.js | 2 +- src/pages/TOTP/NewTOTP.js | 2 +- src/pages/TOTP/TOTPView.js | 2 +- src/pages/Template.js | 2 +- src/pages/Transit/TransitDecrypt.js | 2 +- src/pages/Transit/TransitEncrypt.js | 2 +- src/pages/Transit/TransitView.js | 2 +- src/pages/Transit/TransitViewSecret.js | 2 +- src/pages/Unseal.js | 2 +- src/types/Page.js | 2 +- 25 files changed, 54 insertions(+), 53 deletions(-) rename src/{pageUtils.js => pageUtils.ts} (64%) diff --git a/src/elements/CopyableInputBox.js b/src/elements/CopyableInputBox.js index e4a0c1c..7ad2099 100644 --- a/src/elements/CopyableInputBox.js +++ b/src/elements/CopyableInputBox.js @@ -1,5 +1,5 @@ import { MarginInline } from "./MarginInline.js"; -import { addClipboardNotifications } from "../pageUtils.js"; +import { addClipboardNotifications } from "../pageUtils"; import { makeElement } from "../htmlUtils"; import ClipboardJS from "clipboard"; import i18next from "i18next"; diff --git a/src/elements/CopyableModal.js b/src/elements/CopyableModal.js index 829aae0..224360b 100644 --- a/src/elements/CopyableModal.js +++ b/src/elements/CopyableModal.js @@ -1,4 +1,4 @@ -import { addClipboardNotifications } from "../pageUtils.js"; +import { addClipboardNotifications } from "../pageUtils"; import { makeElement } from "../htmlUtils"; import ClipboardJS from "clipboard"; import FileSaver from 'file-saver'; diff --git a/src/main.js b/src/main.js index f4a3713..1fd67b1 100644 --- a/src/main.js +++ b/src/main.js @@ -16,7 +16,7 @@ Prism.highlightAll(); import { changePage, renderPage, -} from "./pageUtils.js"; +} from "./pageUtils"; import { getSealStatus } from "./api/getSealStatus"; import { makeElement } from "./htmlUtils"; import { pageState } from "./globalPageState.ts"; diff --git a/src/pageUtils.js b/src/pageUtils.ts similarity index 64% rename from src/pageUtils.js rename to src/pageUtils.ts index e3d3048..40d57e3 100644 --- a/src/pageUtils.js +++ b/src/pageUtils.ts @@ -1,7 +1,10 @@ +import { Page } from "./types/Page"; +import { PageState } from "./PageState"; import { getSealStatus } from "./api/getSealStatus"; import { lookupSelf } from "./api/lookupSelf"; import { makeElement } from "./htmlUtils"; -import { pageState } from "./globalPageState.ts"; +import { pageState } from "./globalPageState"; +import ClipboardJS from "clipboard"; import UIkit from 'uikit/dist/js/uikit.min.js'; import i18next from 'i18next'; @@ -15,7 +18,7 @@ async function prePageChecksReal() { throw new Error("Vault URL Not Set"); } - let sealStatus = await getSealStatus(); + const sealStatus = await getSealStatus(); if (sealStatus.sealed) { changePage("UNSEAL"); throw new Error("Vault Sealed"); @@ -29,7 +32,7 @@ async function prePageChecksReal() { } } -export async function prePageChecks() { +export async function prePageChecks(): Promise { try { await prePageChecksReal(); } catch (e) { @@ -41,15 +44,15 @@ export async function prePageChecks() { -export function addClipboardNotifications(clipboard, timeout = 1000) { +export function addClipboardNotifications(clipboard: ClipboardJS, timeout = 1000): void { clipboard.on('success', _ => { - UIkit.notification(i18next.t("notification_copy_success"), { + (UIkit as any).notification(i18next.t("notification_copy_success"), { status: 'success', timeout: timeout }); }); - clipboard.on('error', function (e) { - UIkit.notification(i18next.t("notification_copy_error", { + clipboard.on('error', function (e: Error) { + (UIkit as any).notification(i18next.t("notification_copy_error", { "error": e.message }), { status: 'danger', @@ -58,12 +61,12 @@ export function addClipboardNotifications(clipboard, timeout = 1000) { }); } -export function setErrorText(text) { - let errorTextElement = document.querySelector("#errorText"); +export function setErrorText(text: string): void { + const errorTextElement = document.querySelector("#errorText"); if (errorTextElement) { - document.querySelector("#errorText").innerText = `Error: ${text}`; + (document.querySelector("#errorText") as HTMLElement).innerText = `Error: ${text}`; } - UIkit.notification({ + (UIkit as any).notification({ message: `Error: ${text}`, status: 'danger', pos: 'top-center', @@ -71,9 +74,9 @@ export function setErrorText(text) { }); } -export function changePage(page, shouldSwitch = true) { +export function changePage(page: string, shouldSwitch = true): void { if (pageState.currentPage && shouldSwitch) { - pageState.currentPage.cleanup(); + (pageState.currentPage as Page).cleanup(); } pageState.currentPage = page; if (shouldSwitch) { @@ -81,19 +84,19 @@ export function changePage(page, shouldSwitch = true) { } } -export function renderPage() { +export function renderPage(): void { document.documentElement.dir = pageState.pageDirection; - console.log("Rendering Page: ", pageState.currentPage.name); - document.querySelector("#pageContent").innerHTML = ""; - setPageTitle(pageState.currentPage.name); - pageState.currentPage.render(); + console.log("Rendering Page: ", (pageState.currentPage as Page).name); + (document.querySelector("#pageContent") as HTMLElement).innerHTML = ""; + setPageTitle((pageState.currentPage as Page).name); + (pageState.currentPage as Page).render(); } -export function setPageTitle(title) { - let pageTitle = document.getElementById("pageTitle"); +export function setPageTitle(title: string | HTMLElement): void { + const pageTitle = (document.getElementById("pageTitle") as HTMLElement); pageTitle.innerHTML = ""; - if (typeof title === "string" || title instanceof String) { - pageTitle.innerText = title; + if (typeof title === "string") { + pageTitle.innerText = title.toString(); } else { pageTitle.appendChild(title); } @@ -101,13 +104,12 @@ export function setPageTitle(title) { function currentTitleSecretText() { let currentSecretText = pageState.currentSecret; - currentSecretText += pageState.currentPage.titleSuffix; - + currentSecretText += (pageState.currentPage as Page).titleSuffix; if (pageState.currentSecretVersion !== null) currentSecretText += ` (v${pageState.currentSecretVersion})`; return currentSecretText; } -export function setTitleElement(pageState) { +export function setTitleElement(pageState: PageState): void { const titleElement = makeElement({ tag: "div", children: [ @@ -149,12 +151,11 @@ export function setTitleElement(pageState) { ] }); setPageTitle(titleElement); - return titleElement; } -export function setPageContent(content) { - let pageContent = document.getElementById("pageContent"); - if (typeof content === "string" || content instanceof String) { +export function setPageContent(content: string | HTMLElement): void { + const pageContent = (document.getElementById("pageContent") as HTMLElement); + if (typeof content === "string") { pageContent.innerHTML = content; } else { pageContent.innerHTML = ""; diff --git a/src/pages/Home.js b/src/pages/Home.js index 6d1c673..4a1d923 100644 --- a/src/pages/Home.js +++ b/src/pages/Home.js @@ -1,5 +1,5 @@ import { Page } from "../types/Page.js"; -import { changePage, prePageChecks, setErrorText } from "../pageUtils.js"; +import { changePage, prePageChecks, setErrorText } from "../pageUtils"; import { getMounts } from "../api/getMounts"; import { lookupSelf } from "../api/lookupSelf"; import { makeElement } from "../htmlUtils"; diff --git a/src/pages/KeyValue/KeyValueDelete.js b/src/pages/KeyValue/KeyValueDelete.js index b0039ef..a0fdc69 100644 --- a/src/pages/KeyValue/KeyValueDelete.js +++ b/src/pages/KeyValue/KeyValueDelete.js @@ -1,5 +1,5 @@ import { Page } from "../../types/Page.js"; -import { changePage, setPageContent, setTitleElement } from "../../pageUtils.js"; +import { changePage, setPageContent, setTitleElement } from "../../pageUtils"; import { deleteSecret } from "../../api/deleteSecret"; import { makeElement } from "../../htmlUtils"; import { pageState } from "../../globalPageState.ts"; diff --git a/src/pages/KeyValue/KeyValueNew.js b/src/pages/KeyValue/KeyValueNew.js index 4af26d7..f64508d 100644 --- a/src/pages/KeyValue/KeyValueNew.js +++ b/src/pages/KeyValue/KeyValueNew.js @@ -1,5 +1,5 @@ import { Page } from "../../types/Page.js"; -import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils.js"; +import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils"; import { createOrUpdateSecret } from "../../api/createOrUpdateSecret"; import { makeElement } from "../../htmlUtils"; import { pageState } from "../../globalPageState.ts"; diff --git a/src/pages/KeyValue/KeyValueSecret.js b/src/pages/KeyValue/KeyValueSecret.js index bc69f85..3a30449 100644 --- a/src/pages/KeyValue/KeyValueSecret.js +++ b/src/pages/KeyValue/KeyValueSecret.js @@ -1,6 +1,6 @@ import { CopyableInputBox } from "../../elements/CopyableInputBox.js"; import { Page } from "../../types/Page.js"; -import { changePage, setPageContent, setTitleElement } from "../../pageUtils.js"; +import { changePage, setPageContent, setTitleElement } from "../../pageUtils"; import { getCapabilities } from "../../api/getCapabilities"; import { getSecret } from "../../api/getSecret"; import { makeElement } from "../../htmlUtils"; diff --git a/src/pages/KeyValue/KeyValueSecretsEdit.js b/src/pages/KeyValue/KeyValueSecretsEdit.js index adb195f..258146a 100644 --- a/src/pages/KeyValue/KeyValueSecretsEdit.js +++ b/src/pages/KeyValue/KeyValueSecretsEdit.js @@ -1,6 +1,6 @@ import { CodeJar } from "codejar"; import { Page } from "../../types/Page.js"; -import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils.js"; +import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils"; import { createOrUpdateSecret } from "../../api/createOrUpdateSecret.js"; import { getSecret } from "../../api/getSecret.js"; import { makeElement } from "../../htmlUtils"; diff --git a/src/pages/KeyValue/KeyValueVersions.js b/src/pages/KeyValue/KeyValueVersions.js index 97a566e..c534807 100644 --- a/src/pages/KeyValue/KeyValueVersions.js +++ b/src/pages/KeyValue/KeyValueVersions.js @@ -1,5 +1,5 @@ import { Page } from "../../types/Page.js"; -import { changePage, setPageContent, setTitleElement } from "../../pageUtils.js"; +import { changePage, setPageContent, setTitleElement } from "../../pageUtils"; import { getSecretMetadata } from "../../api/getSecretMetadata.js"; import { makeElement } from "../../htmlUtils"; import { objectToMap } from "../../utils"; diff --git a/src/pages/KeyValue/KeyValueView.js b/src/pages/KeyValue/KeyValueView.js index fe32ed2..87f9a56 100644 --- a/src/pages/KeyValue/KeyValueView.js +++ b/src/pages/KeyValue/KeyValueView.js @@ -1,6 +1,6 @@ import { DoesNotExistError } from "../../types/internalErrors.js"; import { Page } from "../../types/Page.js"; -import { changePage, setErrorText, setTitleElement } from "../../pageUtils.js"; +import { changePage, setErrorText, setTitleElement } from "../../pageUtils"; import { getSecrets } from "../../api/getSecrets"; import { makeElement } from "../../htmlUtils"; import { pageState } from "../../globalPageState.ts"; diff --git a/src/pages/Login.js b/src/pages/Login.js index b25da17..2cb80c3 100644 --- a/src/pages/Login.js +++ b/src/pages/Login.js @@ -1,7 +1,7 @@ import { Margin } from "../elements/Margin.js"; import { MarginInline } from "../elements/MarginInline.js"; import { Page } from "../types/Page.js"; -import { changePage, setErrorText, setPageContent } from "../pageUtils.js"; +import { changePage, setErrorText, setPageContent } from "../pageUtils"; import { lookupSelf } from "../api/lookupSelf"; import { makeElement } from "../htmlUtils"; import { pageState } from "../globalPageState.ts"; diff --git a/src/pages/Me.js b/src/pages/Me.js index 6a69f6f..24f33ac 100644 --- a/src/pages/Me.js +++ b/src/pages/Me.js @@ -1,5 +1,5 @@ import { Page } from "../types/Page.js"; -import { addClipboardNotifications, changePage, prePageChecks, setErrorText, setPageContent } from "../pageUtils.js"; +import { addClipboardNotifications, changePage, prePageChecks, setErrorText, setPageContent } from "../pageUtils"; import { getCapabilitiesPath } from "../api/getCapabilities.js"; import { makeElement } from "../htmlUtils"; import { pageState } from "../globalPageState.ts"; diff --git a/src/pages/PwGen.js b/src/pages/PwGen.js index ff293a3..f052a74 100644 --- a/src/pages/PwGen.js +++ b/src/pages/PwGen.js @@ -2,7 +2,7 @@ import { CopyableInputBox } from "../elements/CopyableInputBox.js"; import { Margin } from "../elements/Margin.js"; import { Page } from "../types/Page.js"; import { makeElement } from "../htmlUtils"; -import { setPageContent } from "../pageUtils.js"; +import { setPageContent } from "../pageUtils"; import i18next from 'i18next'; const passwordLengthMin = 1; diff --git a/src/pages/SetLanguage.js b/src/pages/SetLanguage.js index 99235f6..0a269ad 100644 --- a/src/pages/SetLanguage.js +++ b/src/pages/SetLanguage.js @@ -1,6 +1,6 @@ import { Margin } from "../elements/Margin.js"; import { Page } from "../types/Page.js"; -import { changePage, setPageContent } from "../pageUtils.js"; +import { changePage, setPageContent } from "../pageUtils"; import { makeElement } from "../htmlUtils"; import { pageState } from "../globalPageState.ts"; import i18next from 'i18next'; diff --git a/src/pages/SetVaultURL.js b/src/pages/SetVaultURL.js index 849d750..1295d0b 100644 --- a/src/pages/SetVaultURL.js +++ b/src/pages/SetVaultURL.js @@ -1,5 +1,5 @@ import { Page } from "../types/Page.js"; -import { changePage, setPageContent } from "../pageUtils.js"; +import { changePage, setPageContent } from "../pageUtils"; import { makeElement } from "../htmlUtils"; import { pageState } from "../globalPageState.ts"; diff --git a/src/pages/TOTP/NewTOTP.js b/src/pages/TOTP/NewTOTP.js index e1fa965..180c66e 100644 --- a/src/pages/TOTP/NewTOTP.js +++ b/src/pages/TOTP/NewTOTP.js @@ -2,7 +2,7 @@ import { Margin } from "../../elements/Margin.js"; import { MarginInline } from "../../elements/MarginInline.js"; import { Page } from "../../types/Page.js"; import { addNewTOTP } from "../../api/addNewTOTP"; -import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils.js"; +import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils"; import { makeElement } from "../../htmlUtils"; import { pageState } from "../../globalPageState.ts"; import i18next from 'i18next'; diff --git a/src/pages/TOTP/TOTPView.js b/src/pages/TOTP/TOTPView.js index 3f40ebc..adc40dc 100644 --- a/src/pages/TOTP/TOTPView.js +++ b/src/pages/TOTP/TOTPView.js @@ -1,7 +1,7 @@ import { CopyableInputBox } from "../../elements/CopyableInputBox.js"; import { DoesNotExistError } from "../../types/internalErrors.js"; import { Page } from "../../types/Page.js"; -import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils.js"; +import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils"; import { getTOTPCode } from "../../api/getTOTPCode"; import { getTOTPKeys } from "../../api/getTOTPKeys"; import { makeElement } from "../../htmlUtils"; diff --git a/src/pages/Template.js b/src/pages/Template.js index f85bfba..5dff8fb 100644 --- a/src/pages/Template.js +++ b/src/pages/Template.js @@ -1,5 +1,5 @@ import { Page } from "../types/Page.js"; -import { changePage, setPageContent, setTitleElement } from "../pageUtils.js"; +import { changePage, setPageContent, setTitleElement } from "../pageUtils"; import { makeElement } from "../htmlUtils"; import { pageState } from "../globalPageState.ts"; diff --git a/src/pages/Transit/TransitDecrypt.js b/src/pages/Transit/TransitDecrypt.js index 8ffc1c5..3b168b9 100644 --- a/src/pages/Transit/TransitDecrypt.js +++ b/src/pages/Transit/TransitDecrypt.js @@ -1,7 +1,7 @@ import { CopyableModal } from "../../elements/CopyableModal.js"; import { Margin } from "../../elements/Margin.js"; import { Page } from "../../types/Page.js"; -import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils.js"; +import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils"; import { makeElement } from "../../htmlUtils"; import { pageState } from "../../globalPageState.ts"; import { transitDecrypt } from "../../api/transitDecrypt"; diff --git a/src/pages/Transit/TransitEncrypt.js b/src/pages/Transit/TransitEncrypt.js index 6eff514..464816c 100644 --- a/src/pages/Transit/TransitEncrypt.js +++ b/src/pages/Transit/TransitEncrypt.js @@ -1,7 +1,7 @@ import { CopyableModal } from "../../elements/CopyableModal.js"; import { Margin } from "../../elements/Margin.js"; import { Page } from "../../types/Page.js"; -import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils.js"; +import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils"; import { makeElement } from "../../htmlUtils"; import { pageState } from "../../globalPageState.ts"; import { transitEncrypt } from "../../api/transitEncrypt"; diff --git a/src/pages/Transit/TransitView.js b/src/pages/Transit/TransitView.js index 8f19ae9..cf11053 100644 --- a/src/pages/Transit/TransitView.js +++ b/src/pages/Transit/TransitView.js @@ -1,6 +1,6 @@ import { DoesNotExistError } from "../../types/internalErrors.js"; import { Page } from "../../types/Page.js"; -import { changePage, setErrorText, setTitleElement } from "../../pageUtils.js"; +import { changePage, setErrorText, setTitleElement } from "../../pageUtils"; import { getTransitKeys } from "../../api/getTransitKeys"; import { makeElement } from "../../htmlUtils"; import { pageState } from "../../globalPageState.ts"; diff --git a/src/pages/Transit/TransitViewSecret.js b/src/pages/Transit/TransitViewSecret.js index 163c5c6..6583fce 100644 --- a/src/pages/Transit/TransitViewSecret.js +++ b/src/pages/Transit/TransitViewSecret.js @@ -1,5 +1,5 @@ import { Page } from "../../types/Page.js"; -import { changePage, setPageContent, setTitleElement } from "../../pageUtils.js"; +import { changePage, setPageContent, setTitleElement } from "../../pageUtils"; import { makeElement } from "../../htmlUtils"; import { pageState } from "../../globalPageState.ts"; import i18next from 'i18next'; diff --git a/src/pages/Unseal.js b/src/pages/Unseal.js index 89e95a4..fb4b110 100644 --- a/src/pages/Unseal.js +++ b/src/pages/Unseal.js @@ -1,7 +1,7 @@ import { MarginInline } from "../elements/MarginInline.js"; import { Page } from "../types/Page.js"; import { QRScanner } from "../elements/QRScanner.js"; -import { changePage, setErrorText, setPageContent } from "../pageUtils.js"; +import { changePage, setErrorText, setPageContent } from "../pageUtils"; import { getSealStatus } from "../api/getSealStatus.js"; import { makeElement } from "../htmlUtils"; import { submitUnsealKey } from "../api/submitUnsealKey.js"; diff --git a/src/types/Page.js b/src/types/Page.js index 1b3ad62..23b0e3a 100644 --- a/src/types/Page.js +++ b/src/types/Page.js @@ -1,4 +1,4 @@ -import { changePage } from "../pageUtils.js"; +import { changePage } from "../pageUtils"; export class Page { constructor() {