Add format attribute with AutoConnectText

pull/57/head
Hieromon Ikasamo 6 years ago
parent ff04fde1af
commit d210181e5d
  1. 7
      examples/FileUpload/FileUpload.ino
  2. 4
      src/AutoConnectElementBasis.h
  3. 17
      src/AutoConnectElementBasisImpl.h
  4. 4
      src/AutoConnectElementJson.h
  5. 5
      src/AutoConnectElementJsonImpl.h

@ -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<AutoConnectFile>("filename").value;
aux.getElement<AutoConnectText>("filename").value = filename;
aux.getElement<AutoConnectText>("size").value = String(auxUpload.getElement<AutoConnectFile>("filename").size) + String("bytes uploaded");
aux.getElement<AutoConnectText>("size").value = String(auxUpload.getElement<AutoConnectFile>("filename").size);
String contentType = auxUpload.getElement<AutoConnectFile>("filename").mimeType;
aux.getElement<AutoConnectText>("content_type").value = contentType;
if (contentType.indexOf("text/") >= 0) {

@ -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 <div> contains. A string
* of style-codes are given for '<div style=>'.
*/
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_

@ -10,12 +10,14 @@
#ifndef _AUTOCONNECTELEMENTBASISIMPL_H_
#define _AUTOCONNECTELEMENTBASISIMPL_H_
#include "AutoConnectElementBasis.h"
#include <stdlib.h>
#include <stdio.h>
#if defined(ARDUINO_ARCH_ESP8266)
#include <regex.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <regex>
#endif
#include "AutoConnectElementBasis.h"
/**
* Generate an HTML <button> element. The onclick behavior depends on
@ -251,10 +253,21 @@ const String AutoConnectSubmitBasis::toHTML(void) const {
*/
const String AutoConnectTextBasis::toHTML(void) const {
String html = String("<div");
String value_f = value;
if (style.length())
html += String(F(" style=\"")) + style + String("\"");
html += String(">") + value + String(F("</div>"));
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("</div>"));
return html;
}

@ -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;

@ -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<String>(F(AUTOCONNECT_JSON_KEY_STYLE));
if (json.containsKey(F(AUTOCONNECT_JSON_KEY_FORMAT)))
format = json.get<String>(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_

Loading…
Cancel
Save