-
Notifications
You must be signed in to change notification settings - Fork 6
/
table_select.go
128 lines (108 loc) · 3.01 KB
/
table_select.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
package control
import (
"context"
"fmt"
"github.com/goradd/gengen/pkg/maps"
"github.com/goradd/goradd/pkg/config"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
)
// PrimaryKeyer is an interface that is often implemented by model objects.
type PrimaryKeyer interface {
PrimaryKey() string
}
type SelectTableI interface {
TableI
}
// SelectTable is a table that is row selectable. To detect a row selection, trigger on event.RowSelected
type SelectTable struct {
Table
selectedID string
}
func NewSelectTable(parent page.ControlI, id string) *SelectTable {
t := &SelectTable{}
t.Init(t, parent, id)
return t
}
func (t *SelectTable) Init(self page.ControlI, parent page.ControlI, id string) {
t.Table.Init(self, parent, id)
t.ParentForm().AddJQueryUI()
t.ParentForm().AddJavaScriptFile(config.GoraddAssets() + "/js/jquery.scrollIntoView.js", false, nil)
t.ParentForm().AddJavaScriptFile(config.GoraddAssets() + "/js/select-table.js", false, nil)
}
func (t *SelectTable) this() SelectTableI {
return t.Self.(SelectTableI)
}
func (t *SelectTable) GetRowAttributes(row int, data interface{}) (a *html.Attributes) {
var id string
if t.RowStyler() != nil {
a = t.RowStyler().Attributes(row, data)
id = a.Get("id") // styler might be giving us an id
} else {
a = html.NewAttributes()
}
// try to guess the id from the data
if id == "" {
switch obj := data.(type) {
case IDer:
id = obj.ID()
case PrimaryKeyer:
id = obj.PrimaryKey()
case map[string]string:
id,_ = obj["id"]
case maps.StringGetter:
id = obj.Get("id")
}
}
if id != "" {
// TODO: If configured, encrypt the id so its not publicly showing database ids
a.SetDataAttribute("id", id)
a.AddClass("sel")
if id == t.selectedID {
a.AddClass("selected")
a.Set("aria-selected", "")
}
} else {
a.AddClass("nosel")
}
if row % 2 == 1 {
a.AddClass("odd")
} else {
a.AddClass("even")
}
return a
}
func (t *SelectTable) ΩDrawingAttributes() *html.Attributes {
a := t.Table.ΩDrawingAttributes()
a.SetDataAttribute("grctl", "selecttable")
a.Set("role", "grid")
a.Set("aria-readonly", "true")
return a
}
func (t *SelectTable) ΩUpdateFormValues(ctx *page.Context) {
if data := ctx.CustomControlValue(t.ID(), "selectedId"); data != nil {
t.selectedID = fmt.Sprintf("%v", data)
}
}
func (t *SelectTable) SelectedID() string {
return t.selectedID
}
func (t *SelectTable) SetSelectedID(id string) {
t.selectedID = id
t.ParentForm().Response().ExecuteControlCommand(t.ID(), "selectTable", "option", "selectedId", id)
}
func (t *SelectTable) ΩMarshalState(m maps.Setter) {
m.Set("selId", t.selectedID)
}
func (t *SelectTable) ΩUnmarshalState(m maps.Loader) {
if v,ok := m.Load("selId"); ok {
if id, ok := v.(string); ok {
t.selectedID = id
}
}
}
func (t *SelectTable) ΩPutCustomScript(ctx context.Context, response *page.Response) {
options := map[string]interface{}{}
options["selectedId"] = t.selectedID
response.ExecuteControlCommand(t.ID(), "selectTable", page.PriorityHigh, options)
}