1
0
Fork 0

Remove global "window.pages".

This commit is contained in:
Kitteh 2021-04-17 11:24:43 +01:00
parent 0a2127b626
commit b79e1856ca
21 changed files with 60 additions and 56 deletions

View file

@ -68,10 +68,10 @@ export class PageState extends Page {
} }
get currentPage() { get currentPage() {
let curPage = localStorage.getItem('currentPage') || "HOME"; let curPage = localStorage.getItem('currentPage') || "HOME";
return pages[curPage]; return realPages[curPage];
} }
set currentPage(value) { set currentPage(value) {
let key = getKeyByObjectPropertyValue(pages, value); let key = getKeyByObjectPropertyValue(realPages, value);
localStorage.setItem('currentPage', key); localStorage.setItem('currentPage', key);
} }
} }

View file

@ -60,7 +60,7 @@ const pages = {
var pageState = new PageState(); var pageState = new PageState();
window.pageState = pageState; window.pageState = pageState;
window.pages = pages; window.realPages = pages;
window.changePage = changePage; window.changePage = changePage;
function ListItem(children) { function ListItem(children) {
@ -85,7 +85,7 @@ document.addEventListener('DOMContentLoaded', function () {
ListItem(makeElement({ ListItem(makeElement({
tag: "a", tag: "a",
text: "Home", text: "Home",
onclick: _ => { changePage(pages.HOME); } onclick: _ => { changePage("HOME"); }
})), })),
ListItem(makeElement({ ListItem(makeElement({
tag: "a", tag: "a",
@ -126,10 +126,10 @@ document.addEventListener('DOMContentLoaded', function () {
renderPage(); renderPage();
setInterval(async () => { setInterval(async () => {
if (pageState.currentPage != pages.UNSEAL) { if (pageState.currentPage != "UNSEAL") {
let sealStatus = await getSealStatus(); let sealStatus = await getSealStatus();
if (sealStatus.sealed) { if (sealStatus.sealed) {
changePage(pages.UNSEAL); changePage("UNSEAL");
return; return;
} }
} }

View file

@ -19,7 +19,11 @@ export function changePage(page) {
if (pageState.currentPage) { if (pageState.currentPage) {
pageState.currentPage.cleanup(); pageState.currentPage.cleanup();
} }
pageState.currentPage = page; if (typeof page == 'object') {
pageState.currentPage = page;
} else {
pageState.currentPage = realPages[page];
}
renderPage(); renderPage();
} }
@ -61,11 +65,11 @@ export function setTitleElement(pageState) {
pageState.currentSecretVersion = "0"; pageState.currentSecretVersion = "0";
if (pageState.currentMountType.startsWith("kv") || pageState.currentMountType == "cubbyhole") { if (pageState.currentMountType.startsWith("kv") || pageState.currentMountType == "cubbyhole") {
changePage(pages.KEY_VALUE_VIEW); changePage("KEY_VALUE_VIEW");
} else if (pageState.currentMountType == "totp"){ } else if (pageState.currentMountType == "totp"){
changePage(pages.TOTP); changePage("TOTP");
} else if (pageState.currentMountType == "transit") { } else if (pageState.currentMountType == "transit") {
changePage(pages.TRANSIT_VIEW); changePage("TRANSIT_VIEW");
} }
} }
}), }),
@ -76,7 +80,7 @@ export function setTitleElement(pageState) {
onclick: _ => { onclick: _ => {
if (pageState.currentMountType.startsWith("kv")) { if (pageState.currentMountType.startsWith("kv")) {
pageState.currentSecretPath = secretPaths.slice(0, index + 1); pageState.currentSecretPath = secretPaths.slice(0, index + 1);
changePage(pages.KEY_VALUE_VIEW); changePage("KEY_VALUE_VIEW");
} }
} }
}); });

View file

@ -13,17 +13,17 @@ export class HomePage extends Page {
async render() { async render() {
pageContent.innerHTML = ""; pageContent.innerHTML = "";
if (!getAPIURL()) { if (!getAPIURL()) {
changePage(pages.SET_VAULT_URL); changePage("SET_VAULT_URL");
return; return;
} }
if (!getToken()) { if (!getToken()) {
localStorage.removeItem("token"); localStorage.removeItem("token");
changePage(pages.LOGIN); changePage("LOGIN");
return; return;
} }
let sealStatus = await getSealStatus(); let sealStatus = await getSealStatus();
if (sealStatus.sealed) { if (sealStatus.sealed) {
changePage(pages.UNSEAL); changePage("UNSEAL");
return; return;
} }
@ -57,7 +57,7 @@ export class HomePage extends Page {
text: "Renew Lease", text: "Renew Lease",
onclick: () => { onclick: () => {
renewSelf().then(() => { renewSelf().then(() => {
changePage(pages.HOME); changePage("HOME");
}).catch(e => { }).catch(e => {
setErrorText(e.message); setErrorText(e.message);
}); });
@ -70,7 +70,7 @@ export class HomePage extends Page {
tag: "a", tag: "a",
text: "Password Generator", text: "Password Generator",
onclick: () => { onclick: () => {
changePage(pages.PW_GEN); changePage("PW_GEN");
} }
}) })
}) })
@ -89,7 +89,7 @@ export class HomePage extends Page {
setErrorText(e.message); setErrorText(e.message);
if (e.message == "permission denied") { if (e.message == "permission denied") {
localStorage.removeItem("token"); localStorage.removeItem("token");
changePage(pages.LOGIN); changePage("LOGIN");
} }
} }
@ -109,7 +109,7 @@ export class HomePage extends Page {
onclick: _ => { onclick: _ => {
pageState.currentBaseMount = "/cubbyhole"; pageState.currentBaseMount = "/cubbyhole";
pageState.currentMountType = "cubbyhole"; pageState.currentMountType = "cubbyhole";
changePage(pages.KEY_VALUE_VIEW); changePage("KEY_VALUE_VIEW");
} }
}) })
})); }));
@ -130,13 +130,13 @@ export class HomePage extends Page {
let linkPage; let linkPage;
if (mount.type == "kv") { if (mount.type == "kv") {
linkText = `K/V (v${mount.options.version}) - ${baseMount}`; linkText = `K/V (v${mount.options.version}) - ${baseMount}`;
linkPage = pages.KEY_VALUE_VIEW; linkPage = "KEY_VALUE_VIEW";
} else if (mount.type == "totp") { } else if (mount.type == "totp") {
linkText = `TOTP - ${baseMount}`; linkText = `TOTP - ${baseMount}`;
linkPage = pages.TOTP; linkPage = "TOTP";
} else if (mount.type == "transit"){ } else if (mount.type == "transit"){
linkText = `Transit - ${baseMount}`; linkText = `Transit - ${baseMount}`;
linkPage = pages.TRANSIT_VIEW; linkPage = "TRANSIT_VIEW";
} }
navList.appendChild(makeElement({ navList.appendChild(makeElement({

View file

@ -10,10 +10,10 @@ export class KeyValueDeletePage extends Page {
goBack() { goBack() {
if (pageState.currentSecretVersion != "0") { if (pageState.currentSecretVersion != "0") {
pageState.currentSecretVersion = "0"; pageState.currentSecretVersion = "0";
changePage(pages.KEY_VALUE_SECRETS); changePage("KEY_VALUE_SECRETS");
} else { } else {
pageState.currentSecret = ""; pageState.currentSecret = "";
changePage(pages.KEY_VALUE_VIEW); changePage("KEY_VALUE_VIEW");
} }
} }
render() { render() {

View file

@ -8,7 +8,7 @@ export class KeyValueNewPage extends Page {
super(); super();
} }
goBack() { goBack() {
changePage(pages.KEY_VALUE_VIEW); changePage("KEY_VALUE_VIEW");
} }
render() { render() {
setTitleElement(pageState); setTitleElement(pageState);
@ -69,7 +69,7 @@ export class KeyValueNewPage extends Page {
splitPath[splitPath.length - 1], splitPath[splitPath.length - 1],
keyData keyData
).then(_ => { ).then(_ => {
changePage(pages.KEY_VALUE_VIEW); changePage("KEY_VALUE_VIEW");
return; return;
}).catch(e => { }).catch(e => {
setErrorText(e.message); setErrorText(e.message);

View file

@ -13,11 +13,11 @@ export class KeyValueSecretsPage extends Page {
goBack() { goBack() {
if (pageState.currentSecretVersion != "0") { if (pageState.currentSecretVersion != "0") {
pageState.currentSecretVersion = "0"; pageState.currentSecretVersion = "0";
changePage(pages.KEY_VALUE_VERSIONS); changePage("KEY_VALUE_VERSIONS");
} else { } else {
pageState.currentSecret = ""; pageState.currentSecret = "";
changePage(pages.KEY_VALUE_VIEW); changePage("KEY_VALUE_VIEW");
} }
} }
@ -57,7 +57,7 @@ export class KeyValueSecretsPage extends Page {
tag: "button", tag: "button",
id: "deleteButton", id: "deleteButton",
class: ["uk-button", "uk-button-danger"], class: ["uk-button", "uk-button-danger"],
onclick: _ => { changePage(pages.KEY_VALUE_DELETE); }, onclick: _ => { changePage("KEY_VALUE_DELETE"); },
text: deleteButtonText text: deleteButtonText
})); }));
} }
@ -67,7 +67,7 @@ export class KeyValueSecretsPage extends Page {
tag: "button", tag: "button",
id: "editButton", id: "editButton",
class: ["uk-button", "uk-margin", "uk-button-primary"], class: ["uk-button", "uk-margin", "uk-button-primary"],
onclick: _ => { changePage(pages.KEY_VALUE_SECRETS_EDIT); }, onclick: _ => { changePage("KEY_VALUE_SECRETS_EDIT"); },
text: "Edit" text: "Edit"
})); }));
} }
@ -77,7 +77,7 @@ export class KeyValueSecretsPage extends Page {
tag: "button", tag: "button",
id: "versionsButton", id: "versionsButton",
class: ["uk-button", "uk-button-secondary"], class: ["uk-button", "uk-button-secondary"],
onclick: _ => { changePage(pages.KEY_VALUE_VERSIONS); }, onclick: _ => { changePage("KEY_VALUE_VERSIONS"); },
text: "Versions" text: "Versions"
})); }));
} }

View file

@ -10,7 +10,7 @@ export class KeyValueSecretsEditPage extends Page {
super(); super();
} }
goBack() { goBack() {
changePage(pages.KEY_VALUE_SECRETS); changePage("KEY_VALUE_SECRETS");
} }
render() { render() {
setTitleElement(pageState); setTitleElement(pageState);
@ -55,7 +55,7 @@ export class KeyValueSecretsEditPage extends Page {
pageState.currentSecret, pageState.currentSecret,
JSON.parse(jar.toString()) JSON.parse(jar.toString())
).then(resp => { ).then(resp => {
changePage(pages.KEY_VALUE_SECRETS); changePage("KEY_VALUE_SECRETS");
return; return;
}).catch(e => { }).catch(e => {
setErrorText(e.message); setErrorText(e.message);

View file

@ -8,7 +8,7 @@ export class KeyValueVersionsPage extends Page {
super(); super();
} }
goBack() { goBack() {
changePage(pages.KEY_VALUE_SECRETS); changePage("KEY_VALUE_SECRETS");
} }
async render() { async render() {
setTitleElement(pageState); setTitleElement(pageState);
@ -34,7 +34,7 @@ export class KeyValueVersionsPage extends Page {
text: `v${ver}`, text: `v${ver}`,
onclick: _ => { onclick: _ => {
pageState.currentSecretVersion = ver; pageState.currentSecretVersion = ver;
changePage(pages.KEY_VALUE_SECRETS); changePage("KEY_VALUE_SECRETS");
} }
}) })
})); }));

View file

@ -10,9 +10,9 @@ export class KeyValueViewPage extends Page {
goBack() { goBack() {
if (pageState.currentSecretPath.length != 0) { if (pageState.currentSecretPath.length != 0) {
pageState.popCurrentSecretPath(); pageState.popCurrentSecretPath();
changePage(pages.KEY_VALUE_VIEW); changePage("KEY_VALUE_VIEW");
} else { } else {
changePage(pages.HOME); changePage("HOME");
} }
} }
async render() { async render() {
@ -25,7 +25,7 @@ export class KeyValueViewPage extends Page {
text: "New", text: "New",
class: ["uk-button", "uk-button-primary", "uk-margin-bottom"], class: ["uk-button", "uk-button-primary", "uk-margin-bottom"],
onclick: () => { onclick: () => {
changePage(pages.KEY_VALUE_NEW_SECRET); changePage("KEY_VALUE_NEW_SECRET");
} }
}); });
pageContent.appendChild(newButton); pageContent.appendChild(newButton);
@ -46,10 +46,10 @@ export class KeyValueViewPage extends Page {
onclick: _ => { onclick: _ => {
if (secret.endsWith("/")) { if (secret.endsWith("/")) {
pageState.pushCurrentSecretPath(secret); pageState.pushCurrentSecretPath(secret);
changePage(pages.KEY_VALUE_VIEW); changePage("KEY_VALUE_VIEW");
} else { } else {
pageState.currentSecret = secret; pageState.currentSecret = secret;
changePage(pages.KEY_VALUE_SECRETS); changePage("KEY_VALUE_SECRETS");
} }
} }
}) })

View file

@ -123,7 +123,7 @@ export class LoginPage extends Page {
const token = formData.get("token"); const token = formData.get("token");
localStorage.setItem('token', token); localStorage.setItem('token', token);
lookupSelf().then(res => { lookupSelf().then(res => {
changePage(pages.HOME); changePage("HOME");
}).catch(e => { }).catch(e => {
document.getElementById("tokenInput").classList.add("uk-form-danger"); document.getElementById("tokenInput").classList.add("uk-form-danger");
setErrorText(e.message); setErrorText(e.message);
@ -134,7 +134,7 @@ export class LoginPage extends Page {
let formData = new FormData(usernameLoginForm); let formData = new FormData(usernameLoginForm);
usernameLogin(formData.get("username"), formData.get("password")).then(res => { usernameLogin(formData.get("username"), formData.get("password")).then(res => {
localStorage.setItem("token", res); localStorage.setItem("token", res);
changePage(pages.HOME); changePage("HOME");
}).catch(e => { }).catch(e => {
document.getElementById("usernameInput").classList.add("uk-form-danger"); document.getElementById("usernameInput").classList.add("uk-form-danger");
document.getElementById("passwordInput").classList.add("uk-form-danger"); document.getElementById("passwordInput").classList.add("uk-form-danger");

View file

@ -44,7 +44,7 @@ export class SetVaultURLPage extends Page {
e.preventDefault(); e.preventDefault();
let formData = new FormData(document.querySelector('#setVaultURLForm')); let formData = new FormData(document.querySelector('#setVaultURLForm'));
localStorage.setItem('apiurl', formData.get("vaultURL")); localStorage.setItem('apiurl', formData.get("vaultURL"));
changePage(pages.HOME); changePage("HOME");
}); });
} }
get name() { get name() {

View file

@ -10,7 +10,7 @@ export class NewTOTPPage extends Page {
super(); super();
} }
goBack() { goBack() {
changePage(pages.TOTP); changePage("TOTP");
} }
render() { render() {
setTitleElement(pageState); setTitleElement(pageState);
@ -78,7 +78,7 @@ export class NewTOTPPage extends Page {
generate: false generate: false
}; };
addNewTOTP(pageState.currentBaseMount, parms).then(res => { addNewTOTP(pageState.currentBaseMount, parms).then(res => {
changePage(pages.TOTP); changePage("TOTP");
}).catch(e => { }).catch(e => {
setErrorText(`API Error: ${e.message}`); setErrorText(`API Error: ${e.message}`);
}); });

View file

@ -18,7 +18,7 @@ export class TOTPViewPage extends Page {
makeElement({ makeElement({
tag: "a", tag: "a",
text: "Add new TOTP", text: "Add new TOTP",
onclick: _ => { changePage(pages.NEW_TOTP); } onclick: _ => { changePage("NEW_TOTP"); }
}), }),
makeElement({ makeElement({
tag: "p", tag: "p",

View file

@ -7,7 +7,7 @@ export class TemplatePage extends Page {
super(); super();
} }
goBack() { goBack() {
changePage(pages.HOME); changePage("HOME");
} }
async render() { async render() {
setTitleElement(pageState); setTitleElement(pageState);

View file

@ -12,7 +12,7 @@ export class TransitDecryptPage extends Page {
super(); super();
} }
goBack() { goBack() {
changePage(pages.TRANSIT_VIEW_SECRET); changePage("TRANSIT_VIEW_SECRET");
} }
async render() { async render() {
setTitleElement(pageState); setTitleElement(pageState);

View file

@ -12,7 +12,7 @@ export class TransitEncryptPage extends Page {
super(); super();
} }
goBack() { goBack() {
changePage(pages.TRANSIT_VIEW_SECRET); changePage("TRANSIT_VIEW_SECRET");
} }
async render() { async render() {
setTitleElement(pageState); setTitleElement(pageState);

View file

@ -8,7 +8,7 @@ export class TransitViewPage extends Page {
super(); super();
} }
goBack() { goBack() {
changePage(pages.HOME); changePage("HOME");
} }
async render() { async render() {
pageState.currentSecret = ""; pageState.currentSecret = "";
@ -20,7 +20,7 @@ export class TransitViewPage extends Page {
text: "New", text: "New",
class: ["uk-button", "uk-button-primary", "uk-margin-bottom"], class: ["uk-button", "uk-button-primary", "uk-margin-bottom"],
onclick: () => { onclick: () => {
changePage(pages.TRANSIT_NEW_KEY); changePage("TRANSIT_NEW_KEY");
} }
}); });
pageContent.appendChild(newButton); pageContent.appendChild(newButton);
@ -40,7 +40,7 @@ export class TransitViewPage extends Page {
text: secret, text: secret,
onclick: _ => { onclick: _ => {
pageState.currentSecret = secret; pageState.currentSecret = secret;
changePage(pages.TRANSIT_VIEW_SECRET); changePage("TRANSIT_VIEW_SECRET");
} }
}) })
}); });

View file

@ -7,7 +7,7 @@ export class TransitViewSecretPage extends Page {
super(); super();
} }
goBack() { goBack() {
changePage(pages.TRANSIT_VIEW); changePage("TRANSIT_VIEW");
} }
makeTile(title, description, icon = "code", onclick = _ => { }) { makeTile(title, description, icon = "code", onclick = _ => { }) {
@ -57,13 +57,13 @@ export class TransitViewSecretPage extends Page {
"Encrypt", "Encrypt",
"Encrypt some plaintext or base64 encoded binary.", "Encrypt some plaintext or base64 encoded binary.",
"lock", "lock",
_ => { changePage(pages.TRANSIT_ENCRYPT); } _ => { changePage("TRANSIT_ENCRYPT"); }
), ),
this.makeTile( this.makeTile(
"Decrypt", "Decrypt",
"Decrypt some cyphertext.", "Decrypt some cyphertext.",
"mail", "mail",
_ => { changePage(pages.TRANSIT_DECRYPT); } _ => { changePage("TRANSIT_DECRYPT"); }
), ),
] ]
}), }),

View file

@ -70,7 +70,7 @@ export class UnsealPage extends Page {
progressBar.max = keysNeeded; progressBar.max = keysNeeded;
if (!data.sealed) { if (!data.sealed) {
progressBar.value = keysNeeded; progressBar.value = keysNeeded;
changePage(pages.HOME); changePage("HOME");
} }
} }

View file

@ -11,7 +11,7 @@ export class Page {
return ""; return "";
} }
goBack() { goBack() {
changePage(pages.HOME); changePage("HOME");
} }
cleanup() { cleanup() {
} }