interface.js: added visibleFunc parameter

This commit is contained in:
dogeystamp 2023-01-28 17:29:12 -05:00
parent d271b59da4
commit fd0a0e06b0
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38

View File

@ -15,7 +15,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
class InterfaceElement { class InterfaceElement {
rootNodes = []; rootNodes = [];
constructor({fragment, enabledFunc}) { constructor({fragment, enabledFunc, visibleFunc}) {
if (fragment === undefined) { if (fragment === undefined) {
this.fragment = new DocumentFragment(); this.fragment = new DocumentFragment();
} else { } else {
@ -27,6 +27,16 @@ class InterfaceElement {
} else { } else {
this.enabledFunc = enabledFunc; this.enabledFunc = enabledFunc;
} }
if (visibleFunc === undefined) {
this.visibleFunc = function(){ return true; };
} else {
this.visibleFunc = visibleFunc;
}
}
update() {
this.enabled = this.enabledFunc();
this.hidden = !this.visibleFunc();
} }
scanNodes() { scanNodes() {
@ -128,11 +138,7 @@ class Form extends InterfaceElement {
this.#advanced = x; this.#advanced = x;
for (const element of this.elements) { for (const element of this.elements) {
if (element.advanced === true) { if (element.advanced === true) {
if (this.advanced === true) { element.update();
element.hidden = false;
} else {
element.hidden = true;
}
} }
} }
} }
@ -143,7 +149,9 @@ class Form extends InterfaceElement {
} }
} }
appendElement(elem) { createElem(params) {
params.form = this;
let elem = new FormElement(params);
elem.mount(this.handle); elem.mount(this.handle);
this.elements.push(elem); this.elements.push(elem);
this.rootNodes.push(...elem.rootNodes); this.rootNodes.push(...elem.rootNodes);
@ -160,27 +168,27 @@ class Form extends InterfaceElement {
let labelTag = document.createTextNode(params.label); let labelTag = document.createTextNode(params.label);
params.tag.appendChild(labelTag); params.tag.appendChild(labelTag);
params.label = undefined; params.label = undefined;
return this.appendElement(new FormElement(params)); return this.createElem(params);
} }
createTextBox(params) { createTextBox(params) {
params.tag = document.createElement("input"); params.tag = document.createElement("input");
dataTypeSupports(params, ["plaintext", "b64", "json-b64"]); dataTypeSupports(params, ["plaintext", "b64", "json-b64"]);
return this.appendElement(new FormElement(params)); return this.createElem(params);
} }
createMediumTextBox(params) { createMediumTextBox(params) {
params.tag = document.createElement("textarea"); params.tag = document.createElement("textarea");
params.tag.classList.add("mediumbox") params.tag.classList.add("mediumbox")
dataTypeSupports(params, ["plaintext", "b64", "json-b64"]); dataTypeSupports(params, ["plaintext", "b64", "json-b64"]);
return this.appendElement(new FormElement(params)); return this.createElem(params);
} }
createPasswordInput(params) { createPasswordInput(params) {
params.tag = document.createElement("input"); params.tag = document.createElement("input");
params.tag.setAttribute("type", "password"); params.tag.setAttribute("type", "password");
dataTypeSupports(params, ["plaintext"]); dataTypeSupports(params, ["plaintext"]);
return this.appendElement(new FormElement(params)); return this.createElem(params);
} }
createNumberInput(params) { createNumberInput(params) {
@ -191,7 +199,7 @@ class Form extends InterfaceElement {
if (params.minValue !== undefined) params.tag.min = params.minValue; if (params.minValue !== undefined) params.tag.min = params.minValue;
if (params.step !== undefined) params.tag.step = params.step; if (params.step !== undefined) params.tag.step = params.step;
if (params.required !== undefined) params.tag.required = params.required; if (params.required !== undefined) params.tag.required = params.required;
return this.appendElement(new FormElement(params)); return this.createElem(params);
} }
createDropDown(params) { createDropDown(params) {
@ -221,13 +229,18 @@ class Form extends InterfaceElement {
optTag.appendChild(document.createTextNode(option.name)); optTag.appendChild(document.createTextNode(option.name));
params.tag.appendChild(optTag); params.tag.appendChild(optTag);
} }
return this.appendElement(new FormElement(params)); params.tag.addEventListener("change", function() {
for (const elem of this.elements) {
elem.update();
}
}.bind(this));
return this.createElem(params);
} }
createTextArea(params) { createTextArea(params) {
params.tag = document.createElement("textarea"); params.tag = document.createElement("textarea");
dataTypeSupports(params, ["plaintext", "b64", "json-b64"]); dataTypeSupports(params, ["plaintext", "b64", "json-b64"]);
return this.appendElement(new FormElement(params)); return this.createElem(params);
} }
createButton(params) { createButton(params) {
@ -237,7 +250,7 @@ class Form extends InterfaceElement {
params.tag.appendChild(params.labelTag); params.tag.appendChild(params.labelTag);
params.fragment.appendChild(params.tag); params.fragment.appendChild(params.tag);
dataTypeSupports(params, ["none"]); dataTypeSupports(params, ["none"]);
return this.appendElement(new FormElement(params)); return this.createElem(params);
} }
createCheckBox(params) { createCheckBox(params) {
@ -254,17 +267,17 @@ class Form extends InterfaceElement {
dataTypeSupports(params, ["bool"]); dataTypeSupports(params, ["bool"]);
params.tag.addEventListener("change", function() { params.tag.addEventListener("change", function() {
for (const elem of this.elements) { for (const elem of this.elements) {
elem.enabled = elem.enabledFunc(); elem.update();
} }
}.bind(this)); }.bind(this));
return this.appendElement(new FormElement(params)); return this.createElem(params);
} }
createOutput(params) { createOutput(params) {
params.tag = document.createElement("textarea"); params.tag = document.createElement("textarea");
params.tag.setAttribute("readonly", true); params.tag.setAttribute("readonly", true);
dataTypeSupports(params, ["plaintext", "b64", "json-b64"]); dataTypeSupports(params, ["plaintext", "b64", "json-b64"]);
return this.appendElement(new FormElement(params)); return this.createElem(params);
} }
} }
@ -288,8 +301,36 @@ function bufToB64 (buf) {
} }
class FormElement extends InterfaceElement { class FormElement extends InterfaceElement {
constructor({tag, labelTag, label="", value, fragment, dataType, advanced=false, enabled=true, enabledFunc}) { constructor({tag, fragment, advanced=false, form,
super({fragment, enabled, enabledFunc}); value, dataType,
labelTag, label="",
enabled=true, enabledFunc,
visibleFunc
}) {
let oriVisibleFunc = visibleFunc;
super({
fragment,
enabledFunc,
visibleFunc: function() {
let res;
if (oriVisibleFunc) {
res = oriVisibleFunc()
} else {
res = true;
}
if (form !== undefined) {
if (advanced) {
if (form.advanced) return res;
else return false;
}
}
return res;
}
});
this.form = form;
this.labelText = label; this.labelText = label;
if (labelTag === undefined) { if (labelTag === undefined) {
@ -307,8 +348,6 @@ class FormElement extends InterfaceElement {
this.dataType = dataType; this.dataType = dataType;
this.advanced = advanced; this.advanced = advanced;
if (this.advanced === true) this.hidden = true;
if (value !== undefined) this.value = value; if (value !== undefined) this.value = value;
} }