auto-replace long functions with short names

adI = addInfo
adD = addDropdown
adO = addOption
adF = addField

before: String buffer usage: 3183 of 4037 bytes
after:    String buffer usage: 2805 of 4037 bytes
This commit is contained in:
Frank
2024-07-01 22:54:24 +02:00
parent 31a12f5098
commit 29013a3f83
3 changed files with 28 additions and 3 deletions

View File

@@ -146,6 +146,9 @@
urows += `<br>`;
}
}
function adF(k,f,o,a=false) { //shortcut for addField(key, field, (sub)object, isArray)
return addField(k,f,o,a);
}
// https://stackoverflow.com/questions/39729741/javascript-change-input-text-to-select-option
function addDropdown(um,fld) {
let sel = d.createElement('select');
@@ -170,6 +173,9 @@
}
return null;
}
function adD(um,fld) { // shortcut for addDropdown(um,fld)
return addDropdown(um,fld);
}
function addOption(sel,txt,val) {
if (sel===null) return; // select object missing
let opt = d.createElement("option");
@@ -181,6 +187,9 @@
if (c.value == sel.dataset.val) sel.selectedIndex = i;
}
}
function adO(sel,txt,val) { // shortcut for addOption(sel,txt,val)
return addOption(sel,txt,val);
}
//WLEDMM: replace Option to set globals
function rOpt(name,el,txt,val) {
let obj = d.getElementsByName(name);
@@ -285,6 +294,9 @@
if (txt2!="") obj[el].insertAdjacentHTML('beforebegin', txt2 + '&nbsp;'); //add pre texts
}
}
function adI(name,el,txt, txt2="") { // shortcut for addInfo(name,el,txt, txt2="")
return addInfo(name,el,txt, txt2);
}
// add Help Button
function addHB(um)
{

View File

@@ -362,6 +362,7 @@ int getNumVal(const String* req, uint16_t pos);
void parseNumber(const char* str, byte* val, byte minv=0, byte maxv=255);
bool getVal(JsonVariant elem, byte* val, byte minv=0, byte maxv=255);
bool updateVal(const char* req, const char* key, byte* val, byte minv=0, byte maxv=255);
void oappendUseDeflate(bool OnOff); // enable / disable string squeezing
bool oappend(const char* txt); // append new c string to temp buffer efficiently
bool oappendi(int i); // append new number to temp buffer efficiently
void sappend(char stype, const char* key, int val);

View File

@@ -144,22 +144,34 @@ bool oappendi(int i)
return oappend(s);
}
static bool squeezeStrings = false;
void oappendUseDeflate(bool OnOff) { squeezeStrings = OnOff; }
bool oappend(const char* txt)
{
uint16_t len = strlen(txt);
String str = squeezeStrings ? String(txt) : String("");
if (squeezeStrings) {
// simple fixed-dictionary deflate
str.replace(F("addField("), F("adF("));
str.replace(F("addDropdown("), F("adD("));
str.replace(F("addOption("), F("adO("));
str.replace(F("addInfo("), F("adI("));
}
const char* finalTxt = squeezeStrings ? str.c_str() : txt;
size_t len = strlen(finalTxt);
if ((obuf == nullptr) || (olen + len >= SETTINGS_STACK_BUF_SIZE)) { // sanity checks
if (obuf == nullptr) { USER_PRINTLN(F("oappend() error: obuf == nullptr."));
} else {
USER_PRINT(F("oappend() error: buffer full. Increase SETTINGS_STACK_BUF_SIZE for "));
USER_PRINTF("%2u bytes \t\"", len /*1 + olen + len - SETTINGS_STACK_BUF_SIZE*/);
USER_PRINT(txt);
USER_PRINT(finalTxt);
USER_PRINTLN(F("\""));
errorFlag = ERR_LOW_AJAX_MEM;
}
return false; // buffer full
}
strcpy(obuf + olen, txt);
strcpy(obuf + olen, finalTxt);
olen += len;
return true;
}