-
Notifications
You must be signed in to change notification settings - Fork 5
/
wrapper.go
138 lines (127 loc) · 2.97 KB
/
wrapper.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
package wasm
import (
"syscall/js"
)
type Wrapper struct {
JValue js.Value
Name string
Id string
Value string
Checked bool
}
func NewWrapper(v js.Value) *Wrapper {
w := Wrapper{}
w.JValue = v
return &w
}
func (w *Wrapper) Set(s string, thing any) {
thingS, ok := thing.(string)
if ok {
w.JValue.Set(s, thingS)
return
}
thingB, ok := thing.(bool)
if ok {
w.JValue.Set(s, thingB)
return
}
w.JValue.Set(s, js.FuncOf(thing.(func(this js.Value, args []js.Value) any)))
}
func (w *Wrapper) GetAttribute(s string) string {
return w.JValue.Call("getAttribute", s).String()
}
func (w *Wrapper) Get(s string) string {
return w.JValue.Get(s).String()
}
func (w *Wrapper) GetInt(s string) int {
return w.JValue.Get(s).Int()
}
func (w *Wrapper) GetBool(s string) bool {
return w.JValue.Get(s).Bool()
}
func (w *Wrapper) Call(s string, thing any) any {
return w.JValue.Call(s, thing)
}
func (w *Wrapper) Focus() {
w.JValue.Call("focus")
}
func (w *Wrapper) Blur() {
w.JValue.Call("blur")
}
func (w *Wrapper) AppendChild(c any) {
w.JValue.Call("appendChild", c)
}
func (w *Wrapper) FireClick() {
w.JValue.Call("click")
}
func (w *Wrapper) FireSubmit() {
w.JValue.Call("submit")
}
func (w *Wrapper) IsNull() bool {
return w.JValue.IsNull()
}
func (w *Wrapper) SetItem(key, value any) {
w.JValue.Call("setItem", key, value)
}
func (w *Wrapper) GetItem(key any) string {
item := w.JValue.Call("getItem", key)
if item.IsNull() {
return ""
}
return w.JValue.Call("getItem", key).String()
}
func (w *Wrapper) Click(fn func(js.Value, []js.Value) any) {
w.JValue.Set("onclick", js.FuncOf(fn))
}
func (w *Wrapper) Event(fn func(id string)) {
thefunc := func(this js.Value, p []js.Value) any {
p[0].Call("preventDefault")
fn(p[0].Get("target").Get("id").String())
return nil
}
w.JValue.Set("onclick", js.FuncOf(thefunc))
}
func (w *Wrapper) EventWithId(fn func()) {
thefunc := func(this js.Value, p []js.Value) any {
fn()
return nil
}
w.JValue.Set("onclick", js.FuncOf(thefunc))
}
func (w *Wrapper) Show() {
RemoveClass(w.JValue, "hidden")
}
func (w *Wrapper) Hide() {
AddClass(w.JValue, "hidden")
}
func (w *Wrapper) AddClass(c string) {
AddClass(w.JValue, c)
}
func (w *Wrapper) RemoveClass(c string) {
RemoveClass(w.JValue, c)
}
func (w *Wrapper) HasClass(c string) bool {
return HasClass(w.JValue, c)
}
func (w *Wrapper) SelectAllByClass(s string) []*Wrapper {
return w.SelectAllByQuery("getElementsByClassName", s)
}
func (w *Wrapper) SelectAllByQuery(call, s string) []*Wrapper {
list := w.JValue.Call(call, s)
items := []*Wrapper{}
for i := 0; i < list.Length(); i++ {
item := list.Index(i)
w := NewWrapper(item)
w.Name = item.Get("name").String()
w.Id = item.Get("id").String()
w.Value = item.Get("value").String()
if item.Get("checked").IsUndefined() == false {
w.Checked = item.Get("checked").Bool()
}
items = append(items, w)
}
return items
}
func (w *Wrapper) SelectAll(s string) []*Wrapper {
return w.SelectAllByQuery("querySelectorAll", s)
}