Nui
basic_element.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <nui/frontend/val.hpp>
4 
5 #include <algorithm>
6 #include <cctype>
7 #include <memory>
8 #include <string>
9 #include <optional>
10 
11 namespace Nui::Dom
12 {
13  class BasicElement : public std::enable_shared_from_this<BasicElement>
14  {
15  public:
17  : element_{std::move(val)}
18  {}
19  virtual ~BasicElement() = default;
20 
21  Nui::val const& val() const
22  {
23  return element_;
24  }
26  {
27  return element_;
28  }
29  operator Nui::val const&() const
30  {
31  return element_;
32  }
33  operator Nui::val&()
34  {
35  return element_;
36  }
37  operator Nui::val&&() &&
38  {
39  return std::move(element_);
40  }
41 
42  template <class Derived>
43  std::shared_ptr<Derived> shared_from_base()
44  {
45  return std::static_pointer_cast<Derived>(shared_from_this());
46  }
47  template <class Derived>
48  std::weak_ptr<Derived> weak_from_base()
49  {
50  return std::weak_ptr<Derived>(shared_from_base<Derived>());
51  }
52  std::string tagName() const
53  {
54  auto tag = element_["tagName"].as<std::string>();
55  std::transform(tag.begin(), tag.end(), tag.begin(), [](unsigned char c) {
56  return std::tolower(c);
57  });
58  return tag;
59  }
60  std::optional<std::string> namespaceUri() const
61  {
62  if (!element_.isUndefined() && element_.hasOwnProperty("namespaceURI"))
63  return element_["namespaceURI"].as<std::string>();
64  return std::nullopt;
65  }
66 
67  protected:
68  explicit BasicElement()
69  : element_{Nui::val::undefined()}
70  {}
71 
73  };
74 }
Definition: basic_element.hpp:14
std::weak_ptr< Derived > weak_from_base()
Definition: basic_element.hpp:48
BasicElement()
Definition: basic_element.hpp:68
virtual ~BasicElement()=default
Nui::val element_
Definition: basic_element.hpp:72
Nui::val const & val() const
Definition: basic_element.hpp:21
std::shared_ptr< Derived > shared_from_base()
Definition: basic_element.hpp:43
std::string tagName() const
Definition: basic_element.hpp:52
BasicElement(Nui::val val)
Definition: basic_element.hpp:16
std::optional< std::string > namespaceUri() const
Definition: basic_element.hpp:60
Nui::val & val()
Definition: basic_element.hpp:25
Definition: basic_element.hpp:12
Definition: file_dialog.hpp:6
emscripten::val val
Definition: val.hpp:5