-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.go
367 lines (329 loc) · 6.32 KB
/
layout.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package layout
import (
"context"
"honnef.co/go/js/dom"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/gopherjs/vecty/event"
"github.com/gopherjs/vecty/prop"
)
const (
SmallScreenOnly = "mdl-layout--small-screen-only"
LargeScreenOnly = "mdl-layout--large-screen-only"
)
var layoutTabUpdates chan string
var doc dom.Document
func init() {
layoutTabUpdates = make(chan string)
doc = dom.GetWindow().Document()
}
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)
}
}
type Layout struct {
vecty.Core
IsJS bool
Header *Header
Children vecty.MarkupOrComponentOrHTML
Content vecty.MarkupOrComponentOrHTML
FixedDrawer bool
FixedHeader bool
NoDrawerBtn bool
NoDestopDrawerBtn bool
LargeScreenOnly bool
TabView bool
Panes []*Panel
cancel func()
activePane string
Drawer *Drawer
}
func New(tabview bool) *Layout {
l := &Layout{TabView: tabview}
if tabview {
ctx, cancel := context.WithCancel(context.Background())
l.cancel = cancel
return l.watch(ctx)
}
return l
}
func (l *Layout) watch(ctx context.Context) *Layout {
go func() {
done:
for {
select {
case id := <-layoutTabUpdates:
if id != l.activePane {
switchState(l.activePane, false)
switchState(id, true)
l.activePane = id
}
case <-ctx.Done():
break done
}
}
}()
return l
}
func (l *Layout) Unmount() {
if l.cancel != nil {
l.cancel()
}
}
func (l *Layout) Render() *vecty.HTML {
c := make(vecty.ClassMap)
c["mdl-layout"] = true
if l.IsJS {
c["mdl-js-layout"] = true
}
if l.FixedDrawer {
c["mdl-layout--fixed-drawer"] = true
}
if l.FixedHeader {
c["mdl-layout--fixed-header"] = true
}
if l.NoDrawerBtn {
c["mdl-layout--no-drawer-button"] = true
}
if l.NoDestopDrawerBtn {
c["mdl-layout--no-desktop-drawer-button"] = true
}
var p vecty.List
if l.LargeScreenOnly {
c["mdl-layout--large-screen-only"] = true
}
if l.Header != nil {
p = append(p, l.Header)
}
if l.Drawer != nil {
p = append(p, l.Drawer)
}
if l.TabView {
b := &TabBar{}
var pt vecty.List
for i := 0; i < len(l.Panes); i++ {
b.Links = append(b.Links, &TabLink{
ID: l.Panes[i].ID,
Text: l.Panes[i].Name,
IsActive: l.Panes[i].IsActive,
})
pt = append(pt, l.Panes[i])
}
if l.Header == nil {
l.Header = &Header{}
p = append(p, l.Header)
}
l.Header.TabView = true
l.Header.TabNav = b
p = append(p, elem.Main(
pt,
))
} else {
if l.Content != nil {
p = append(p, elem.Main(prop.Class("mdl-layout__content"), l.Content))
}
}
return elem.Div(
l.Children,
c,
p,
)
}
type Header struct {
vecty.Core
Icon *Icon
Row *HeaderRow
Children vecty.MarkupOrComponentOrHTML
Scroll bool
Waterfall bool
WaterfallHideTop bool
Transparent bool
Seamed bool
TabView bool
TabNav *TabBar
ManualTabSwitch bool
}
func (h *Header) Render() *vecty.HTML {
var l vecty.List
if h.Icon != nil {
l = append(l, h.Icon)
}
c := make(vecty.ClassMap)
c["mdl-layout__header"] = true
if h.Scroll {
c["mdl-layout__header--scroll"] = true
}
if h.Waterfall {
c["mdl-layout__header--waterfall"] = true
}
if h.WaterfallHideTop {
c["mdl-layout__header--waterfall-hide-top"] = true
}
if h.Transparent {
c["mdl-layout__header--transparent"] = true
}
if h.Seamed {
c["mdl-layout__header--seamed"] = true
}
if h.TabView {
return elem.Header(
h.Children,
c,
h.Row,
h.TabNav,
)
}
return elem.Header(
h.Children,
c,
h.Row,
)
}
type Icon struct {
vecty.Core
Children vecty.MarkupOrComponentOrHTML
}
func (i *Icon) Render() *vecty.HTML {
return elem.Div(i.Children, prop.Class("mdl-layout-icon"))
}
type HeaderRow struct {
vecty.Core
Children vecty.MarkupOrComponentOrHTML
Title *Title
AddSpacer bool
Nav *Nav
}
func (h *HeaderRow) Render() *vecty.HTML {
var l vecty.List
if h.Title != nil {
l = append(l, h.Title)
}
if h.AddSpacer {
l = append(l, elem.Div(prop.Class("mdl-layout-spacer")))
}
if h.Nav != nil {
l = append(l, h.Nav)
}
return elem.Div(
h.Children,
prop.Class("mdl-layout__header-row"),
l,
)
}
type Nav struct {
vecty.Core
Links []*NavLink
Children vecty.MarkupOrComponentOrHTML
}
func (n *Nav) Render() *vecty.HTML {
var l vecty.List
for i := 0; i < len(n.Links); i++ {
l = append(l, n.Links[i])
}
return elem.Navigation(
n.Children,
prop.Class("mdl-navigation"),
l,
)
}
type NavLink struct {
vecty.Core
Href string
Text string
Children vecty.MarkupOrComponentOrHTML
}
func (n *NavLink) Render() *vecty.HTML {
return elem.Anchor(
n.Children,
prop.Class("mdl-navigation__link"),
vecty.Text(n.Text),
)
}
type Title struct {
vecty.Core
Text string
Children vecty.MarkupOrComponentOrHTML
}
func (t *Title) Render() *vecty.HTML {
return elem.Span(
t.Children,
prop.Class("mdl-layout-title"),
vecty.Text(t.Text),
)
}
type TabBar struct {
vecty.Core
Links []*TabLink
Manual bool
}
type TabLink struct {
vecty.Core
ID string
IsActive bool
Text string
Children vecty.MarkupOrComponentOrHTML
}
func (t *TabLink) Render() *vecty.HTML {
c := make(vecty.ClassMap)
c["mdl-layout__tab"] = true
if t.IsActive {
c["is-active"] = true
}
return elem.Anchor(
t.Children,
prop.ID("#"+t.ID),
prop.ID(t.ID+"-bar"), c,
vecty.Text(t.Text),
event.Click(func(e *vecty.Event) {
go func() {
layoutTabUpdates <- t.ID
}()
}),
)
}
type TabNav struct {
vecty.Core
TabBar *TabBar
Tabs []*Panel
Children vecty.MarkupOrComponentOrHTML
}
type Panel struct {
vecty.Core
ID string
Name string
IsActive bool
Children vecty.MarkupOrComponentOrHTML
}
func (t *Panel) Render() *vecty.HTML {
c := make(vecty.ClassMap)
c["mdl-layout__tab-panel"] = true
if t.IsActive {
c["is-active"] = true
}
return elem.Section(
c,
prop.ID(t.ID),
t.Children,
)
}
type Drawer struct {
vecty.Core
Title *Title
Nav *Nav
}
func (d *Drawer) Render() *vecty.HTML {
c := make(vecty.ClassMap)
c["mdl-layout__drawer"] = true
return elem.Div(
c, d.Title, d.Nav,
)
}