-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
reference-content.go
119 lines (101 loc) · 2.37 KB
/
reference-content.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
package main
import (
"github.com/maxence-charriere/go-app/v10/pkg/app"
"github.com/maxence-charriere/go-app/v10/pkg/ui"
)
type referenceContent struct {
app.Compo
Iid string
Iclass string
Iindex bool
content htmlContent
currentFragment string
}
func newReferenceContent() *referenceContent {
return &referenceContent{}
}
func (c *referenceContent) ID(v string) *referenceContent {
c.Iid = v
return c
}
func (c *referenceContent) Class(v string) *referenceContent {
c.Iclass = app.AppendClass(c.Iclass, v)
return c
}
func (c *referenceContent) Index(v bool) *referenceContent {
c.Iindex = v
return c
}
func (c *referenceContent) OnMount(ctx app.Context) {
c.load(ctx)
}
func (c *referenceContent) OnNav(ctx app.Context) {
c.handleFragment(ctx)
}
func (c *referenceContent) load(ctx app.Context) {
ctx.ObserveState(referenceState, &c.content).
OnChange(func() {
ctx.Defer(c.handleFragment)
ctx.Defer(c.scrollTo)
})
ctx.NewAction(getReference)
}
func (c *referenceContent) Render() app.UI {
loaderSize := 60
loaderSpacing := 18
if c.Iindex {
loaderSize = 30
loaderSpacing = 9
}
return app.Section().
ID(c.Iid).
Class(c.Iclass).
Body(
ui.Loader().
Class("separator").
Class("fill").
Class("heading").
Loading(c.content.Status == loading).
Err(c.content.Err).
Size(loaderSize).
Spacing(loaderSpacing),
app.If(!c.Iindex && c.content.Content != "", func() app.UI {
return app.Raw(c.content.Content)
}).ElseIf(c.Iindex && c.content.Index != "", func() app.UI {
return app.Raw(c.content.Index)
}),
app.Div().Text(c.content.Err),
)
}
func (c *referenceContent) handleFragment(ctx app.Context) {
if !c.Iindex {
return
}
if c.currentFragment != "" {
c.unfocusCurrentIndex(ctx)
}
c.focusCurrentIndex(ctx)
}
func (c *referenceContent) unfocusCurrentIndex(ctx app.Context) {
link := app.Window().GetElementByID(refLinkID(c.currentFragment))
if !link.Truthy() {
return
}
link.Set("className", "")
}
func (c *referenceContent) focusCurrentIndex(ctx app.Context) {
fragment := ctx.Page().URL().Fragment
link := app.Window().GetElementByID(refLinkID(fragment))
if !link.Truthy() {
return
}
link.Set("className", "focus")
c.currentFragment = fragment
}
func (c *referenceContent) scrollTo(ctx app.Context) {
id := ctx.Page().URL().Fragment
if c.Iindex {
id = refLinkID(id)
}
ctx.ScrollTo(id)
}