-
Notifications
You must be signed in to change notification settings - Fork 5
/
node.go
171 lines (135 loc) · 2.8 KB
/
node.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
package domui
import (
"fmt"
"reflect"
"syscall/js"
)
type NodeKind uint8
const (
TagNode NodeKind = iota
TextNode
)
type Node struct {
Kind NodeKind
Text string
ID string
Style string
Styles SortedMap // string: string
Classes SortedMap // string: struct{}
Attributes SortedMap // string: any
Events map[string][]EventSpec
childNodes []*Node
Focus bool
args []reflect.Value
}
func (_ *Node) IsSpec() {}
func (n *Node) ToElement(scope Scope) (_ js.Value, err error) {
defer he(&err)
switch n.Kind {
case TagNode:
element := document.Call(
"createElement",
n.Text,
)
if len(n.childNodes) > 0 {
fragment := document.Call("createDocumentFragment")
for _, childNode := range n.childNodes {
childElement, err := childNode.ToElement(scope)
ce(err)
fragment.Call("append", childElement)
}
element.Call("appendChild", fragment)
}
if n.ID != "" {
element.Set("id", n.ID)
}
if n.Style != "" {
element.Set("style", n.Style)
}
if len(n.Styles) > 0 {
style := element.Get("style")
for _, item := range n.Styles {
style.Set(item.Key, item.Value)
}
}
if len(n.Classes) > 0 {
list := element.Get("classList")
for _, item := range n.Classes {
list.Call("add", item.Key)
}
}
if len(n.Attributes) > 0 {
for _, item := range n.Attributes {
element.Call("setAttribute", item.Key, item.Value)
element.Set(item.Key, item.Value)
}
}
// events
if len(n.Events) > 0 {
var app *App
scope.Assign(&app)
setEventSpecs(app.wrapElement, element, n.Events)
}
if n.Focus {
element.Call("focus")
}
return element, nil
case TextNode:
element := document.Call(
"createTextNode",
n.Text,
)
return element, nil
}
panic("bad kind")
}
func (node *Node) ApplySpec(spec Spec) {
if spec == nil {
return
}
switch spec := spec.(type) {
case IDSpec:
node.ID = spec.Value
case StyleString:
node.Style = string(spec)
case StyleSpec:
node.Styles.Set(spec.Name, spec.Value)
case StylesSpec:
for k, v := range spec.Styles {
node.Styles.Set(k, v)
}
case ClassesSpec:
for k := range spec.Classes {
node.Classes.Set(k, struct{}{})
}
case AttrsSpec:
for k, v := range spec.Attrs {
node.Attributes.Set(k, v)
}
case AttrSpec:
node.Attributes.Set(spec.Name, spec.Value)
case EventSpec:
if node.Events == nil {
node.Events = make(map[string][]EventSpec)
}
node.Events[spec.Event] = append(
node.Events[spec.Event],
spec,
)
case *Node:
if spec != nil {
node.childNodes = append(node.childNodes, spec)
}
case Specs:
for _, s := range spec {
node.ApplySpec(s)
}
case FocusSpec:
node.Focus = true
case Lazy:
s := spec()
node.ApplySpec(s)
default:
panic(fmt.Errorf("unknown spec: %#v", spec))
}
}