Added parsing usermod settings parameters.

Added nested object handling in usermod settings parameters.
This commit is contained in:
Blaz Kristan
2021-06-25 23:20:01 +02:00
parent 2424df0d18
commit 81182bb125
4 changed files with 54 additions and 14 deletions

View File

@@ -29,7 +29,10 @@
if (numM > 0 || locip) ldS();
else gId("um").innerHTML = "No Usermods installed.";
}
function check(o,k) {
// https://stackoverflow.com/questions/3885817/how-do-i-check-that-a-number-is-float-or-integer
function isF(n) { return n === +n && n !== (n|0); }
function isI(n) { return n === +n && n === (n|0); }
function check(o,k) { // input object, pin owner key
var n = o.name.replace("[]","").substr(-3);
if (o.type=="number" && n.substr(0,3)=="pin") {
for (var i=0; i<pins.length; i++) {
@@ -58,10 +61,12 @@
}
}
}
function addField(k,f,o,a=false) {
function addField(k,f,o,a=false) { //key, field, (sub)object, isArray
if (isO(o)) {
for (const [s,v] of Object.entries(o)) {
addField(k,s,v);
// possibility to nest objects (only 1 level)
if (f!=='unknown' && !k.includes("_")) addField(k+"_"+f,s,v);
else addField(k,s,v);
}
} else if (Array.isArray(o)) {
for (var j=0; j<o.length; j++) {
@@ -73,15 +78,26 @@
case "boolean":
t = "checkbox"; c = o ? `checked value="on"` : ""; break;
case "number":
t = "number"; c = `value="${parseInt(o,10)}"`; break;
if (isF(o)) {
c = `value="${parseFloat(o)}" step="0.01"`;
t = "float";
} else {
c = `value="${parseInt(o,10)}"`;
if (f==="pin") c += ' max="39" min="-1"';
t = "int";
}
break;
case "string":
t = "text"; c = `value="${o}"`; break;
default:
t = "text"; c = `value="${o}"`; break;
}
if (k.includes("_")) urows += k.substr(k.indexOf("_")+1);
urows += ` ${f}: `;
// https://stackoverflow.com/questions/11657123/posting-both-checked-and-unchecked-checkboxes
if (t=="checkbox") urows += `<input type="hidden" name="${k}_${f}${a?"[]":""}" value="off">`;
urows += `${f}: <input type="${t}" name="${k}_${f}${a?"[]":""}" ${c} oninput="check(this,'${k}')"><br>`;
else if (!a) urows += `<input type="hidden" name="${k}_${f}${a?"[]":""}" value="${t}">`;
urows += `<input type="${t==="float"||t==="int"?"number":t}" name="${k}_${f}${a?"[]":""}" ${c} oninput="check(this,'${k.substr(k.indexOf("_")+1)}')"><br>`;
}
}
function ldS() {