-
Notifications
You must be signed in to change notification settings - Fork 6
/
column_node.go
153 lines (138 loc) · 4.04 KB
/
column_node.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
package column
import (
"context"
"github.com/goradd/goradd/pkg/iface"
"github.com/goradd/goradd/pkg/orm/query"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/control"
)
// NodeColumn is a column that uses a query.NodeI to get its text out of data that is coming from the ORM.
// Create it with NewNodeColumn
type NodeColumn struct {
control.ColumnBase
node query.NodeI
}
// NewNodeColumn creates a table column that uses a query.NodeI object to get its text out of an ORM object.
// node should point to data that is preloaded in the ORM object. If the node points to a Date, Time or DateTime type
// of column, you MUST specify a time format by calling SetTimeFormat.
func NewNodeColumn(node query.NodeI) *NodeColumn {
i := NodeColumn{}
i.Init(node)
i.node = node
return &i
}
func (c *NodeColumn) Init(node query.NodeI) {
if node == nil {
panic("node is required")
}
c.ColumnBase.Init(c)
c.SetTitle(query.NodeGoName(node))
}
func (c *NodeColumn) GetNode() query.NodeI {
return c.node
}
func (c *NodeColumn) CellData(_ context.Context, _ int, _ int, data interface{}) interface{} {
if v, ok := data.(Getter); !ok {
return ""
} else {
n := c.node
var names []string
// walk up the chain of nodes to figure out how to walk down the chain of data
for {
name := query.NodeGoName(n)
if name == "" {
break
}
names = append(names, name)
n = query.ParentNode(n)
if n == nil {
break
}
}
if len(names) < 2 {
panic("bad node passed to the column_node column. These nodes must start with a table node and end with a column node")
}
var i int
v2 := v
for i = len(names) - 2; i > 0; i-- {
obj := v2.Get(names[i])
if iface.IsNil(obj) {
return nil // Either an object was not joined, or the joined object does not exist in the database.
}
v2, ok = obj.(Getter)
if !ok {
panic("node chain does not match a chain of Getters")
}
}
s := v2.Get(names[0]) // This should be a column node
return s
}
}
func (c *NodeColumn) Serialize(e page.Encoder) {
c.ColumnBase.Serialize(e)
if err := e.Encode(&c.node); err != nil {
panic(err)
}
return
}
func (c *NodeColumn) Deserialize(dec page.Decoder) {
c.ColumnBase.Deserialize(dec)
if err := dec.Decode(&c.node); err != nil {
panic(err)
}
}
type NodeGetter interface {
GetNode() query.NodeI
}
// MakeNodeSlice is a convenience method to convert a slice of columns into a slice of nodes derived from
// those columns. The column slice would typically come from the table's SortColumns method, and the returned
// slice would be passed to the database's OrderBy clause when building a query. Since this is a common use, it
// will also add sort info to the nodes.
func MakeNodeSlice(columns []control.ColumnI) []query.NodeI {
var nodes []query.NodeI
for _, c := range columns {
if getter, ok := c.(NodeGetter); ok {
node := getter.GetNode()
if nodeSorter, ok2 := node.(query.NodeSorter); ok2 {
switch c.SortDirection() {
case control.SortAscending:
nodeSorter.Ascending()
case control.SortDescending:
nodeSorter.Descending()
}
}
nodes = append(nodes, node)
} else {
panic("Column is not a NodeGetter.")
}
}
return nodes
}
// NodeColumnCreator creates a column that treats each row as data from the ORM, and gets to that data using
// a database Node.
type NodeColumnCreator struct {
// ID will assign the given id to the column. If you do not specify it, an id will be given it by the framework.
ID string
// Node is a database node generated by the code generator
Node query.NodeI
// Title is the title of the column that will appear in the header
Title string
// Sortable makes the column display sort arrows in the header
Sortable bool
control.ColumnOptions
}
func (c NodeColumnCreator) Create(ctx context.Context, parent control.TableI) control.ColumnI {
col := NewNodeColumn(c.Node)
if c.ID != "" {
col.SetID(c.ID)
}
col.SetTitle(c.Title)
if c.Sortable {
col.SetSortable()
}
col.ApplyOptions(ctx, parent, c.ColumnOptions)
return col
}
func init() {
control.RegisterColumn(NodeColumn{})
}