Webdar 1.0.0
Web user interface to libdar
html_form_dynamic_table.hpp
1/*********************************************************************/
2// webdar - a web server and interface program to libdar
3// Copyright (C) 2013-2025 Denis Corbin
4//
5// This file is part of Webdar
6//
7// Webdar is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// Webdar is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Webdar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef HTML_FORM_DYNAMIC_TABLE_HPP
25#define HTML_FORM_DYNAMIC_TABLE_HPP
26
27 // C system header files
28#include "my_config.h"
29extern "C"
30{
31
32}
33
34 // C++ system header files
35#include <memory>
36#include <string>
37
38 // webdar headers
39#include "actor.hpp"
40#include "events.hpp"
41#include "html_div.hpp"
42#include "html_table.hpp"
43#include "html_button.hpp"
44#include "html_text.hpp"
45#include "html_form_input.hpp"
46#include "html_form_select.hpp"
47#include "jsoner.hpp"
48
50
57
58
60{
61public:
63
72
73 virtual std::unique_ptr<body_builder> provide_object_of_type(unsigned int num,
74 const std::string & context,
75 std::string & changed_event) const = 0;
76};
77
78
80
85
115
116
118 public actor,
119 public events,
120 public jsoner
121{
122private:
123 struct line; // referred before the public part for the ::iterator nested class
124
125public:
126 static const std::string changed;
127
129 html_form_dynamic_table(bool has_left_labels,
130 bool selector_below,
131 const std::string & adder_text,
132 const std::string & adder_default_choice
133 );
134
136 html_form_dynamic_table(html_form_dynamic_table && ref) noexcept = delete;
137 html_form_dynamic_table & operator = (const html_form_dynamic_table & ref) = delete;
138 html_form_dynamic_table & operator = (html_form_dynamic_table && ref) noexcept = delete;
139 ~html_form_dynamic_table() = default;
140
142 {
143 public:
144 iterator() {};
145 iterator(const iterator & ref) = default;
146 iterator(iterator && ref) = default;
147 iterator & operator = (const iterator & ref) = default;
148 iterator & operator = (iterator && ref) = default;
149 ~iterator() = default;
150
151 iterator & operator ++ (signed int n) { ++ptr; return *this; };
152 iterator operator ++ () { iterator ret; ret.ptr = ptr; ++ptr; return ret; };
153 bool operator == (const iterator & ref) const { return ptr == ref.ptr; };
154 bool operator != (const iterator & ref) const { return ptr != ref.ptr; };
155 std::shared_ptr<html_text> get_left_label() const { return ptr->left_label; };
156 unsigned int get_object_type() const { return ptr->object_type_index; };
157 std::shared_ptr<body_builder> get_object() const { return ptr->dynobj; };
158
159 private:
160 std::list<line>::const_iterator ptr;
161
163 };
164
165 iterator begin() const { iterator ret; ret.ptr = table_content.begin(); return ret; };
166 iterator end() const { iterator ret; ret.ptr = table_content.end(); return ret; };
167 iterator last() const { iterator ret; ret.ptr = prev(table_content.end()); return ret; };
168
170 bool empty() const { return table_content.empty(); };
171
173
175
183
192
194 const html_form_dynamic_table_object_provider* get_current_provider() const { return my_provider; };
195
197
200 void set_obj_type_context(const std::string & ctxt) { context = ctxt; };
201
203
205 void add_obj_type(const std::string & label);
206
208 void clear_obj_type();
209
211 unsigned int obj_type_size() const { return adder.size() - 1; };
212
214 void clear() { table_content.clear(); del_event_to_content.clear(); events_to_delete.clear(); };
215
217
222 void add_line(unsigned int typenum);
223
225 virtual void load_json(const json & source) override;
226
228 virtual json save_json() const override;
229
231 virtual void clear_json() override;
232
234 virtual void on_event(const std::string & event_name) override;
235
237 void css_border_collapsed(bool mode) { table.css_border_collapsed(mode); };
238
240 void set_css_class_first_row(const std::string & val) { table.set_css_class_first_row(val); };
241
244
246 void set_css_class_first_column(const std::string & val) { table.set_css_class_first_column(val); };
247
250
252 void set_css_class_cells(const std::string & val) { table.set_css_class_cells(val); };
253
256
257
258protected:
260 virtual std::string inherited_get_body_part(const chemin & path,
261 const request & req) override;
262
264 virtual void new_css_library_available() override;
265
266private:
267
268 // events
269 static constexpr const char* new_line_to_add = "new_line";
270
272 struct line
273 {
274 line() { left_label.reset(); dynobj.reset(); del.reset(); };
275
276 std::shared_ptr<html_text> left_label;
277 unsigned int object_type_index;
278 std::shared_ptr<body_builder> dynobj;
279 std::unique_ptr<html_form_input> del;
280 };
281
282 bool left_label;
283 bool ignore_events;
284 const html_form_dynamic_table_object_provider* my_provider;
285 std::string context;
286 std::list<line> table_content;
287 unsigned int event_del_count;
288 std::map<std::string, std::list<line>::iterator> del_event_to_content;
289 std::deque<std::string> events_to_delete;
290 std::string default_choice;
291
292 html_table table;
293 html_form_select adder;
294
295 void del_line(const std::string & event_name);
296 void purge_to_delete();
297
298 static constexpr const unsigned int format_version = 1;
299 static constexpr const char* myclass_id = "html_form_dynamic_table";
300
301 static constexpr const char* jlabel_has_left_label = "has-lef-labels";
302 static constexpr const char* jlabel_contents = "contents";
303 static constexpr const char* jlabel_left_label = "left-label";
304 static constexpr const char* jlabel_index_type = "type";
305 static constexpr const char* jlabel_dynobj = "object";
306
308};
309
310#endif
class of object that are pointed/triggered to by others
Definition: actor.hpp:55
class chemin definition
Definition: chemin.hpp:51
class events
Definition: events.hpp:52
class html_div is the implementation of
Definition: html_div.hpp:46
Definition: html_form_dynamic_table.hpp:142
class html_form_dynamic_table_object_provider
Definition: html_form_dynamic_table.hpp:60
virtual std::unique_ptr< body_builder > provide_object_of_type(unsigned int num, const std::string &context, std::string &changed_event) const =0
method invoked by a html_form_dynamic_table when a new line has to be created
class html_form_dynamic_table web user dynamically add new lines in a table
Definition: html_form_dynamic_table.hpp:121
virtual void clear_json() override
inherited from jsoner
Definition: html_form_dynamic_table.cpp:264
virtual std::string inherited_get_body_part(const chemin &path, const request &req) override
inherited methods from body_builder
Definition: html_form_dynamic_table.cpp:290
void set_css_class_first_column()
exposed css method from underlying html_table
Definition: html_form_dynamic_table.hpp:249
void set_obj_type_context(const std::string &ctxt)
set context passed to the object provider
Definition: html_form_dynamic_table.hpp:200
virtual void on_event(const std::string &event_name) override
inherited from actor
Definition: html_form_dynamic_table.cpp:269
void set_css_class_cells(const std::string &val)
exposed css method from underlying html_table
Definition: html_form_dynamic_table.hpp:252
void add_line(unsigned int typenum)
manually add an line in the table with the object type give in number
Definition: html_form_dynamic_table.cpp:102
html_form_dynamic_table(bool has_left_labels, bool selector_below, const std::string &adder_text, const std::string &adder_default_choice)
default event name for all object of this clas
Definition: html_form_dynamic_table.cpp:46
void clear()
clear table content
Definition: html_form_dynamic_table.hpp:214
void set_css_class_first_row()
exposed css method from underlying html_table
Definition: html_form_dynamic_table.hpp:243
void set_obj_type_provider(const html_form_dynamic_table_object_provider *provider)
define a object provider
Definition: html_form_dynamic_table.cpp:79
void clear_obj_type()
clear all object types added by mean of add_obj_type
Definition: html_form_dynamic_table.cpp:87
void add_obj_type(const std::string &label)
add a new object type to be proposed to the user from the "adder" selector
Definition: html_form_dynamic_table.cpp:93
void set_css_class_first_row(const std::string &val)
exposed css method from underlying html_table
Definition: html_form_dynamic_table.hpp:240
unsigned int obj_type_size() const
return the number of object type available to the user
Definition: html_form_dynamic_table.hpp:211
void set_css_class_cells()
exposed css method from underlying html_table
Definition: html_form_dynamic_table.hpp:255
void set_css_class_first_column(const std::string &val)
exposed css method from underlying html_table
Definition: html_form_dynamic_table.hpp:246
void css_border_collapsed(bool mode)
exposed css method from underlying html_table
Definition: html_form_dynamic_table.hpp:237
virtual void new_css_library_available() override
inherited from body_builder
Definition: html_form_dynamic_table.cpp:298
bool empty() const
whether the current dynamic_table is empty
Definition: html_form_dynamic_table.hpp:170
const html_form_dynamic_table_object_provider * get_current_provider() const
get the current object provider
Definition: html_form_dynamic_table.hpp:194
virtual json save_json() const override
inherited from jsoner
Definition: html_form_dynamic_table.cpp:226
virtual void load_json(const json &source) override
inherited from jsoner
Definition: html_form_dynamic_table.cpp:150
class html_form_select implements HTML input of type "select"
Definition: html_form_select.hpp:53
unsigned int size() const
number of available choices provided to the web user by this object
Definition: html_form_select.hpp:84
html component implementing the html table structure
Definition: html_table.hpp:52
void css_border_collapsed(bool mode)
whether border are collapsed or separated (collasped by default)
Definition: html_table.cpp:52
void set_css_class_first_row(const std::string &val)
define a specific css object for the cells first row
Definition: html_table.cpp:63
void set_css_class_first_column(const std::string &val)
define a specific css class for the cells first column
Definition: html_table.cpp:84
void set_css_class_cells(const std::string &val)
assigned to all cells except those of the first raw if set_css_cells_first_raw was set
Definition: html_table.cpp:104
class jsoner
Definition: jsoner.hpp:73
class holding fields of an HTTP request (method, URI, header, cookies, and so on)
Definition: request.hpp:45
defines the event class
defines jsoner class and class exception_json