1
0
Fork 0

Make a Form element to make creating Forms easier.

This commit is contained in:
Kitteh 2021-05-20 16:17:05 +01:00
parent 5a2b22f7fa
commit 5e82683f6c
15 changed files with 459 additions and 493 deletions

18
src/elements/Form.ts Normal file
View file

@ -0,0 +1,18 @@
import { makeElement } from "z-makeelement";
export function Form(
children: Element[],
submit: (form: HTMLFormElement) => unknown,
): HTMLFormElement {
const form = makeElement({
tag: "form",
children: children,
}) as HTMLFormElement;
form.addEventListener("submit", (e: Event) => {
e.preventDefault();
submit(form);
});
return form;
}

View file

@ -1,3 +1,4 @@
import { Form } from "../elements/Form";
import { Margin } from "../elements/Margin"; import { Margin } from "../elements/Margin";
import { MarginInline } from "../elements/MarginInline"; import { MarginInline } from "../elements/MarginInline";
import { Page } from "../types/Page"; import { Page } from "../types/Page";
@ -12,9 +13,8 @@ export class LoginPage extends Page {
super(); super();
} }
async render(): Promise<void> { async render(): Promise<void> {
const tokenLoginForm = makeElement({ const tokenLoginForm = Form(
tag: "form", [
children: [
Margin( Margin(
makeElement({ makeElement({
tag: "input", tag: "input",
@ -38,11 +38,28 @@ export class LoginPage extends Page {
}), }),
), ),
], ],
}) as HTMLFormElement; async (form: HTMLFormElement) => {
const formData = new FormData(form);
const token = formData.get("token");
this.state.token = token as string;
const usernameLoginForm = makeElement({ try {
tag: "form", await lookupSelf();
children: [ await this.router.changePage("HOME");
} catch (e: unknown) {
const error = e as Error;
document.getElementById("tokenInput").classList.add("uk-form-danger");
if (error.message == "permission denied") {
setErrorText(i18next.t("token_login_error"));
} else {
setErrorText(error.message);
}
}
},
);
const usernameLoginForm = Form(
[
Margin( Margin(
makeElement({ makeElement({
tag: "input", tag: "input",
@ -80,7 +97,24 @@ export class LoginPage extends Page {
}), }),
), ),
], ],
}) as HTMLFormElement; async (form: HTMLFormElement) => {
const formData = new FormData(form);
try {
const res = await usernameLogin(
formData.get("username") as string,
formData.get("password") as string,
);
this.state.token = res;
await this.router.changePage("HOME");
} catch (e: unknown) {
const error = e as Error;
document.getElementById("usernameInput").classList.add("uk-form-danger");
document.getElementById("passwordInput").classList.add("uk-form-danger");
setErrorText(error.message);
}
},
);
await this.router.setPageContent( await this.router.setPageContent(
makeElement({ makeElement({
@ -130,44 +164,6 @@ export class LoginPage extends Page {
], ],
}), }),
); );
tokenLoginForm.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(tokenLoginForm);
const token = formData.get("token");
this.state.token = token as string;
try {
await lookupSelf();
await this.router.changePage("HOME");
} catch (e: unknown) {
const error = e as Error;
document.getElementById("tokenInput").classList.add("uk-form-danger");
if (error.message == "permission denied") {
setErrorText(i18next.t("token_login_error"));
} else {
setErrorText(error.message);
}
}
});
usernameLoginForm.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(usernameLoginForm);
try {
const res = await usernameLogin(
formData.get("username") as string,
formData.get("password") as string,
);
this.state.token = res;
await this.router.changePage("HOME");
} catch (e: unknown) {
const error = e as Error;
document.getElementById("usernameInput").classList.add("uk-form-danger");
document.getElementById("passwordInput").classList.add("uk-form-danger");
setErrorText(error.message);
}
});
} }
get name(): string { get name(): string {

View file

@ -1,4 +1,5 @@
import { CopyableInputBox, CopyableInputBoxType } from "../elements/CopyableInputBox"; import { CopyableInputBox, CopyableInputBoxType } from "../elements/CopyableInputBox";
import { Form } from "../elements/Form";
import { Margin } from "../elements/Margin"; import { Margin } from "../elements/Margin";
import { Option } from "../elements/Option"; import { Option } from "../elements/Option";
import { Page } from "../types/Page"; import { Page } from "../types/Page";
@ -90,9 +91,8 @@ export class PwGenPage extends Page {
], ],
}) as HTMLSelectElement; }) as HTMLSelectElement;
this.passwordForm = makeElement({ this.passwordForm = Form(
tag: "form", [
children: [
this.passwordLengthTitle, this.passwordLengthTitle,
Margin(this.passwordLengthRange), Margin(this.passwordLengthRange),
Margin(this.passwordAlphabet), Margin(this.passwordAlphabet),
@ -106,9 +106,8 @@ export class PwGenPage extends Page {
}), }),
), ),
], ],
}) as HTMLFormElement; (_) => this.updatePassword(),
);
this.passwordForm.addEventListener("submit", (e) => this.formEvent(e));
await this.router.setPageContent(this.passwordForm); await this.router.setPageContent(this.passwordForm);
} }
@ -119,11 +118,6 @@ export class PwGenPage extends Page {
}); });
} }
formEvent(e: Event): void {
e.preventDefault();
this.updatePassword();
}
updatePassword(): void { updatePassword(): void {
this.passwordLengthTitle.innerText = this.getPasswordLengthText(); this.passwordLengthTitle.innerText = this.getPasswordLengthText();
this.passwordBox.setText( this.passwordBox.setText(

View file

@ -1,3 +1,4 @@
import { Form } from "../../../elements/Form";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
import { SecretTitleElement } from "../SecretTitleElement"; import { SecretTitleElement } from "../SecretTitleElement";
import { createOrUpdateSecret } from "../../../api/kv/createOrUpdateSecret"; import { createOrUpdateSecret } from "../../../api/kv/createOrUpdateSecret";
@ -14,13 +15,10 @@ export class KeyValueNewPage extends Page {
await this.router.changePage("KEY_VALUE_VIEW"); await this.router.changePage("KEY_VALUE_VIEW");
} }
addKVNewForm: HTMLFormElement;
async render(): Promise<void> { async render(): Promise<void> {
this.addKVNewForm = makeElement({ await this.router.setPageContent(
tag: "form", Form(
id: "addKVNewForm", [
children: [
makeElement({ makeElement({
tag: "div", tag: "div",
class: "uk-margin", class: "uk-margin",
@ -49,17 +47,13 @@ export class KeyValueNewPage extends Page {
}, },
}), }),
], ],
}) as HTMLFormElement; async (form: HTMLFormElement) => await this.newKVSecretHandleForm(form),
await this.router.setPageContent(this.addKVNewForm); ),
);
this.addKVNewForm.addEventListener("submit", async (e: Event) => {
e.preventDefault();
await this.newKVSecretHandleForm();
});
} }
async newKVSecretHandleForm(): Promise<void> { async newKVSecretHandleForm(form: HTMLFormElement): Promise<void> {
const formData = new FormData(this.addKVNewForm); const formData = new FormData(form);
const path = formData.get("path") as string; const path = formData.get("path") as string;
let keyData = {}; let keyData = {};

View file

@ -1,3 +1,4 @@
import { Form } from "../../../elements/Form";
import { Margin } from "../../../elements/Margin"; import { Margin } from "../../../elements/Margin";
import { Option } from "../../../elements/Option"; import { Option } from "../../../elements/Option";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
@ -14,9 +15,9 @@ export class NewKVEnginePage extends Page {
await this.router.changePage("SECRETS_HOME"); await this.router.changePage("SECRETS_HOME");
} }
async render(): Promise<void> { async render(): Promise<void> {
const newEngineForm = makeElement({ await this.router.setPageContent(
tag: "form", Form(
children: [ [
Margin( Margin(
makeElement({ makeElement({
tag: "input", tag: "input",
@ -56,13 +57,8 @@ export class NewKVEnginePage extends Page {
}, },
}), }),
], ],
}) as HTMLFormElement; async (form: HTMLFormElement) => {
const formData = new FormData(form);
await this.router.setPageContent(newEngineForm);
newEngineForm.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(newEngineForm);
const name = formData.get("name") as string; const name = formData.get("name") as string;
const version = formData.get("version") as string; const version = formData.get("version") as string;
@ -82,7 +78,9 @@ export class NewKVEnginePage extends Page {
const error = e as Error; const error = e as Error;
setErrorText(error.message); setErrorText(error.message);
} }
}); },
),
);
} }
get name(): string { get name(): string {
return i18next.t("new_kv_engine_title"); return i18next.t("new_kv_engine_title");

View file

@ -1,3 +1,4 @@
import { Form } from "../../../elements/Form";
import { Margin } from "../../../elements/Margin"; import { Margin } from "../../../elements/Margin";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
import { makeElement } from "z-makeelement"; import { makeElement } from "z-makeelement";
@ -13,9 +14,9 @@ export class NewTOTPEnginePage extends Page {
await this.router.changePage("SECRETS_HOME"); await this.router.changePage("SECRETS_HOME");
} }
async render(): Promise<void> { async render(): Promise<void> {
const newEngineForm = makeElement({ await this.router.setPageContent(
tag: "form", Form(
children: [ [
Margin( Margin(
makeElement({ makeElement({
tag: "input", tag: "input",
@ -42,13 +43,8 @@ export class NewTOTPEnginePage extends Page {
}, },
}), }),
], ],
}) as HTMLFormElement; async (form: HTMLFormElement) => {
const formData = new FormData(form);
await this.router.setPageContent(newEngineForm);
newEngineForm.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(newEngineForm);
const name = formData.get("name") as string; const name = formData.get("name") as string;
@ -64,7 +60,9 @@ export class NewTOTPEnginePage extends Page {
const error = e as Error; const error = e as Error;
setErrorText(error.message); setErrorText(error.message);
} }
}); },
),
);
} }
get name(): string { get name(): string {
return i18next.t("new_totp_engine_title"); return i18next.t("new_totp_engine_title");

View file

@ -1,3 +1,4 @@
import { Form } from "../../../elements/Form";
import { Margin } from "../../../elements/Margin"; import { Margin } from "../../../elements/Margin";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
import { makeElement } from "z-makeelement"; import { makeElement } from "z-makeelement";
@ -13,9 +14,9 @@ export class NewTransitEnginePage extends Page {
await this.router.changePage("SECRETS_HOME"); await this.router.changePage("SECRETS_HOME");
} }
async render(): Promise<void> { async render(): Promise<void> {
const newEngineForm = makeElement({ await this.router.setPageContent(
tag: "form", Form(
children: [ [
Margin( Margin(
makeElement({ makeElement({
tag: "input", tag: "input",
@ -42,13 +43,8 @@ export class NewTransitEnginePage extends Page {
}, },
}), }),
], ],
}) as HTMLFormElement; async (form: HTMLFormElement) => {
const formData = new FormData(form);
await this.router.setPageContent(newEngineForm);
newEngineForm.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(newEngineForm);
const name = formData.get("name") as string; const name = formData.get("name") as string;
@ -64,7 +60,9 @@ export class NewTransitEnginePage extends Page {
const error = e as Error; const error = e as Error;
setErrorText(error.message); setErrorText(error.message);
} }
}); },
),
);
} }
get name(): string { get name(): string {
return i18next.t("new_transit_engine_title"); return i18next.t("new_transit_engine_title");

View file

@ -1,3 +1,4 @@
import { Form } from "../../../elements/Form";
import { Margin } from "../../../elements/Margin"; import { Margin } from "../../../elements/Margin";
import { MarginInline } from "../../../elements/MarginInline"; import { MarginInline } from "../../../elements/MarginInline";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
@ -24,9 +25,9 @@ export class NewTOTPPage extends Page {
await this.router.changePage("TOTP"); await this.router.changePage("TOTP");
} }
async render(): Promise<void> { async render(): Promise<void> {
const totpForm = makeElement({ await this.router.setPageContent(
tag: "form", Form(
children: [ [
Margin( Margin(
makeElement({ makeElement({
tag: "input", tag: "input",
@ -81,13 +82,8 @@ export class NewTOTPPage extends Page {
}), }),
), ),
], ],
}) as HTMLFormElement; async (form: HTMLFormElement) => {
await this.router.setPageContent(totpForm); const formData = new FormData(form);
totpForm.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(totpForm);
const parms = { const parms = {
url: formData.get("uri") as string, url: formData.get("uri") as string,
@ -103,7 +99,9 @@ export class NewTOTPPage extends Page {
const error = e as Error; const error = e as Error;
setErrorText(`API Error: ${error.message}`); setErrorText(`API Error: ${error.message}`);
} }
}); },
),
);
} }
async getPageTitle(): Promise<Element | string> { async getPageTitle(): Promise<Element | string> {

View file

@ -1,3 +1,4 @@
import { Form } from "../../../elements/Form";
import { Margin } from "../../../elements/Margin"; import { Margin } from "../../../elements/Margin";
import { Option } from "../../../elements/Option"; import { Option } from "../../../elements/Option";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
@ -12,9 +13,9 @@ export class NewTransitKeyPage extends Page {
super(); super();
} }
async render(): Promise<void> { async render(): Promise<void> {
const newTransitKeyForm = makeElement({ await this.router.setPageContent(
tag: "form", Form(
children: [ [
Margin( Margin(
makeElement({ makeElement({
tag: "input", tag: "input",
@ -62,13 +63,8 @@ export class NewTransitKeyPage extends Page {
}, },
}), }),
], ],
}) as HTMLFormElement; async (form: HTMLFormElement) => {
const formData = new FormData(form);
await this.router.setPageContent(newTransitKeyForm);
newTransitKeyForm.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(newTransitKeyForm);
const name = formData.get("name") as string; const name = formData.get("name") as string;
const type = formData.get("type") as string; const type = formData.get("type") as string;
@ -84,7 +80,9 @@ export class NewTransitKeyPage extends Page {
const error = e as Error; const error = e as Error;
setErrorText(error.message); setErrorText(error.message);
} }
}); },
),
);
} }
async getPageTitle(): Promise<Element | string> { async getPageTitle(): Promise<Element | string> {

View file

@ -1,5 +1,6 @@
import { CopyableModal } from "../../../elements/CopyableModal"; import { CopyableModal } from "../../../elements/CopyableModal";
import { FileUploadInput } from "../../../elements/FileUploadInput"; import { FileUploadInput } from "../../../elements/FileUploadInput";
import { Form } from "../../../elements/Form";
import { Margin } from "../../../elements/Margin"; import { Margin } from "../../../elements/Margin";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
import { SecretTitleElement } from "../SecretTitleElement"; import { SecretTitleElement } from "../SecretTitleElement";
@ -21,14 +22,8 @@ export class TransitDecryptPage extends Page {
transitDecryptForm: HTMLFormElement; transitDecryptForm: HTMLFormElement;
async render(): Promise<void> { async render(): Promise<void> {
await this.router.setPageContent( this.transitDecryptForm = Form(
makeElement({ [
tag: "div",
}),
);
this.transitDecryptForm = makeElement({
tag: "form",
children: [
Margin( Margin(
makeElement({ makeElement({
tag: "textarea", tag: "textarea",
@ -73,12 +68,9 @@ export class TransitDecryptPage extends Page {
}, },
}), }),
], ],
}) as HTMLFormElement; async (_) => await this.transitDecryptFormHandler(),
);
await this.router.setPageContent(this.transitDecryptForm); await this.router.setPageContent(this.transitDecryptForm);
this.transitDecryptForm.addEventListener("submit", async (e: Event) => {
e.preventDefault();
await this.transitDecryptFormHandler();
});
} }
async transitDecryptFormHandler(): Promise<void> { async transitDecryptFormHandler(): Promise<void> {

View file

@ -1,5 +1,6 @@
import { CopyableModal } from "../../../elements/CopyableModal"; import { CopyableModal } from "../../../elements/CopyableModal";
import { FileUploadInput } from "../../../elements/FileUploadInput"; import { FileUploadInput } from "../../../elements/FileUploadInput";
import { Form } from "../../../elements/Form";
import { Margin } from "../../../elements/Margin"; import { Margin } from "../../../elements/Margin";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
import { SecretTitleElement } from "../SecretTitleElement"; import { SecretTitleElement } from "../SecretTitleElement";
@ -19,16 +20,9 @@ export class TransitEncryptPage extends Page {
} }
transitEncryptForm: HTMLFormElement; transitEncryptForm: HTMLFormElement;
async render(): Promise<void> { async render(): Promise<void> {
await this.router.setPageContent( this.transitEncryptForm = Form(
makeElement({ [
tag: "div",
}),
);
this.transitEncryptForm = makeElement({
tag: "form",
children: [
Margin( Margin(
makeElement({ makeElement({
tag: "textarea", tag: "textarea",
@ -73,13 +67,9 @@ export class TransitEncryptPage extends Page {
}, },
}), }),
], ],
}) as HTMLFormElement; async (_) => await this.transitEncryptFormHandler(),
);
await this.router.setPageContent(this.transitEncryptForm); await this.router.setPageContent(this.transitEncryptForm);
this.transitEncryptForm.addEventListener("submit", async (e: Event) => {
e.preventDefault();
await this.transitEncryptFormHandler();
});
} }
async transitEncryptFormHandler(): Promise<void> { async transitEncryptFormHandler(): Promise<void> {

View file

@ -1,4 +1,5 @@
import { CopyableModal } from "../../../elements/CopyableModal"; import { CopyableModal } from "../../../elements/CopyableModal";
import { Form } from "../../../elements/Form";
import { Margin } from "../../../elements/Margin"; import { Margin } from "../../../elements/Margin";
import { Option } from "../../../elements/Option"; import { Option } from "../../../elements/Option";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
@ -47,9 +48,8 @@ export class TransitRewrapPage extends Page {
}); });
await this.router.setPageContent(""); await this.router.setPageContent("");
this.transitRewrapForm = makeElement({ this.transitRewrapForm = Form(
tag: "form", [
children: [
makeElement({ makeElement({
tag: "select", tag: "select",
name: "version", name: "version",
@ -80,13 +80,9 @@ export class TransitRewrapPage extends Page {
}, },
}), }),
], ],
}) as HTMLFormElement; async (_) => await this.transitRewrapFormHandler(),
);
await this.router.setPageContent(this.transitRewrapForm); await this.router.setPageContent(this.transitRewrapForm);
this.transitRewrapForm.addEventListener("submit", async (e: Event) => {
e.preventDefault();
await this.transitRewrapFormHandler();
});
} }
async transitRewrapFormHandler(): Promise<void> { async transitRewrapFormHandler(): Promise<void> {

View file

@ -1,3 +1,4 @@
import { Form } from "../elements/Form";
import { Margin } from "../elements/Margin"; import { Margin } from "../elements/Margin";
import { Page } from "../types/Page"; import { Page } from "../types/Page";
import { makeElement } from "z-makeelement"; import { makeElement } from "z-makeelement";
@ -14,10 +15,9 @@ export class SetLanguagePage extends Page {
super(); super();
} }
async render(): Promise<void> { async render(): Promise<void> {
const setLanguageForm = makeElement({ await this.router.setPageContent(
tag: "form", Form(
id: "setLanguageForm", [
children: [
Margin( Margin(
makeElement({ makeElement({
tag: "select", tag: "select",
@ -48,11 +48,8 @@ export class SetLanguagePage extends Page {
}, },
}), }),
], ],
}) as HTMLFormElement; async (form: HTMLFormElement) => {
await this.router.setPageContent(setLanguageForm); const formData = new FormData(form);
setLanguageForm.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(setLanguageForm);
const language = formData.get("language") as string; const language = formData.get("language") as string;
this.state.language = language; this.state.language = language;
@ -61,7 +58,9 @@ export class SetLanguagePage extends Page {
this.state.pageDirection = t("language_direction"); this.state.pageDirection = t("language_direction");
reloadNavBar(this.router); reloadNavBar(this.router);
await this.router.changePage("HOME"); await this.router.changePage("HOME");
}); },
),
);
} }
get name(): string { get name(): string {
return i18next.t("set_language_title"); return i18next.t("set_language_title");

View file

@ -1,3 +1,4 @@
import { Form } from "../elements/Form";
import { Page } from "../types/Page"; import { Page } from "../types/Page";
import { makeElement } from "z-makeelement"; import { makeElement } from "z-makeelement";
@ -7,10 +8,8 @@ export class SetVaultURLPage extends Page {
} }
async render(): Promise<void> { async render(): Promise<void> {
await this.router.setPageContent( await this.router.setPageContent(
makeElement({ Form(
tag: "form", [
id: "setVaultURLForm",
children: [
makeElement({ makeElement({
tag: "div", tag: "div",
class: "uk-margin", class: "uk-margin",
@ -39,14 +38,13 @@ export class SetVaultURLPage extends Page {
}, },
}), }),
], ],
}), async (form: HTMLFormElement) => {
); const formData = new FormData(form);
document.getElementById("setVaultURLForm").addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(document.querySelector("#setVaultURLForm"));
this.state.apiURL = formData.get("vaultURL") as string; this.state.apiURL = formData.get("vaultURL") as string;
await this.router.changePage("HOME"); await this.router.changePage("HOME");
}); },
),
);
} }
get name(): string { get name(): string {
return "Set Vault URL"; return "Set Vault URL";

View file

@ -1,3 +1,4 @@
import { Form } from "../elements/Form";
import { MarginInline } from "../elements/MarginInline"; import { MarginInline } from "../elements/MarginInline";
import { Page } from "../types/Page"; import { Page } from "../types/Page";
import { QRScanner, QRScannerType } from "../elements/QRScanner"; import { QRScanner, QRScannerType } from "../elements/QRScanner";
@ -118,9 +119,8 @@ export class UnsealPage extends Page {
} }
makeUnsealForm(): void { makeUnsealForm(): void {
this.unsealKeyForm = makeElement({ this.unsealKeyForm = Form(
tag: "form", [
children: [
MarginInline( MarginInline(
makeElement({ makeElement({
tag: "input", tag: "input",
@ -141,12 +141,11 @@ export class UnsealPage extends Page {
}), }),
), ),
], ],
}) as HTMLFormElement; async (_) => {
this.unsealInputContent.appendChild(this.unsealKeyForm);
this.unsealKeyForm.addEventListener("submit", async (e: Event) => {
e.preventDefault();
await this.handleKeySubmit(); await this.handleKeySubmit();
}); },
);
this.unsealInputContent.appendChild(this.unsealKeyForm);
} }
async makeQRInput(): Promise<void> { async makeQRInput(): Promise<void> {