-
Notifications
You must be signed in to change notification settings - Fork 6
/
nav_link.go
91 lines (79 loc) · 2.65 KB
/
nav_link.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
package control
import (
"context"
"github.com/goradd/goradd/pkg/page"
grctl "github.com/goradd/goradd/pkg/page/control"
"github.com/goradd/html5tag"
)
type NavLinkI interface {
grctl.ActiveLinkI
}
// NavLink creates an anchor tag with a nav-link class. It is used to create a link in a NavBar, and
// generally should be a child item of a NavGroup.
type NavLink struct {
grctl.ActiveLink
}
// NewNavLink creates a new NavLink.
func NewNavLink(parent page.ControlI, id string) *NavLink {
l := new(NavLink)
l.Self = l
l.Init(parent, id)
return l
}
// Init initializes the button
func (l *NavLink) Init(parent page.ControlI, id string) {
l.ActiveLink.Init(parent, id)
l.ActiveAttributes().
AddClass("active").
Set("aria-current", "page")
}
func (l *NavLink) this() NavLinkI {
return l.Self.(NavLinkI)
}
// DrawingAttributes returns the attributes to add to the tag just before the button is drawn.
func (l *NavLink) DrawingAttributes(ctx context.Context) html5tag.Attributes {
a := l.ActiveLink.DrawingAttributes(ctx)
a.AddClass("nav-link")
a.SetData("grctl", "bs-navlink")
return a
}
// NavLinkCreator is the initialization structure for the declarative creation of the control.
type NavLinkCreator struct {
// ID is the control id. This is also the eventValue sent by the enclosing Navbar.
ID string
// Text is the text displayed in the link
Text string
// Location is the content of the href, which is the url where the link will go.
// Alternatively, add an On handler to the ControlOptions
Location string
// ActiveAttributes are additional attributes to add to the active link. By default, the ActiveAttributes
// will add the "active" class and aria-current=page.
ActiveAttributes html5tag.Attributes
page.ControlOptions
}
// Create is called by the framework to create a new control from the Creator. You
// do not normally need to call this.
func (c NavLinkCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewNavLink(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
// Init is called by implementations of NavLinks to initialize a control with the
// creator. You do not normally need to call this.
func (c NavLinkCreator) Init(ctx context.Context, ctrl NavLinkI) {
sub := grctl.ActiveLinkCreator{
ID: c.ID,
Text: c.Text,
Location: c.Location,
ActiveAttributes: c.ActiveAttributes,
ControlOptions: c.ControlOptions,
}
sub.Init(ctx, ctrl)
}
// GetNavLink is a convenience method to return the button with the given id from the page.
func GetNavLink(c page.ControlI, id string) *NavLink {
return c.Page().GetControl(id).(*NavLink)
}
func init() {
page.RegisterControl(&NavLink{})
}