4#ifndef SRC_SUPLA_NETWORK_WEB_SENDER_H_
5#define SRC_SUPLA_NETWORK_WEB_SENDER_H_
10#include <supla/network/html_generator.h>
12#if defined(ARDUINO_ARCH_ESP8266)
14#define SUPLA_WEB_PROGMEM PROGMEM
15#define SUPLA_WEB_SNPRINTF(buffer, size, format, ...) \
16 snprintf_P(buffer, size, PSTR(format), ##__VA_ARGS__)
18#define SUPLA_WEB_PROGMEM
19#define SUPLA_WEB_SNPRINTF(buffer, size, format, ...) \
20 snprintf(buffer, size, format, ##__VA_ARGS__)
33 constexpr FixedValue(
int value) : raw(value), precision(0) {
35 constexpr FixedValue(
int value,
int prec) : raw(value), precision(prec) {
39constexpr FixedValue fixed(
int value,
int precision) {
90 HtmlTag& attrStatic(
const char* name,
92 size_t size =
static_cast<size_t>(-1));
105 HtmlTag&
attr(
const char* name,
int value,
int precision);
134 template <
typename Fn>
144 void body(
char* text);
150 void body(
const char (&text)[N]) {
151 body(
static_cast<const char*
>(text));
157 void body(
const char* text);
171 const char* tagName_ =
nullptr;
173 bool closed_ =
false;
174 bool finished_ =
false;
198 virtual void send(
const char*,
int size = -1) = 0;
208 size_t size =
static_cast<size_t>(-1),
209 bool escape =
false);
211 virtual void sendSafe(
const char*,
int size = -1);
212 virtual void send(
int number);
213 virtual void send(
int number,
int precision);
214 virtual void sendNameAndId(
const char*
id);
215 virtual void sendLabelFor(
const char*
id,
const char* label);
216 virtual void sendSelectItem(
int value,
219 bool emptyValue =
false);
220 virtual void sendCsrfField();
221 virtual void sendHidden(
bool hidden);
222 virtual void sendReadonly(
bool readonly);
223 virtual void sendDisabled(
bool disabled);
224 virtual void sendTimestamp(uint32_t timestamp);
232 template <
typename Fn>
233 void formField(Fn&& fn,
const char* className =
"form-field") {
234 auto field =
tag(
"div");
235 field.attr(
"class", className);
245 template <
typename Fn>
249 const char* className =
"form-field") {
264 template <
typename Fn>
268 const char* className =
nullptr) {
269 auto box =
tag(
"div");
274 box.attr(
"class", className);
276 box.attr(
"style", visible ?
"display: block" :
"display: none");
283 template <
typename Fn>
295 auto select =
tag(
"select");
297 select.attr(
"name", name);
300 select.attr(
"id",
id);
308 void labelFor(
const char*
id,
const char* text);
318 const char* value =
nullptr,
320 const char* listId =
nullptr,
321 const char* onInput =
nullptr,
322 const char* onChange =
nullptr);
338 const char* value =
"on");
348 const char* cssClass =
nullptr);
358 const char* cssClass =
nullptr);
366 const char* cssClass =
nullptr);
374 const char* cssClass =
nullptr);
379 void selectOption(
int value,
int text,
bool selected =
false);
380 void selectOption(
int value,
const char* text,
bool selected =
false);
381 void selectOption(
const char* value,
int text,
bool selected =
false);
382 void selectOption(
const char* value,
const char* text,
bool selected =
false);
391 return HtmlTag(
this, tagName, paired);
398 return HtmlTag(
this, tagName,
false);
RAII helper for emitting a single HTML tag.
Definition web_sender.h:73
void body(const char(&text)[N])
Emit a text body using escaped content.
Definition web_sender.h:150
void body(Fn &&fn)
Emit a paired body using a callback.
Definition web_sender.h:135
HtmlTag & close()
Emit the closing > for the opening tag.
Definition web_sender.cpp:102
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:128
HtmlTag & attrIf(const char *name, bool enabled)
Append a boolean HTML attribute when enabled.
Definition web_sender.cpp:94
HtmlTag & finish()
Close the opening tag and mark it as finished.
Definition web_sender.cpp:110
Definition web_sender.h:177
void labeledField(const char *id, const char *text, Fn &&fn, const char *className="form-field")
Emit a labeled field wrapper.
Definition web_sender.h:246
void selectOption(int value, int text, bool selected=false)
Emit a single <option> element.
Definition web_sender.cpp:332
void formField(Fn &&fn, const char *className="form-field")
Emit a <div class="form-field">...</div> block.
Definition web_sender.h:233
HtmlTag voidTag(const char *tagName)
Start a builder for a void HTML tag such as <input>.
Definition web_sender.h:397
HtmlTag selectTag(const char *name, const char *id)
Start a <select> tag builder.
Definition web_sender.h:294
void labelFor(const char *id, const char *text)
Emit a <label for="...">...</label> pair.
Definition web_sender.cpp:184
void sendStatic(const char *data, size_t size=static_cast< size_t >(-1), bool escape=false)
Emit a static HTML asset stored in program memory.
Definition web_sender.cpp:149
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:190
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:265
virtual ~WebSender()
Base interface for emitting generated HTML.
Definition web_sender.cpp:147
HtmlTag tag(const char *tagName, bool paired=true)
Start an HTML tag builder.
Definition web_sender.h:390
void selectInput(const char *name, const char *id, Fn &&fn)
Emit a <select> block with optional name and id.
Definition web_sender.h:284
void checkboxInput(const char *name, const char *id, bool checked, const char *value="on")
Emit a checkbox input control.
Definition web_sender.cpp:235
void numberInput(const char *key, const NumericInputSpec &spec, const char *cssClass=nullptr)
Emit a numeric input control.
Definition web_sender.cpp:252
void rangeInput(const char *key, const NumericInputSpec &spec, const char *cssClass=nullptr)
Emit a range input control.
Definition web_sender.cpp:258
void passwordInput(const char *name, const char *id)
Emit a password input control.
Definition web_sender.cpp:223
Definition web_sender.h:27