IotWebConfOptionalGroup.h 4.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * IotWebConfOptionalGroup.h -- IotWebConf is an ESP8266/ESP32
 *   non blocking WiFi/AP web configuration library for Arduino.
 *   https://github.com/prampec/IotWebConf
 *
 * Copyright (C) 2020 Balazs Kelemen <prampec+arduino@gmail.com>
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

#ifndef IotWebConfOptionalGroup_h
#define IotWebConfOptionalGroup_h

#include "IotWebConf.h" // For HtmlFormatProvider ... TODO: should be reorganized
#include "IotWebConfParameter.h"

const char IOTWEBCONF_HTML_FORM_OPTIONAL_GROUP_JAVASCRIPT[] PROGMEM =
  "    function show(id) { var x=document.getElementById(id); x.classList.remove('hide'); }\n"
  "    function hide(id) { var x=document.getElementById(id); x.classList.add('hide'); }\n"
  "    function val(id) { var x=document.getElementById(id); return x.value; }\n"
  "    function setVal(id, val) { var x=document.getElementById(id); x.value = val; }\n"
  "    function showFs(id) {\n"
  "      show(id); hide(id + 'b'); setVal(id + 'v', 'active'); var n=document.getElementById(id + 'next');\n"
  "      if (n) { var nId = n.value; if (val(nId + 'v') == 'inactive') { show(nId + 'b'); }}\n"
  "    }\n"
  "    function hideFs(id) {\n"
  "      hide(id); show(id + 'b'); setVal(id + 'v', 'inactive'); var n=document.getElementById(id + 'next');\n"
  "      if (n) { var nId = n.value; if (val(nId + 'v') == 'inactive') { hide(nId + 'b'); }}\n"
  "    }\n";
const char IOTWEBCONF_HTML_FORM_OPTIONAL_GROUP_CSS[] PROGMEM =
  ".hide{display: none;}\n";
const char IOTWEBCONF_HTML_FORM_OPTIONAL_GROUP_START[] PROGMEM =
  "<button id='{i}b' class='{cb}' onclick=\"showFs('{i}'); return false;\">+ {b}</button>\n"
  "<fieldset id='{i}' class='{cf}'><legend>{b}</legend>\n"
Eric Duminil's avatar
Doc    
Eric Duminil committed
36
  "<button onclick=\"hideFs('{i}'); return false;\">Disable {b}</button>\n"
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  "<input id='{i}v' name='{i}v' type='hidden' value='{v}'/>\n"
  "\n";
const char IOTWEBCONF_HTML_FORM_OPTIONAL_GROUP_END[] PROGMEM =
  "</fieldset>\n";
const char IOTWEBCONF_HTML_FORM_CHAINED_GROUP_NEXTID[] PROGMEM =
  "<input type='hidden' id='{i}next' value='{in}'/>\n";

namespace iotwebconf
{

class OptionalGroupHtmlFormatProvider : public HtmlFormatProvider
{
protected:
  String getScriptInner() override
  {
    return
      HtmlFormatProvider::getScriptInner() +
      String(FPSTR(IOTWEBCONF_HTML_FORM_OPTIONAL_GROUP_JAVASCRIPT));
  }
  String getStyleInner() override
  {
    return
      HtmlFormatProvider::getStyleInner() +
      String(FPSTR(IOTWEBCONF_HTML_FORM_OPTIONAL_GROUP_CSS));
  }
};

/**
 * With OptionalParameterGroup buttons will appear in the GUI,
 * to show and hide this specific group of parameters. The idea
 * behind this feature to add/remove optional parameter set
 * in the config portal.
 */
class OptionalParameterGroup : public ParameterGroup
{
public:
  OptionalParameterGroup(const char* id, const char* label, bool defaultActive);
  bool isActive() { return this->_active; }
  void setActive(bool active) { this->_active = active; }

protected:
  int getStorageSize() override;
  void applyDefaultValue() override;
  void storeValue(std::function<void(
    SerializationData* serializationData)> doStore) override;
  void loadValue(std::function<void(
    SerializationData* serializationData)> doLoad) override;
  void renderHtml(bool dataArrived, WebRequestWrapper* webRequestWrapper) override;
  virtual String getStartTemplate() { return FPSTR(IOTWEBCONF_HTML_FORM_OPTIONAL_GROUP_START); };
  virtual String getEndTemplate() { return FPSTR(IOTWEBCONF_HTML_FORM_OPTIONAL_GROUP_END); };
  void update(WebRequestWrapper* webRequestWrapper) override;
  void debugTo(Stream* out) override;

private:
  bool _defaultActive;
  bool _active;
};

class ChainedParameterGroup;

class ChainedParameterGroup : public OptionalParameterGroup
{
public:
  ChainedParameterGroup(const char* id, const char* label, bool defaultActive = false) :
    OptionalParameterGroup(id, label, defaultActive) { };
  void setNext(ChainedParameterGroup* nextGroup) { this->_nextGroup = nextGroup; nextGroup->_prevGroup = this; };
  ChainedParameterGroup* getNext() { return this->_nextGroup; };

protected:
  virtual String getStartTemplate() override;
  virtual String getEndTemplate() override;

protected:
  ChainedParameterGroup* _prevGroup = nullptr;
  ChainedParameterGroup* _nextGroup = nullptr;
};

}

Eric Duminil's avatar
Doc    
Eric Duminil committed
116
#endif