From d210181e5de7d8ad5897d0bb5dc66f9784ca925c Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Sat, 23 Mar 2019 19:10:09 +0900 Subject: [PATCH] Add format attribute with AutoConnectText --- examples/FileUpload/FileUpload.ino | 7 ++++--- src/AutoConnectElementBasis.h | 4 +++- src/AutoConnectElementBasisImpl.h | 17 +++++++++++++++-- src/AutoConnectElementJson.h | 4 +++- src/AutoConnectElementJsonImpl.h | 5 ++++- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/examples/FileUpload/FileUpload.ino b/examples/FileUpload/FileUpload.ino index 53f3a6f..e44d0dd 100644 --- a/examples/FileUpload/FileUpload.ino +++ b/examples/FileUpload/FileUpload.ino @@ -73,12 +73,13 @@ static const char PAGE_BROWSE[] PROGMEM = R"( }, { "name": "size", - "type": "ACText" + "type": "ACText", + "format": "%s bytes uploaded" }, { "name": "content_type", "type": "ACText", - "value": "Content: " + "format": "Content: %s" } ] } @@ -108,7 +109,7 @@ String postUpload(AutoConnectAux& aux, PageArgument& args) { String content; String filename = auxUpload.getElement("filename").value; aux.getElement("filename").value = filename; - aux.getElement("size").value = String(auxUpload.getElement("filename").size) + String("bytes uploaded"); + aux.getElement("size").value = String(auxUpload.getElement("filename").size); String contentType = auxUpload.getElement("filename").mimeType; aux.getElement("content_type").value = contentType; if (contentType.indexOf("text/") >= 0) { diff --git a/src/AutoConnectElementBasis.h b/src/AutoConnectElementBasis.h index dd7fb7c..a718b7e 100644 --- a/src/AutoConnectElementBasis.h +++ b/src/AutoConnectElementBasis.h @@ -233,18 +233,20 @@ class AutoConnectSubmitBasis : virtual public AutoConnectElementBasis { * @param name Text name string. * @param value Text value string. * @param style A string of style-code for decoration, optionally. + * @param format C string that contains the value to be formatted. * An arrangement text would be placed with
contains. A string * of style-codes are given for '
'. */ class AutoConnectTextBasis : virtual public AutoConnectElementBasis { public: - explicit AutoConnectTextBasis(const char* name = "", const char* value = "", const char* style = "") : AutoConnectElementBasis(name, value), style(String(style)) { + explicit AutoConnectTextBasis(const char* name = "", const char* value = "", const char* style = "", const char* format = "") : AutoConnectElementBasis(name, value), style(String(style)), format(String(format)) { _type = AC_Text; } virtual ~AutoConnectTextBasis() {} const String toHTML(void) const override; String style; /**< CSS style modifier native code */ + String format; /**< C string that contains the text to be written */ }; #endif // _AUTOCONNECTELEMENTBASIS_H_ diff --git a/src/AutoConnectElementBasisImpl.h b/src/AutoConnectElementBasisImpl.h index e6f6fa1..8437da0 100644 --- a/src/AutoConnectElementBasisImpl.h +++ b/src/AutoConnectElementBasisImpl.h @@ -10,12 +10,14 @@ #ifndef _AUTOCONNECTELEMENTBASISIMPL_H_ #define _AUTOCONNECTELEMENTBASISIMPL_H_ -#include "AutoConnectElementBasis.h" +#include +#include #if defined(ARDUINO_ARCH_ESP8266) #include #elif defined(ARDUINO_ARCH_ESP32) #include #endif +#include "AutoConnectElementBasis.h" /** * Generate an HTML
")); + html += String(">"); + if (format.length()) { + int buflen = (value.length() + format.length() + 16 + 1) & (~0xf); + char* buffer; + if ((buffer = (char*)malloc(buflen))) { + snprintf(buffer, buflen, format.c_str(), value.c_str()); + value_f = String(buffer); + free(buffer); + } + } + html += value_f + String(F("
")); return html; } diff --git a/src/AutoConnectElementJson.h b/src/AutoConnectElementJson.h index 88b0720..bffb427 100644 --- a/src/AutoConnectElementJson.h +++ b/src/AutoConnectElementJson.h @@ -17,6 +17,7 @@ #define AUTOCONNECT_JSON_KEY_ARRANGE "arrange" #define AUTOCONNECT_JSON_KEY_CHECKED "checked" #define AUTOCONNECT_JSON_KEY_ELEMENT "element" +#define AUTOCONNECT_JSON_KEY_FORMAT "format" #define AUTOCONNECT_JSON_KEY_LABEL "label" #define AUTOCONNECT_JSON_KEY_MENU "menu" #define AUTOCONNECT_JSON_KEY_NAME "name" @@ -234,10 +235,11 @@ class AutoConnectSubmitJson : public AutoConnectElementJson, public AutoConnectS */ class AutoConnectTextJson : public AutoConnectElementJson, public AutoConnectTextBasis { public: - explicit AutoConnectTextJson(const char* name = "", const char* value = "", const char* style = "") { + explicit AutoConnectTextJson(const char* name = "", const char* value = "", const char* style = "", const char* format = "") { AutoConnectTextBasis::name = String(name); AutoConnectTextBasis::value = String(value); AutoConnectTextBasis::style = String(style); + AutoConnectTextBasis::format = String(format); } ~AutoConnectTextJson() {} size_t getObjectSize(void) const override; diff --git a/src/AutoConnectElementJsonImpl.h b/src/AutoConnectElementJsonImpl.h index 3ef0394..eb53214 100644 --- a/src/AutoConnectElementJsonImpl.h +++ b/src/AutoConnectElementJsonImpl.h @@ -394,7 +394,7 @@ void AutoConnectSubmitJson::serialize(JsonObject& json) { * @return An object size for JsonBuffer. */ size_t AutoConnectTextJson::getObjectSize() const { - return AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(1); + return AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(2); } /** @@ -409,6 +409,8 @@ bool AutoConnectTextJson::loadMember(const JsonObject& json) { _setMember(json); if (json.containsKey(F(AUTOCONNECT_JSON_KEY_STYLE))) style = json.get(F(AUTOCONNECT_JSON_KEY_STYLE)); + if (json.containsKey(F(AUTOCONNECT_JSON_KEY_FORMAT))) + format = json.get(F(AUTOCONNECT_JSON_KEY_FORMAT)); return true; } return false; @@ -423,6 +425,7 @@ void AutoConnectTextJson::serialize(JsonObject& json) { json.set(F(AUTOCONNECT_JSON_KEY_TYPE), F(AUTOCONNECT_JSON_TYPE_ACTEXT)); json.set(F(AUTOCONNECT_JSON_KEY_VALUE), value); json.set(F(AUTOCONNECT_JSON_KEY_STYLE), style); + json.set(F(AUTOCONNECT_JSON_KEY_FORMAT), format); } #endif // _AUTOCONNECTELEMENTJSONIMPL_H_