forked from dennwc/dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.go
78 lines (62 loc) · 1.59 KB
/
element.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
package dom
import (
"fmt"
"github.com/dennwc/dom/js"
sjs "syscall/js"
)
var _ Node = (*Element)(nil)
func AsElement(v js.Value) *Element {
if !v.Valid() {
return nil
}
return &Element{NodeBase{v: v}}
}
func AsNodeList(v js.Value) NodeList {
if !v.Valid() {
return nil
}
arr := make(NodeList, v.Length())
for i := range arr {
arr[i] = AsElement(v.Index(i))
}
return arr
}
var _ Node = (*Element)(nil)
type Element struct {
NodeBase
}
func (e *Element) SetInnerHTML(s string) {
e.v.Set("innerHTML", s)
}
func (e *Element) SetAttribute(k string, v interface{}) {
e.v.Call("setAttribute", k, fmt.Sprint(v))
}
func (e *Element) GetAttribute(k string) js.Value {
return e.v.Call("getAttribute", k)
}
func (e *Element) Style() *Style {
return &Style{v: e.v.Get("style")}
}
func (e *Element) GetBoundingClientRect() Rect {
rv := e.v.Call("getBoundingClientRect")
x, y := rv.Get("x").Int(), rv.Get("y").Int()
w, h := rv.Get("width").Int(), rv.Get("height").Int()
return Rect{Min: Point{x, y}, Max: Point{x + w, y + h}}
}
func (e *Element) onMouseEvent(typ string, flags int, h MouseEventHandler) {
e.AddEventListenerFlags(typ, flags, func(e Event) {
h(e.(*MouseEvent))
})
}
func (e *Element) OnClick(h MouseEventHandler) {
e.onMouseEvent("click", int(sjs.StopPropagation), h)
}
func (e *Element) OnMouseDown(h MouseEventHandler) {
e.onMouseEvent("mousedown", int(sjs.StopPropagation), h)
}
func (e *Element) OnMouseMove(h MouseEventHandler) {
e.onMouseEvent("mousemove", 0, h)
}
func (e *Element) OnMouseUp(h MouseEventHandler) {
e.onMouseEvent("mouseup", int(sjs.StopPropagation), h)
}