supla-device
Loading...
Searching...
No Matches
web_sender.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_NETWORK_WEB_SENDER_H_
5#define SRC_SUPLA_NETWORK_WEB_SENDER_H_
6
7#include <stddef.h>
8#include <stdint.h>
9#include <supla/network/html_generator.h>
10
11namespace Supla {
12
13class WebSender;
14
15struct FixedValue {
16 int raw = 0;
17 int precision = 0;
18
19 constexpr FixedValue() = default;
20 // NOLINTNEXTLINE(runtime/explicit)
21 constexpr FixedValue(int value) : raw(value), precision(0) {
22 }
23 constexpr FixedValue(int value, int prec) : raw(value), precision(prec) {
24 }
25};
26
27constexpr FixedValue fixed(int value, int precision) {
28 return FixedValue(value, precision);
29}
30
32 FixedValue min;
33 FixedValue max;
34 FixedValue value;
35 FixedValue step;
36};
37
61class HtmlTag {
62 public:
63 HtmlTag(WebSender* sender, const char* tagName, bool paired = true);
64 ~HtmlTag();
65
66 HtmlTag(const HtmlTag&) = delete;
67 HtmlTag& operator=(const HtmlTag&) = delete;
68
69 HtmlTag(HtmlTag&& other) noexcept;
70 HtmlTag& operator=(HtmlTag&& other) noexcept;
71
75 HtmlTag& attr(const char* name, const char* value);
76
80 HtmlTag& attr(const char* name, int value);
81
88 HtmlTag& attr(const char* name, int value, int precision);
89
95 HtmlTag& attrIf(const char* name, bool enabled);
96
100 HtmlTag& close();
101
109 HtmlTag& finish();
110
117 template <typename Fn>
118 void body(Fn&& fn) {
119 close();
120 fn();
121 end();
122 }
123
127 void body(char* text);
128
132 template <size_t N>
133 void body(const char (&text)[N]) {
134 body(static_cast<const char*>(text));
135 }
136
140 void body(const char* text);
141
148 void end();
149
150 private:
151 void release();
152
153 WebSender* sender_ = nullptr;
154 const char* tagName_ = nullptr;
155 bool paired_ = true;
156 bool closed_ = false;
157 bool finished_ = false;
158};
159
161 public:
180 virtual ~WebSender();
181 virtual void send(const char*, int size = -1) = 0;
182 virtual void sendSafe(const char*, int size = -1);
183 virtual void send(int number);
184 virtual void send(int number, int precision);
185 virtual void sendNameAndId(const char* id);
186 virtual void sendLabelFor(const char* id, const char* label);
187 virtual void sendSelectItem(int value,
188 const char* label,
189 bool selected,
190 bool emptyValue = false);
191 virtual void sendCsrfField();
192 virtual void sendHidden(bool hidden);
193 virtual void sendReadonly(bool readonly);
194 virtual void sendDisabled(bool disabled);
195 virtual void sendTimestamp(uint32_t timestamp);
196
203 template <typename Fn>
204 void formField(Fn&& fn, const char* className = "form-field") {
205 auto field = tag("div");
206 field.attr("class", className);
207 field.body(fn);
208 }
209
216 template <typename Fn>
217 void labeledField(const char* id,
218 const char* text,
219 Fn&& fn,
220 const char* className = "form-field") {
221 formField(
222 [&]() {
223 labelFor(id, text);
224 fn();
225 },
226 className);
227 }
228
235 template <typename Fn>
236 void toggleBox(const char* id,
237 bool visible,
238 Fn&& fn,
239 const char* className = nullptr) {
240 auto box = tag("div");
241 if (id) {
242 box.attr("id", id);
243 }
244 if (className) {
245 box.attr("class", className);
246 }
247 box.attr("style", visible ? "display: block" : "display: none");
248 box.body(fn);
249 }
250
254 template <typename Fn>
255 void selectInput(const char* name, const char* id, Fn&& fn) {
256 selectTag(name, id).body(fn);
257 }
258
265 HtmlTag selectTag(const char* name, const char* id) {
266 auto select = tag("select");
267 if (name) {
268 select.attr("name", name);
269 }
270 if (id) {
271 select.attr("id", id);
272 }
273 return select;
274 }
275
279 void labelFor(const char* id, const char* text);
280
287 void textInput(const char* name,
288 const char* id,
289 const char* value = nullptr,
290 int maxLength = -1,
291 const char* listId = nullptr,
292 const char* onInput = nullptr,
293 const char* onChange = nullptr);
294
298 void passwordInput(const char* name, const char* id);
299
306 void checkboxInput(const char* name,
307 const char* id,
308 bool checked,
309 const char* value = "on");
310
317 void numberInput(const char* key,
318 const NumericInputSpec& spec,
319 const char* cssClass = nullptr);
320
327 void rangeInput(const char* key,
328 const NumericInputSpec& spec,
329 const char* cssClass = nullptr);
330
334 void numberInput(const char* name,
335 const char* id,
336 const NumericInputSpec& spec,
337 const char* cssClass = nullptr);
338
342 void rangeInput(const char* name,
343 const char* id,
344 const NumericInputSpec& spec,
345 const char* cssClass = nullptr);
346
350 void selectOption(int value, int text, bool selected = false);
351 void selectOption(int value, const char* text, bool selected = false);
352 void selectOption(const char* value, int text, bool selected = false);
353 void selectOption(const char* value, const char* text, bool selected = false);
354
361 HtmlTag tag(const char* tagName, bool paired = true) {
362 return HtmlTag(this, tagName, paired);
363 }
364
368 HtmlTag voidTag(const char* tagName) {
369 return HtmlTag(this, tagName, false);
370 }
371};
372}; // namespace Supla
373
374#endif // SRC_SUPLA_NETWORK_WEB_SENDER_H_
RAII helper for emitting a single HTML tag.
Definition web_sender.h:61
void body(const char(&text)[N])
Emit a text body using escaped content.
Definition web_sender.h:133
void body(Fn &&fn)
Emit a paired body using a callback.
Definition web_sender.h:118
HtmlTag & close()
Emit the closing > for the opening tag.
Definition web_sender.cpp:91
HtmlTag & attr(const char *name, const char *value)
Append a quoted HTML attribute with escaped value.
Definition web_sender.cpp:50
void end()
Explicitly finish the tag.
Definition web_sender.cpp:117
HtmlTag & attrIf(const char *name, bool enabled)
Append a boolean HTML attribute when enabled.
Definition web_sender.cpp:83
HtmlTag & finish()
Close the opening tag and mark it as finished.
Definition web_sender.cpp:99
Definition web_sender.h:160
void labeledField(const char *id, const char *text, Fn &&fn, const char *className="form-field")
Emit a labeled field wrapper.
Definition web_sender.h:217
void selectOption(int value, int text, bool selected=false)
Emit a single <option> element.
Definition web_sender.cpp:286
void formField(Fn &&fn, const char *className="form-field")
Emit a <div class="form-field">...</div> block.
Definition web_sender.h:204
HtmlTag voidTag(const char *tagName)
Start a builder for a void HTML tag such as <input>.
Definition web_sender.h:368
HtmlTag selectTag(const char *name, const char *id)
Start a <select> tag builder.
Definition web_sender.h:265
void labelFor(const char *id, const char *text)
Emit a <label for="...">...</label> pair.
Definition web_sender.cpp:138
void textInput(const char *name, const char *id, const char *value=nullptr, int maxLength=-1, const char *listId=nullptr, const char *onInput=nullptr, const char *onChange=nullptr)
Emit a text input control.
Definition web_sender.cpp:144
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:236
virtual ~WebSender()
Base interface for emitting generated HTML.
Definition web_sender.cpp:136
HtmlTag tag(const char *tagName, bool paired=true)
Start an HTML tag builder.
Definition web_sender.h:361
void selectInput(const char *name, const char *id, Fn &&fn)
Emit a <select> block with optional name and id.
Definition web_sender.h:255
void checkboxInput(const char *name, const char *id, bool checked, const char *value="on")
Emit a checkbox input control.
Definition web_sender.cpp:189
void numberInput(const char *key, const NumericInputSpec &spec, const char *cssClass=nullptr)
Emit a numeric input control.
Definition web_sender.cpp:206
void rangeInput(const char *key, const NumericInputSpec &spec, const char *cssClass=nullptr)
Emit a range input control.
Definition web_sender.cpp:212
void passwordInput(const char *name, const char *id)
Emit a password input control.
Definition web_sender.cpp:177
Definition web_sender.h:15
Definition web_sender.h:31