-
Notifications
You must be signed in to change notification settings - Fork 6
/
list_checkbox.go
131 lines (109 loc) · 3.42 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
package control
import (
"bytes"
"context"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/control"
)
type ItemDirection int
const (
HorizontalItemDirection ItemDirection = 0
VerticalItemDirection = 1
)
type CheckboxListI interface {
control.CheckboxListI
renderItem(item control.ListItemI) 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-table table, or a single-table
// scrolling list much like a standard html select list.
type CheckboxList struct {
control.CheckboxList
isInline bool
}
func NewCheckboxList(parent page.ControlI, id string) *CheckboxList {
l := &CheckboxList{}
l.Init(l, parent, id)
return l
}
func (l *CheckboxList) Init(self CheckboxListI, parent page.ControlI, id string) {
l.CheckboxList.Init(self, parent, id)
l.SetLabelDrawingMode(html.LabelAfter)
}
func (l *CheckboxList) this() CheckboxListI {
return l.Self.(CheckboxListI)
}
func (l *CheckboxList) SetIsInline(i bool) {
l.isInline = i
}
// Ω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() // skip default checkbox list attributes
a.SetDataAttribute("grctl", "bs-checkboxlist")
/*
a.AddAttributeValue("gr-cbl")
if l.isScrolling {
a.AddAttributeValue("gr-cbl-scroller")
} else {
a.AddAttributeValue("gr-cbl-table")
}*/
return a
}
func (l *CheckboxList) ΩDrawInnerHtml(ctx context.Context, buf *bytes.Buffer) (err error) {
h := l.getItemsHtml()
buf.WriteString(h)
return nil
}
func (l *CheckboxList) getItemsHtml() (h string) {
items := l.ListItems()
var order = make([]int, len(items), len(items))
rowCount := (len(items) - 1) / l.ColumnCount() + 1
if l.Direction() == VerticalItemDirection {
for i := 0; i < len(items); i++ {
row := i % rowCount
col := i / rowCount
order[i] = row * l.ColumnCount() + col
}
} else {
for i := 0; i < len(items); i++ {
order[i] = i
}
}
i := 0
for row := 0; row < rowCount; row++ {
rowHtml := ""
for col := 0; col < l.ColumnCount(); col++ {
rowHtml += l.this().renderItem(items[order[i]])
i++
}
if l.ColumnCount() > 1 {
h += html.RenderTag("div", html.NewAttributes().AddClass("row"), rowHtml)
} else {
h += rowHtml
}
}
return
}
func (l *CheckboxList) renderItem(item control.ListItemI) (h string) {
attributes := html.NewAttributes()
attributes.SetID(item.ID())
attributes.Set("name", item.ID())
attributes.Set("type", "checkbox")
if l.IsIdSelected(item.ID()) {
attributes.Set("checked", "")
}
attributes.AddClass("form-check-input")
ctrl := html.RenderVoidTag("input", attributes)
h = html.RenderLabel(html.NewAttributes().Set("for", item.ID()).AddClass("form-check-label"), item.Label(), ctrl, html.LabelAfter)
attributes = item.Attributes().Copy()
attributes.AddClass("form-check")
if l.isInline {
attributes.AddClass("form-check-inline")
}
h = html.RenderTag("div", attributes, h)
return
}