Nui
childless_element.hpp
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <optional>
8 #include <string>
9 #include <string_view>
10 #include <variant>
11 
12 namespace Nui::Dom
13 {
19  {
20  public:
21  explicit ChildlessElement(HtmlElement const& elem)
23  {}
25  : BasicElement{std::move(val)}
26  {}
27 
28  void setProperty(std::string_view key, std::string const& value)
29  {
30  element_.set(Nui::val{std::string{key}}, Nui::val{value});
31  }
32  void setProperty(std::string_view key, std::string_view value)
33  {
34  setProperty(key, std::string{value});
35  }
36  void setProperty(std::string_view key, char const* value)
37  {
38  if (value[0] == '\0')
39  element_.delete_(std::string{key});
40  else
41  setProperty(key, std::string{value});
42  }
43  void setProperty(std::string_view key, std::invocable<Nui::val> auto&& value)
44  {
45  element_.set(Nui::val{std::string{key}}, Nui::bind(value, std::placeholders::_1));
46  }
47  template <typename T>
48  void setProperty(std::string_view key, std::optional<T> const& value)
49  {
50  if (value)
51  setProperty(key, *value);
52  else
53  element_.delete_(std::string{key});
54  }
55  void setProperty(std::string_view key, bool value)
56  {
57  element_.set(Nui::val{std::string{key}}, Nui::val{value});
58  }
59  template <typename... List>
60  void setProperty(std::string_view key, std::variant<List...> const& variant)
61  {
62  std::visit(
63  [this, &key](auto const& value) {
64  this->setProperty(key, value);
65  },
66  variant);
67  }
68  template <typename T>
69  requires std::integral<T>
70  void setProperty(std::string_view key, T value)
71  {
72  element_.set(Nui::val{std::string{key}}, Nui::val{static_cast<int>(value)});
73  }
74  template <typename T>
75  requires std::floating_point<T>
76  void setProperty(std::string_view key, T value)
77  {
78  element_.set(Nui::val{std::string{key}}, Nui::val{static_cast<double>(value)});
79  }
80 
81  void addEventListener(std::string_view event, std::invocable<Nui::val> auto&& callback)
82  {
83  element_.call<void>(
84  "addEventListener", Nui::val{std::string{event}}, Nui::bind(callback, std::placeholders::_1));
85  }
86 
87  // TODO: more overloads?
88  void setAttribute(std::string_view key, std::string const& value)
89  {
90  // FIXME: performance, keys are turned to std::string
91  if (value.empty())
92  element_.call<Nui::val>("removeAttribute", Nui::val{std::string{key}});
93  else
94  element_.call<Nui::val>("setAttribute", Nui::val{std::string{key}}, Nui::val{value});
95  }
96  void setAttribute(std::string_view key, std::string_view value)
97  {
98  setAttribute(key, std::string{value});
99  }
100  void setAttribute(std::string_view key, std::invocable<Nui::val> auto&& value)
101  {
102  element_.set(Nui::val{std::string{key}}, Nui::bind(value, std::placeholders::_1));
103  }
104  void setAttribute(std::string_view key, char const* value)
105  {
106  if (value[0] == '\0')
107  element_.call<Nui::val>("removeAttribute", Nui::val{std::string{key}});
108  else
109  element_.call<Nui::val>("setAttribute", Nui::val{std::string{key}}, Nui::val{std::string{value}});
110  }
111  void setAttribute(std::string_view key, bool value)
112  {
113  if (value)
114  element_.call<Nui::val>("setAttribute", Nui::val{std::string{key}}, Nui::val{std::string{key}});
115  else
116  element_.call<Nui::val>("removeAttribute", Nui::val{std::string{key}});
117  }
118  template <typename T>
119  requires std::integral<T>
120  void setAttribute(std::string_view key, T value)
121  {
122  element_.call<Nui::val>("setAttribute", Nui::val{std::string{key}}, Nui::val{static_cast<int>(value)});
123  }
124  template <typename T>
125  requires std::floating_point<T>
126  void setAttribute(std::string_view key, T value)
127  {
128  element_.call<Nui::val>("setAttribute", Nui::val{std::string{key}}, Nui::val{static_cast<double>(value)});
129  }
130  void setAttribute(std::string_view key, Nui::val value)
131  {
132  element_.call<Nui::val>("setAttribute", Nui::val{std::string{key}}, value);
133  }
134  template <typename T>
135  void setAttribute(std::string_view key, std::optional<T> const& value)
136  {
137  if (value)
138  setAttribute(key, *value);
139  else
140  element_.call<Nui::val>("removeAttribute", Nui::val{std::string{key}});
141  }
142  template <typename... List>
143  void setAttribute(std::string_view key, std::variant<List...> const& variant)
144  {
145  std::visit(
146  [this, &key](auto const& value) {
147  this->setAttribute(key, value);
148  },
149  variant);
150  }
151 
152  void setNodeValue(std::string_view value)
153  {
154  element_.set("nodeValue", Nui::val{std::string{value}});
155  }
156  void setNodeValue(std::string const& value)
157  {
158  element_.set("nodeValue", Nui::val{value});
159  }
160 
161  protected:
162  explicit ChildlessElement()
163  : BasicElement{}
164  {}
165 
166  static Nui::val createElement(HtmlElement const& element)
167  {
168  return element.bridge()->createElement(element);
169  }
170  };
171 };
Definition: basic_element.hpp:14
Nui::val element_
Definition: basic_element.hpp:72
The basic element cannot have children and does not hold explicit ownership of them.
Definition: childless_element.hpp:19
requires std::integral< T > void setProperty(std::string_view key, T value)
Definition: childless_element.hpp:70
void setProperty(std::string_view key, std::optional< T > const &value)
Definition: childless_element.hpp:48
void setProperty(std::string_view key, std::invocable< Nui::val > auto &&value)
Definition: childless_element.hpp:43
void setAttribute(std::string_view key, std::string const &value)
Definition: childless_element.hpp:88
void addEventListener(std::string_view event, std::invocable< Nui::val > auto &&callback)
Definition: childless_element.hpp:81
void setAttribute(std::string_view key, std::invocable< Nui::val > auto &&value)
Definition: childless_element.hpp:100
requires std::floating_point< T > void setAttribute(std::string_view key, T value)
Definition: childless_element.hpp:126
void setProperty(std::string_view key, bool value)
Definition: childless_element.hpp:55
ChildlessElement(HtmlElement const &elem)
Definition: childless_element.hpp:21
void setAttribute(std::string_view key, std::variant< List... > const &variant)
Definition: childless_element.hpp:143
void setAttribute(std::string_view key, std::optional< T > const &value)
Definition: childless_element.hpp:135
void setAttribute(std::string_view key, char const *value)
Definition: childless_element.hpp:104
ChildlessElement(Nui::val val)
Definition: childless_element.hpp:24
ChildlessElement()
Definition: childless_element.hpp:162
void setProperty(std::string_view key, std::variant< List... > const &variant)
Definition: childless_element.hpp:60
requires std::integral< T > void setAttribute(std::string_view key, T value)
Definition: childless_element.hpp:120
static Nui::val createElement(HtmlElement const &element)
Definition: childless_element.hpp:166
requires std::floating_point< T > void setProperty(std::string_view key, T value)
Definition: childless_element.hpp:76
void setNodeValue(std::string_view value)
Definition: childless_element.hpp:152
void setAttribute(std::string_view key, Nui::val value)
Definition: childless_element.hpp:130
void setProperty(std::string_view key, std::string_view value)
Definition: childless_element.hpp:32
void setAttribute(std::string_view key, bool value)
Definition: childless_element.hpp:111
void setProperty(std::string_view key, char const *value)
Definition: childless_element.hpp:36
void setAttribute(std::string_view key, std::string_view value)
Definition: childless_element.hpp:96
void setProperty(std::string_view key, std::string const &value)
Definition: childless_element.hpp:28
void setNodeValue(std::string const &value)
Definition: childless_element.hpp:156
Definition: html_element.hpp:60
HtmlElementBridge const * bridge() const
Definition: html_element.hpp:356
Definition: basic_element.hpp:12
emscripten::val bind(F &&f, Args &&... args)
Equivalent of std::bind returning a javascript functor.
Definition: functions.hpp:98
requires(IsObservedLike< ObservedValues > &&...) ObservedValueCombinator< std
Definition: observed_value_combinator.hpp:191
emscripten::val val
Definition: val.hpp:5
Nui::val(* createElement)(HtmlElement const &element)
Definition: html_element_bridge.hpp:15