forked from albrow/vdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.go
203 lines (180 loc) · 5.27 KB
/
patch.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
package vdom
import (
"fmt"
"github.com/gopherjs/gopherwasm/js"
dom "github.com/gowasm/go-js-dom"
)
var document dom.Document
func init() {
// We only want to initialize document if we are running in the browser.
// We can detect this by checking if the document is defined.
if js.Global() != js.Null() && js.Global().Get("document") != js.Undefined() {
document = dom.GetWindow().Document()
}
}
// Patcher represents changes that can be made to the DOM.
type Patcher interface {
// Patch applies the given patch to the DOM. The given root
// is a relative starting point for the virtual tree in the
// actual DOM.
Patch(root dom.Element) error
}
// PatchSet is a set of zero or more Patchers
type PatchSet []Patcher
// Patch satisfies the Patcher interface and sequentially applies
// all the patches in the patch set.
func (ps PatchSet) Patch(root dom.Element) error {
for _, patch := range ps {
if err := patch.Patch(root); err != nil {
return err
}
}
return nil
}
// Append is a Patcher which will append a child Node to a parent Node.
type Append struct {
Child Node
Parent *Element
}
// Patch satisfies the Patcher interface and applies the change to the
// actual DOM.
func (p *Append) Patch(root dom.Element) error {
fmt.Println("APPEND PATCH")
var parent dom.Node
if p.Parent != nil {
fmt.Println("Finding parent in DOM")
parent = findInDOM(p.Parent, root)
} else {
fmt.Println("Setting parent as root")
parent = root
}
fmt.Println("Computed parent: ", parent)
child := createForDOM(p.Child)
fmt.Println("Created child: ", child)
parent.AppendChild(child)
fmt.Println("Successfully appended")
return nil
}
// Replace is a Patcher will will replace an old Node with a new Node.
type Replace struct {
Old Node
New Node
}
// Patch satisfies the Patcher interface and applies the change to the
// actual DOM.
func (p *Replace) Patch(root dom.Element) error {
fmt.Println("REPLACE PATCH")
var parent dom.Node
if p.Old.Parent() != nil {
parent = findInDOM(p.Old.Parent(), root)
} else {
parent = root
}
oldChild := findInDOM(p.Old, root)
newChild := createForDOM(p.New)
parent.ReplaceChild(newChild, oldChild)
return nil
}
// Remove is a Patcher which will remove the given Node.
type Remove struct {
Node Node
}
// Patch satisfies the Patcher interface and applies the change to the
// actual DOM.
func (p *Remove) Patch(root dom.Element) error {
fmt.Println("REMOVE PATCH")
var parent dom.Node
if p.Node.Parent() != nil {
parent = findInDOM(p.Node.Parent(), root)
} else {
parent = root
}
self := findInDOM(p.Node, root)
parent.RemoveChild(self)
// p.Node was removed, so subtract one from the final index for all
// siblings that come after it.
if p.Node.Parent() != nil {
lastIndex := p.Node.Index()[len(p.Node.Index())-1]
for _, sibling := range p.Node.Parent().Children()[lastIndex:] {
switch t := sibling.(type) {
case *Element:
t.index[len(t.index)-1] = t.index[len(t.index)-1] - 1
case *Text:
t.index[len(t.index)-1] = t.index[len(t.index)-1] - 1
case *Comment:
t.index[len(t.index)-1] = t.index[len(t.index)-1] - 1
default:
panic("unreachable")
}
}
}
return nil
}
// SettAttr is a Patcher which will set the attribute of the given Node to
// the given Attr. It will overwrite any previous values for the given Attr.
type SetAttr struct {
Node Node
Attr *Attr
}
// Patch satisfies the Patcher interface and applies the change to the
// actual DOM.
func (p *SetAttr) Patch(root dom.Element) error {
fmt.Println("SET ATTR PATCH")
self := findInDOM(p.Node, root).(dom.Element)
self.SetAttribute(p.Attr.Name, p.Attr.Value)
return nil
}
// RemoveAttr is a Patcher which will remove the attribute with the given
// name from the given Node.
type RemoveAttr struct {
Node Node
AttrName string
}
// Patch satisfies the Patcher interface and applies the change to the
// actual DOM.
func (p *RemoveAttr) Patch(root dom.Element) error {
fmt.Println("REMOVE ATTR PATCH")
self := findInDOM(p.Node, root).(dom.Element)
self.RemoveAttribute(p.AttrName)
return nil
}
// findInDOM finds the node in the actual DOM corresponding
// to the given virtual node, using the given root as a relative
// starting point.
func findInDOM(node Node, root dom.Element) dom.Node {
el := root.ChildNodes()[node.Index()[0]]
fmt.Println("ELEMENT:", el)
fmt.Println("NODE.INDEX", node.Index())
for _, i := range node.Index()[1:] {
el = el.ChildNodes()[i]
}
return el
}
// createForDOM creates a real node corresponding to the given
// virtual node. It does not insert it into the actual DOM.
func createForDOM(node Node) dom.Node {
switch node.(type) {
case *Element:
fmt.Println("Creating an element")
vEl := node.(*Element)
el := document.CreateElement(vEl.Name)
for _, attr := range vEl.Attrs {
el.SetAttribute(attr.Name, attr.Value)
}
el.SetInnerHTML(string(vEl.InnerHTML()))
return el
case *Text:
fmt.Println("Creating a text")
vText := node.(*Text)
textNode := document.CreateTextNode(string(vText.Value))
return textNode
case *Comment:
fmt.Println("Creating a comment")
vComment := node.(*Comment)
commentNode := document.Underlying().Call("createComment", string(vComment.Value))
return dom.WrapNode(commentNode)
default:
msg := fmt.Sprintf("Don't know how to create node for type %T", node)
panic(msg)
}
}