19#ifndef SRC_SUPLA_NETWORK_WEB_SENDER_H_
20#define SRC_SUPLA_NETWORK_WEB_SENDER_H_
24#include <supla/network/html_generator.h>
34 constexpr FixedValue() =
default;
36 constexpr FixedValue(
int value) : raw(value), precision(0) {
38 constexpr FixedValue(
int value,
int prec) : raw(value), precision(prec) {
42constexpr FixedValue fixed(
int value,
int precision) {
78 HtmlTag(
WebSender* sender,
const char* tagName,
bool paired =
true);
81 HtmlTag(
const HtmlTag&) =
delete;
82 HtmlTag& operator=(
const HtmlTag&) =
delete;
84 HtmlTag(HtmlTag&& other)
noexcept;
85 HtmlTag& operator=(HtmlTag&& other)
noexcept;
90 HtmlTag&
attr(
const char* name,
const char* value);
95 HtmlTag&
attr(
const char* name,
int value);
103 HtmlTag&
attr(
const char* name,
int value,
int precision);
110 HtmlTag&
attrIf(
const char* name,
bool enabled);
132 template <
typename Fn>
142 void body(
char* text);
148 void body(
const char (&text)[N]) {
149 body(
static_cast<const char*
>(text));
155 void body(
const char* text);
169 const char* tagName_ =
nullptr;
171 bool closed_ =
false;
172 bool finished_ =
false;
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,
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);
217 template <
typename Fn>
218 void formField(Fn&& fn,
const char* className =
"form-field") {
219 auto field =
tag(
"div");
220 field.attr(
"class", className);
230 template <
typename Fn>
234 const char* className =
"form-field") {
249 template <
typename Fn>
253 const char* className =
nullptr) {
254 auto box =
tag(
"div");
259 box.attr(
"class", className);
261 box.attr(
"style", visible ?
"display: block" :
"display: none");
268 template <
typename Fn>
280 auto select =
tag(
"select");
282 select.attr(
"name", name);
285 select.attr(
"id",
id);
293 void labelFor(
const char*
id,
const char* text);
303 const char* value =
nullptr,
320 const char* value =
"on");
330 const char* cssClass =
nullptr);
340 const char* cssClass =
nullptr);
348 const char* cssClass =
nullptr);
356 const char* cssClass =
nullptr);
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);
373 return HtmlTag(
this, tagName, paired);
380 return HtmlTag(
this, tagName,
false);
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