Skip to content

Commit

Permalink
Rename variable functions to on<event>
Browse files Browse the repository at this point in the history
General
- rename   f_ValueFun to onSetValue
- rename f_UIFun to onUI
- rename f_ChangeFun to onChange
- rename f_LoopFun to onLoop
- rename f_AddRow to onAddRow
- rename f_DelRow to  onDeleteRow,

index.js
- rename uiFunCommands to onUICommands
  • Loading branch information
ewoudwijma committed Jun 24, 2024
1 parent d9f6479 commit a058d34
Show file tree
Hide file tree
Showing 18 changed files with 1,606 additions and 1,601 deletions.
40 changes: 20 additions & 20 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let ws = null;

let nrOfMdlColumns = 4;
let jsonValues = {};
let uiFunCommands = [];
let onUICommands = [];
let model = []; //model.json (as send by the server), used by FindVar
let savedView = null;

Expand Down Expand Up @@ -139,7 +139,7 @@ function makeWS() {

gId("vApp").value = appName(); //tbd: should be set by server

//send request for uiFun
//send request for onUI
flushUIFunCommands();
}
else
Expand Down Expand Up @@ -239,10 +239,10 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {
let labelNode = cE("label");
let parentVar = findParentVar(variable.id);
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 uiFun
labelNode.innerText = initCap(variable.id.substring(parentVar.id.length)); // the default when not overridden by onUI
}
else
labelNode.innerText = initCap(variable.id); // the default when not overridden by uiFun
labelNode.innerText = initCap(variable.id); // the default when not overridden by onUI

divNode = cE("div");
divNode.id = variable.id + (rowNr != UINT8_MAX?"#" + rowNr:"") + "_d";
Expand Down Expand Up @@ -327,7 +327,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {
// console.log("tableChild", parentNode, variable);

varNode = cE("th");
varNode.innerText = initCap(variable.id); //label uiFun response can change it
varNode.innerText = initCap(variable.id); //label onUI response can change it

} else if (variable.type == "select" || variable.type == "pin" || variable.type == "ip") {

Expand Down Expand Up @@ -377,7 +377,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {
varNode = cE("label");
if (parentNodeType != "td") {
let spanNode = cE("span");
spanNode.innerText = initCap(variable.id) + " "; // the default when not overridden by uiFun
spanNode.innerText = initCap(variable.id) + " "; // the default when not overridden by onUI
varNode.appendChild(spanNode);
}
let inputNode = cE("input");
Expand Down Expand Up @@ -528,12 +528,12 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {
createHTML(variable.n, varNode, rowNr); //details (e.g. module)
}

//don't call uiFun on table rows (the table header calls uiFun and propagate this to table row columns in changeHTML when needed - e.g. select)
//don't call onUI on table rows (the table header calls onUI and propagate this to table row columns in changeHTML when needed - e.g. select)
if (variable.fun == null || variable.fun == -2) { //request processed
variable.chk = "gen2";
changeHTML(variable, variable, rowNr); // set the variable with its own changed values
}
else { //uiFun
else { //onUI
if (variable.value)
changeHTML(variable, {"value":variable.value, "chk":"gen1"}, rowNr); //set only the value

Expand All @@ -542,8 +542,8 @@ 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
uiFunCommands.push(variable.id);
if (uiFunCommands.length > 4) { //every 4 vars (to respect responseDoc size) check WS_EVT_DATA info
onUICommands.push(variable.id);
if (onUICommands.length > 4) { //every 4 vars (to respect responseDoc size) check WS_EVT_DATA info
flushUIFunCommands();
}
variable.fun = -1; //requested
Expand Down Expand Up @@ -614,7 +614,7 @@ function receiveData(json) {
//tbd: for each node of a variable (rowNr)

//special commands
if (key == "uiFun") {
if (key == "onUI") {
ppf("receiveData no action", key, value); //should not happen anymore
}
else if (key == "view") {
Expand All @@ -631,7 +631,7 @@ function receiveData(json) {
let variable = value.var;
let rowNr = value.rowNr == null?UINT8_MAX:value.rowNr;
let nodeId = variable.id + ((rowNr != UINT8_MAX)?"#" + rowNr:"");
//if var object with .n, create .n (e.g. see fx.changefun (setEffect) and fixtureGenChFun, tbd: )
//if var object with .n, create .n (e.g. see fx.onChange (setEffect) and fixtureGenChFun, tbd: )
ppf("receiveData details", key, variable.id, nodeId, rowNr);
if (gId(nodeId + "_n")) gId(nodeId + "_n").remove(); //remove old ndiv

Expand All @@ -646,7 +646,7 @@ function receiveData(json) {
gId(nodeId).parentNode.appendChild(ndivNode);
createHTML(modelVar.n, ndivNode, rowNr);
}
flushUIFunCommands(); //make sure uiFuns of new elements are called
flushUIFunCommands(); //make sure onUIs of new elements are called
}
else if (key == "addRow") { //update the row of a table
ppf("receiveData", key, value);
Expand Down Expand Up @@ -716,7 +716,7 @@ function receiveData(json) {
// ppf("receiveData ", variable, value);
variable.fun = -2; // request processed

value.chk = "uiFun";
value.chk = "onUI";
changeHTML(variable, value, rowNr); //changeHTML will find the rownumbers if needed
}
else
Expand Down Expand Up @@ -912,7 +912,7 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {
newRowNr++;
}

flushUIFunCommands(); //make sure uiFuns of new elements are called
flushUIFunCommands(); //make sure onUIs of new elements are called

if (variable.id == "insTbl")
setInstanceTableColumns();
Expand Down Expand Up @@ -958,7 +958,7 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {
changeHTML(variable, {"value":null, "chk":"column"}, newRowNr); //new row cell has no value
}

flushUIFunCommands(); //make sure uiFuns of new elements are called
flushUIFunCommands(); //make sure onUIs of new elements are called

}
else if (node.parentNode.parentNode.nodeName.toLocaleLowerCase() == "td" && Array.isArray(commandJson.value)) { //table column, called for each column cell!!!
Expand Down Expand Up @@ -1033,7 +1033,7 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {
else if (node.className == "select" || node.className == "pin" || node.className == "ip") {
if (variable.ro) {
var index = 0;
if (variable.options && commandJson.value != null) { // not always the case e.g. data / table / uiFun. Then value set if uiFun returns
if (variable.options && commandJson.value != null) { // not always the case e.g. data / table / onUI. Then value set if onUI returns
for (var value of variable.options) {
if (parseInt(commandJson.value) == index) {
// console.log("changeHTML select1", value, node, node.textContent, index);
Expand Down Expand Up @@ -1172,12 +1172,12 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {
} //changeHTML

function flushUIFunCommands() {
if (uiFunCommands.length > 0) { //if something to flush
if (onUICommands.length > 0) { //if something to flush
var command = {};
command.uiFun = uiFunCommands; //ask to run uiFun for vars (to add the options)
command.onUI = onUICommands; //ask to run onUI for vars (to add the options)
// console.log("flushUIFunCommands", command);
requestJson(command);
uiFunCommands = [];
onUICommands = [];
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/App/AppModDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class AppModDemo: public SysModule {
parentVar = ui->initAppMod(parentVar, name, 1100);

JsonObject currentVar = ui->initCheckBox(parentVar, "on", true, false, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun:
case onUI:
ui->setLabel(var, "On");
return true;
case f_ChangeFun:
case onChange:
//implement on/off behaviour
return true;
default: return false;
Expand All @@ -38,10 +38,10 @@ class AppModDemo: public SysModule {

//logarithmic slider (10)
currentVar = ui->initSlider(parentVar, "bri", 10, 0, 255, false, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun:
case onUI:
ui->setLabel(var, "Brightness");
return true;
case f_ChangeFun: {
case onChange: {
return true; }
default: return false;
}});
Expand All @@ -51,11 +51,11 @@ class AppModDemo: public SysModule {
ui->initText(parentVar, "textField", "text");

ui->initPin(parentVar, "blinkPin", blinkPin, false, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun: {
case onUI: {
ui->setLabel(var, "Blink Pin");
ui->setComment(var, "🚧 tbd: reserved and allocated pins");
return true; }
case f_ChangeFun: {
case onChange: {
//deallocate old value...
pins->deallocatePin(var["oldValue"], "Blink");
if (!var["value"].isNull()) {
Expand All @@ -68,7 +68,7 @@ class AppModDemo: public SysModule {
}});

ui->initSlider(parentVar, "frequency", frequency, 0, UINT8_MAX, false, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_ChangeFun:
case onChange:
frequency = var["value"];
return true;
default: return false;
Expand Down
26 changes: 13 additions & 13 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ void SysModFiles::setup() {
parentVar = ui->initSysMod(parentVar, name, 2101);

JsonObject tableVar = ui->initTable(parentVar, "fileTbl", nullptr, false, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun:
case onUI:
ui->setLabel(var, "Files");
ui->setComment(var, "List of files");
return true;
case f_AddRow:
case onAddRow:
rowNr = fileList.size();
web->getResponseObject()["addRow"]["rowNr"] = rowNr;
//add a row with all defaults
return true;
case f_DelRow:
case onDeleteRow:
if (rowNr != UINT8_MAX && rowNr < fileList.size()) {
const char * fileName = fileList[rowNr].name;
// ppf("chFun delRow %s[%d] = %s %s\n", mdl->varID(var), rowNr, var["value"].as<String>().c_str(), fileName);
Expand All @@ -53,55 +53,55 @@ void SysModFiles::setup() {
}});

ui->initText(tableVar, "flName", nullptr, 32, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_ValueFun:
case onSetValue:
for (forUnsigned8 rowNr = 0; rowNr < fileList.size(); rowNr++)
mdl->setValue(var, JsonString(fileList[rowNr].name, JsonString::Copied), rowNr);
return true;
case f_UIFun:
case onUI:
ui->setLabel(var, "Name");
return true;
default: return false;
}});

ui->initNumber(tableVar, "flSize", UINT16_MAX, 0, UINT16_MAX, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_ValueFun:
case onSetValue:
for (forUnsigned8 rowNr = 0; rowNr < fileList.size(); rowNr++)
mdl->setValue(var, fileList[rowNr].size, rowNr);
return true;
case f_UIFun:
case onUI:
ui->setLabel(var, "Size (B)");
return true;
default: return false;
}});

ui->initURL(tableVar, "flLink", nullptr, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_ValueFun:
case onSetValue:
for (forUnsigned8 rowNr = 0; rowNr < fileList.size(); rowNr++) {
char urlString[32] = "file/";
strncat(urlString, fileList[rowNr].name, sizeof(urlString)-1);
mdl->setValue(var, JsonString(urlString, JsonString::Copied), rowNr);
}
return true;
case f_UIFun:
case onUI:
ui->setLabel(var, "Show");
return true;
default: return false;
}});

ui->initFile(parentVar, "upload", nullptr, UINT16_MAX, false, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun:
case onUI:
ui->setLabel(var, "Upload File");
default: return false;
}});

ui->initProgress(parentVar, "drsize", UINT16_MAX, 0, files->totalBytes(), true, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_ValueFun:
case onSetValue:
mdl->setValue(var, files->usedBytes());
return true;
case f_UIFun:
case onUI:
ui->setLabel(var, "FS Size");
return true;
case f_ChangeFun:
case onChange:
var["max"] = files->totalBytes(); //makes sense?
web->addResponseV(var["id"], "comment", "%d / %d B", files->usedBytes(), files->totalBytes());
return true;
Expand Down
Loading

0 comments on commit a058d34

Please sign in to comment.