-
Notifications
You must be signed in to change notification settings - Fork 6
/
navbar.go
175 lines (148 loc) · 4.78 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
package control
import (
"context"
"github.com/goradd/goradd/pkg/bootstrap/config"
"github.com/goradd/goradd/pkg/html"
"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
SetHeaderAnchor(a string) NavbarI
SetNavbarStyle(style NavbarStyle) NavbarI
SetBrandPlacement(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.Control
headerAnchor string
style NavbarStyle
//container ContainerClass ??
background BackgroundColorClass
expand NavbarExpandClass
brandLocation NavbarCollapsedBrandPlacement
}
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 page.ControlI, parent page.ControlI, id string) {
b.Control.Init(self, 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) SetHeaderAnchor(a string) NavbarI {
b.headerAnchor = a
return b.this()
}
func (b *Navbar) SetExpand(e NavbarExpandClass) NavbarI {
b.expand = e
return b.this()
}
// SetBrandPlacement places the brand left, right, or hidden (meaning inside the collapse area).
// The expand button location will be affected by the placement
func (b *Navbar) SetBrandPlacement(p NavbarCollapsedBrandPlacement) NavbarI {
b.brandLocation = p
return b.this()
}
func (b *Navbar) ΩDrawingAttributes() html.Attributes {
a := b.Control.ΩDrawingAttributes()
a.AddClass("navbar")
a.AddClass(string(b.style))
a.AddClass(string(b.expand))
a.AddClass(string(b.background))
a.SetDataAttribute("grctl", "bs-navbar")
return a
}
type NavbarCreator struct {
ID string
HeaderAnchor string
Style NavbarStyle
BackgroundColorClass BackgroundColorClass
Expand NavbarExpandClass
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.HeaderAnchor != "" {
ctrl.SetHeaderAnchor(c.HeaderAnchor)
}
if c.Style != "" {
ctrl.SetNavbarStyle(c.Style)
}
if c.BackgroundColorClass != "" {
ctrl.SetBackgroundClass(c.BackgroundColorClass)
}
if c.Expand != "" {
ctrl.SetExpand (c.Expand)
}
ctrl.SetBrandPlacement(c.BrandLocation)
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)
}