-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.go
87 lines (71 loc) · 1.61 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
// +build js,wasm
package engine
import "syscall/js"
type Element struct {
Node js.Value
child []*Element
draw func()
}
func NewElement(tag string) *Element {
e := &Element{}
e.Node = js.Global().Get("document").Call("createElement", tag)
e.child = make([]*Element, 0)
return e
}
func (e *Element) SetInnerHtml(innerhtml string) *Element {
e.Node.Set("innerHTML", innerhtml)
return e
}
func (e *Element) AddChild(element *Element) *Element {
e.Node.Call("appendChild", element.Node)
return e
//e.child = append(e.child , element)
}
func (e *Element) RemoveSingleChild(element *Element) *Element {
//e.child = nil
e.Node.Call("removeChild", element.Node)
return e
}
func (e *Element) RemoveChild() *Element {
//e.child = nil
for {
n := e.Node.Get("firstChild")
if n == js.Null() {
break
}
e.Node.Call("removeChild", n)
}
return e
}
func (e *Element) SetClass(class string) *Element {
e.Node.Set("className", class)
return e
}
func (e *Element) AppendClass(class string) *Element {
c := e.Node.Get("className").String()
c += " " + class
e.Node.Set("className", c)
return e
}
func (e *Element) SetStyle(key string, value string) *Element {
e.Node.Get("style").Set(key, value)
return e
}
func (e *Element) SetId(id string) *Element {
e.Node.Set("id", id)
return e
}
func (e *Element) SetCallBack(cbType string, cb js.Callback) *Element {
e.Node.Call("addEventListener", cbType, cb)
return e
}
func (e *Element) Set(key string, value string) *Element {
e.Node.Set(key, value)
return e
}
func (e *Element) Nest(el ...*Element) *Element {
for _, i := range el {
e.AddChild(i)
}
return e
}