/
tabnav.go
132 lines (118 loc) · 3.06 KB
/
tabnav.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
package component
import (
"fmt"
"github.com/shurcooL/home/internal/route"
"github.com/shurcooL/htmlg"
"github.com/shurcooL/octicon"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// TODO: Dedup.
// TabNav is a left-aligned horizontal row of tabs Primer CSS component.
//
// http://primercss.io/nav/#tabnav
type TabNav struct {
Tabs []Tab
}
func (t TabNav) Render() []*html.Node {
nav := &html.Node{
Type: html.ElementNode, Data: atom.Nav.String(),
Attr: []html.Attribute{{Key: atom.Class.String(), Val: "tabnav-tabs"}},
}
for _, t := range t.Tabs {
htmlg.AppendChildren(nav, t.Render()...)
}
return []*html.Node{htmlg.DivClass("tabnav", nav)}
}
// Tab is a single tab entry within a TabNav.
type Tab struct {
Content htmlg.Component
URL string
OnClick string
Selected bool
}
func (t Tab) Render() []*html.Node {
aClass := "tabnav-tab"
if t.Selected {
aClass += " selected"
}
a := &html.Node{
Type: html.ElementNode, Data: atom.A.String(),
Attr: []html.Attribute{
{Key: atom.Href.String(), Val: t.URL},
{Key: atom.Class.String(), Val: aClass},
},
}
if t.OnClick != "" {
a.Attr = append(a.Attr, html.Attribute{Key: atom.Onclick.String(), Val: t.OnClick})
}
htmlg.AppendChildren(a, t.Content.Render()...)
return []*html.Node{a}
}
type RepositoryTab uint8
const (
NoTab RepositoryTab = iota
PackagesTab
HistoryTab
IssuesTab
ChangesTab
)
func RepositoryTabNav(selected RepositoryTab, repoPath string, packages int, openIssues, openChanges uint64) htmlg.Component {
return TabNav{
Tabs: []Tab{
{
Content: contentCounter{
Content: iconText{Icon: octicon.Package, Text: "Packages"},
Count: packages,
},
URL: route.RepoIndex(repoPath),
Selected: selected == PackagesTab,
},
{
Content: iconText{Icon: octicon.History, Text: "History"},
URL: route.RepoHistory(repoPath),
Selected: selected == HistoryTab,
},
{
Content: contentCounter{
Content: iconText{Icon: octicon.IssueOpened, Text: "Issues"},
Count: int(openIssues),
},
URL: route.RepoIssues(repoPath), OnClick: "Open(event, this)",
Selected: selected == IssuesTab,
},
{
Content: contentCounter{
Content: iconText{Icon: octicon.GitPullRequest, Text: "Changes"},
Count: int(openChanges),
},
URL: route.RepoChanges(repoPath), OnClick: "Open(event, this)",
Selected: selected == ChangesTab,
},
},
}
}
type contentCounter struct {
Content htmlg.Component
Count int
}
func (cc contentCounter) Render() []*html.Node {
var ns []*html.Node
ns = append(ns, cc.Content.Render()...)
ns = append(ns, htmlg.SpanClass("counter", htmlg.Text(fmt.Sprint(cc.Count))))
return ns
}
// iconText is an icon with text on the right.
// Icon must be not nil.
type iconText struct {
Icon func() *html.Node // Must be not nil.
Text string
}
func (it iconText) Render() []*html.Node {
icon := htmlg.Span(it.Icon())
icon.Attr = append(icon.Attr, html.Attribute{
Key: atom.Style.String(), Val: "margin-right: 4px;",
})
text := htmlg.Text(it.Text)
return []*html.Node{icon, text}
}