forked from hexops/vecty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.go
268 lines (228 loc) · 6.68 KB
/
dom.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package vecty
import "github.com/gopherjs/gopherjs/js"
// Component represents a Vecty component.
type Component interface {
Markup
Reconcile(oldComp Component)
Node() *js.Object
Unmount()
}
// Render renders a component into the given container element. It is appended
// as a child element.
func Render(comp Component, container *js.Object) {
comp.Reconcile(nil)
container.Call("appendChild", comp.Node())
}
// RenderAsBody renders the given component as the body of the page, replacing
// whatever existing content in the page body there may be.
func RenderAsBody(comp Component) {
doc := js.Global.Get("document")
body := doc.Call("createElement", "body")
Render(comp, body)
if doc.Get("readyState").String() == "loading" {
doc.Call("addEventListener", "DOMContentLoaded", func() { // avoid duplicate body
doc.Set("body", body)
})
return
}
doc.Set("body", body)
}
type textComponent struct {
text string
node *js.Object
}
// Apply implements the Markup interface.
func (s *textComponent) Apply(element *Element) {
element.Children = append(element.Children, s)
}
func (s *textComponent) Reconcile(oldComp Component) {
if oldText, ok := oldComp.(*textComponent); ok {
s.node = oldText.node
if oldText.text != s.text {
s.node.Set("nodeValue", s.text)
}
return
}
s.node = js.Global.Get("document").Call("createTextNode", s.text)
}
func (s *textComponent) Node() *js.Object {
return s.node
}
func (e *textComponent) Unmount() {}
// Text returns a component which renders the given text. The text is always
// escaped, and as such feeding arbitrary user input to this function is safe.
func Text(text string) Component {
return &textComponent{text: text}
}
// Element is a Component which virtually represents a DOM element.
type Element struct {
TagName string
Properties map[string]interface{}
Style map[string]interface{}
Dataset map[string]string
EventListeners []*EventListener
Children []Component
node *js.Object
}
// AddChild adds a child component.
func (e *Element) AddChild(s Component) {
e.Children = append(e.Children, s)
}
// Apply implements the Markup interface.
func (e *Element) Apply(element *Element) {
element.Children = append(element.Children, e)
}
// Reconcile implements the Component interface.
func (e *Element) Reconcile(oldComp Component) {
for _, l := range e.EventListeners {
l.wrapper = func(jsEvent *js.Object) {
if l.callPreventDefault {
jsEvent.Call("preventDefault")
}
if l.callStopPropagation {
jsEvent.Call("stopPropagation")
}
l.Listener(&Event{Target: jsEvent.Get("target")})
}
}
if oldElement, ok := oldComp.(*Element); ok && oldElement.TagName == e.TagName {
e.node = oldElement.node
for name, value := range e.Properties {
oldValue := oldElement.Properties[name]
if value != oldValue || name == "value" || name == "checked" {
if name == "value" {
isFocussed := js.Global.Get("document").Get("activeElement") == e.node &&
js.Global.Get("document").Call("hasFocus").Bool()
if isFocussed {
// If the element has focus, only update the value if the old value matches
// the current value. This prevents the value property being set while
// the user is typing.
currentValue := e.node.Get("value").String()
if value != oldValue && oldValue == currentValue {
// We preserve the selection start and end points to prevent the cursor
// from moving.
start := e.node.Get("selectionStart").Int()
end := e.node.Get("selectionEnd").Int()
e.node.Set(name, value)
e.node.Call("setSelectionRange", start, end)
}
} else {
e.node.Set(name, value)
}
} else {
e.node.Set(name, value)
}
}
}
for name := range oldElement.Properties {
if _, ok := e.Properties[name]; !ok {
e.node.Set(name, nil)
}
}
style := e.node.Get("style")
for name, value := range e.Style {
style.Call("setProperty", name, value)
}
for name := range oldElement.Style {
if _, ok := e.Style[name]; !ok {
style.Call("removeProperty", name)
}
}
for _, l := range oldElement.EventListeners {
e.node.Call("removeEventListener", l.Name, l.wrapper)
}
for _, l := range e.EventListeners {
e.node.Call("addEventListener", l.Name, l.wrapper)
}
// TODO better list element reuse
for i, newChild := range e.Children {
if i >= len(oldElement.Children) {
newChild.Reconcile(nil)
e.node.Call("appendChild", newChild.Node())
continue
}
oldChild := oldElement.Children[i]
newChild.Reconcile(oldChild)
replaceNode(newChild.Node(), oldChild.Node())
}
for i := len(e.Children); i < len(oldElement.Children); i++ {
removeNode(oldElement.Children[i].Node())
}
return
}
e.node = js.Global.Get("document").Call("createElement", e.TagName)
for name, value := range e.Properties {
e.node.Set(name, value)
}
for name, value := range e.Dataset {
e.node.Get("dataset").Set(name, value)
}
style := e.node.Get("style")
for name, value := range e.Style {
style.Call("setProperty", name, value)
}
for _, l := range e.EventListeners {
e.node.Call("addEventListener", l.Name, l.wrapper)
}
for _, c := range e.Children {
c.Reconcile(nil)
e.node.Call("appendChild", c.Node())
}
}
// Node implements the Component interface.
func (e *Element) Node() *js.Object {
return e.node
}
func (e *Element) Unmount() {
for _, c := range e.Children {
c.Unmount()
}
}
// SetTitle sets the title of the document.
func SetTitle(title string) {
js.Global.Get("document").Set("title", title)
}
// AddStylesheet adds an external stylesheet to the document.
func AddStylesheet(url string) {
link := js.Global.Get("document").Call("createElement", "link")
link.Set("rel", "stylesheet")
link.Set("href", url)
js.Global.Get("document").Get("head").Call("appendChild", link)
}
// Composite is the struct which all components embed.
type Composite struct {
RenderFunc func() Component
Body Component
}
// Node implements the Component interface.
func (c *Composite) Node() *js.Object {
return c.Body.Node()
}
func (c *Composite) Unmount() {
if c.Body != nil {
c.Body.Unmount()
}
}
// ReconcileBody implements the Component interface.
func (c *Composite) ReconcileBody() {
c.Unmount()
oldBody := c.Body
c.Body = c.RenderFunc()
c.Body.Reconcile(oldBody)
if oldBody != nil {
replaceNode(c.Body.Node(), oldBody.Node())
}
}
type nilComponent struct {
node *js.Object
}
// Apply implements the Markup interface.
func (s *nilComponent) Apply(element *Element) {}
func (s *nilComponent) Reconcile(oldComp Component) {}
func (s *nilComponent) Node() *js.Object {
return nil
}
func (e *nilComponent) Unmount() {}
func Nil() Component {
return &nilComponent{}
}