From 210ee6d19f54303a7fb106bbf04a77909ceb4672 Mon Sep 17 00:00:00 2001 From: Ewoud Date: Thu, 26 Sep 2024 15:37:23 +0200 Subject: [PATCH] index.js supporting gId(pid, id) index.js: - findVar: deal with pid.id (temp) - remove findParentVar and use findVar(pid) - use gid("pid.id") e.g. Print.log, Pins.board, mdlTbl.mdlName, m.moduleName, Instances.insTbl - node.id = variable.pid + variable.id SysModModel - findVar: support pid.id and recursively call using the parent JsonObject - remove findParentVar and use findVar(pid) SysModUI - callVarFun: set rowNr in response --- data/index.js | 87 ++++++++++++++++------------------------- data/newui/modules.js | 33 ++++++++-------- src/Sys/SysModFiles.cpp | 2 +- src/Sys/SysModModel.cpp | 57 ++++++++++++++------------- src/Sys/SysModModel.h | 4 +- src/Sys/SysModUI.h | 1 + 6 files changed, 85 insertions(+), 99 deletions(-) diff --git a/data/index.js b/data/index.js index 646530e6..e8495a10 100644 --- a/data/index.js +++ b/data/index.js @@ -56,7 +56,11 @@ async function fetchModel() { class Modules { findVar(id, parent = model) { // console.log("findVar", id, parent, model); - + //temp: if pid.id, then search for id + let ids = id.split(".") + if (ids[1]) + id = ids[1] + let foundVar = null; for (var variable of parent) { if (foundVar == null) { @@ -69,29 +73,6 @@ class Modules { return foundVar; } - findParentVar(id, parent = model) { - // console.log("findParentVar", id, parent, model); - - let varArray; - if (Array.isArray(parent)) - varArray = parent; - else if (parent.n) - varArray = parent.n; - - let foundVar = null; - - if (varArray) { - for (var variable of varArray) { - if (foundVar == null) { - if (variable.id == id) - foundVar = parent; - else if (variable.n) - foundVar = this.findParentVar(id, variable); //recursive - } - } - } - return foundVar; - } } class Controller { @@ -124,7 +105,7 @@ function onLoad() { } function ppf() { - let logNode = gId("log"); + let logNode = gId("Print.log"); let sep = ""; if (logNode) { // console.log("logNode", logNode); @@ -188,7 +169,7 @@ function makeWS() { if (e.data instanceof ArrayBuffer) { // preview packet let buffer = new Uint8Array(e.data); if (buffer[0]==0) { - let canvasNode = gId("board"); + let canvasNode = gId("Pins.board"); // console.log(buffer, canvasNode); previewBoard(canvasNode, buffer); } @@ -316,7 +297,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) { let ndivNeeded = true; //for details ("n"), module and table do not need an extra div for details let labelNode = cE("label"); - let parentVar = controller.modules.findParentVar(variable.id); + let parentVar = controller.modules.findVar(variable.pid); if (parentVar && variable.id != parentVar.id && parentVar.id && variable.id.substring(0, parentVar.id.length) == parentVar.id) { // if parent id is beginning of the name of the child id then remove that part labelNode.innerText = initCap(variable.id.substring(parentVar.id.length)); // the default when not overridden by onUI } @@ -324,7 +305,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) { labelNode.innerText = initCap(variable.id); // the default when not overridden by onUI divNode = cE("div"); - divNode.id = variable.id + (rowNr != UINT8_MAX?"#" + rowNr:"") + "_d"; + divNode.id = variable.pid + "." + variable.id + (rowNr != UINT8_MAX?"#" + rowNr:"") + "_d"; //table cells and buttons don't get a label if (parentNodeType != "td" && variable.type != "checkbox") { //has its own label @@ -335,7 +316,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) { ndivNeeded = false; varNode = cE("div"); - let mdlName = controller.modules.findVar("mdlName"); + let mdlName = controller.modules.findVar("mdlTbl.mdlName"); if (mdlName && mdlName.value) { //sometimes value not set yet // console.log("createModule", variable, mdlName); let index = mdlName.value.indexOf(variable.id); //find this module @@ -394,7 +375,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) { var command = {}; command.onAdd = {}; - command.onAdd.id = variable.id; + command.onAdd.id = variable.pid + "." + variable.id; requestJson(command); }); divNode.appendChild(buttonNode); @@ -536,7 +517,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) { varNode.max = variable.max?variable.max:255; //range slider default 0..255 varNode.disabled = variable.ro; //numerical ui value changes while draging the slider (oninput) - let rvNode = variable.id + (rowNr != UINT8_MAX?"#" + rowNr:"") + "_rv"; + let rvNode = variable.pid + "." + variable.id + (rowNr != UINT8_MAX?"#" + rowNr:"") + "_rv"; varNode.addEventListener('input', (event) => { if (gId(rvNode)) { gId(rvNode).innerText = variable.log?linearToLogarithm(variable, event.target.value):event.target.value; @@ -635,7 +616,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) { divNode.appendChild(varNode); parentNode.appendChild(divNode); } - varNode.id = variable.id + (rowNr != UINT8_MAX?"#" + rowNr:""); + varNode.id = variable.pid + "." + variable.id + (rowNr != UINT8_MAX?"#" + rowNr:""); varNode.className = variable.type; if (rangeValueNode) divNode.appendChild(rangeValueNode); //_rv value of range / sliders @@ -653,7 +634,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) { //add a div with _n extension and details have this as parent if (ndivNeeded) { let ndivNode = cE("div"); - ndivNode.id = variable.id + (rowNr != UINT8_MAX?"#" + rowNr:"") + "_n"; + ndivNode.id = variable.pid + "." + variable.id + (rowNr != UINT8_MAX?"#" + rowNr:"") + "_n"; ndivNode.className = "ndiv"; divNode.appendChild(ndivNode); // add to the parent of the node createHTML(variable.n, ndivNode, rowNr); @@ -683,7 +664,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) { //call ui Functionality, if defined (to set label, comment, select etc) if (variable.fun >= 0) { //>=0 as element in var - onUICommands.push(variable.id); + onUICommands.push(variable.pid + "." + variable.id); if (onUICommands.length > 4) { //every 4 vars (to respect responseDoc size) check WS_EVT_DATA info flushOnUICommands(); } @@ -712,7 +693,7 @@ function genTableRowHTML(json, parentNode = null, rowNr = UINT8_MAX) { if (!variable.ro) { let tdNode = cE("td"); let buttonNode = cE("input"); - buttonNode.id = variable.id + "#" + rowNr + "_del"; + buttonNode.id = variable.pid + "." + variable.id + "#" + rowNr + "_del"; buttonNode.type = "button"; buttonNode.value = "-"; buttonNode.addEventListener('click', (event) => { @@ -720,7 +701,7 @@ function genTableRowHTML(json, parentNode = null, rowNr = UINT8_MAX) { var command = {}; command.onDelete = {}; - command.onDelete.id = variable.id; + command.onDelete.id = variable.pid + "." + variable.id; command.onDelete.rowNr = rowNr; requestJson(command); @@ -771,12 +752,12 @@ function receiveData(json) { } else if (key == "details") { let variable = value.var; let rowNr = value.rowNr == null?UINT8_MAX:value.rowNr; - let nodeId = variable.id + ((rowNr != UINT8_MAX)?"#" + rowNr:""); + let nodeId = variable.pid + "." + variable.id + ((rowNr != UINT8_MAX)?"#" + rowNr:""); //if var object with .n, create .n (e.g. see fx.onChange (setEffect) and fixtureGenonChange, tbd: ) ppf("receiveData details", key, variable.id, nodeId, rowNr); if (gId(nodeId + "_n")) gId(nodeId + "_n").remove(); //remove old ndiv - let modelVar = controller.modules.findVar(variable.id); + let modelVar = controller.modules.findVar(variable.pid + "." + variable.id); modelVar.n = variable.n; //create new ndiv @@ -790,7 +771,7 @@ function receiveData(json) { flushOnUICommands(); //make sure onUIs of new elements are called } else if (key == "onAdd") { //update the row of a table - ppf("receiveData", key, value); + ppf("receiveData", key, value); //e.g. "onAdd" {"id":"ArtNet.anTbl","rowNr":255} if (value.id && value.rowNr != null) { let tableId = value.id; @@ -800,7 +781,7 @@ function receiveData(json) { let tableNode = gId(tableId); let tbodyNode = tableNode.querySelector("tbody"); - ppf("onAdd ", tableVar, tableNode, rowNr); + console.log("onAdd ", tableVar, tableNode, rowNr); let newRowNr = tbodyNode.querySelectorAll("tr").length; @@ -810,8 +791,8 @@ function receiveData(json) { ppf("dev receiveData onAdd no id and/or rowNr specified", key, value); } else if (key == "onDelete") { //update the row of a table + ppf("receiveData", key, value); //e.g. "onDelete" {"id":"ArtNet.anTbl","rowNr":255} - ppf("receiveData", key, value); let tableId = value.id; let tableVar = controller.modules.findVar(tableId); let rowNr = value.rowNr; @@ -824,7 +805,7 @@ function receiveData(json) { varRemoveValuesForRow(tableVar, rowNr); - ppf("onDelete ", tableVar, tableNode, rowNr); + console.log("onDelete ", tableVar, tableNode, rowNr); } else if (key == "updRow") { //update the row of a table @@ -874,14 +855,14 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) { let node = null; - if (rowNr != UINT8_MAX) node = gId(variable.id + "#" + rowNr); - else node = gId(variable.id); + if (rowNr != UINT8_MAX) node = gId(variable.pid + "." + variable.id + "#" + rowNr); + else node = gId(variable.pid + "." + variable.id); if (!node) { //we should find all nodes - let rowNodes = document.querySelectorAll(`${variable.type}[id^="${variable.id}#"]`); //find nodes from variable.type class with id + #nr (^: starting with) + let rowNodes = document.querySelectorAll(`${variable.type}[id^="${variable.pid + "." + variable.id}#"]`); //find nodes from variable.type class with id + #nr (^: starting with) for (let subNode of rowNodes) { - let rowNr = parseInt(subNode.id.substring(variable.id.length + 1)); + let rowNr = parseInt(subNode.id.substring((variable.pid + "." + variable.id).length + 1)); // console.log("changeHTML found row nodes !", variable.id, subNode.id, commandJson, rowNr); changeHTML(variable, commandJson, rowNr); //recursive call of all nodes } @@ -953,7 +934,7 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) { //check if there are also column select cells which also needs to be updated if (nodeType == "th") { let tableNode = node.parentNode.parentNode.parentNode; - selectNodes = tableNode.querySelector('tbody').querySelectorAll(`select[id*="${variable.id}"]`); + selectNodes = tableNode.querySelector('tbody').querySelectorAll(`select[id*="${variable.pid + "." + variable.id}"]`); } else if (nodeType == "select") { //span/ro will be set in .value selectNodes.push(node); @@ -1076,8 +1057,8 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) { newValue = commandJson.value[newRowNr]; //hide/show disabled/enabled modules if (variable.id == "mdlEnabled") { - let nameVar = controller.modules.findVar("mdlName"); - let mdlNode = nameVar.value?gId(nameVar.value[newRowNr]):null; //Live Server Mode: no value + let nameVar = controller.modules.findVar("mdlTbl.mdlName"); + let mdlNode = nameVar.value?gId("m."+nameVar.value[newRowNr]):null; //Live Server Mode: no value // console.log("mdlEnabled", variable, node, newValue, newRowNr, nameVar, mdlNode); if (mdlNode) { if (mdlNode.hidden && newValue) mdlNode.hidden = false; @@ -1243,7 +1224,7 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) { } //'hack' show the instanceName on top of the page - if (variable.id == "name") { + if (variable.pid == "System" && variable.id == "name") { gId("serverName").innerText = commandJson.value; document.title = commandJson.value; } @@ -1498,8 +1479,8 @@ function setupModule(item) { item.addEventListener('drop', handleDrop); // item.onclick = function() { // console.log("click", this, lastPage); - // if (lastPage) document.getElementById(lastPage.id+"-page").hidden = true; - // document.getElementById(this.id+"-page").hidden = false; + // if (lastPage) gId(lastPage.id+"-page").hidden = true; + // gId(this.id+"-page").hidden = false; // lastPage = this; // }; } @@ -1614,7 +1595,7 @@ function uploadFileWithText(name, text) function setInstanceTableColumns() { - let tbl = gId("insTbl"); + let tbl = gId("Instances.insTbl"); if (!tbl) return; let isDashView = gId("vDash").classList.contains("selected"); let thead = tbl.querySelector("thead"); diff --git a/data/newui/modules.js b/data/newui/modules.js index e1a5649c..e95ad5d9 100644 --- a/data/newui/modules.js +++ b/data/newui/modules.js @@ -91,7 +91,7 @@ class Modules { //finds a var with id in the model, if found returns it findVar(id) { - //temp: of pid.id, then search for id + //temp: if pid.id, then search for id let ids = id.split(".") if (ids[1]) id = ids[1] @@ -235,10 +235,10 @@ class Variable { constructor(variable) { this.variable = variable - this.node = gId(this.variable.pid+"."+variable.id); + this.node = gId(this.variable.pid + "." + this.variable.id); } - createHTML(node = ``) { + createHTML(node = ``) { let code = `

${node} @@ -250,7 +250,7 @@ class Variable { controller.requestJson(command); // this can be done here because of the async nature of requestJson, better is to do it after innerHTML+=... if (this.variable.n) { - code += `

` + code += `
` for (let childVar of this.variable.n) { let childClass = varJsonToClass(childVar) code += childClass.createHTML(); @@ -320,7 +320,7 @@ class TextVariable extends Variable { class CheckboxVariable extends Variable { createHTML() { - return super.createHTML(``); + return super.createHTML(``); } receiveData(properties) { @@ -340,12 +340,12 @@ class CheckboxVariable extends Variable { class RangeVariable extends Variable { createHTML() { - return super.createHTML(`${this.variable.value}`) + return super.createHTML(`${this.variable.value}`) } receiveData(properties) { super.receiveData(properties) - let rvNode = gId(this.variable.pid+"."+this.variable.id + "_rv") + let rvNode = gId(this.variable.pid + "." + this.variable.id + "_rv") if (rvNode && properties.value != null) rvNode.innerText = properties.value } @@ -359,7 +359,7 @@ class RangeVariable extends Variable { class ButtonVariable extends Variable { createHTML() { - return super.createHTML(``) + return super.createHTML(``) } generateData() { @@ -371,7 +371,7 @@ class ButtonVariable extends Variable { class SelectVariable extends Variable { createHTML() { - return super.createHTML(``) + return super.createHTML(``) } receiveData(properties) { @@ -400,7 +400,7 @@ class SelectVariable extends Variable { class ProgressVariable extends Variable { createHTML() { - return super.createHTML(``) + return super.createHTML(``) } } //class ProgressVariable @@ -413,7 +413,7 @@ class CanvasVariable extends Variable { this.variable.file.new = true; // console.log("canvas createHTML", this.variable.file, this.variable.file.new); } - return super.createHTML(``); + return super.createHTML(``); } receiveData(properties) { @@ -421,9 +421,10 @@ class CanvasVariable extends Variable { } generateData() { - //tbd: this is StarLight, remove here... - // if (this.node && this.variable.file == null) - // super.generateData(`"file":"F_panel2x2-16x16.json"`) + //LEDs specific + if (this.node && this.variable.file == null) + super.generateData(`"file":"F_panel2x2-16x16.json"`) + //end LEDs specific super.generateData(`"value":"n/a"`) //no value needed for canvas, but only comment update ... } @@ -433,7 +434,7 @@ class CanvasVariable extends Variable { class TableVariable extends Variable { createHTML() { - return super.createHTML(`
`); + return super.createHTML(`
`); } generateData() { @@ -445,7 +446,7 @@ class TableVariable extends Variable { class Coord3DVariable extends Variable { createHTML() { - return super.createHTML(``); + return super.createHTML(``); } receiveData(properties) { diff --git a/src/Sys/SysModFiles.cpp b/src/Sys/SysModFiles.cpp index 7d37cda9..da3645f0 100644 --- a/src/Sys/SysModFiles.cpp +++ b/src/Sys/SysModFiles.cpp @@ -37,7 +37,7 @@ void SysModFiles::setup() { return true; case onAdd: rowNr = fileNames.size(); - web->getResponseObject()["onAdd"]["rowNr"] = rowNr; + web->getResponseObject()["onAdd"]["rowNr"] = rowNr; //also done in callVarFun?? //add a row with all defaults //tbd: File upload does not call onAdd (bug?) return true; diff --git a/src/Sys/SysModModel.cpp b/src/Sys/SysModModel.cpp index 6cdcc01c..25f9ba55 100644 --- a/src/Sys/SysModModel.cpp +++ b/src/Sys/SysModModel.cpp @@ -149,7 +149,7 @@ void SysModModel::cleanUpModel(JsonObject parent, bool oPos, bool ro) { } } -JsonObject SysModModel::findVar(const char * id, JsonArray parent) { +JsonObject SysModModel::findVar(const char * id, JsonObject parent) { JsonArray root; // print ->print("findVar %s %s\n", id, parent.isNull()?"root":"n"); if (parent.isNull()) { @@ -157,14 +157,39 @@ JsonObject SysModModel::findVar(const char * id, JsonArray parent) { modelParentVar = JsonObject(); } else - root = parent; + root = parent["n"]; + + //temp: if pid.id, then search for id + size_t i = 0; + for (i=0; i ", id); + id+= i+1; + pid[i]='\0'; + ppf("%s and %s\n", pid, id); + // strncpy(pid, id, i); + // ppf(" %s.%s", pid, id); + } + else { + //need to replace vars without pid. ... + // ppf("🐍%s", id); + } + + for (JsonObject var : root) { // if (foundVar.isNull()) { - if (var["id"] == id) + if ((pid[0]=='\0' || var["pid"] == pid) && var["id"] == id) return var; else if (!var["n"].isNull()) { - JsonObject foundVar = findVar(id, var["n"]); + JsonObject foundVar = findVar(id, var); if (!foundVar.isNull()) { if (modelParentVar.isNull()) modelParentVar = var; //only recursive lowest assigns parentVar // ppf("findvar parent of %s is %s\n", id, Variable(modelParentVar).id()); @@ -176,28 +201,6 @@ JsonObject SysModModel::findVar(const char * id, JsonArray parent) { return JsonObject(); } -JsonObject SysModModel::findParentVar(const char * id, JsonObject parent) { - JsonArray varArray; - // print ->print("findParentVar %s %s\n", id, parent.isNull()?"root":"n"); - if (parent.isNull()) { - varArray = model->as(); - } - else - varArray = parent["n"]; - - JsonObject foundVar = JsonObject(); - for (JsonObject var : varArray) { - if (foundVar.isNull()) { - if (var["id"] == id) - foundVar = parent; - else if (!var["n"].isNull()) { - foundVar = findParentVar(id, var); - } - } - } - return foundVar; -} - void SysModModel::findVars(const char * property, bool value, FindFun fun, JsonArray parent) { JsonArray root; // print ->print("findVar %s %s\n", id, parent.isNull()?"root":"n"); @@ -233,7 +236,7 @@ bool checkDash(JsonObject var) { if (var["dash"]) return true; else { - JsonObject parentVar = mdl->findParentVar(var["id"]); + JsonObject parentVar = mdl->findVar(var["pid"]); if (!parentVar.isNull()) return checkDash(parentVar); } diff --git a/src/Sys/SysModModel.h b/src/Sys/SysModModel.h index 292d1baa..6768a883 100644 --- a/src/Sys/SysModModel.h +++ b/src/Sys/SysModModel.h @@ -507,8 +507,8 @@ class SysModModel:public SysModule { } //returns the var defined by id (parent to recursively call findVar) - JsonObject findVar(const char * id, JsonArray parent = JsonArray()); - JsonObject findParentVar(const char * id, JsonObject parent = JsonObject()); + JsonObject findVar(const char * id, JsonObject parent = JsonObject()); + // JsonObject findParentVar(const char * id, JsonObject parent = JsonObject()); void findVars(const char * id, bool value, FindFun fun, JsonArray parent = JsonArray()); //recursively add values in a variant, currently not used diff --git a/src/Sys/SysModUI.h b/src/Sys/SysModUI.h index 53486868..d3b3d9e5 100644 --- a/src/Sys/SysModUI.h +++ b/src/Sys/SysModUI.h @@ -345,6 +345,7 @@ class SysModUI: public SysModule { } } } + web->getResponseObject()[funType==onAdd?"onAdd":"onDelete"]["rowNr"] = rowNr; } //call varFun if exists