supla-device
Loading...
Searching...
No Matches
web_sender.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_NETWORK_WEB_SENDER_H_
20#define SRC_SUPLA_NETWORK_WEB_SENDER_H_
21
22#include <stddef.h>
23#include <stdint.h>
24#include <supla/network/html_generator.h>
25
26namespace Supla {
27
28class WebSender;
29
30struct FixedValue {
31 int raw = 0;
32 int precision = 0;
33
34 constexpr FixedValue() = default;
35 // NOLINTNEXTLINE(runtime/explicit)
36 constexpr FixedValue(int value) : raw(value), precision(0) {
37 }
38 constexpr FixedValue(int value, int prec) : raw(value), precision(prec) {
39 }
40};
41
42constexpr FixedValue fixed(int value, int precision) {
43 return FixedValue(value, precision);
44}
45
47 FixedValue min;
48 FixedValue max;
49 FixedValue value;
50 FixedValue step;
51};
52
76class HtmlTag {
77 public:
78 HtmlTag(WebSender* sender, const char* tagName, bool paired = true);
79 ~HtmlTag();
80
81 HtmlTag(const HtmlTag&) = delete;
82 HtmlTag& operator=(const HtmlTag&) = delete;
83
84 HtmlTag(HtmlTag&& other) noexcept;
85 HtmlTag& operator=(HtmlTag&& other) noexcept;
86
90 HtmlTag& attr(const char* name, const char* value);
91
95 HtmlTag& attr(const char* name, int value);
96
103 HtmlTag& attr(const char* name, int value, int precision);
104
110 HtmlTag& attrIf(const char* name, bool enabled);
111
115 HtmlTag& close();
116
124 HtmlTag& finish();
125
132 template <typename Fn>
133 void body(Fn&& fn) {
134 close();
135 fn();
136 end();
137 }
138
142 void body(char* text);
143
147 template <size_t N>
148 void body(const char (&text)[N]) {
149 body(static_cast<const char*>(text));
150 }
151
155 void body(const char* text);
156
163 void end();
164
165 private:
166 void release();
167
168 WebSender* sender_ = nullptr;
169 const char* tagName_ = nullptr;
170 bool paired_ = true;
171 bool closed_ = false;
172 bool finished_ = false;
173};
174
176 public:
195 virtual ~WebSender();
196 virtual void send(const char*, int size = -1) = 0;
197 virtual void sendSafe(const char*, int size = -1);
198 virtual void send(int number);
199 virtual void send(int number, int precision);
200 virtual void sendNameAndId(const char* id);
201 virtual void sendLabelFor(const char* id, const char* label);
202 virtual void sendSelectItem(int value,
203 const char* label,
204 bool selected,
205 bool emptyValue = false);
206 virtual void sendHidden(bool hidden);
207 virtual void sendReadonly(bool readonly);
208 virtual void sendDisabled(bool disabled);
209 virtual void sendTimestamp(uint32_t timestamp);
210
217 template <typename Fn>
218 void formField(Fn&& fn, const char* className = "form-field") {
219 auto field = tag("div");
220 field.attr("class", className);
221 field.body(fn);
222 }
223
230 template <typename Fn>
231 void labeledField(const char* id,
232 const char* text,
233 Fn&& fn,
234 const char* className = "form-field") {
235 formField(
236 [&]() {
237 labelFor(id, text);
238 fn();
239 },
240 className);
241 }
242
249 template <typename Fn>
250 void toggleBox(const char* id,
251 bool visible,
252 Fn&& fn,
253 const char* className = nullptr) {
254 auto box = tag("div");
255 if (id) {
256 box.attr("id", id);
257 }
258 if (className) {
259 box.attr("class", className);
260 }
261 box.attr("style", visible ? "display: block" : "display: none");
262 box.body(fn);
263 }
264
268 template <typename Fn>
269 void selectInput(const char* name, const char* id, Fn&& fn) {
270 selectTag(name, id).body(fn);
271 }
272
279 HtmlTag selectTag(const char* name, const char* id) {
280 auto select = tag("select");
281 if (name) {
282 select.attr("name", name);
283 }
284 if (id) {
285 select.attr("id", id);
286 }
287 return select;
288 }
289
293 void labelFor(const char* id, const char* text);
294
301 void textInput(const char* name,
302 const char* id,
303 const char* value = nullptr,
304 int maxLength = -1);
305
309 void passwordInput(const char* name, const char* id);
310
317 void checkboxInput(const char* name,
318 const char* id,
319 bool checked,
320 const char* value = "on");
321
328 void numberInput(const char* key,
329 const NumericInputSpec& spec,
330 const char* cssClass = nullptr);
331
338 void rangeInput(const char* key,
339 const NumericInputSpec& spec,
340 const char* cssClass = nullptr);
341
345 void numberInput(const char* name,
346 const char* id,
347 const NumericInputSpec& spec,
348 const char* cssClass = nullptr);
349
353 void rangeInput(const char* name,
354 const char* id,
355 const NumericInputSpec& spec,
356 const char* cssClass = nullptr);
357
361 void selectOption(int value, int text, bool selected = false);
362 void selectOption(int value, const char* text, bool selected = false);
363 void selectOption(const char* value, int text, bool selected = false);
364 void selectOption(const char* value, const char* text, bool selected = false);
365
372 HtmlTag tag(const char* tagName, bool paired = true) {
373 return HtmlTag(this, tagName, paired);
374 }
375
379 HtmlTag voidTag(const char* tagName) {
380 return HtmlTag(this, tagName, false);
381 }
382};
383}; // namespace Supla
384
385#endif // SRC_SUPLA_NETWORK_WEB_SENDER_H_
RAII helper for emitting a single HTML tag.
Definition web_sender.h:76
void body(const char(&text)[N])
Emit a text body using escaped content.
Definition web_sender.h:148
void body(Fn &&fn)
Emit a paired body using a callback.
Definition web_sender.h:133
HtmlTag & close()
Emit the closing > for the opening tag.
Definition web_sender.cpp:105
HtmlTag & attr(const char *name, const char *value)
Append a quoted HTML attribute with escaped value.
Definition web_sender.cpp:64
void end()
Explicitly finish the tag.
Definition web_sender.cpp:131
HtmlTag & attrIf(const char *name, bool enabled)
Append a boolean HTML attribute when enabled.
Definition web_sender.cpp:97
HtmlTag & finish()
Close the opening tag and mark it as finished.
Definition web_sender.cpp:113
Definition web_sender.h:175
void labeledField(const char *id, const char *text, Fn &&fn, const char *className="form-field")
Emit a labeled field wrapper.
Definition web_sender.h:231
void selectOption(int value, int text, bool selected=false)
Emit a single <option> element.
Definition web_sender.cpp:288
void formField(Fn &&fn, const char *className="form-field")
Emit a <div class="form-field">...</div> block.
Definition web_sender.h:218
HtmlTag voidTag(const char *tagName)
Start a builder for a void HTML tag such as <input>.
Definition web_sender.h:379
HtmlTag selectTag(const char *name, const char *id)
Start a <select> tag builder.
Definition web_sender.h:279
void labelFor(const char *id, const char *text)
Emit a <label for="...">...</label> pair.
Definition web_sender.cpp:152
void toggleBox(const char *id, bool visible, Fn &&fn, const char *className=nullptr)
Emit a <div> whose visibility is controlled via display.
Definition web_sender.h:250
virtual ~WebSender()
Base interface for emitting generated HTML.
Definition web_sender.cpp:150
HtmlTag tag(const char *tagName, bool paired=true)
Start an HTML tag builder.
Definition web_sender.h:372
void selectInput(const char *name, const char *id, Fn &&fn)
Emit a <select> block with optional name and id.
Definition web_sender.h:269
void checkboxInput(const char *name, const char *id, bool checked, const char *value="on")
Emit a checkbox input control.
Definition web_sender.cpp:191
void numberInput(const char *key, const NumericInputSpec &spec, const char *cssClass=nullptr)
Emit a numeric input control.
Definition web_sender.cpp:208
void rangeInput(const char *key, const NumericInputSpec &spec, const char *cssClass=nullptr)
Emit a range input control.
Definition web_sender.cpp:214
void passwordInput(const char *name, const char *id)
Emit a password input control.
Definition web_sender.cpp:179
void textInput(const char *name, const char *id, const char *value=nullptr, int maxLength=-1)
Emit a text input control.
Definition web_sender.cpp:158
Definition web_sender.h:30
Definition web_sender.h:46