forked from dennwc/dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
186 lines (147 loc) · 3.27 KB
/
event.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
package dom
import (
"fmt"
"github.com/dennwc/dom/js"
)
type EventTarget interface {
Value
AddEventListener(typ string, h EventHandler)
// TODO: removeEventListener
// TODO: dispatchEvent
}
type Event interface {
Value
Bubbles() bool
Cancelable() bool
Composed() bool
CurrentTarget() *Element
DefaultPrevented() bool
Target() *Element
Type() string
IsTrusted() bool
Path() NodeList
PreventDefault()
StopPropagation()
StopImmediatePropagation()
}
type EventHandler func(Event)
type EventConstructor func(e BaseEvent) Event
func RegisterEventType(typ string, fnc EventConstructor) {
cl := js.Get(typ)
if !cl.Valid() {
panic(fmt.Errorf("class undefined: %q", typ))
}
eventClasses = append(eventClasses, eventClass{
Class: cl, New: fnc,
})
}
func init() {
RegisterEventType("MouseEvent", func(e BaseEvent) Event {
return &MouseEvent{e}
})
}
type eventClass struct {
Class js.Value
New EventConstructor
}
var (
eventClasses []eventClass
)
func convertEvent(v js.Value) Event {
e := BaseEvent{v: v}
// TODO: get class name directly
for _, cl := range eventClasses {
if v.InstanceOf(cl.Class) {
return cl.New(e)
}
}
return &e
}
type BaseEvent struct {
v js.Value
}
func (e *BaseEvent) getBool(name string) bool {
return e.v.Get(name).Bool()
}
func (e *BaseEvent) Bubbles() bool {
return e.getBool("bubbles")
}
func (e *BaseEvent) Cancelable() bool {
return e.getBool("cancelable")
}
func (e *BaseEvent) Composed() bool {
return e.getBool("composed")
}
func (e *BaseEvent) CurrentTarget() *Element {
return AsElement(e.v.Get("currentTarget"))
}
func (e *BaseEvent) DefaultPrevented() bool {
return e.getBool("defaultPrevented")
}
func (e *BaseEvent) IsTrusted() bool {
return e.getBool("isTrusted")
}
func (e *BaseEvent) JSRef() js.Ref {
return e.v.JSRef()
}
func (e *BaseEvent) Type() string {
return e.v.Get("type").String()
}
func (e *BaseEvent) Target() *Element {
return AsElement(e.v.Get("target"))
}
func (e *BaseEvent) Path() NodeList {
return AsNodeList(e.v.Get("path"))
}
func (e *BaseEvent) PreventDefault() {
e.v.Call("preventDefault")
}
func (e *BaseEvent) StopPropagation() {
e.v.Call("stopPropagation")
}
func (e *BaseEvent) StopImmediatePropagation() {
e.v.Call("stopImmediatePropagation")
}
type MouseEventHandler func(*MouseEvent)
type MouseEvent struct {
BaseEvent
}
func (e *MouseEvent) getPos(nameX, nameY string) Point {
x := e.v.Get(nameX).Int()
y := e.v.Get(nameY).Int()
return Point{X: x, Y: y}
}
func (e *MouseEvent) getPosPref(pref string) Point {
return e.getPos(pref+"X", pref+"Y")
}
const (
MouseLeft = MouseButton(0)
)
type MouseButton int
func (e *MouseEvent) Button() MouseButton {
return MouseButton(e.v.Get("button").Int())
}
func (e *MouseEvent) ClientPos() Point {
return e.getPosPref("client")
}
func (e *MouseEvent) OffsetPos() Point {
return e.getPosPref("offset")
}
func (e *MouseEvent) PagePos() Point {
return e.getPosPref("page")
}
func (e *MouseEvent) ScreenPos() Point {
return e.getPosPref("screen")
}
func (e *MouseEvent) AltKey() bool {
return e.v.Get("altKey").Bool()
}
func (e *MouseEvent) CtrlKey() bool {
return e.v.Get("ctrlKey").Bool()
}
func (e *MouseEvent) ShiftKey() bool {
return e.v.Get("shiftKey").Bool()
}
func (e *MouseEvent) MetaKey() bool {
return e.v.Get("metaKey").Bool()
}