-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
112 lines (91 loc) · 2.28 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
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
// +build js,wasm
package livedom
import (
"fmt"
sjs "syscall/js"
"github.com/gowasm/livedom/js"
)
func NewElement(val js.Value) *Element {
return &Element{NodeBase{v: val}}
}
func (e *Element) HTML() (string, error) {
htmlVal := e.v.Get("innerHTML")
if !htmlVal.Valid() {
return "", fmt.Errorf("no html")
}
return htmlVal.String(), nil
}
func (e *Element) AppendHTML(toAppend string) error {
oldHTML, err := e.HTML()
if err != nil {
return err
}
newHTML := oldHTML + toAppend
e.v.Set("innerHTML", newHTML)
return nil
}
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) InnerHTML() string {
return e.v.Get("innerHTML").String()
}
func (e *Element) QuerySelector(s string) *Element {
return AsElement(e.v.Call("querySelector", s))
}
func (e *Element) Valid() bool {
return e.v.Valid()
}
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)
}