-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
207 lines (179 loc) · 4.83 KB
/
driver.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (C) 2019 rameshvk. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.
// Package js implements a basic gopherjs driver for dom
package js
import (
"strings"
"unsafe"
"github.com/dotchain/fuss/dom"
"github.com/gopherjs/gopherjs/js"
)
func init() {
events := map[string]*js.Object{
"change": js.Global.Get("Map").New(),
"click": js.Global.Get("Map").New(),
"focus": js.Global.Get("Map").New(),
"input": js.Global.Get("Map").New(),
"blur": js.Global.Get("Map").New(),
}
dom.RegisterDriver(driver{events})
}
type driver struct {
events map[string]*js.Object
}
type cbInfo struct {
*dom.EventHandler
listener func(*js.Object)
}
func (d driver) NewElement(props dom.Props, children ...dom.Element) dom.Element {
tag := strings.ToLower(props.Tag)
if tag == "" {
tag = "div"
}
elt := element{js.Global.Get("document").Call("createElement", tag), &d}
for k, v := range props.ToMap() {
elt.SetProp(k, v)
}
for kk, child := range children {
elt.InsertChild(kk, child)
}
return elt
}
type element struct {
n *js.Object
d *driver
}
func (e element) SetProp(key string, value interface{}) {
switch key {
case "Tag":
tag := strings.ToLower(value.(string))
if tag == "" {
tag = "div"
}
if tag != strings.ToLower(e.n.Get("tagName").String()) {
panic("Cannot change the tag of an element: " + tag)
}
case "Checked":
e.n.Set("checked", value.(bool))
case "Type", "ID", "For", "Href", "Placeholder", "TabIndex":
e.setAttr(strings.ToLower(key), value.(string))
case "TextContent":
if strings.ToLower(e.n.Get("tagName").String()) == "input" {
e.n.Set("value", value.(string))
} else {
e.n.Set("textContent", value.(string))
}
case "Src":
e.setAttr("src", value.(string))
case "Styles":
e.setAttr("style", value.(dom.Styles).String())
case "OnChange":
e.onEvent("change", value.(*dom.EventHandler))
case "OnClick":
e.onEvent("click", value.(*dom.EventHandler))
case "OnFocus":
e.onEvent("focus", value.(*dom.EventHandler))
e.onEvent("blur", value.(*dom.EventHandler))
case "OnInput":
e.onEvent("input", value.(*dom.EventHandler))
default:
panic("Unknown key: " + key)
}
}
func (e element) setAttr(key, val string) {
if val != "" {
e.n.Call("setAttribute", key, val)
} else {
e.n.Call("removeAttribute", key)
}
}
func (e element) onEvent(key string, h *dom.EventHandler) {
dict := e.d.events[key]
info, ok := get(dict, e.n)
switch {
case !ok && h != nil:
listener := listener(e.n, dict)
e.n.Call("addEventListener", key, js.InternalObject(listener), false)
dict.Call("set", e.n, js.InternalObject(&cbInfo{h, listener}))
case ok && h == nil:
dict.Call("delete", e.n)
e.n.Call("removeEventListener", key, js.InternalObject(info.listener))
case ok && h != nil:
info.EventHandler = h
}
}
func (e element) Children() []dom.Element {
if x := e.n.Get("firstChild"); x != nil && x.Get("nodeType").Int() == 3 /* text node */ {
return nil
}
result := []dom.Element{}
for n := e.n.Get("firstChild"); n != nil; n = n.Get("nextSibling") {
result = append(result, element{n, e.d})
}
return result
}
func (e element) RemoveChild(index int) {
n := e.n.Get("firstChild")
for kk := 0; kk < index; kk++ {
n = n.Get("nextSibling")
}
e.n.Call("removeChild", n)
}
func (e element) InsertChild(index int, elt dom.Element) {
if n := elt.(element).n; n.Get("parentElement") == e.n {
e.n.Call("removeChild", n)
}
n := e.n.Get("firstChild")
for kk := 0; kk < index; kk++ {
n = n.Get("nextSibling")
}
if n != nil {
e.n.Call("insertBefore", elt.(element).n, n)
} else {
e.n.Call("appendChild", elt.(element).n)
}
}
func (e element) Close() {
for k := range e.d.events {
e.onEvent(k, nil)
}
}
func get(m *js.Object, key *js.Object) (*cbInfo, bool) {
if ok := m.Call("has", key).Bool(); !ok {
return nil, false
}
jso := m.Call("get", key)
return (*cbInfo)(unsafe.Pointer(jso.Unsafe())), true // nolint
}
func listener(n *js.Object, dict *js.Object) func(*js.Object) {
return func(native *js.Object) {
if info, ok := get(dict, n); ok {
counter++
info.EventHandler.Handle(event{native, counter})
}
}
}
var counter int64
// QuerySelector returns a DOM node wrapped in the dom.Element interface
func QuerySelector(s string) dom.Element {
return element{js.Global.Get("document").Call("querySelector", s), nil}
}
type event struct {
native *js.Object
counter int64
}
func (e event) EpochNano() int64 {
return int64(e.native.Get("timestamp").Int())*1e6 + e.counter
}
func (e event) Value() string {
n := e.native.Get("currentTarget")
if strings.ToLower(n.Get("tagName").String()) != "input" {
return e.native.Get("type").String()
}
if n.Get("type").String() == "checkbox" {
m := map[bool]string{true: "on", false: "off"}
return m[n.Get("checked").Bool()]
}
return n.Get("value").String()
}