forked from dominikh/go-js-dom
-
Notifications
You must be signed in to change notification settings - Fork 4
/
events_wasm.go
415 lines (368 loc) · 12 KB
/
events_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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// +build js,wasm
package dom
import (
"syscall/js"
"time"
)
func WrapEvent(o js.Value) Event {
return wrapEvent(o)
}
func wrapEvent(o js.Value) Event {
if o == js.Null() || o == js.Undefined() {
return nil
}
ev := &BasicEvent{o}
c := o.Get("constructor")
switch c {
case js.Global().Get("AnimationEvent"):
return &AnimationEvent{ev}
case js.Global().Get("AudioProcessingEvent"):
return &AudioProcessingEvent{ev}
case js.Global().Get("BeforeInputEvent"):
return &BeforeInputEvent{ev}
case js.Global().Get("BeforeUnloadEvent"):
return &BeforeUnloadEvent{ev}
case js.Global().Get("BlobEvent"):
return &BlobEvent{ev}
case js.Global().Get("ClipboardEvent"):
return &ClipboardEvent{ev}
case js.Global().Get("CloseEvent"):
return &CloseEvent{BasicEvent: ev}
case js.Global().Get("CompositionEvent"):
return &CompositionEvent{ev}
case js.Global().Get("CSSFontFaceLoadEvent"):
return &CSSFontFaceLoadEvent{ev}
case js.Global().Get("CustomEvent"):
return &CustomEvent{ev}
case js.Global().Get("DeviceLightEvent"):
return &DeviceLightEvent{ev}
case js.Global().Get("DeviceMotionEvent"):
return &DeviceMotionEvent{ev}
case js.Global().Get("DeviceOrientationEvent"):
return &DeviceOrientationEvent{ev}
case js.Global().Get("DeviceProximityEvent"):
return &DeviceProximityEvent{ev}
case js.Global().Get("DOMTransactionEvent"):
return &DOMTransactionEvent{ev}
case js.Global().Get("DragEvent"):
return &DragEvent{ev}
case js.Global().Get("EditingBeforeInputEvent"):
return &EditingBeforeInputEvent{ev}
case js.Global().Get("ErrorEvent"):
return &ErrorEvent{ev}
case js.Global().Get("FocusEvent"):
return &FocusEvent{ev}
case js.Global().Get("GamepadEvent"):
return &GamepadEvent{ev}
case js.Global().Get("HashChangeEvent"):
return &HashChangeEvent{ev}
case js.Global().Get("IDBVersionChangeEvent"):
return &IDBVersionChangeEvent{ev}
case js.Global().Get("KeyboardEvent"):
return &KeyboardEvent{BasicEvent: ev}
case js.Global().Get("MediaStreamEvent"):
return &MediaStreamEvent{ev}
case js.Global().Get("MessageEvent"):
return &MessageEvent{BasicEvent: ev}
case js.Global().Get("MouseEvent"):
return &MouseEvent{UIEvent: &UIEvent{ev}}
case js.Global().Get("MutationEvent"):
return &MutationEvent{ev}
case js.Global().Get("OfflineAudioCompletionEvent"):
return &OfflineAudioCompletionEvent{ev}
case js.Global().Get("PageTransitionEvent"):
return &PageTransitionEvent{ev}
case js.Global().Get("PointerEvent"):
return &PointerEvent{ev}
case js.Global().Get("PopStateEvent"):
return &PopStateEvent{ev}
case js.Global().Get("ProgressEvent"):
return &ProgressEvent{ev}
case js.Global().Get("RelatedEvent"):
return &RelatedEvent{ev}
case js.Global().Get("RTCPeerConnectionIceEvent"):
return &RTCPeerConnectionIceEvent{ev}
case js.Global().Get("SensorEvent"):
return &SensorEvent{ev}
case js.Global().Get("StorageEvent"):
return &StorageEvent{ev}
case js.Global().Get("SVGEvent"):
return &SVGEvent{ev}
case js.Global().Get("SVGZoomEvent"):
return &SVGZoomEvent{ev}
case js.Global().Get("TimeEvent"):
return &TimeEvent{ev}
case js.Global().Get("TouchEvent"):
return &TouchEvent{BasicEvent: ev}
case js.Global().Get("TrackEvent"):
return &TrackEvent{ev}
case js.Global().Get("TransitionEvent"):
return &TransitionEvent{ev}
case js.Global().Get("UIEvent"):
return &UIEvent{ev}
case js.Global().Get("UserProximityEvent"):
return &UserProximityEvent{ev}
case js.Global().Get("WheelEvent"):
return &WheelEvent{BasicEvent: ev}
default:
return ev
}
}
const (
EvPhaseNone = 0
EvPhaseCapturing = 1
EvPhaseAtTarget = 2
EvPhaseBubbling = 3
)
type Event interface {
Bubbles() bool
Cancelable() bool
CurrentTarget() Element
DefaultPrevented() bool
EventPhase() int
Target() Element
Timestamp() time.Time
Type() string
PreventDefault()
StopImmediatePropagation()
StopPropagation()
Underlying() js.Value
}
// Type BasicEvent implements the Event interface and is embedded by
// concrete event types.
type BasicEvent struct{ Object js.Value }
type EventOptions struct {
Bubbles bool
Cancelable bool
}
func CreateEvent(typ string, opts EventOptions) *BasicEvent {
var event = js.Global().Get("Event").New(
typ,
map[string]interface{}{
"bubbles": opts.Bubbles,
"cancelable": opts.Cancelable,
})
return &BasicEvent{event}
}
func (ev *BasicEvent) Bubbles() bool {
return ev.Object.Get("bubbles").Bool()
}
func (ev *BasicEvent) Cancelable() bool {
return ev.Object.Get("cancelable").Bool()
}
func (ev *BasicEvent) CurrentTarget() Element {
return wrapElement(ev.Object.Get("currentTarget"))
}
func (ev *BasicEvent) DefaultPrevented() bool {
return ev.Object.Get("defaultPrevented").Bool()
}
func (ev *BasicEvent) EventPhase() int {
return ev.Object.Get("eventPhase").Int()
}
func (ev *BasicEvent) Target() Element {
return wrapElement(ev.Object.Get("target"))
}
func (ev *BasicEvent) Timestamp() time.Time {
ms := ev.Object.Get("timeStamp").Int()
s := ms / 1000
ns := (ms % 1000 * 1e6)
return time.Unix(int64(s), int64(ns))
}
func (ev *BasicEvent) Type() string {
return ev.Object.Get("type").String()
}
func (ev *BasicEvent) PreventDefault() {
ev.Object.Call("preventDefault")
}
func (ev *BasicEvent) StopImmediatePropagation() {
ev.Object.Call("stopImmediatePropagation")
}
func (ev *BasicEvent) StopPropagation() {
ev.Object.Call("stopPropagation")
}
func (ev *BasicEvent) Underlying() js.Value {
return ev.Object
}
type AnimationEvent struct{ *BasicEvent }
type AudioProcessingEvent struct{ *BasicEvent }
type BeforeInputEvent struct{ *BasicEvent }
type BeforeUnloadEvent struct{ *BasicEvent }
type BlobEvent struct{ *BasicEvent }
type ClipboardEvent struct{ *BasicEvent }
type CloseEvent struct {
*BasicEvent
Code int `js:"code"`
Reason string `js:"reason"`
WasClean bool `js:"wasClean"`
}
type CompositionEvent struct{ *BasicEvent }
type CSSFontFaceLoadEvent struct{ *BasicEvent }
type CustomEvent struct{ *BasicEvent }
type DeviceLightEvent struct{ *BasicEvent }
type DeviceMotionEvent struct{ *BasicEvent }
type DeviceOrientationEvent struct{ *BasicEvent }
type DeviceProximityEvent struct{ *BasicEvent }
type DOMTransactionEvent struct{ *BasicEvent }
type DragEvent struct{ *BasicEvent }
type EditingBeforeInputEvent struct{ *BasicEvent }
type ErrorEvent struct{ *BasicEvent }
type FocusEvent struct{ *BasicEvent }
func (ev *FocusEvent) RelatedTarget() Element {
return wrapElement(ev.Object.Get("relatedTarget"))
}
type GamepadEvent struct{ *BasicEvent }
type HashChangeEvent struct{ *BasicEvent }
type IDBVersionChangeEvent struct{ *BasicEvent }
const (
KeyLocationStandard = 0
KeyLocationLeft = 1
KeyLocationRight = 2
KeyLocationNumpad = 3
)
type KeyboardEvent struct {
*BasicEvent
AltKey bool `js:"altKey"`
CharCode int `js:"charCode"`
CtrlKey bool `js:"ctrlKey"`
Key string `js:"key"`
KeyIdentifier string `js:"keyIdentifier"`
KeyCode int `js:"keyCode"`
Locale string `js:"locale"`
Location int `js:"location"`
KeyLocation int `js:"keyLocation"`
MetaKey bool `js:"metaKey"`
Repeat bool `js:"repeat"`
ShiftKey bool `js:"shiftKey"`
}
func (ev *KeyboardEvent) ModifierState(mod string) bool {
return ev.Object.Call("getModifierState", mod).Bool()
}
type MediaStreamEvent struct{ *BasicEvent }
type MessageEvent struct {
*BasicEvent
Data js.Value `js:"data"`
}
type MouseEvent struct {
*UIEvent
AltKey bool `js:"altKey"`
Button int `js:"button"`
ClientX int `js:"clientX"`
ClientY int `js:"clientY"`
CtrlKey bool `js:"ctrlKey"`
MetaKey bool `js:"metaKey"`
MovementX int `js:"movementX"`
MovementY int `js:"movementY"`
ScreenX int `js:"screenX"`
ScreenY int `js:"screenY"`
ShiftKey bool `js:"shiftKey"`
}
func (ev *MouseEvent) RelatedTarget() Element {
return wrapElement(ev.Object.Get("relatedTarget"))
}
func (ev *MouseEvent) ModifierState(mod string) bool {
return ev.Object.Call("getModifierState", mod).Bool()
}
type MutationEvent struct{ *BasicEvent }
type OfflineAudioCompletionEvent struct{ *BasicEvent }
type PageTransitionEvent struct{ *BasicEvent }
type PointerEvent struct{ *BasicEvent }
type PopStateEvent struct{ *BasicEvent }
type ProgressEvent struct{ *BasicEvent }
type RelatedEvent struct{ *BasicEvent }
type RTCPeerConnectionIceEvent struct{ *BasicEvent }
type SensorEvent struct{ *BasicEvent }
type StorageEvent struct{ *BasicEvent }
type SVGEvent struct{ *BasicEvent }
type SVGZoomEvent struct{ *BasicEvent }
type TimeEvent struct{ *BasicEvent }
// TouchEvent represents an event sent when the state of contacts with a touch-sensitive
// surface changes. This surface can be a touch screen or trackpad, for example. The event
// can describe one or more points of contact with the screen and includes support for
// detecting movement, addition and removal of contact points, and so forth.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent.
type TouchEvent struct {
*BasicEvent
AltKey bool `js:"altKey"`
CtrlKey bool `js:"ctrlKey"`
MetaKey bool `js:"metaKey"`
ShiftKey bool `js:"shiftKey"`
}
// ChangedTouches lists all individual points of contact whose states changed between
// the previous touch event and this one.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/changedTouches.
func (ev *TouchEvent) ChangedTouches() []*Touch {
return touchListToTouches(ev.Object.Get("changedTouches"))
}
// TargetTouches lists all points of contact that are both currently in contact with the
// touch surface and were also started on the same element that is the target of the event.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/targetTouches.
func (ev *TouchEvent) TargetTouches() []*Touch {
return touchListToTouches(ev.Object.Get("targetTouches"))
}
// Touches lists all current points of contact with the surface, regardless of target
// or changed status.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/touches.
func (ev *TouchEvent) Touches() []*Touch {
return touchListToTouches(ev.Object.Get("touches"))
}
func touchListToTouches(tl js.Value) []*Touch {
out := make([]*Touch, tl.Length())
for i := range out {
out[i] = &Touch{Value: tl.Index(i)}
}
return out
}
// Touch represents a single contact point on a touch-sensitive device. The contact point
// is commonly a finger or stylus and the device may be a touchscreen or trackpad.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/Touch.
type Touch struct {
js.Value
Identifier int `js:"identifier"`
ScreenX float64 `js:"screenX"`
ScreenY float64 `js:"screenY"`
ClientX float64 `js:"clientX"`
ClientY float64 `js:"clientY"`
PageX float64 `js:"pageX"`
PageY float64 `js:"pageY"`
RadiusX float64 `js:"radiusX"`
RadiusY float64 `js:"radiusY"`
RotationAngle float64 `js:"rotationAngle"`
Force float64 `js:"force"`
}
// Target returns the Element on which the touch point started when it was first placed
// on the surface, even if the touch point has since moved outside the interactive area
// of that element or even been removed from the document.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/Touch/target.
func (t *Touch) Target() Element {
return wrapElement(t.Get("target"))
}
type TrackEvent struct{ *BasicEvent }
type TransitionEvent struct{ *BasicEvent }
type UIEvent struct{ *BasicEvent }
type UserProximityEvent struct{ *BasicEvent }
const (
DeltaPixel = 0
DeltaLine = 1
DeltaPage = 2
)
type WheelEvent struct {
*BasicEvent
DeltaX float64 `js:"deltaX"`
DeltaY float64 `js:"deltaY"`
DeltaZ float64 `js:"deltaZ"`
DeltaMode int `js:"deltaMode"`
}
type EventTarget interface {
// AddEventListener adds a new event listener and returns the
// function it created. If using RemoveEventListener,
// that function has to be used.
AddEventListener(typ string, listener func(Event), useCapture bool) js.Func
RemoveEventListener(typ string, listener js.Func, useCapture bool)
DispatchEvent(event Event) bool
}