-
Notifications
You must be signed in to change notification settings - Fork 6
/
navbar.go
261 lines (223 loc) · 7.22 KB
/
navbar.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package control
import (
"context"
"github.com/goradd/goradd/pkg/bootstrap/config"
"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/event"
"github.com/goradd/html5tag"
)
const (
NavTabs = "nav-tabs"
NavPills = "nav-pills"
NavJustified = "nav-justified"
NavbarHeader = "navbar-header"
NavbarCollapse = "navbar-collapse"
NavbarBrand = "navbar-brand"
NavbarToggle = "navbar-toggle"
NavbarNav = "navbar-nav"
NavbarLeft = "navbar-left"
NavbarRight = "navbar-right"
NavbarForm = "navbar-form"
)
type NavbarExpandClass string
const (
NavbarExpandExtraLarge NavbarExpandClass = "navbar-expand-xl"
NavbarExpandLarge = "navbar-expand-lg"
NavbarExpandMedium = "navbar-expand-md"
NavbarExpandSmall = "navbar-expand-sm"
// NavbarExpandNone will always show the navbar as collapsed at any size
NavbarExpandNone = ""
)
// NavbarCollapsedBrandPlacement controls the location of the brand when the navbar is collapsed
type NavbarCollapsedBrandPlacement int
const (
// NavbarCollapsedBrandLeft will place the brand on the left and the toggle button on the right when collapsed
NavbarCollapsedBrandLeft NavbarCollapsedBrandPlacement = iota
// NavbarCollapsedBrandRight will place the brand on the right and the toggle button on the left when collapsed
NavbarCollapsedBrandRight
// NavbarCollapsedBrandHidden means the brand will be hidden when collapsed, and shown when expanded
NavbarCollapsedBrandHidden
)
const NavbarSelect = "gr-bs-navbarselect"
type NavbarI interface {
page.ControlI
SetNavbarStyle(style NavbarStyle) NavbarI
SetBrand(label string, anchor string, p NavbarCollapsedBrandPlacement) NavbarI
SetBackgroundClass(c BackgroundColorClass) NavbarI
SetExpand(e NavbarExpandClass) NavbarI
SetContainerClass(class ContainerClass) NavbarI
OnClick(action action.ActionI) NavbarI
}
// Navbar is a bootstrap navbar object. Use SetText() to set the logo text of the navbar, and
// SetTextIsHtml() to true to turn off encoding if needed. Add child controls to populate it.
// When adding NavLink objects, they should be grouped together using the NavGroup object.
type Navbar struct {
page.ControlBase
brandAnchor string
brandLocation NavbarCollapsedBrandPlacement
style NavbarStyle
background BackgroundColorClass
expand NavbarExpandClass
containerClass ContainerClass
}
type NavbarStyle string
const (
NavbarDark NavbarStyle = "navbar-dark" // black on white
NavbarLight = "navbar-light"
)
// NewNavbar returns a newly created Bootstrap Navbar object
func NewNavbar(parent page.ControlI, id string) *Navbar {
b := &Navbar{}
b.Init(b, parent, id)
return b
}
func (b *Navbar) Init(self any, parent page.ControlI, id string) {
b.ControlBase.Init(self, parent, id)
b.Tag = "nav"
b.style = NavbarDark // default
b.background = BackgroundColorDark
b.expand = NavbarExpandLarge
b.containerClass = Container
config.LoadBootstrap(b.ParentForm())
b.On(event.Click().Selector(`a[href="#"][data-grctl="bs-navlink"]`).Capture(),
action.Trigger(b.ID(), NavbarSelect, javascript.JsCode(`g$(event.target).id`)))
}
func (b *Navbar) this() NavbarI {
return b.Self().(NavbarI)
}
func (b *Navbar) SetNavbarStyle(style NavbarStyle) NavbarI {
b.style = style
return b.this()
}
func (b *Navbar) SetBackgroundClass(c BackgroundColorClass) NavbarI {
b.background = c
return b.this()
}
func (b *Navbar) SetBrand(label string, anchor string, p NavbarCollapsedBrandPlacement) NavbarI {
b.SetText(label)
b.brandAnchor = anchor
b.brandLocation = p
return b.this()
}
func (b *Navbar) SetExpand(e NavbarExpandClass) NavbarI {
b.expand = e
return b.this()
}
func (b *Navbar) SetContainerClass(class ContainerClass) NavbarI {
b.containerClass = class
return b.this()
}
// OnClick sets the action to take when a link in the Navbar is selected.
// It will only respond to links whose href is "#", which indicates its an empty link.
// The ActionValue will be the id of the link clicked.
func (b *Navbar) OnClick(a action.ActionI) NavbarI {
b.On(NavbarSelectEvent(), a)
return b.this()
}
func (b *Navbar) DrawingAttributes(ctx context.Context) html5tag.Attributes {
a := b.ControlBase.DrawingAttributes(ctx)
a.AddClass("navbar")
a.AddClass(string(b.style))
a.AddClass(string(b.expand))
a.AddClass(string(b.background))
a.SetData("grctl", "bs-navbar")
return a
}
func (b *Navbar) Serialize(e page.Encoder) {
b.ControlBase.Serialize(e)
if err := e.Encode(b.brandAnchor); err != nil {
panic(err)
}
if err := e.Encode(b.brandLocation); err != nil {
panic(err)
}
if err := e.Encode(b.style); err != nil {
panic(err)
}
if err := e.Encode(b.background); err != nil {
panic(err)
}
if err := e.Encode(b.expand); err != nil {
panic(err)
}
return
}
func (b *Navbar) Deserialize(d page.Decoder) {
b.ControlBase.Deserialize(d)
if err := d.Decode(&b.brandAnchor); err != nil {
panic(err)
}
if err := d.Decode(&b.brandLocation); err != nil {
panic(err)
}
if err := d.Decode(&b.style); err != nil {
panic(err)
}
if err := d.Decode(&b.background); err != nil {
panic(err)
}
if err := d.Decode(&b.expand); err != nil {
panic(err)
}
}
type NavbarCreator struct {
ID string
Brand string
// BrandAnchor is the url to go to when the main logo in the navbar is clicked
BrandAnchor string
// Style is either NavbarDark or NavbarLight
Style NavbarStyle
// BackgroundColorClass is one of the background colors that you can assign the navbar
BackgroundColorClass BackgroundColorClass
// Expand determines at what screen width the navbar will be expanded.
Expand NavbarExpandClass
// BrandLocation controls the placement of the brand item
BrandLocation NavbarCollapsedBrandPlacement
// ContainerClass is the class of the container that will wrap the contents of the navbar
ContainerClass ContainerClass
// OnClick is the action to take when a link is clicked. It will only respond
// to nav-link items that have an href of "#". The EventValue will be the id of the item clicked.
OnClick action.ActionI
page.ControlOptions
Children []page.Creator
}
// Create is called by the framework to create a new control from the Creator. You
// do not normally need to call this.
func (c NavbarCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewNavbar(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
func (c NavbarCreator) Init(ctx context.Context, ctrl NavbarI) {
if c.Brand != "" {
ctrl.SetBrand(c.Brand, c.BrandAnchor, c.BrandLocation)
}
if c.Style != "" {
ctrl.SetNavbarStyle(c.Style)
}
if c.BackgroundColorClass != "" {
ctrl.SetBackgroundClass(c.BackgroundColorClass)
}
if c.Expand != "" {
ctrl.SetExpand(c.Expand)
}
if c.OnClick != nil {
ctrl.OnClick(c.OnClick)
}
if c.ContainerClass != "" {
ctrl.SetContainerClass(c.ContainerClass)
}
ctrl.AddControls(ctx, c.Children...)
}
// GetNavbar is a convenience method to return the control with the given id from the page.
func GetNavbar(c page.ControlI, id string) *Navbar {
return c.Page().GetControl(id).(*Navbar)
}
func init() {
page.RegisterControl(&Navbar{})
}
func NavbarSelectEvent() *event.Event {
return event.NewEvent(NavbarSelect)
}