1 module awebview.gui.widgets.table;
2 
3 __EOF__
4 
5 import std.array;
6 import std.range;
7 
8 
9 class Table(alias attrs) : TemplateHTMLElement!(HTMLElement, `<table id="%[id%]" ` ~ buildHTMLTagAttr(attrs) ~ `>%s</table>`)
10 {
11     size_t[2] headerSize() const;
12     size_t[2] bodySize() const;
13     size_t[2] footerSize() const;
14 
15     inout(HTMLElement) getHeaderElement(size_t i, size_t j) inout;
16     inout(HTMLElement) getBodyElement(size_t i, size_t j) inout;
17     inout(HTMLElement) getFooterElement(size_t i, size_t j) inout;
18 
19     void makeTdTag(size_t i, size_t j, HTMLElement element, scope void delegate(const(char)[]) sink) const
20     { .put(sink, "<td>"); .put(sink, element.html); .put(sink, "</td>"); }
21 
22     void makeTrTag(size_t i, string content, scope void delegate(const(char)[]) sink) const
23     { .put(sink, "<tr>"); .put(sink, content); .put(sink, "</tr>"); }
24 
25     void makeTdTagOfHeader(size_t i, size_t j, HTMLElement element, scope void delegate(const(char)[]) sink) const
26     { makeTdTag(i, j, element, sink); }
27 
28     void makeTrTagOfHeader(size_t i, string content, scope void delegate(const(char)[]) sink) const
29     { makeTrTag(i, content, sink); }
30 
31     void makeTdTagOfFooter(size_t i, size_t j, HTMLElement element, scope void delegate(const(char)[]) sink) const
32     { makeTdTag(i, j, element, sink); }
33 
34     void makeTrTagOfFooter(size_t i, string content, scope void delegate(const(char)[]) sink) const
35     { makeTrTag(i, content, sink); }
36 
37     void makeTheadTag(string content, scope void delegate(const(char)[]) sink) const
38     { .put(sink, "<thead>"); .put(sink, content); .put(sink, "</thead>"); }
39 
40     void makeTbodyTag(string content, scope void delegate(const(char)[]) sink) const
41     { .put(sink, "<tbody>"); .put(sink, content); .put(sink, "</tbody>"); }
42 
43     void makeTfootTag(string content, scope void delegate(const(char)[]) sink) const
44     { .put(sink, "<tfoot>"); .put(sink, content); .put(sink, "</tfoot>"); }
45 
46     override
47     @property
48     string html() const
49     {
50         auto appHB = appender!string();
51         {
52             auto appTHs = appender!string();
53             foreach(i, e; _hs)
54                 _g.makeTdTagOfHeader(0, i, e, (const(char)[] buf){ .put(appTHs, buf); });
55 
56             auto appHTR = appender!string();
57             g.tr(0, appTHs.data, appHTR);
58 
59             auto appH = appender!string();
60             g.thead(appHTR.data, appHB);
61         }
62 
63         {
64             auto appTRs = appender!string();
65             foreach(i, e; _ds){
66                 auto appTDs = appender!string();
67                 foreach(j, ee; e)
68                     g.td(i+1, j, ee, appTDs);
69 
70                 g.tr(i+1, appTDs.data, appTRs);
71             }
72             g.tbody(appTRs.data, appHB);
73         }
74 
75         auto appT = appender!string();
76         g.table(appHB.data, appT);
77         return appT.data;
78     }
79 
80 
81     void addHeaderRow(HTMLElement[] elems)
82     {
83         _thead ~= elems;
84     }
85 
86 
87     void addHeaderCol(HTMLElement[] elems)
88     {
89         foreach(i, e; elems)
90             _thead[i] ~= e;
91     }
92 
93 
94     void addBodyRow(HTMLElement[] elems)
95     {
96         _tbody ~= elems;
97     }
98 
99 
100     void addBodyCol(HTMLElement[] elems)
101     {
102         foreach(i, e; elems)
103             _tbody[i] ~= e;
104     }
105 
106 
107     void addFooterRow(HTMLElement[] elems)
108     {
109         _tfoot ~= elems;
110     }
111 
112 
113     void addFooterCol(HTMLElement[] elems)
114     {
115         foreach(i, e; elems)
116             _tfoot[i] ~= e;
117     }
118 
119 
120     final
121     ref inout(HTMLElement[][]) theadContents() inout pure nothrow @safe @nogc @property { return _thead; }
122 
123     final
124     ref inout(HTMLElement[][]) tbodyContents() inout pure nothrow @safe @nogc @property { return _tbody; }
125 
126     final
127     ref inout(HTMLElement[][]) tfootContents() inout pure nothrow @safe @nogc @property { return _tfoot; }
128 
129 
130   private:
131     HTMLElement[][] _thead;
132     HTMLElement[][] _tbody;
133     HTMLElement[][] _tfoot;
134 }
135 
136 
137 /*
138 class Table(G) : HTMLElement
139 {
140     this(string id, bool doCreateObject, G g)
141     {
142         super(id, doCreateObject);
143         _g = g;
144         _g.id = id;
145     }
146 
147 
148     void genTD(size_t i, size_t j, )
149 
150 
151     override
152     @property
153     string html()
154     {
155         auto appHB = appender!string();
156         {
157             auto appTHs = appender!string();
158             foreach(i, e; _hs)
159                 _g.td(0, i, e, appTHs);
160 
161             auto appHTR = appender!string();
162             g.tr(0, appTHs.data, appHTR);
163 
164             auto appH = appender!string();
165             g.thead(appHTR.data, appHB);
166         }
167 
168         {
169             auto appTRs = appender!string();
170             foreach(i, e; _ds){
171                 auto appTDs = appender!string();
172                 foreach(j, ee; e)
173                     g.td(i+1, j, ee, appTDs);
174 
175                 g.tr(i+1, appTDs.data, appTRs);
176             }
177             g.tbody(appTRs.data, appHB);
178         }
179 
180         auto appT = appender!string();
181         g.table(appHB.data, appT);
182         return appT.data;
183     }
184 
185 
186   private:
187     string[] _hs;
188     string[][] _ds;
189     G _g;
190 }
191 
192 
193 //interface ITable
194 //{
195 //    void header(IHeader);
196     
197 //}
198 
199 
200 //abstract class TableHeader : HTMLElement
201 //{
202 //    this(string id, bool doCreateObject)
203 //    {
204 //        super(id, doCreateObject);
205 //    }
206 
207 
208 //    TableHeaderData[] data();
209 //}
210 
211 
212 //abstract class TableHeaderData : HTMLElement
213 //{
214 //    this(string id, bool doCreateObject)
215 //    {
216 //        super(id, doCreateObject);
217 //    }
218 //}
219 
220 
221 //abstract class TableRow : HTMLElement
222 //{
223 //    this(string id, bool doCreateObject)
224 //    {
225 //        super(id, doCreateObject);
226 //    }
227 //}
228 
229 
230 //abstract class TableData : HTMLElement
231 //{
232 //    this(string id, bool doCreateObject)
233 //    {
234 //        super(id, doCreateObject);
235 //    }
236 //}
237 
238 ///**
239 //auto table = new Table!(["class": "table table-striped"])("tweetTable");
240 
241 //foreach(i; 0 .. 10){
242 
243 //    auto row = new Row;
244 //    foreach(j; 0 .. 10)
245 //        row ~= new MyData(....);
246 
247 //    table ~= row;
248 //}
249 //*/
250 //abstract class Table : HTMLElement
251 //{
252 //    this(string id)
253 //    {
254 //        super(id);
255 //    }
256 
257 
258 //    //void header(TableHeader h) {}
259 //    //TableHeader header() {}
260 
261 
262 //    //TableRow[] data() {}
263 //    //void data(TableRow[]) {}
264 //}
265 
266 
267 //class TableHeader
268 //{
269 
270 //}
271 
272 
273 //class TableBody
274 //{
275 
276 //}
277 
278 
279 //class Table
280 */