forked from dominikh/go-js-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
332 lines (296 loc) · 8.92 KB
/
events.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
package dom
import (
"time"
"github.com/gopherjs/gopherjs/js"
)
func WrapEvent(o *js.Object) Event {
return wrapEvent(o)
}
func wrapEvent(o *js.Object) Event {
if o == nil || 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{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.Object
}
// Type BasicEvent implements the Event interface and is embedded by
// concrete event types.
type BasicEvent struct{ *js.Object }
type EventOptions struct {
Bubbles bool
Cancelable bool
}
func CreateEvent(typ string, opts EventOptions) *BasicEvent {
var event = js.Global.Get("Event").New(typ, js.M{
"bubbles": opts.Bubbles,
"cancelable": opts.Cancelable,
})
return &BasicEvent{event}
}
func (ev *BasicEvent) Bubbles() bool {
return ev.Get("bubbles").Bool()
}
func (ev *BasicEvent) Cancelable() bool {
return ev.Get("cancelable").Bool()
}
func (ev *BasicEvent) CurrentTarget() Element {
return wrapElement(ev.Get("currentTarget"))
}
func (ev *BasicEvent) DefaultPrevented() bool {
return ev.Get("defaultPrevented").Bool()
}
func (ev *BasicEvent) EventPhase() int {
return ev.Get("eventPhase").Int()
}
func (ev *BasicEvent) Target() Element {
return wrapElement(ev.Get("target"))
}
func (ev *BasicEvent) Timestamp() time.Time {
ms := ev.Get("timeStamp").Int()
s := ms / 1000
ns := (ms % 1000 * 1e6)
return time.Unix(int64(s), int64(ns))
}
func (ev *BasicEvent) Type() string {
return ev.Get("type").String()
}
func (ev *BasicEvent) PreventDefault() {
ev.Call("preventDefault")
}
func (ev *BasicEvent) StopImmediatePropagation() {
ev.Call("stopImmediatePropagation")
}
func (ev *BasicEvent) StopPropagation() {
ev.Call("stopPropagation")
}
func (ev *BasicEvent) Underlying() *js.Object {
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 }
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.Call("getModifierState", mod).Bool()
}
type MediaStreamEvent struct{ *BasicEvent }
type MessageEvent struct {
*BasicEvent
Data *js.Object `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.Get("target"))
}
func (ev *MouseEvent) ModifierState(mod string) bool {
return ev.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 }
type TouchEvent struct{ *BasicEvent }
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
// wrapper function it generated. If using RemoveEventListener,
// that wrapper has to be used.
AddEventListener(typ string, useCapture bool, listener func(Event)) func(*js.Object)
RemoveEventListener(typ string, useCapture bool, listener func(*js.Object))
DispatchEvent(event Event) bool
}