-
Notifications
You must be signed in to change notification settings - Fork 6
/
navbar_list.go
211 lines (184 loc) · 6.46 KB
/
navbar_list.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
package control
import (
"bytes"
"context"
"fmt"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/javascript"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/action"
"github.com/goradd/goradd/pkg/page/control"
"github.com/goradd/goradd/pkg/page/control/data"
"github.com/goradd/goradd/pkg/page/event"
)
type NavbarListI interface {
page.ControlI
control.ItemListI
data.DataManagerEmbedder
OnSelect (actions ...action.ActionI) page.ControlI
}
type NavbarList struct {
page.Control
control.ItemList
subItemTag string
data.DataManager
}
func NavbarSelectEvent() page.EventI {
e := &page.Event{JsEvent: "gr-bs-navbarselect"}
e.ActionValue(javascript.JsCode("ui")) // This will be the action value sent by the proxy...the id of the item
return e
}
func NewNavbarList(parent page.ControlI, id string) *NavbarList {
t := &NavbarList{}
t.ItemList = control.NewItemList(t)
t.Init(t, parent, id)
return t
}
func (l *NavbarList) Init(self NavbarListI, parent page.ControlI, id string) {
l.Control.Init(self, parent, id)
l.Tag = "ul"
l.subItemTag = "li"
pxy := control.NewProxy(l, l.proxyID())
pxy.On(event.Click(),
action.Trigger(l.ID(), "gr-bs-navbarselect", javascript.JsCode("g$(this).data('grAv')")))
}
func (l *NavbarList) proxyID() string {
return l.ID() + "-pxy"
}
func (l *NavbarList) ItemProxy() *control.Proxy {
return control.GetProxy(l, l.proxyID())
}
func (l *NavbarList) this() NavbarListI {
return l.Self.(NavbarListI)
}
func (l *NavbarList) ΩDrawTag(ctx context.Context) string {
if l.DataManager.HasDataProvider() {
l.LoadData(ctx, l)
defer l.Clear()
}
return l.Control.ΩDrawTag(ctx)
}
// Ω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 *NavbarList) ΩDrawingAttributes() html.Attributes {
a := l.Control.ΩDrawingAttributes()
a.SetDataAttribute("grctl", "navbarlist")
a.AddClass("navbar-nav")
return a
}
func (l *NavbarList) ΩDrawInnerHtml(ctx context.Context, buf *bytes.Buffer) (err error) {
h := l.getItemsHtml(ctx, l.ListItems(), false)
buf.WriteString(h)
return nil
}
func (l *NavbarList) getItemsHtml(ctx context.Context, items []control.ListItemI, hasParent bool) string {
var h = ""
for i, item := range items {
if item.HasChildItems() {
if hasParent {
// A dropdown inside a dropdown
h += fmt.Sprintf(
`<a class="dropdown-item dropdown-toggle" id="%s_menu" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
%s
</a>
<div class="dropdown-menu" aria-labelledby="%s_menu">`, item.ID(), item.RenderLabel(), item.ID())
h += l.getItemsHtml(ctx, item.ListItems(), true)
h += "</div>"
} else {
// top level menu
var lastClass = ""
if i == len(items)-1 {
// last item, so modify dropdown menu so it does not go off of screen
// If there is only one item in the navbar, and this is the left navbar, this might cause a problem.
// We can potentially fix that by asking the parent item if that is the situation.
lastClass = "dropdown-menu-right"
}
h += fmt.Sprintf(
`<%s class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="%s_menu" role="menu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
%s
</a>
<div class="dropdown-menu %s" aria-labelledby="%s_menu">`, l.subItemTag, item.ID(), item.RenderLabel(), lastClass, item.ID())
h += l.getItemsHtml(ctx, item.ListItems(), true)
h += fmt.Sprintf("</div></%s>", l.subItemTag)
}
} else {
if item.IsDivider() {
h += html.RenderTag("div", html.NewAttributes().AddClass("dropdown-divider"), "")
} else if item.Disabled() {
if !hasParent {
h += fmt.Sprintf(`<li class="nav-item">
<a class="nav-link disabled" href="#">%s</a>
</li>`, item.RenderLabel())
} else {
h += fmt.Sprintf(`<a class="dropdown-item disabled" href="#">%s</a>
</li>`, item.RenderLabel())
}
} else if hasParent {
itemH := item.RenderLabel()
itemAttributes := item.Attributes().Copy()
itemAttributes.AddClass("nav-item")
linkAttributes := html.NewAttributes()
itemAttributes.Set("role", "menuitem")
linkAttributes.AddClass("dropdown-item")
if item.Anchor() == "" {
itemH = l.ItemProxy().LinkHtml(ctx, itemH, item.ID(), linkAttributes)
}
h += itemH
} else {
item.AnchorAttributes().AddClass("nav-link")
itemH := item.RenderLabel()
itemAttributes := item.Attributes().Copy()
itemAttributes.AddClass("nav-item")
if item.Anchor() == "" {
linkAttributes := html.NewAttributes()
linkAttributes.AddClass("nav-link")
itemH = l.ItemProxy().LinkHtml(ctx, itemH, item.ID(), linkAttributes)
}
itemH = html.RenderTag(l.subItemTag, itemAttributes, itemH)
h += itemH
}
}
}
return h
}
func (l *NavbarList) OnSelect (actions ...action.ActionI) page.ControlI {
return l.On(NavbarSelectEvent(), actions...)
}
type NavbarListCreator struct {
// ID is the control id of the html widget and must be unique to the page
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 data.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
// OnSelect is the action to take when a list item is selected.
OnSelect action.ActionI
}
func (c NavbarListCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewNavbarList(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
func (c NavbarListCreator) Init(ctx context.Context, ctrl NavbarListI) {
if c.Items != nil {
ctrl.AddListItems(c.Items)
}
if c.DataProvider != nil {
ctrl.SetDataProvider(c.DataProvider)
} else if c.DataProviderID != "" {
provider := ctrl.Page().GetControl(c.DataProviderID).(data.DataBinder)
ctrl.SetDataProvider(provider)
}
ctrl.ApplyOptions(c.ControlOptions)
if c.OnSelect != nil {
ctrl.OnSelect(c.OnSelect)
}
}
// GetNavbarList is a convenience method to return the control with the given id from the page.
func GetNavbarList(c page.ControlI, id string) *NavbarList {
return c.Page().GetControl(id).(*NavbarList)
}