-
-
Notifications
You must be signed in to change notification settings - Fork 355
/
app_wasm.go
276 lines (230 loc) · 4.92 KB
/
app_wasm.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
package app
import (
"context"
"net/url"
"reflect"
"syscall/js"
"github.com/maxence-charriere/go-app/pkg/log"
)
var (
page *dom
cursorX int
cursorY int
)
func init() {
log.DefaultColor = ""
log.InfoColor = ""
log.ErrorColor = ""
log.WarnColor = ""
log.DebugColor = ""
log.CurrentLevel = log.DebugLevel
}
func run() {
go func() {
page = &dom{
compoBuilder: components,
callOnUI: UI,
msgs: msgs,
trackCursorPosition: trackCursorPosition,
contextMenu: &contextMenu{},
}
defer page.clean()
LocalStorage = newJSStorage("localStorage")
SessionStorage = newJSStorage("sessionStorage")
overrideAnchorClick := js.FuncOf(overrideAnchorClick)
defer overrideAnchorClick.Release()
js.Global().Set("onclick", overrideAnchorClick)
onpopstate := js.FuncOf(onPopState)
defer onpopstate.Release()
js.Global().Set("onpopstate", onpopstate)
url := locationURL()
if err := renderPage(url); err != nil {
log.Error("rendering page failed").
T("reason", err).
T("url", url).
Panic()
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for {
select {
case <-ctx.Done():
return
case f := <-ui:
f()
}
}
}()
select {}
log.Info("wasm exit")
return
}
func render(c Compo) {
if err := page.render(c); err != nil {
log.Error("rendering component failed").
T("reason", err).
T("component", reflect.TypeOf(c))
}
}
func reload() {
js.Global().Get("location").Call("reload")
}
func bind(msg string, c Compo) *Binding {
b, close := msgs.bind(msg, c)
if err := page.setBindingClose(c, close); err != nil {
log.Error("creating a binding failed").
T("reason", err).
T("component", reflect.TypeOf(c)).
T("message", msg).
Panic()
}
return b
}
// NewContextMenu displays a context menu filled with the given menu items.
func NewContextMenu(items ...MenuItem) {
Emit("__app.NewContextMenu", items)
}
// MenuItem represents a menu item.
type MenuItem struct {
Disabled bool
Keys string
Icon string
Label string
OnClick func(s, e js.Value)
Separator bool
}
func locationURL() *url.URL {
rawurl := js.Global().
Get("location").
Get("href").
String()
url, err := url.Parse(rawurl)
if err != nil {
log.Error("getting current url failed").
T("reason", err).
Panic()
}
return url
}
func renderPage(url *url.URL) error {
if url.Path == "" || url.Path == "/" {
url.Path = DefaultPath
}
if !components.isImported(compoNameFromURL(url)) {
url.Path = NotFoundPath
}
compoName := compoNameFromURL(url)
compo, err := components.new(compoName)
if err != nil {
return err
}
mapCompoFieldFromURLQuery(compo, url.Query())
return page.newBody(compo)
}
func trackCursorPosition(e js.Value) {
x := e.Get("clientX")
if !x.Truthy() {
return
}
cursorX = x.Int()
y := e.Get("clientY")
if !y.Truthy() {
return
}
cursorY = y.Int()
}
func navigate(rawurl string, updateHistory bool) {
currentURL := locationURL()
u, err := url.Parse(rawurl)
if err != nil {
log.Error("navigating failed").
T("reason", err).
T("url", rawurl)
return
}
if u.Host == "" && u.Scheme == "" {
u.Scheme = currentURL.Scheme
u.Host = currentURL.Host
}
fragmentNav := u.Host == currentURL.Host &&
u.Path == currentURL.Path &&
u.Fragment != currentURL.Fragment &&
u.Fragment != ""
otherHostNav := u.Host != currentURL.Host
if otherHostNav || fragmentNav {
js.Global().Get("location").Set("href", u.String())
return
}
page.clean()
if err := renderPage(u); err != nil {
log.Error("rendering page failed").
T("reason", err).
T("url", u).
Panic()
return
}
if updateHistory {
js.Global().Get("history").Call("pushState", nil, "", rawurl)
}
}
func overrideAnchorClick(this js.Value, args []js.Value) interface{} {
event := args[0]
elem := event.Get("target")
if !elem.Truthy() {
elem = event.Get("srcElement")
}
if elem.Get("tagName").String() != "A" {
return nil
}
event.Call("preventDefault")
Navigate(elem.Get("href").String())
return nil
}
func onPopState(this js.Value, args []js.Value) interface{} {
UI(func() {
rawurl := js.Global().Get("location").Get("href").String()
navigate(rawurl, false)
})
return nil
}
func windowSize() (w, h int) {
return windowWidth(), windowHeight()
}
func windowWidth() int {
w := js.Global().Get("innerWidth")
if !w.Truthy() {
w = js.Global().
Get("document").
Get("documentElement").
Get("clientWidth")
}
if !w.Truthy() {
w = js.Global().
Get("document").
Get("body").
Get("clientWidth")
}
if w.Type() != js.TypeNumber {
return 0
}
return w.Int()
}
func windowHeight() int {
h := js.Global().Get("innerHeight")
if !h.Truthy() {
h = js.Global().
Get("document").
Get("documentElement").
Get("clientHeight")
}
if !h.Truthy() {
h = js.Global().
Get("document").
Get("body").
Get("clientHeight")
}
if h.Type() != js.TypeNumber {
return 0
}
return h.Int()
}