-
Notifications
You must be signed in to change notification settings - Fork 6
/
navbar.go
221 lines (189 loc) · 5.73 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
package control
import (
"context"
"github.com/goradd/goradd/pkg/bootstrap/config"
"github.com/goradd/html5tag"
"github.com/goradd/goradd/pkg/page"
)
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
)
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
}
// 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.
type Navbar struct {
page.ControlBase
brandAnchor string
brandLocation NavbarCollapsedBrandPlacement
style NavbarStyle
//container ContainerClass ??
background BackgroundColorClass
expand NavbarExpandClass
}
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.Self = b
b.Init(parent, id)
return b
}
func (b *Navbar) Init(parent page.ControlI, id string) {
b.ControlBase.Init(parent, id)
b.Tag = "nav"
b.style = NavbarDark // default
b.background = BackgroundColorDark
b.expand = NavbarExpandLarge
config.LoadBootstrap(b.ParentForm())
}
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) 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
// HeaderAnchor 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
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)
}
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{})
}