summaryrefslogtreecommitdiff
path: root/webcards/scripts/gui/input.js
diff options
context:
space:
mode:
Diffstat (limited to 'webcards/scripts/gui/input.js')
-rw-r--r--webcards/scripts/gui/input.js182
1 files changed, 98 insertions, 84 deletions
diff --git a/webcards/scripts/gui/input.js b/webcards/scripts/gui/input.js
index b0bbec0..6ed3d39 100644
--- a/webcards/scripts/gui/input.js
+++ b/webcards/scripts/gui/input.js
@@ -1,29 +1,9 @@
-function customSelectValue (el) {
- var sel = el.getAttribute("selected");
-
- if(typeof sel != "undefined") {
- return el.children[parseInt(sel)].getAttribute("value");
- }
-
- return "";
-}
-
-function customSelectOption (el) {
- var sn = Array.prototype.indexOf.call(el.parentElement.children, el);
- var psn = el.parentElement.getAttribute("selected");
-
- if(typeof psn == "string")
- el.parentElement.children[parseInt(psn)].setAttribute("selected", false);
+'use strict';
- if(typeof sn == "string")
- el.parentElement.setAttribute("selected", parseInt(sn));
-
- el.setAttribute("selected", true);
- el.parentElement.setAttribute("selected", parseInt(sn));
-}
-
-var InputFuncs = {
- createInput: function(type = "text", id) {
+//This whole clusterfuq of functions needs fixing.
+class MakeInput {
+ static createInput(type = "text", id)
+ {
var el = document.createElement("input");
el.setAttribute("type", type);
@@ -35,38 +15,41 @@ var InputFuncs = {
}
return el;
- },
+ }
- inputLabel(text, id) {
+ static inputLabel(text, id)
+ {
var el = document.createElement("label");
el.innerText = text;
if(typeof id == "string")
el.setAttribute("for", id);
return el;
- },
+ }
- colorInput: function(value, id) {
- var el = this.createInput("color", id);
+ static colorInput (value, id) {
+ var el = MakeInput.createInput("color", id);
el.value = value;
return el;
- },
+ }
- textInput: function(value, placeholder, id) {
- var el = this.createInput("text", id);
+ static textInput (value, placeholder, id)
+ {
+ var el = MakeInput.createInput("text", id);
el.setAttribute("placeholder", placeholder);
el.value = value;
return el;
- },
+ }
- numberInput: function(value, id) {
- var el = this.createInput("number", id);
+ static numberInput (value, id)
+ {
+ var el = MakeInput.createInput("number", id);
el.value = value;
return el;
- },
+ }
//To fix
- fileInput: function(value, id) {
- var el = this.createInput("file", id);
+ static fileInput (value, id) {
+ var el = MakeInput.createInput("file", id);
el.value = value;
@@ -89,38 +72,38 @@ var InputFuncs = {
}
return el;
- },
+ }
- checkboxInput: function(checked = false, id) {
- var el = this.createInput("checkbox", false, id);
+ static checkboxInput (checked = false, id) {
+ var el = MakeInput.createInput("checkbox", false, id);
if(checked)
el.setAttribute("checked");
return el;
- },
+ }
- radioInput: function(group, value, checked = false, id) {
- var el = this.createInput("radio", false, id);
+ static radioInput (group, value, checked = false, id) {
+ var el = MakeInput.createInput("radio", false, id);
el.setAttribute("name", group);
el.setAttribute("value", value);
if(checked)
el.checked = true;
return el;
- },
+ }
- radioInputs: function(group, names, values, checked = 0, id) {
+ static radioInputs (group, names, values, checked = 0, id) {
let toWrap = [];
for(let i = 0; i < values.length; i++) {
- toWrap.push(this.inputLabel(names[i], group+"-"+i));
+ toWrap.push(MakeInput.inputLabel(names[i], group+"-"+i));
if(i == checked)
- toWrap.push(this.radioInput(group, values[i], true, group+"-"+i));
+ toWrap.push(MakeInput.radioInput(group, values[i], true, group+"-"+i));
else
- toWrap.push(this.radioInput(group, values[i], false, group+"-"+i));
+ toWrap.push(MakeInput.radioInput(group, values[i], false, group+"-"+i));
toWrap.push(document.createElement("br"));
}
- var wrapper = this.wrapInputs("radio", ...toWrap);
+ var wrapper = MakeInput.wrapInputs("radio", ...toWrap);
wrapper.getValue = function() {
for(let i = 0; i < this.children.length; i++){
@@ -133,9 +116,9 @@ var InputFuncs = {
wrapper.setAttribute("id", id);
return wrapper;
- },
+ }
- selectOption: function(text, value, selected) {
+ static selectOption (text, value, selected) {
var so = document.createElement("div");
so.innerText = text;
so.setAttribute("value", value);
@@ -145,9 +128,9 @@ var InputFuncs = {
so.setAttribute("selected", true);
return so
- },
+ }
- selectInput: function(names, values, id, select = 0) {
+ static selectInput (names, values, id, select = 0) {
var se = document.createElement("div");
se.className = "input-select";
se.setAttribute("tabindex", 0);
@@ -155,20 +138,20 @@ var InputFuncs = {
for(let i in names)
{
- se.appendChild(this.selectOption(names[i], values[i], i == select));
+ se.appendChild(MakeInput.selectOption(names[i], values[i], i == select));
}
if(typeof id == "string")
se.setAttribute("id", id);
- var wrapper = this.wrapInputs("select", se);
- wrapper.getValue = customSelectValue.bind(null, se);
+ var wrapper = MakeInput.wrapInputs("select", se);
+ wrapper.getValue = MakeInput.selValue.bind(null, se);
wrapper.setAttribute("tabindex", 0);
return wrapper;
- },
+ }
- wrapInputs: function(type, ...el) {
+ static wrapInputs (type, ...el) {
var wrapper = document.createElement("div");
wrapper.className = "input-container";
@@ -181,37 +164,68 @@ var InputFuncs = {
return wrapper;
}
-};
-function Settings (settings = {}) {
- this.settings = settings;
-
- this.genSettings();
+ static selValue (el) {
+ let sel = parseInt(el.getAttribute("selected"));
+
+ if(typeof sel != "undefined") {
+ return el.children[sel].getAttribute("value");
+ }
+
+ return "";
+ }
+
+ static selOption (el) {
+ let sn = Array.prototype.indexOf.call(el.parentElement.children, el);
+ let psn = parseInt(el.parentElement.getAttribute("selected"));
+
+ if(Number.isInteger(psn))
+ el.parentElement.children[psn].setAttribute("selected", false);
+
+ el.parentElement.setAttribute("selected", sn);
+ el.setAttribute("selected", true);
+ }
}
-Settings.prototype = {
- getSettings: function() {
- var out = {};
- for(let key in this.settings) {
-
- }
- },
+class Settings {
+ constructor (template = {})
+ {
+ this.settings = Settings.genSettings(template);
+ }
- putSettings: function (el) {
- for(let key in this.settings) {
- el.appendChild(this.settings[key]);
- }
- },
+ static genSettings (template)
+ {
+ var out = {};
- genSettings: function() {
- for(let key in this.settings) {
- switch(this.settings[key].type) {
+ for(let key in template)
+ {
+ switch(template[key].type)
+ {
case "radio":
- this.settings[key] = inputFuncs.radioInputs(...this.settings[key].args);
+ out[key] = MakeInput.radioInputs(...template[key].args);
+ break;
default:
- if(typeof inputFuncs[this.settings[key].type+"Input"] != null)
- this.settings[key] = inputFuncs[this.settings[key].type+"Input"](...this.settings[key].args);
+ if(typeof MakeInput[template[key].type+"Input"] != null)
+ out[key] = MakeInput[template[key].type+"Input"](...template[key].args);
}
}
+
+ return out;
+ }
+
+ getSettings ()
+ {
+ var out = {};
+
+ for(let key in this.settings)
+ out[key] = this.settings[key].getValue();
+
+ return out;
}
-}; \ No newline at end of file
+
+ putSettings (el)
+ {
+ for(let key in this.settings)
+ el.appendChild(this.settings[key]);
+ }
+}