Supports saving and loading of the selected option in the AutoConnectSelect element. (Issue #64)

pull/66/head
Hieromon Ikasamo 6 years ago
parent 16d9fe078f
commit a447518c70
  1. 8
      src/AutoConnectAux.cpp
  2. 5
      src/AutoConnectElementBasis.h
  3. 32
      src/AutoConnectElementBasisImpl.h
  4. 4
      src/AutoConnectElementJson.h
  5. 9
      src/AutoConnectElementJsonImpl.h

@ -145,7 +145,11 @@ bool AutoConnectAux::release(const String& name) {
bool AutoConnectAux::setElementValue(const String& name, const String value) { bool AutoConnectAux::setElementValue(const String& name, const String value) {
AutoConnectElement* elm = getElement(name); AutoConnectElement* elm = getElement(name);
if (elm) { if (elm) {
if (elm->typeOf() != AC_Select) { if (elm->typeOf() == AC_Select) {
AutoConnectSelect* elmSelect = reinterpret_cast<AutoConnectSelect*>(elm);
elmSelect->select(value);
}
else {
if (elm->typeOf() == AC_Checkbox) { if (elm->typeOf() == AC_Checkbox) {
if (value == "checked") { if (value == "checked") {
AutoConnectCheckbox* elmCheckbox = reinterpret_cast<AutoConnectCheckbox*>(elm); AutoConnectCheckbox* elmCheckbox = reinterpret_cast<AutoConnectCheckbox*>(elm);
@ -160,8 +164,6 @@ bool AutoConnectAux::setElementValue(const String& name, const String value) {
elm->value = value; elm->value = value;
return true; return true;
} }
else
AC_DBG("Element<%s> value type mismatch\n", name.c_str());
} }
return false; return false;
} }

@ -194,7 +194,7 @@ class AutoConnectRadioBasis : virtual public AutoConnectElementBasis {
*/ */
class AutoConnectSelectBasis : virtual public AutoConnectElementBasis { class AutoConnectSelectBasis : virtual public AutoConnectElementBasis {
public: public:
explicit AutoConnectSelectBasis(const char* name = "", std::vector<String> const& options = {}, const char* label = "") : AutoConnectElementBasis(name, ""), label(String(label)), _options(options) { explicit AutoConnectSelectBasis(const char* name = "", std::vector<String> const& options = {}, const char* label = "", const uint8_t selected = 0) : AutoConnectElementBasis(name, ""), label(String(label)), selected(selected), _options(options) {
_type = AC_Select; _type = AC_Select;
} }
virtual ~AutoConnectSelectBasis() {} virtual ~AutoConnectSelectBasis() {}
@ -203,9 +203,12 @@ class AutoConnectSelectBasis : virtual public AutoConnectElementBasis {
void add(const String& option) { _options.push_back(String(option)); } void add(const String& option) { _options.push_back(String(option)); }
size_t size(void) const { return _options.size(); } size_t size(void) const { return _options.size(); }
const String& at(const std::size_t n) const { return _options.at(n); } const String& at(const std::size_t n) const { return _options.at(n); }
void select(const String& value);
void empty(const size_t reserve = 0); void empty(const size_t reserve = 0);
const String& value(void) const;
String label; /**< A label for a subsequent input box */ String label; /**< A label for a subsequent input box */
uint8_t selected; /**< Index of checked value (1-based) */
protected: protected:
std::vector<String> _options; /**< List options array */ std::vector<String> _options; /**< List options array */

@ -198,7 +198,7 @@ const String AutoConnectRadioBasis::toHTML(void) const {
/** /**
* Returns current selected value in the radio same group * Returns current selected value in the radio same group
*/ */
const String& AutoConnectRadioBasis::value() const { const String& AutoConnectRadioBasis::value(void) const {
static const String _nullString = String(); static const String _nullString = String();
return checked ? _values.at(checked - 1) : _nullString; return checked ? _values.at(checked - 1) : _nullString;
} }
@ -216,6 +216,19 @@ void AutoConnectSelectBasis::empty(const size_t reserve) {
_options.reserve(reserve); _options.reserve(reserve);
} }
/**
* Indicate an entry with the specified value in the value's collection.
* @param value The value to indicates in the collection.
*/
void AutoConnectSelectBasis::select(const String& value) {
for (std::size_t n = 0; n < _options.size(); n++) {
if (at(n).equalsIgnoreCase(value)) {
selected = n + 1;
break;
}
}
}
/** /**
* Generate an HTML <select> element with an <option> element. * Generate an HTML <select> element with an <option> element.
* The attribute value of the <option> element is given to the * The attribute value of the <option> element is given to the
@ -230,12 +243,25 @@ const String AutoConnectSelectBasis::toHTML(void) const {
if (label.length()) if (label.length())
html = String(F("<label for=\"")) + name + String("\">") + label + String(F("</label>")); html = String(F("<label for=\"")) + name + String("\">") + label + String(F("</label>"));
html += String(F("<select name=\"")) + name + String(F("\" id=\"")) + name + String("\">"); html += String(F("<select name=\"")) + name + String(F("\" id=\"")) + name + String("\">");
for (const String option : _options) uint8_t n = 1;
html += String(F("<option value=\"")) + option + "\">" + option + String(F("</option>")); for (const String option : _options) {
html += String(F("<option value=\"")) + option + "\"";
if (n++ == selected)
html += String(F(" selected"));
html += ">" + option + String(F("</option>"));
}
html += String(F("</select>")); html += String(F("</select>"));
return html; return html;
} }
/**
* Returns current selected value in the radio same group
*/
const String& AutoConnectSelectBasis::value(void) const {
static const String _nullString = String();
return selected ? _options.at(selected - 1) : _nullString;
}
/** /**
* Generate an HTML <input type=button> element. This element is used * Generate an HTML <input type=button> element. This element is used
* for form submission. An 'onclick' attribute calls fixed JavaScript * for form submission. An 'onclick' attribute calls fixed JavaScript

@ -24,6 +24,7 @@
#define AUTOCONNECT_JSON_KEY_OPTION "option" #define AUTOCONNECT_JSON_KEY_OPTION "option"
#define AUTOCONNECT_JSON_KEY_PATTERN "pattern" #define AUTOCONNECT_JSON_KEY_PATTERN "pattern"
#define AUTOCONNECT_JSON_KEY_PLACEHOLDER "placeholder" #define AUTOCONNECT_JSON_KEY_PLACEHOLDER "placeholder"
#define AUTOCONNECT_JSON_KEY_SELECTED "selected"
#define AUTOCONNECT_JSON_KEY_STORE "store" #define AUTOCONNECT_JSON_KEY_STORE "store"
#define AUTOCONNECT_JSON_KEY_STYLE "style" #define AUTOCONNECT_JSON_KEY_STYLE "style"
#define AUTOCONNECT_JSON_KEY_TITLE "title" #define AUTOCONNECT_JSON_KEY_TITLE "title"
@ -237,10 +238,11 @@ class AutoConnectRadioJson : public AutoConnectElementJson, public AutoConnectRa
*/ */
class AutoConnectSelectJson : public AutoConnectElementJson, public AutoConnectSelectBasis { class AutoConnectSelectJson : public AutoConnectElementJson, public AutoConnectSelectBasis {
public: public:
explicit AutoConnectSelectJson(const char* name = "", std::vector<String> const& options = {}, const char* label = "") { explicit AutoConnectSelectJson(const char* name = "", std::vector<String> const& options = {}, const char* label = "", const uint8_t selected = 0) {
AutoConnectSelectBasis::name = String(name); AutoConnectSelectBasis::name = String(name);
AutoConnectSelectBasis::_options = options; AutoConnectSelectBasis::_options = options;
AutoConnectSelectBasis::label = String(label); AutoConnectSelectBasis::label = String(label);
AutoConnectSelectBasis::selected = selected;
} }
~AutoConnectSelectJson() {} ~AutoConnectSelectJson() {}
size_t getObjectSize(void) const override; size_t getObjectSize(void) const override;

@ -320,7 +320,8 @@ void AutoConnectRadioJson::serialize(JsonObject& json) {
json[F(AUTOCONNECT_JSON_KEY_ARRANGE)] = String(F(AUTOCONNECT_JSON_VALUE_VERTICAL)); json[F(AUTOCONNECT_JSON_KEY_ARRANGE)] = String(F(AUTOCONNECT_JSON_VALUE_VERTICAL));
break; break;
} }
json[F(AUTOCONNECT_JSON_KEY_CHECKED)] = checked; if (checked > 0)
json[F(AUTOCONNECT_JSON_KEY_CHECKED)] = checked;
} }
/** /**
@ -328,7 +329,7 @@ void AutoConnectRadioJson::serialize(JsonObject& json) {
* @return An object size for JsonBuffer. * @return An object size for JsonBuffer.
*/ */
size_t AutoConnectSelectJson::getObjectSize() const { size_t AutoConnectSelectJson::getObjectSize() const {
size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(3) + _options.size() * JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1); size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(4) + _options.size() * JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1);
size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length(); size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length();
for (String _option : _options) for (String _option : _options)
size += _option.length(); size += _option.length();
@ -355,6 +356,8 @@ bool AutoConnectSelectJson::loadMember(const JsonObject& json) {
add(value.as<String>()); add(value.as<String>());
return true; return true;
} }
if (json.containsKey(F(AUTOCONNECT_JSON_KEY_SELECTED)))
selected = static_cast<uint8_t>(json[F(AUTOCONNECT_JSON_KEY_SELECTED)].as<int>());
} }
return false; return false;
} }
@ -370,6 +373,8 @@ void AutoConnectSelectJson::serialize(JsonObject& json) {
for (String o : _options) for (String o : _options)
options.add(o); options.add(o);
json[F(AUTOCONNECT_JSON_KEY_LABEL)] = label; json[F(AUTOCONNECT_JSON_KEY_LABEL)] = label;
if (selected > 0)
json[F(AUTOCONNECT_JSON_KEY_SELECTED)] = selected;
} }
/** /**

Loading…
Cancel
Save