/
browser.go
133 lines (115 loc) · 3.15 KB
/
browser.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
package app
import "time"
type browser struct {
AppUpdatable bool
anchorClick Func
popState Func
navigationFromJS Func
appUpdate Func
appInstallChange Func
appResize Func
resizeTimer *time.Timer
}
func (b *browser) HandleEvents(ctx Context, notifyComponentEvent func(any)) {
b.handleAnchorClick(ctx)
b.handlePopState(ctx)
b.handleNavigationFromJS(ctx)
b.handleAppUpdate(ctx, notifyComponentEvent)
b.handleAppInstallChange(ctx, notifyComponentEvent)
b.handleAppResize(ctx, notifyComponentEvent)
}
func (b *browser) handleAnchorClick(ctx Context) {
b.anchorClick = FuncOf(func(this Value, args []Value) any {
ctx.dispatch(func() {
event := Event{Value: args[0]}
for target := event.Get("target"); target.Truthy(); target = target.Get("parentElement") {
switch target.Get("tagName").String() {
case "A":
if meta := event.Get("metaKey"); meta.Truthy() && meta.Bool() {
return
}
if ctrl := event.Get("ctrlKey"); ctrl.Truthy() && ctrl.Bool() {
return
}
if download := target.Call("getAttribute", "download"); !download.IsNull() {
return
}
event.PreventDefault()
if href := target.Get("href"); href.Truthy() {
ctx.Navigate(target.Get("href").String())
}
return
case "BODY":
return
}
}
})
return nil
})
Window().Set("onclick", b.anchorClick)
}
func (b *browser) handlePopState(ctx Context) {
b.popState = FuncOf(func(this Value, args []Value) any {
ctx.dispatch(func() {
ctx.navigate(Window().URL(), false)
})
return nil
})
Window().Set("onpopstate", b.popState)
}
func (b *browser) handleNavigationFromJS(ctx Context) {
b.navigationFromJS = FuncOf(func(this Value, args []Value) any {
ctx.dispatch(func() {
ctx.Navigate(args[0].String())
})
return nil
})
Window().Set("goappNav", b.navigationFromJS)
}
func (b *browser) handleAppUpdate(ctx Context, notifyComponentEvent func(any)) {
appUpdate := func() {
ctx.dispatch(func() {
b.AppUpdatable = true
notifyComponentEvent(appUpdate{})
})
ctx.defere(func() {
Log(Window().URL().Hostname() + " has been updated, reload to see changes")
})
}
b.appUpdate = FuncOf(func(this Value, args []Value) any {
appUpdate()
return nil
})
if Window().Get("goappOnUpdate").Truthy() {
defer appUpdate()
}
Window().Set("goappOnUpdate", b.appUpdate)
}
func (b *browser) handleAppInstallChange(ctx Context, notifyComponentEvent func(any)) {
b.appInstallChange = FuncOf(func(this Value, args []Value) any {
ctx.dispatch(func() {
notifyComponentEvent(appInstallChange{})
})
return nil
})
Window().Set("goappOnAppInstallChange", b.appInstallChange)
}
func (b *browser) handleAppResize(ctx Context, notifyComponentEvent func(any)) {
const resizeCooldown = time.Millisecond * 250
b.appResize = FuncOf(func(this Value, args []Value) any {
ctx.dispatch(func() {
if b.resizeTimer != nil {
b.resizeTimer.Stop()
b.resizeTimer.Reset(resizeCooldown)
return
}
b.resizeTimer = time.AfterFunc(resizeCooldown, func() {
ctx.dispatch(func() {
notifyComponentEvent(resize{})
})
})
})
return nil
})
Window().Set("onresize", b.appResize)
}