-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab.go
178 lines (159 loc) · 2.88 KB
/
tab.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
package tabs
import (
"context"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/gopherjs/vecty/event"
"github.com/gopherjs/vecty/prop"
"honnef.co/go/js/dom"
)
var tabUpdates chan string
var doc dom.Document
func init() {
tabUpdates = make(chan string, 100)
doc = dom.GetWindow().Document()
}
type Tabs struct {
vecty.Core
IsJS bool
Rippled bool
Children vecty.MarkupOrComponentOrHTML
Panels []*Panel
Bar *Bar
active string
cancel func()
}
func New() *Tabs {
ctx, cancel := context.WithCancel(context.Background())
t := &Tabs{cancel: cancel}
return t.watch(ctx)
}
func switchState(id string, active bool) {
toActive(id+"-bar", active)
toActive(id, active)
}
func toActive(id string, state bool) {
e := doc.GetElementByID(id)
a := "is-active"
if state {
e.Class().Add(a)
} else {
e.Class().Remove(a)
}
}
func (t *Tabs) Unmount() {
t.cancel()
}
func (t *Tabs) watch(ctx context.Context) *Tabs {
go func() {
done:
for {
select {
case id := <-tabUpdates:
if id != t.active {
switchState(t.active, false)
switchState(id, true)
t.active = id
}
case <-ctx.Done():
break done
}
}
}()
return t
}
func (t *Tabs) Render() *vecty.HTML {
c := make(vecty.ClassMap)
c["mdl-tabs"] = true
if t.IsJS {
c["mdl-js-tabs"] = true
if t.Rippled {
c[" mdl-js-ripple-effect"] = true
}
}
if t.Bar == nil || len(t.Bar.Links) == 0 {
t.Bar = &Bar{}
for i := 0; i < len(t.Panels); i++ {
l := &Link{
ID: t.Panels[i].ID,
Name: t.Panels[i].Name,
}
if t.Panels[i].IsActive {
l.IsActive = true
t.active = t.Panels[i].ID
}
t.Bar.Links = append(t.Bar.Links, l)
}
}
var p vecty.List
for i := 0; i < len(t.Panels); i++ {
p = append(p, t.Panels[i])
}
return elem.Div(
c,
vecty.List{t.Bar, p},
)
}
type Link struct {
vecty.Core
ID string
Name string
IsActive bool
}
func (l *Link) Render() *vecty.HTML {
c := make(vecty.ClassMap)
c["mdl-tabs__tab"] = true
if l.IsActive {
c["is-active"] = true
}
return elem.Anchor(
prop.Href("#"+l.ID),
prop.ID(l.ID+"-bar"),
c,
vecty.Text(l.Name),
event.Click(func(e *vecty.Event) {
go func() {
tabUpdates <- l.ID
}()
}),
)
}
type Bar struct {
vecty.Core
Links []*Link
Children vecty.MarkupOrComponentOrHTML
}
func (b *Bar) Render() *vecty.HTML {
var l vecty.List
for i := 0; i < len(b.Links); i++ {
l = append(l, b.Links[i])
}
return elem.Div(
prop.Class("mdl-tabs__tab-bar"), l,
b.Children,
)
}
type Panel struct {
vecty.Core
IsActive bool
Name string
ID string
Children vecty.MarkupOrComponentOrHTML
}
func (p *Panel) Render() *vecty.HTML {
c := make(vecty.ClassMap)
c["mdl-tabs__panel "] = true
if p.IsActive {
c["is-active"] = true
}
return elem.Div(
c,
p.Children,
prop.ID(p.ID),
)
}
func (p *Panel) Activate() {
if !p.IsActive {
p.IsActive = true
}
}