/
table.go
283 lines (225 loc) · 5.24 KB
/
table.go
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package prago
import (
"fmt"
"html/template"
"io"
"reflect"
"strings"
"github.com/tealeg/xlsx"
)
type Table struct {
app *App
data []*tableData
}
type tableData struct {
Rows []*tableRow
FooterText []string
Graphs []*Graph
}
type tableRow struct {
Cells []*TableCell
}
type TableCell struct {
data *tableCellData
}
type tableCellData struct {
CSSClasses []string
Href string
DescriptionBefore string
Text string
DescriptionAfter string
Colspan int64
Rowspan int64
Checkboxes []*tableCellCheckbox
Buttons []*TableCellButton
}
type tableCellCheckbox struct {
Name string
Checked bool
}
type TableCellButton struct {
Name string
URL string
OnClick template.JS
}
type tableView struct {
Rows []*tableRowView
FooterText []string
Graphs []*graphDataView
}
type tableRowView struct {
Cells []*tableCellData
}
func (app *App) Table() *Table {
ret := &Table{
app: app,
}
ret.Table()
return ret
}
func (table *Table) Table() *Table {
table.data = append(table.data, &tableData{})
table.newRow()
return table
}
func (table *Table) newRow() {
table.currentTable().Rows = append(table.currentTable().Rows, &tableRow{})
}
func (table *Table) Header(items ...string) *Table {
var headerCells = []*TableCell{}
for _, item := range items {
cell := Cell(item)
cell.Header()
headerCells = append(headerCells, cell)
}
table.Row(headerCells...)
//table.newRow()
return table
}
func (table *Table) Row(items ...*TableCell) *Table {
row := table.currentRow()
row.Cells = append(row.Cells, items...)
table.newRow()
return table
}
func (cell *TableCell) Header() *TableCell {
cell.CSSClass("form_table_cell-header")
return cell
}
func (cell *TableCell) Pre() *TableCell {
cell.CSSClass("form_table_cell-pre")
return cell
}
func (cell *TableCell) Center() *TableCell {
cell.CSSClass("form_table_cell-center")
return cell
}
func (cell *TableCell) Right() *TableCell {
cell.CSSClass("form_table_cell-right")
return cell
}
func (cell *TableCell) Green() *TableCell {
cell.CSSClass("form_table_cell-green")
return cell
}
func (cell *TableCell) Nowrap() *TableCell {
cell.CSSClass("form_table_cell-nowrap")
return cell
}
func (cell *TableCell) DescriptionBefore(description string) *TableCell {
cell.data.DescriptionBefore = description
return cell
}
func (cell *TableCell) DescriptionAfter(description string) *TableCell {
cell.data.DescriptionAfter = description
return cell
}
func (cell *TableCell) URL(link string) *TableCell {
cell.data.Href = link
return cell
}
func (cell *TableCell) Colspan(i int64) *TableCell {
cell.data.Colspan = i
return cell
}
func (cell *TableCell) Rowspan(i int64) *TableCell {
cell.data.Rowspan = i
return cell
}
func (cell *TableCell) CSSClass(class string) *TableCell {
cell.data.CSSClasses = append(cell.data.CSSClasses, class)
return cell
}
func (cell *TableCell) Checkbox(name string, checked bool) *TableCell {
cell.data.Checkboxes = append(cell.data.Checkboxes, &tableCellCheckbox{
Name: name,
Checked: checked,
})
return cell
}
func (cell *TableCell) Button(btn *TableCellButton) *TableCell {
cell.data.Buttons = append(cell.data.Buttons, btn)
return cell
}
func (cell *tableCellData) GetClassesString() template.CSS {
return template.CSS(strings.Join(cell.CSSClasses, " "))
}
func (table *Table) currentTable() *tableData {
return table.data[len(table.data)-1]
}
func (table *Table) currentRow() *tableRow {
currentTable := table.currentTable()
return currentTable.Rows[len(currentTable.Rows)-1]
}
func Cell(item interface{}) *TableCell {
var number bool
if reflect.TypeOf(item).Kind() == reflect.Int {
item = humanizeNumber(int64(item.(int)))
number = true
}
if reflect.TypeOf(item).Kind() == reflect.Int64 {
item = humanizeNumber(item.(int64))
number = true
}
ret := &TableCell{
data: &tableCellData{
Text: fmt.Sprintf("%v", item),
},
}
ret.CSSClass("form_table_cell")
linkData, ok := item.([2]string)
if ok {
ret = Cell(linkData[1])
ret.URL(linkData[0])
}
if number {
ret.Right()
ret.Nowrap()
}
return ret
}
func (t *Table) AddFooterText(text string) {
t.currentTable().FooterText = append(t.currentTable().FooterText, text)
}
// TODO execute right into response
func (t *Table) ExecuteHTML() template.HTML {
return template.HTML(
t.app.ExecuteTemplateToString("form_table", t.templateData()),
)
}
func (t *Table) templateData() []*tableView {
var ret []*tableView
for _, v := range t.data {
view := &tableView{
FooterText: v.FooterText,
}
for _, v2 := range v.Rows {
row := &tableRowView{}
for _, v3 := range v2.Cells {
//cell := &tableCellData{}
row.Cells = append(row.Cells, v3.data)
}
view.Rows = append(view.Rows, row)
}
for _, graph := range v.Graphs {
view.Graphs = append(view.Graphs, graph.View())
}
ret = append(ret, view)
}
return ret
}
func (t *Table) ExportXLSX(writer io.Writer) error {
f := xlsx.NewFile()
for k, table := range t.data {
sheet, err := f.AddSheet(fmt.Sprintf("Sheet %d", k+1))
must(err)
for _, v1 := range table.Rows {
row := sheet.AddRow()
for _, v2 := range v1.Cells {
cell := row.AddCell()
cell.SetValue(v2.data.Text)
}
}
}
return f.Write(writer)
}