-
Notifications
You must be signed in to change notification settings - Fork 6
/
list_checkbox.go
235 lines (210 loc) · 6.94 KB
/
list_checkbox.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
package control
import (
"bytes"
"context"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
"strings"
)
type ItemDirection int
const (
HorizontalItemDirection ItemDirection = 0
VerticalItemDirection = 1
)
type CheckboxListI interface {
MultiselectListI
ΩRenderItem(tag string, item ListItemI) (h string)
}
// CheckboxList is a multi-select control that presents its choices as a list of checkboxes.
// Styling is provided by divs and spans that you can provide css for in your style sheets. The
// goradd.css file has default styling to handle the basics. It wraps the whole thing in a div that can be set
// to scroll as well, so that the final structure can be styled like a multi-column table, or a single-column
// scrolling list much like a standard html select list.
type CheckboxList struct {
MultiselectList
columnCount int
direction ItemDirection
labelDrawingMode html.LabelDrawingMode
isScrolling bool
}
// NewCheckboxList creates a new CheckboxList
func NewCheckboxList(parent page.ControlI, id string) *CheckboxList {
l := &CheckboxList{}
l.Init(l, parent, id)
return l
}
// Init is called by subclasses
func (l *CheckboxList) Init(self page.ControlI, parent page.ControlI, id string) {
l.MultiselectList.Init(self, parent, id)
l.Tag = "div"
l.columnCount = 1
l.labelDrawingMode = page.DefaultCheckboxLabelDrawingMode
}
func (l *CheckboxList) this() CheckboxListI {
return l.Self.(CheckboxListI)
}
// SetColumnCount sets the number of columns to use to display the list. Items will be evenly distributed
// across the columns.
func (l *CheckboxList) SetColumnCount(columns int) CheckboxListI {
if l.columnCount <= 0 {
panic("Columns must be at least 1.")
}
l.columnCount = columns
l.Refresh()
return l.this()
}
// ColumnCount returns the current column count.
func (l *CheckboxList) ColumnCount() int {
return l.columnCount
}
// SetDirection specifies how items are distributed across the columns.
func (l *CheckboxList) SetDirection(direction ItemDirection) CheckboxListI {
l.direction = direction
l.Refresh()
return l.this()
}
// Direction returns the direction of how items are spread across the columns.
func (l *CheckboxList) Direction() ItemDirection {
return l.direction
}
// SetLabelDrawingMode indicates how labels for each of the checkboxes are drawn.
func (l *CheckboxList) SetLabelDrawingMode(mode html.LabelDrawingMode) CheckboxListI {
l.labelDrawingMode = mode
l.Refresh()
return l.this()
}
// SetIsScrolling sets whether the list will scroll if it gets bigger than its bounding box.
// You will need to style the bounding box to give it limits, or else it will simply grow as
// big as the list.
func (l *CheckboxList) SetIsScrolling(s bool) CheckboxListI {
l.isScrolling = s
l.Refresh()
return l.this()
}
// ΩDrawingAttributes retrieves the tag's attributes at draw time.
// You should not normally need to call this, and the
// attributes are disposed of after drawing, so they are essentially read-only.
func (l *CheckboxList) ΩDrawingAttributes() *html.Attributes {
a := l.Control.ΩDrawingAttributes()
a.SetDataAttribute("grctl", "checkboxlist")
a.AddClass("gr-cbl")
if l.isScrolling {
a.AddClass("gr-cbl-scroller")
} else {
a.AddClass("gr-cbl-table")
}
return a
}
// ΩDrawInnerHtml is called by the framework to draw the contents of the list.
func (l *CheckboxList) ΩDrawInnerHtml(ctx context.Context, buf *bytes.Buffer) (err error) {
h := l.getItemsHtml(l.items)
if l.isScrolling {
h = html.RenderTag("div", html.NewAttributes().SetClass("gr-cbl-table").SetID(l.ID() + "_cbl"), h)
}
buf.WriteString(h)
return nil
}
func (l *CheckboxList) getItemsHtml(items []ListItemI) string {
if l.direction == VerticalItemDirection {
return l.verticalHtml(items)
} else {
return l.horizontalHtml(items)
}
}
func (l *CheckboxList) verticalHtml(items []ListItemI) (h string) {
lines := l.verticalHtmlItems(items)
if l.columnCount == 1 {
return strings.Join(lines, "\n")
} else {
columnHeight := len(lines)/l.columnCount + 1
for col := 0; col < l.columnCount; col++ {
colHtml := strings.Join(lines[col*columnHeight:(col+1)*columnHeight], "\n")
colHtml = html.RenderTag("div", html.NewAttributes().AddClass("gr-cbl-table"), colHtml)
h += colHtml
}
return
}
}
func (l *CheckboxList) verticalHtmlItems(items []ListItemI) (h []string) {
for _, item := range items {
if item.HasChildItems() {
tag := "div"
attributes := item.Attributes().Copy()
attributes.AddClass("gr-cbl-heading")
subItems := l.verticalHtmlItems(item.ListItems())
h = append(h, html.RenderTag(tag, attributes, item.Label()))
h = append(h, subItems...)
} else {
h = append(h, l.this().ΩRenderItem("div", item))
}
}
return
}
// ΩRenderItem draws an item in the list. You do not normally need to call this, but subclasses can override it.
func (l *CheckboxList) ΩRenderItem(tag string, item ListItemI) (h string) {
attributes := html.NewAttributes()
attributes.SetID(item.ID())
attributes.Set("name", l.ID())
attributes.Set("value", item.ID())
attributes.Set("type", "checkbox")
if l.IsIdSelected(item.ID()) {
attributes.Set("checked", "")
}
ctrl := html.RenderVoidTag("input", attributes)
h = html.RenderLabel(html.NewAttributes().Set("for", item.ID()), item.Label(), ctrl, l.labelDrawingMode)
attributes = item.Attributes().Copy()
attributes.SetID(item.ID() + "_cell")
attributes.AddClass("gr-cbl-item")
h = html.RenderTag(tag, attributes, h)
return
}
func (l *CheckboxList) horizontalHtml(items []ListItemI) (h string) {
var itemNum int
var rowHtml string
for _, item := range items {
if item.HasChildItems() {
if itemNum != 0 {
// output a row
h += html.RenderTag("div", html.NewAttributes().SetClass("gr-cbl-row"), rowHtml)
rowHtml = ""
itemNum = 0
}
tag := "div"
attributes := item.Attributes().Copy()
attributes.AddClass("gr-cbl-heading")
h += html.RenderTag(tag, attributes, item.Label())
h += l.horizontalHtml(item.ListItems())
} else {
rowHtml += l.this().ΩRenderItem("span", item)
itemNum++
if itemNum == l.columnCount {
// output a row
h += html.RenderTag("div", html.NewAttributes().SetClass("gr-cbl-row"), rowHtml)
rowHtml = ""
itemNum = 0
}
}
}
if itemNum != 0 {
h += html.RenderTag("div", html.NewAttributes().SetClass("gr-cbl-row"), rowHtml)
}
return
}
// ΩUpdateFormValues is called by the framework to tell the control to update its internal values
// based on the form values sent by the browser.
func (l *CheckboxList) ΩUpdateFormValues(ctx *page.Context) {
controlID := l.ID()
if ctx.RequestMode() == page.Server {
// Using name attribute to return rendered checkboxes that are turned on.
if v, ok := ctx.FormValues(controlID); ok {
l.SetSelectedIdsNoRefresh(v)
}
} else {
// Ajax will only send changed items based on their ids
for _,item := range l.ListItems() {
if v, ok := ctx.FormValue(item.ID()); ok {
l.SetSelectedIdNoRefresh(item.ID(), page.ConvertToBool(v))
}
}
}
}