Loading...
Searching...
No Matches
TreeUtilsHTML.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31/*
32 * TreeXML.h
33 *
34 * Created on: Jun 24, 2012
35 * Author: mpeura
36 */
37
38
39
40#ifndef DRAIN_TREE_UTILS_HTML
41#define DRAIN_TREE_UTILS_HTML
42
43// #include <ostream>
44
45#include "TreeHTML.h"
46
47namespace drain {
48
49
50
52
53public:
54
56
59 static
60 drain::TreeHTML & initHtml(drain::TreeHTML & html, const std::string & key = "");
61
62
63
64 static inline
65 drain::TreeHTML & getFirstElem(drain::TreeHTML & elem, drain::html::tag_t tagType){
66 if (elem.hasChildren()){
67 return elem.getChildren().begin()->second; // last
68 }
69 else {
70 //
71 return elem.addChild()(tagType); // addChild(elem, tagType);
72 }
73 }
74
75
76
78
82 // static // compare with TreeHTML::addChild( - is needed?
83 //drain::TreeHTML & addChild(drain::TreeHTML & elem, drain::html::tag_t tagType, const std::string & key);
84
85 template <class T>
86 static inline
87 drain::TreeHTML & appendElem(drain::TreeHTML & elem, drain::html::tag_t tagType, const T & arg){
88 drain::TreeHTML & child = elem.addChild()(tagType); // addChild(elem,tagType);
89 child = arg;
90 return child;
91 };
92
93
94 template <class T, class ...TT>
95 static inline
96 drain::TreeHTML & appendElem(drain::TreeHTML & elem, drain::html::tag_t tagType, const T & arg, const TT & ...args) {
97 appendElem(elem, tagType, arg);
98 return appendElem(elem, tagType, args...);
99 }
100
101 static inline
102 drain::TreeHTML & createTable(drain::TreeHTML & body, const std::list<std::string> & columnTitles){ // "var", "ref", no whitespace.
103
104 drain::TreeHTML & table = body.addChild()(drain::NodeHTML::TABLE); // drain::TreeUtilsHTML::addChild(body, drain::NodeHTML::TABLE);
105
106 drain::TreeHTML & tr = table["header"](drain::NodeHTML::TR);
107
108 for (const auto & title: columnTitles){
109 drain::TreeHTML & th = tr[title](drain::NodeHTML::TH);
110 th = title;
111 }
112
113 return table;
114 }
115
117
120 template <class T>
121 static
122 // drain::TreeHTML & fillTableRow(drain::TreeHTML & table, drain::TreeHTML & tr, const std::string value = "");
124
125 for (const auto & entry: table.getChildren()){
126 // Using keys of the first row, create a new row. Often, it is the title row (TH elements).
127 for (const auto & e: entry.second.getChildren()){
128 tr[e.first]->setType(drain::NodeHTML::TD);
129 tr[e.first] = value;
130 }
131 // Return after investigating the first row:
132 return tr;
133 }
134
135 // If table is empty, also tr is.
136 return tr;
137
138 }
139
140 static
141 drain::TreeHTML & addTableRow(drain::TreeHTML & table, const std::string value = ""){
142 drain::TreeHTML & tr = table.addChild()(html::TR); // addChild(table, HTML::TR);
143 return fillTableRow(table, tr, value);
144 }
145
146
147protected:
148
149 // Dummy end... TODO: redesign logic, perhaps addChild();
150 template <class T>
151 static inline
152 drain::TreeHTML & appendElem(drain::TreeHTML & elem, drain::html::tag_t tagType){
153 if (elem.hasChildren()){
154 return elem.getChildren().rbegin()->second; // last
155 }
156 else {
157 //
158 return elem;
159 }
160 }
161
162};
163
164} // drain::
165
166#endif /* TREE_UTILS_XML_H_ */
167
Definition TreeUtilsHTML.h:51
static drain::TreeHTML & initHtml(drain::TreeHTML &html, const std::string &key="")
Initialize a HTML object with "head" (including "title", "style") and "body" elements.
Definition TreeUtilsHTML.cpp:43
static drain::TreeHTML & fillTableRow(drain::TreeHTML &table, drain::TreeHTML &tr, const T &value)
Creates a new table row (TD) using first row as a template.
Definition TreeUtilsHTML.h:123
static drain::TreeHTML & appendElem(drain::TreeHTML &elem, drain::html::tag_t tagType, const T &arg)
Add element of given type. The path key is generated automatically, unless given.
Definition TreeUtilsHTML.h:87
Definition DataSelector.cpp:1277
drain::UnorderedMultiTree< NodeHTML, false, NodeXML<>::path_t > TreeHTML
The HTML data structure.
Definition TreeHTML.h:62
Definition TreeHTML.h:66