IotWebConfOptionalGroup.cpp 3.91 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
36
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * IotWebConfOptionalGroup.cpp -- IotWebConf is an ESP8266/ESP32
 *   non blocking WiFi/AP web configuration library for Arduino.
 *   https://github.com/prampec/IotWebConf
 *s
 * 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.
 */

#include "IotWebConfOptionalGroup.h"

namespace iotwebconf
{

OptionalParameterGroup::OptionalParameterGroup(const char* id, const char* label, bool defaultVisible)
  : ParameterGroup(id, label)
{
  this->_defaultActive = defaultVisible;
}


int OptionalParameterGroup::getStorageSize()
{
  return ParameterGroup::getStorageSize() + 1;
}

void OptionalParameterGroup::applyDefaultValue()
{
  this->_active = this->_defaultActive;
  ParameterGroup::applyDefaultValue();
}

void OptionalParameterGroup::storeValue(
  std::function<void(SerializationData* serializationData)> doStore)
{
  // -- Store active flag.
  byte data[1];
  data[0] = (byte)this->_active;
  SerializationData serializationData;
  serializationData.length = 1;
  serializationData.data = data;
  doStore(&serializationData);

  // -- Store other items.
  ParameterGroup::storeValue(doStore);
}
void OptionalParameterGroup::loadValue(
  std::function<void(SerializationData* serializationData)> doLoad)
{
  // -- Load activity.
  byte data[1];
  SerializationData serializationData;
  serializationData.length = 1;
  serializationData.data = data;
  doLoad(&serializationData);
  this->_active = (bool)data[0];
  
  // -- Load other items.
  ParameterGroup::loadValue(doLoad);
}

void OptionalParameterGroup::renderHtml(
  bool dataArrived, WebRequestWrapper* webRequestWrapper)
{
    if (this->label != nullptr)
    {
      String content = getStartTemplate();
      content.replace("{b}", this->label);
      content.replace("{i}", this->getId());
      content.replace("{v}", this->_active ? "active" : "inactive");
      if (this->_active)
      {
        content.replace("{cb}", "hide");
        content.replace("{cf}", "");
      }
      else
      {
        content.replace("{cb}", "");
        content.replace("{cf}", "hide");
      }
      webRequestWrapper->sendContent(content);
    }
    ConfigItem* current = this->_firstItem;
    while (current != nullptr)
    {
      if (current->visible)
      {
        current->renderHtml(dataArrived, webRequestWrapper);
      }
      current = this->getNextItemOf(current);
    }
    if (this->label != nullptr)
    {
      String content = getEndTemplate();
      content.replace("{b}", this->label);
      content.replace("{i}", this->getId());
      webRequestWrapper->sendContent(content);
    }
}

void OptionalParameterGroup::update(WebRequestWrapper* webRequestWrapper)
{
  // -- Get active variable
  String activeId = String(this->getId());
  activeId += 'v';
  if (webRequestWrapper->hasArg(activeId))
  {
    String activeStr = webRequestWrapper->arg(activeId);
    this->_active = activeStr.equals("active");
  }

  // Update other items.
  ParameterGroup::update(webRequestWrapper);
}

void OptionalParameterGroup::debugTo(Stream* out)
{
  out->print('(');
  out->print(this->_active ? "active" : "inactive");
  out->print(')');

  // Print rest.
  ParameterGroup::debugTo(out);
}

///////////////////////////////////////////////////////////////////////////////

String ChainedParameterGroup::getStartTemplate()
{
  String result = OptionalParameterGroup::getStartTemplate();

  if ((this->_prevGroup != nullptr) && (!this->_prevGroup->isActive()))
  {
    result.replace("{cb}", "hide");
  }
  return result;
};

String ChainedParameterGroup::getEndTemplate()
{
  String result;
  if (this->_nextGroup == nullptr)
  {
    result = OptionalParameterGroup::getEndTemplate();
  }
  else
  {
    result = FPSTR(IOTWEBCONF_HTML_FORM_CHAINED_GROUP_NEXTID);
    result.replace("{in}", this->_nextGroup->getId());
    result += OptionalParameterGroup::getEndTemplate();
  }
  return result;
};


}