-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_target.go
111 lines (92 loc) · 3.29 KB
/
event_target.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
//go:build js
package goji
import "syscall/js"
func init() {
EventTarget = eventTargetJS(js.Global().Get("EventTarget"))
}
type eventTargetJS js.Value
// EventTarget wraps the EventTarget global interface.
//
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
var EventTarget eventTargetJS
// New wraps the EventTarget constructor.
//
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/EventTarget
func (e eventTargetJS) New() EventTargetValue {
res := js.Value(e).New()
return EventTargetValue(res)
}
// EventTargetValue is an instance of EventTarget.
type EventTargetValue js.Value
// AddEventListener wraps the EventTarget addEventListener instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
func (e EventTargetValue) AddEventListener(eventType string, listener js.Value, opts ...eventListenerOption) {
switch {
case len(opts) > 0:
options := js.ValueOf(map[string]any{})
for _, opt := range opts {
opt(options)
}
js.Value(e).Call("addEventListener", eventType, listener, options)
default:
js.Value(e).Call("addEventListener", eventType, listener)
}
}
// DispatchEvent wraps the EventTarget dispatchEvent instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent
func (e EventTargetValue) DispatchEvent(event js.Value) bool {
return js.Value(e).Call("dispatchEvent", event).Bool()
}
// RemoveEventListener wraps the EventTarget removeEventListener instance method.
//
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener
func (e EventTargetValue) RemoveEventListener(eventType string, listener js.Value, options js.Value) {
js.Value(e).Call("removeEventListener", eventType, listener, options)
}
// EventListenerOptions is used to set event listener options.
var EventListenerOptions = &eventListenerOptions{}
type eventListenerOptions struct{}
type eventListenerOption func(value js.Value)
// WithCapture sets the capture option.
//
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture
func (e eventListenerOptions) WithCapture(enable bool) eventListenerOption {
return func(value js.Value) {
value.Set("capture", enable)
}
}
// WithOnce sets the once option.
//
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#once
func (e eventListenerOptions) WithOnce(enable bool) eventListenerOption {
return func(value js.Value) {
value.Set("once", enable)
}
}
// WithPassive sets the passive option.
//
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#passive
func (e eventListenerOptions) WithPassive(enable bool) eventListenerOption {
return func(value js.Value) {
value.Set("passive", enable)
}
}
// WithSignal sets the signal option.
//
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#signal
func (e eventListenerOptions) WithSignal(signal js.Value) eventListenerOption {
return func(value js.Value) {
value.Set("signal", signal)
}
}
// EventListener returns a new event listener callback
// that calls the given func when an event is received.
func EventListener(fn func(event EventValue)) js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
event := EventValue(args[0])
fn(event)
return js.Undefined()
})
}