-
Notifications
You must be signed in to change notification settings - Fork 6
/
list_group.go
90 lines (76 loc) · 2.81 KB
/
list_group.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
package control
import (
"context"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/control"
)
type ListGroupI interface {
control.UnorderedListI
}
// A ListGroup implements the Bootstrap ListGroup control.
// Since just a static list isn't all that interesting, this is a dynamic list whose
// individual items are considered clickable. To conform with the
// html standard and accessibility rules, items should appear as anchors if they link to another page, but as buttons
// if they cause a different action, like popping up a dialog.
//
// Use the data provider to AddItems to the list, assigning attributes as needed to produce the items you want.
// You can also use a proxy control to create the attributes.
type ListGroup struct {
control.UnorderedList
}
func NewListGroup(parent page.ControlI, id string) *ListGroup {
l := &ListGroup{}
l.Self = l
l.Init(parent, id)
return l
}
func (l *ListGroup) Init(parent page.ControlI, id string) {
l.UnorderedList.Init(parent, id)
l.Tag = "div"
l.SetItemTag("a") // default to anchor tags. Change it to something else if needed.
l.AddClass("list-group")
}
func (l *ListGroup) GetItemsHtml(items []*control.ListItem) string {
// make sure the list items have the correct classes before drawing them
for _, item := range items {
item.Attributes().AddClass("list-group-item list-group-item-action")
}
return l.UnorderedList.GetItemsHtml(items)
}
type ListGroupCreator struct {
ID string
// Items is a static list of labels and values that will be in the list. Or, use a DataProvider to dynamically generate the items.
Items []control.ListValue
// DataProvider is the control that will dynamically provide the data for the list and that implements the DataBinder interface.
DataProvider control.DataBinder
// DataProviderID is the id of a control that will dynamically provide the data for the list and that implements the DataBinder interface.
DataProviderID string
page.ControlOptions
// ItemTag is the tag of the items. It defaults to "li".
ItemTag string
}
// Create is called by the framework to create a new control from the Creator. You
// do not normally need to call this.
func (c ListGroupCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewListGroup(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
func (c ListGroupCreator) Init(ctx context.Context, ctrl ListGroupI) {
sub := control.UnorderedListCreator{
Items:c.Items,
DataProvider:c.DataProvider,
ControlOptions: c.ControlOptions,
}
sub.Init(ctx, ctrl)
if c.ItemTag != "" {
ctrl.SetItemTag(c.ItemTag)
}
}
// GetListGroup is a convenience method to return the control with the given id from the page.
func GetListGroup(c page.ControlI, id string) *ListGroup {
return c.Page().GetControl(id).(*ListGroup)
}
func init() {
page.RegisterControl(&ListGroup{})
}