Nui
table.hpp
Go to the documentation of this file.
1 #pragma once
2 
11 
12 #include <string>
13 #include <optional>
14 #include <functional>
15 #include <memory>
16 #include <vector>
17 
18 namespace Nui::Components
19 {
20  template <template <typename...> typename ContainerT, typename ElementT, typename... OtherArgs>
22  {
24  Nui::Observed<ContainerT<ElementT, OtherArgs...>>& tableModel;
25 
27  std::variant<std::monostate, std::string, Nui::Observed<std::string> const*> caption = std::monostate{};
28 
30  std::function<Nui::ElementRenderer()> headerRenderer = {};
31 
33  std::function<Nui::ElementRenderer(long long i, ElementT const&)> rowRenderer = {};
34 
36  std::function<Nui::ElementRenderer()> footerRenderer = {};
37 
39  std::vector<Attribute> tableAttributes = {};
40 
42  std::vector<Attribute> captionAttributes = {};
43 
45  std::vector<Attribute> headerAttributes = {};
46 
48  std::vector<Attribute> bodyAttributes = {};
49 
51  std::vector<Attribute> footerAttributes = {};
52  };
53 
59  template <typename ModelT>
60  class Table : public Table<std::vector<ModelT>>
61  {
63  };
64  template <template <typename...> typename ContainerT, typename ElementT, typename... OtherArgs>
65  class Table<ContainerT<ElementT, OtherArgs...>>
66  {
67  public:
69  : tableParams_{std::move(args)}
70  {}
71 
73  {
74  using namespace Elements;
75 
76  // clang-format off
77  return table{tableParams_.tableAttributes}(
78  // caption
79  [cap = std::move(tableParams_.caption), capAttributes = std::move(tableParams_.captionAttributes)]() -> Nui::ElementRenderer {
80  return visitOverloaded(cap,
81  [](std::monostate) -> Nui::ElementRenderer{
82  return nil();
83  },
84  [&capAttributes](std::string const& content) -> Nui::ElementRenderer{
85  return caption{capAttributes}(content);
86  },
87  [&capAttributes](Observed<std::string> const* model) -> Nui::ElementRenderer{
88  return caption{capAttributes}(*model);
89  }
90  );
91  }(),
92  // header
93  [headerRenderer = std::move(tableParams_.headerRenderer), headerAttributes = std::move(tableParams_.headerAttributes)]() -> Nui::ElementRenderer {
94  if (headerRenderer)
95  return thead{headerAttributes}(headerRenderer());
96  else
97  return nil();
98  }(),
99  // body
100  tbody{tableParams_.bodyAttributes}(
101  range(tableParams_.tableModel),
102  [renderer = std::move(tableParams_.rowRenderer)](long i, auto const& row) -> Nui::ElementRenderer {
103  using namespace std::string_literals;
104 
105  if (renderer)
106  return renderer(i, row);
107  else
108  return nil();
109  }
110  ),
111  // footer
112  [footerRenderer = std::move(tableParams_.footerRenderer), footerAttributes = std::move(tableParams_.footerAttributes)]() -> Nui::ElementRenderer {
113  if (footerRenderer)
114  return
115  tfoot{footerAttributes}(footerRenderer());
116  else
117  return nil();
118  }()
119  );
120  // clang-format on
121  }
122 
123  private:
124  TableArguments<ContainerT, ElementT, OtherArgs...> tableParams_;
125  };
126 
127  template <template <typename...> typename ContainerT, typename ElementT, typename... OtherArgs>
128  Table(TableArguments<ContainerT, ElementT, OtherArgs...>&& args) -> Table<ContainerT<ElementT, OtherArgs...>>;
129 }
constexpr Table(TableArguments< ContainerT, ElementT, OtherArgs... > &&args)
Definition: table.hpp:68
Nui::ElementRenderer operator()() &&
Definition: table.hpp:72
A wrapper around the html table elements.
Definition: table.hpp:61
Definition: observed_value.hpp:1202
Definition: dialog.hpp:11
Table(TableArguments< ContainerT, ElementT, OtherArgs... > &&args) -> Table< ContainerT< ElementT, OtherArgs... >>
UnoptimizedRange< IteratorAccessor< ContainerT const >, std::decay_t< Detail::ObservedAddReference_t< Observed > >... > range(ContainerT const &container, Observed &&... observed)
Definition: range.hpp:97
static auto nil()
Definition: nil.hpp:7
auto visitOverloaded(std::variant< VariantTypes... > const &variant, VisitFunctionTypes &&... visitFunctions)
Definition: visit_overloaded.hpp:10
std::function< std::shared_ptr< Dom::Element >(Dom::Element &, Renderer const &)> ElementRenderer
Definition: element_renderer.hpp:11
Definition: table.hpp:22
std::vector< Attribute > tableAttributes
Attributes to be forwarded to the table element.
Definition: table.hpp:39
std::function< Nui::ElementRenderer()> footerRenderer
The renderer for the footer. Can be empty.
Definition: table.hpp:36
std::vector< Attribute > footerAttributes
Attributes to be forwarded to the footer element.
Definition: table.hpp:51
std::vector< Attribute > bodyAttributes
Attributes to be forwarded to the body element.
Definition: table.hpp:48
std::function< Nui::ElementRenderer()> headerRenderer
The renderer for the header. Can be empty.
Definition: table.hpp:30
std::vector< Attribute > captionAttributes
Attributes to be forwarded to the caption element.
Definition: table.hpp:42
Nui::Observed< ContainerT< ElementT, OtherArgs... > > & tableModel
Contains the data to be displayed in the table.
Definition: table.hpp:24
std::function< Nui::ElementRenderer(long long i, ElementT const &)> rowRenderer
The renderer for each row.
Definition: table.hpp:33
std::variant< std::monostate, std::string, Nui::Observed< std::string > const * > caption
The caption of the table. Can be static, observed or off.
Definition: table.hpp:27
std::vector< Attribute > headerAttributes
Attributes to be forwarded to the header element.
Definition: table.hpp:45