22 val.call<
void>(
"remove");
26 if (
val.hasOwnProperty(
"parentNode"))
28 auto parent =
val[
"parentNode"];
29 if (!parent.isUndefined() && !parent.isNull())
30 parent.call<
void>(
"removeChild",
val);
32 val.call<
void>(
"remove");
35 val.call<
void>(
"remove");
89 auto elem = std::make_shared<Element>(element);
96 return std::begin(children_);
100 return std::end(children_);
104 return std::begin(children_);
108 return std::end(children_);
119 if (elem->deferredSetup_)
120 elem->deferredSetup_(element);
121 return children_.emplace_back(std::move(elem));
133 return shared_from_base<Element>();
141 replaceElementImpl(element);
142 return shared_from_base<Element>();
147 throw std::runtime_error(
"Element is not empty, cannot emplace");
152 deferredSetup_(element);
153 return shared_from_base<Element>();
158 return shared_from_base<Element>();
177 for (
auto const& element : elements)
186 element_.call<
Nui::val>(
"insertBefore", elem->element_, (*where)->element_);
187 if (elem->deferredSetup_)
188 elem->deferredSetup_(element);
189 return *children_.insert(where, std::move(elem));
197 std::vector<std::function<void()>> eventClearers;
198 eventClearers.reserve(element.
attributes().size());
199 std::vector<std::size_t> deferredIndices;
201 for (std::size_t i = 0; i != element.
attributes().size(); ++i)
203 auto const& attribute = element.
attributes()[i];
205 if (attribute.defer())
207 deferredIndices.push_back(i);
210 if (attribute.isRegular())
211 attribute.setOn(*
this);
213 auto clear = attribute.getEventClear();
216 const auto id = attribute.createEvent(weak_from_base<Element>());
219 eventClearers.push_back([clear = std::move(clear),
id]() {
225 if (!eventClearers.empty())
227 eventClearers.shrink_to_fit();
228 unsetup_ = [eventClearers = std::move(eventClearers)]() {
229 for (
auto const& clear : eventClearers)
238 if (!deferredIndices.empty())
240 deferredSetup_ = [
this, deferredIndices = std::move(deferredIndices)](
HtmlElement const& element) {
241 std::vector<std::function<void()>> eventClearers;
242 eventClearers.reserve(deferredIndices.size());
244 for (
auto index : deferredIndices)
246 auto const& attribute = element.
attributes()[index];
248 if (attribute.isRegular())
249 attribute.setOn(*
this);
251 auto clear = attribute.getEventClear();
254 const auto id = attribute.createEvent(weak_from_base<Element>());
257 eventClearers.push_back([clear = std::move(clear),
id]() {
263 if (!eventClearers.empty())
265 eventClearers.shrink_to_fit();
266 unsetup_ = [unsetup1 = std::move(unsetup_), eventClearers = std::move(eventClearers)]() {
268 for (
auto const& clear : eventClearers)
278 if (where >= children_.size())
281 return insert(
begin() +
static_cast<decltype(children_)::difference_type
>(where), element);
286 return children_[index];
290 return children_[index];
295 return children_.erase(where);
299 return children_.erase(first, last);
309 return !children_.empty();
314 return children_.size();
319 return element_[
"tagName"].as<std::string>();
323 void replaceElementImpl(
HtmlElement const& element)
332 throw std::runtime_error(
"Element is undefined");
340 deferredSetup_(element);
344 using destroy_fn = void (*)(
Nui::val&);
347 std::function<void()> unsetup_;
348 std::function<void(HtmlElement
const& element)> deferredSetup_;
353 auto elem = std::make_shared<Element>();
359 #include <nui/frontend/elements/impl/html_element.tpp>
360 #include <nui/frontend/elements/impl/range_renderer.tpp>
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
static Nui::val createElement(HtmlElement const &element)
Definition: childless_element.hpp:166
Definition: element.hpp:42
iterator begin()
Definition: element.hpp:94
auto appendElement(HtmlElement const &element)
Definition: element.hpp:115
void replaceElement(std::invocable< Element &, Renderer const & > auto &&fn)
Definition: element.hpp:135
auto emplaceElement(std::invocable< Element &, Renderer const & > auto &&fn)
Definition: element.hpp:155
const_iterator begin() const
Definition: element.hpp:102
std::string tagName() const
Definition: element.hpp:317
auto slotFor(value_type const &value)
Definition: element.hpp:123
const_iterator end() const
Definition: element.hpp:106
auto insert(std::size_t where, HtmlElement const &element)
Definition: element.hpp:276
void clearChildren()
Definition: element.hpp:302
collection_type::const_iterator const_iterator
Definition: element.hpp:46
Element(Element &&)=delete
~Element()
Definition: element.hpp:81
void setup(HtmlElement const &element)
Relies on weak_from_this and cannot be used from the constructor.
Definition: element.hpp:195
auto replaceElement(HtmlElement const &element)
Definition: element.hpp:139
auto emplaceElement(HtmlElement const &element)
Definition: element.hpp:144
void setTextContent(std::string_view text)
Definition: element.hpp:169
collection_type::value_type value_type
Definition: element.hpp:47
void setTextContent(std::string const &text)
Definition: element.hpp:161
void setTextContent(char const *text)
Definition: element.hpp:165
Element(Nui::val val)
This constructor takes ownership of a val.
Definition: element.hpp:62
auto & operator[](std::size_t index)
Definition: element.hpp:284
auto const & operator[](std::size_t index) const
Definition: element.hpp:288
Element & operator=(Element &&)=delete
static std::shared_ptr< Element > makeElement(HtmlElement const &element)
Definition: element.hpp:87
auto erase(iterator where)
Definition: element.hpp:293
std::size_t childCount() const
Definition: element.hpp:312
Element & operator=(Element const &)=delete
Element()
Definition: element.hpp:69
void appendElement(std::invocable< Element &, Renderer const & > auto &&fn)
Definition: element.hpp:111
iterator end()
Definition: element.hpp:98
void appendElements(std::vector< std::function< std::shared_ptr< Element >(Element &, Renderer const &)>> const &elements)
Definition: element.hpp:175
Element(HtmlElement const &elem)
Definition: element.hpp:49
Element(Element const &)=delete
auto erase(iterator first, iterator last)
Definition: element.hpp:297
bool hasChildren() const
Definition: element.hpp:307
auto insert(iterator where, HtmlElement const &element)
Definition: element.hpp:181
std::vector< std::shared_ptr< Element > > collection_type
Definition: element.hpp:44
collection_type::iterator iterator
Definition: element.hpp:45
constexpr static auto invalidEventId
Definition: event_context.hpp:41
Definition: html_element.hpp:60
std::vector< Attribute > const & attributes() const
Definition: html_element.hpp:346
static void destroyByParentChildRemoval(Nui::val &val)
Definition: element.hpp:24
static void destroyByRemove(Nui::val &val)
Definition: element.hpp:20
static void doNotDestroy(Nui::val &)
Definition: element.hpp:37
Definition: basic_element.hpp:12
std::shared_ptr< Element > makeStandaloneElement(std::invocable< Element &, Renderer const & > auto &&fn)
Definition: element.hpp:351
emscripten::val val
Definition: val.hpp:5
Definition: materialize.hpp:52
RendererType type
Definition: materialize.hpp:53