1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use std::borrow::BorrowMut;
use crate::{DefaultModifiers, sp};
use crate::components::*;
use crate::node::{Node, NodeContainer};
use crate::Renderable;
#[derive(Debug, Clone)]
pub struct Column {
    pub node: Node,
    pub title: Option<String>,
}
impl NodeContainer for Column {
    fn get_node(&mut self) -> &mut Node {
        self.node.borrow_mut()
    }
}
impl DefaultModifiers<Column> for Column {}
impl Column {
    pub fn new(title: Option<&str>) -> Self {
        Self {
            node: Default::default(),
            title: title.map(|title| title.to_string()),
        }
    }
    pub fn span(&mut self, col_nbs: i32) -> Self {
        self.set_attr("span", col_nbs.to_string().as_str());
        self.clone()
    }
}
impl Renderable for Column {
    fn render(&self) -> Node {
        self.clone()
            .tag("col")
            .node
    }
}
#[derive(Debug, Clone)]
pub struct Row {
    children: Vec<Box<dyn Renderable>>,
    node: Node,
    pub name: String,
    pub action: Option<String>,
    pub action_target: Option<String>,
    pub download: Option<String>,
}
impl NodeContainer for Row {
    fn get_node(&mut self) -> &mut Node {
        self.node.borrow_mut()
    }
}
impl DefaultModifiers<Row> for Row {}
impl ChildContainer for Row {
    fn get_children(&mut self) -> &mut Vec<Box<dyn Renderable>> {
        return self.children.borrow_mut();
    }
}
impl Appendable for Row {}
impl Row {
    pub fn new(name: &str) -> Self {
        Self {
            children: vec![],
            node: Default::default(),
            name: name.to_string(),
            action: None,
            action_target: None,
            download: None,
        }
    }
    pub fn action(&mut self, url: &str) -> Self {
        self.action = Some(url.to_string());
        self.clone()
    }
    pub fn action_target(&mut self, target: &str) -> Self {
        self.action_target = Some(target.to_string());
        self.clone()
    }
    pub fn download_action(&mut self, url: &str, file_name: &str) -> Self {
        self.action = Some(url.to_string());
        self.download = Some(file_name.to_string());
        self.clone()
    }
}
impl Renderable for Row {
    fn render(&self) -> Node {
        let mut row = self.clone()
            .tag("tr");
        if self.action.is_some() {
            row.add_class("clickable");
        }
        let mut node = row.node;
        row.children.iter()
            .for_each(|child| {
                let mut td = View::new()
                    .tag("td");
                if let Some(url) = &self.action {
                    td.get_children().push({
                        Box::new({
                            let mut link = View::new()
                                .add_class("link-row")
                                .tag("a")
                                .set_attr("href", url)
                                .append_child(child.clone());
                            if let Some(target) = &self.action_target {
                                link.set_attr("target", target);
                            }
                            if let Some(file_name) = &self.download {
                                link.set_attr("download", file_name);
                            }
                            link
                        })
                    })
                } else {
                    td.get_children().push(child.clone());
                }
                node.children.push(td.render())
            });
        node
    }
}
#[derive(Debug, Clone)]
pub struct Table {
    name: String,
    children: Vec<Box<dyn Renderable>>,
    columns: Vec<Column>,
    rows: Vec<Row>,
    node: Node,
    selectable: bool,
}
impl Table {
    pub fn new(name: &str, columns: Vec<Column>) -> Self {
        Self {
            name: name.to_string(),
            children: vec![],
            columns,
            rows: vec![],
            node: Default::default(),
            selectable: false,
        }
    }
    pub fn selectable(&mut self, is_selectable: bool) -> Self {
        self.selectable = is_selectable;
        self.clone()
    }
    pub fn alternate_row_color(&mut self, is_alternate: bool) -> Self {
        self.add_class("alternate-row-color");
        self.clone()
    }
    pub fn append_child(&mut self, row: Row) -> Self
    {
        self.rows.push(row);
        self.clone()
    }
}
impl NodeContainer for Table {
    fn get_node(&mut self) -> &mut Node {
        self.node.borrow_mut()
    }
}
impl DefaultModifiers<Table> for Table {}
impl ChildContainer for Table {
    fn get_children(&mut self) -> &mut Vec<Box<dyn Renderable>> {
        return self.children.borrow_mut();
    }
}
impl Renderable for Table {
    fn render(&self) -> Node {
        let mut table = self.clone()
            .tag("table")
            .add_class("table");
        table.get_node().children.push({
            let mut colgroup = View::new()
                .tag("colgroup");
            if self.selectable {
                colgroup.append_child({
                    Column::new(None)
                        .width(&sp(30))
                });
            }
            self.clone().columns.into_iter()
                .for_each(|col| {
                    colgroup.append_child(col);
                });
            colgroup
        }.render());
        if self.clone().columns.iter().any(|col| col.title.is_some()) {
            table.get_node().children.push({
                View::new()
                    .tag("thead")
                    .append_child({
                        let mut tr = View::new()
                            .tag("tr");
                        if self.selectable {
                            tr.prepend_child({
                                View::new()
                                    .tag("th")
                            });
                        }
                        self.clone().columns.into_iter()
                            .for_each(|col| {
                                if let Some(title) = col.title {
                                    tr.append_child({
                                        Text::new(title.as_str(), TextStyle::Label)
                                            .tag("th")
                                    });
                                } else {
                                    tr.append_child({
                                        View::new().tag("th")
                                    });
                                }
                            });
                        tr
                    })
            }.render());
        }
        table.get_node().children.push({
            let mut tbody = View::new()
                .tag("tbody");
            for mut row in self.clone().rows {
                if self.selectable {
                    row.prepend_child({
                        Checkbox::new(&self.name, &row.name, CheckboxStyle::Checkbox)
                    });
                }
                tbody.append_child(row);
            }
            tbody
        }.render());
        table.node
    }
}