1
0
Fork 0
VaultUI/src/pageUtils.ts

164 lines
4.7 KiB
TypeScript
Raw Normal View History

2021-05-07 23:21:38 +01:00
import { Page } from "./types/Page";
import { PageState } from "./PageState";
2021-05-07 11:53:26 +01:00
import { getSealStatus } from "./api/getSealStatus";
import { lookupSelf } from "./api/lookupSelf";
import { makeElement } from "./htmlUtils";
2021-05-07 23:21:38 +01:00
import { pageState } from "./globalPageState";
import ClipboardJS from "clipboard";
import UIkit from 'uikit/dist/js/uikit.min.js';
import i18next from 'i18next';
2021-04-15 13:01:58 +01:00
async function prePageChecksReal() {
if (pageState.language.length == 0) {
changePage("SET_LANGUAGE");
throw new Error("Language Not Set");
}
if (!pageState.apiURL) {
changePage("SET_VAULT_URL");
throw new Error("Vault URL Not Set");
}
2021-05-07 23:21:38 +01:00
const sealStatus = await getSealStatus();
2021-05-03 09:25:42 +01:00
if (sealStatus.sealed) {
changePage("UNSEAL");
throw new Error("Vault Sealed");
}
try {
await lookupSelf();
} catch (e) {
changePage("LOGIN")
throw e;
}
}
2021-05-07 23:21:38 +01:00
export async function prePageChecks(): Promise<boolean> {
try {
await prePageChecksReal();
} catch (e) {
console.log("OHNO", e)
return false;
}
return true;
}
2021-05-07 23:21:38 +01:00
export function addClipboardNotifications(clipboard: ClipboardJS, timeout = 1000): void {
2021-05-07 23:26:33 +01:00
clipboard.on('success', () => {
2021-05-07 23:21:38 +01:00
(UIkit as any).notification(i18next.t("notification_copy_success"), {
status: 'success',
timeout: timeout
});
});
2021-05-07 23:21:38 +01:00
clipboard.on('error', function (e: Error) {
(UIkit as any).notification(i18next.t("notification_copy_error", {
"error": e.message
}), {
status: 'danger',
timeout: timeout
});
});
}
2021-04-15 13:01:58 +01:00
2021-05-07 23:21:38 +01:00
export function setErrorText(text: string): void {
const errorTextElement = document.querySelector("#errorText");
2021-04-15 13:01:58 +01:00
if (errorTextElement) {
2021-05-07 23:21:38 +01:00
(document.querySelector("#errorText") as HTMLElement).innerText = `Error: ${text}`;
2021-04-15 13:01:58 +01:00
}
2021-05-07 23:21:38 +01:00
(UIkit as any).notification({
2021-04-15 13:01:58 +01:00
message: `Error: ${text}`,
status: 'danger',
pos: 'top-center',
timeout: 2000
});
}
2021-05-07 23:21:38 +01:00
export function changePage(page: string, shouldSwitch = true): void {
2021-04-19 20:17:07 +01:00
if (pageState.currentPage && shouldSwitch) {
2021-05-07 23:21:38 +01:00
(pageState.currentPage as Page).cleanup();
2021-04-15 13:01:58 +01:00
}
pageState.currentPage = page;
2021-04-19 20:17:07 +01:00
if (shouldSwitch) {
renderPage();
}
2021-04-15 13:01:58 +01:00
}
2021-05-07 23:21:38 +01:00
export function renderPage(): void {
document.documentElement.dir = pageState.pageDirection;
2021-05-07 23:21:38 +01:00
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();
2021-04-15 13:01:58 +01:00
}
2021-05-07 23:21:38 +01:00
export function setPageTitle(title: string | HTMLElement): void {
const pageTitle = (document.getElementById("pageTitle") as HTMLElement);
2021-04-15 13:01:58 +01:00
pageTitle.innerHTML = "";
2021-05-07 23:21:38 +01:00
if (typeof title === "string") {
pageTitle.innerText = title.toString();
2021-04-15 13:01:58 +01:00
} else {
pageTitle.appendChild(title);
}
}
function currentTitleSecretText() {
2021-04-16 18:57:59 +01:00
let currentSecretText = pageState.currentSecret;
2021-05-07 23:21:38 +01:00
currentSecretText += (pageState.currentPage as Page).titleSuffix;
if (pageState.currentSecretVersion !== null) currentSecretText += ` (v${pageState.currentSecretVersion})`;
2021-04-15 13:01:58 +01:00
return currentSecretText;
}
2021-05-07 23:21:38 +01:00
export function setTitleElement(pageState: PageState): void {
2021-04-15 13:01:58 +01:00
const titleElement = makeElement({
tag: "div",
children: [
makeElement({
tag: "a",
text: pageState.currentBaseMount + " ",
2021-05-07 23:26:33 +01:00
onclick: () => {
2021-04-15 13:01:58 +01:00
pageState.currentSecretPath = [];
pageState.currentSecret = "";
pageState.currentSecretVersion = null;
2021-04-15 13:01:58 +01:00
if (pageState.currentMountType.startsWith("kv") || pageState.currentMountType == "cubbyhole") {
2021-04-17 11:24:43 +01:00
changePage("KEY_VALUE_VIEW");
} else if (pageState.currentMountType == "totp") {
2021-04-17 11:24:43 +01:00
changePage("TOTP");
} else if (pageState.currentMountType == "transit") {
2021-04-17 11:24:43 +01:00
changePage("TRANSIT_VIEW");
2021-04-15 13:01:58 +01:00
}
}
}),
...pageState.currentSecretPath.map(function (secretPath, index, secretPaths) {
return makeElement({
tag: "a",
text: secretPath + " ",
2021-05-07 23:26:33 +01:00
onclick: () => {
pageState.currentSecretVersion = null;
2021-04-15 13:01:58 +01:00
if (pageState.currentMountType.startsWith("kv")) {
pageState.currentSecretPath = secretPaths.slice(0, index + 1);
2021-04-17 11:24:43 +01:00
changePage("KEY_VALUE_VIEW");
2021-04-15 13:01:58 +01:00
}
}
});
}),
makeElement({
tag: "span",
condition: pageState.currentSecret.length != 0,
text: currentTitleSecretText()
})
]
});
setPageTitle(titleElement);
}
2021-05-07 23:21:38 +01:00
export function setPageContent(content: string | HTMLElement): void {
const pageContent = (document.getElementById("pageContent") as HTMLElement);
if (typeof content === "string") {
2021-04-15 13:01:58 +01:00
pageContent.innerHTML = content;
} else {
pageContent.innerHTML = "";
pageContent.appendChild(content);
}
}