added alert box functionality

This commit is contained in:
dogeystamp 2022-12-30 16:18:53 -05:00
parent 31c6958277
commit d2a140fa79
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
2 changed files with 93 additions and 14 deletions

View File

@ -1,4 +1,11 @@
class Element { class Element {
constructor({id, tag}) {
this.id = id;
if (tag !== undefined) {
this.handle = tag;
}
}
#hidden = false; #hidden = false;
get hidden() { get hidden() {
return this.#hidden; return this.#hidden;
@ -10,21 +17,23 @@ class Element {
if (this.label !== undefined) { if (this.label !== undefined) {
this.label.hidden = this.hidden; this.label.hidden = this.hidden;
} }
if (this.hidden === true) this.clearAlerts();
} }
} }
class Form extends Element { class Form extends Element {
constructor({id, formTag}) { constructor({id, tag}) {
super(); super(id, tag);
this.id = id;
if (formTag !== undefined) { if (tag === undefined) {
this.handle = formTag;
} else {
this.handle = document.createElement("div"); this.handle = document.createElement("div");
} }
this.elements = new Map(); this.elements = new Map();
this.clearAlerts = this.clearAlerts.bind(this);
} }
#advanced = false; #advanced = false;
@ -43,6 +52,12 @@ class Form extends Element {
} }
} }
} }
clearAlerts() {
for (const [id, element] of this.elements.entries()) {
element.clearAlerts();
}
}
} }
function b64ToBuf (b64) { function b64ToBuf (b64) {
@ -74,19 +89,15 @@ class FormElement extends Element {
this.handle.disabled = !this.#enabled; this.handle.disabled = !this.#enabled;
} }
constructor({id, type, form, label="", dataType="plaintext", advanced=false, enabled=true}) { constructor({id, type, form, tag, label="", dataType="plaintext", advanced=false, enabled=true}) {
super(); super(id);
this.id = id; this.id = id;
this.advanced = advanced; this.advanced = advanced;
if (label !== "") {
this.label = document.createElement("label");
this.label.innerHTML = label;
}
this.type = type; this.type = type;
this.clearAlerts = this.clearAlerts.bind(this);
switch (type) { switch (type) {
case "textbox": case "textbox":
this.handle = document.createElement("input"); this.handle = document.createElement("input");
@ -100,6 +111,8 @@ class FormElement extends Element {
break; break;
case "button": case "button":
this.handle = document.createElement("button"); this.handle = document.createElement("button");
this.handle.innerHTML = label;
label = "";
dataType = "none" dataType = "none"
break; break;
case "output": case "output":
@ -110,6 +123,11 @@ class FormElement extends Element {
throw `Unknown input type: ${type}`; throw `Unknown input type: ${type}`;
} }
if (label !== "") {
this.label = document.createElement("label");
this.label.innerHTML = label;
}
this.dataType = dataType; this.dataType = dataType;
this.enabled = enabled; this.enabled = enabled;
@ -181,4 +199,51 @@ class FormElement extends Element {
this.handle.value = bufToB64(x); this.handle.value = bufToB64(x);
} }
} }
alerts = [];
alertBox(type, message, title) {
// type is alert-error or alert-info
if (this.handle === undefined) {
throw `can not add alert for '${this.id}': still undefined`;
}
if (this.hidden === true) {
throw `can not add alert for '${this.id}': hidden`;
}
if (title === undefined) {
switch (type) {
case "alert-info":
title = "Info: ";
break;
case "alert-error":
title = "Error: ";
break;
default:
title = "";
break;
}
}
let box = document.createElement("div");
box.classList.add(type);
box.classList.add("alert");
box.innerHTML = message;
if (title !== "") {
let titleTag = document.createElement("strong");
titleTag.innerHTML = title;
box.prepend(titleTag);
}
this.handle.after(box);
this.alerts.push(box);
}
clearAlerts() {
for (const box of this.alerts) {
box.remove();
}
this.alerts = [];
}
} }

View File

@ -55,6 +55,20 @@ textarea:disabled, input:disabled {
opacity: 40%; opacity: 40%;
} }
.alert {
border-radius: 3px;
margin-top: 0.5em;
padding: 0.75em;
background: #ffffff44;
width: 75%;
}
.alert-error {
background: #ffaaaa44;
}
.alert-info {
background: #aaaaff44;
}
[hidden] { [hidden] {
animation: fadeOut 0.2s, disappear 0.3s; animation: fadeOut 0.2s, disappear 0.3s;
opacity: 0; opacity: 0;