-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
node.go
203 lines (161 loc) · 3.73 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
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 app
import (
"context"
"fmt"
"io"
"net/url"
"reflect"
"github.com/maxence-charriere/go-app/v7/pkg/errors"
)
// UI is the interface that describes a user interface element such as
// components and HTML elements.
type UI interface {
// Kind represents the specific kind of a UI element.
Kind() Kind
// JSValue returns the javascript value linked to the element.
JSValue() Value
// Reports whether the element is mounted.
Mounted() bool
name() string
self() UI
setSelf(UI)
context() context.Context
attributes() map[string]string
eventHandlers() map[string]eventHandler
parent() UI
setParent(UI)
children() []UI
mount() error
dismount()
update(UI) error
onNav(*url.URL)
}
// Kind represents the specific kind of a user interface element.
type Kind uint
func (k Kind) String() string {
switch k {
case SimpleText:
return "text"
case HTML:
return "html"
case Component:
return "component"
case Selector:
return "selector"
case RawHTML:
return "raw"
default:
return "undefined"
}
}
const (
// UndefinedElem represents an undefined UI element.
UndefinedElem Kind = iota
// SimpleText represents a simple text element.
SimpleText
// HTML represents an HTML element.
HTML
// Component represents a customized, independent and reusable UI element.
Component
// Selector represents an element that is used to select a subset of
// elements within a given list.
Selector
// RawHTML represents an HTML element obtained from a raw HTML code snippet.
RawHTML
)
// FilterUIElems returns a filtered version of the given UI elements where
// selector elements such as If and Range are interpreted and removed. It also
// remove nil elements.
//
// It should be used only when implementing components that can accept content
// with variadic arguments like HTML elements Body method.
func FilterUIElems(uis ...UI) []UI {
if len(uis) == 0 {
return nil
}
elems := make([]UI, 0, len(uis))
for _, n := range uis {
// Ignore nil elements:
if v := reflect.ValueOf(n); n == nil ||
v.Kind() == reflect.Ptr && v.IsNil() {
continue
}
switch n.Kind() {
case SimpleText, HTML, Component, RawHTML:
elems = append(elems, n)
case Selector:
elems = append(elems, n.children()...)
default:
panic(errors.New("filtering ui elements failed").
Tag("reason", "unexpected element type found").
Tag("kind", n.Kind()).
Tag("name", n.name()),
)
}
}
return elems
}
// EventHandler represents a function that can handle HTML events. They are
// always called on the UI goroutine.
type EventHandler func(ctx Context, e Event)
type eventHandler struct {
event string
jsvalue Func
value EventHandler
}
func (h eventHandler) equal(o eventHandler) bool {
return h.event == o.event &&
fmt.Sprintf("%p", h.value) == fmt.Sprintf("%p", o.value)
}
func makeJsEventHandler(src UI, h EventHandler) Func {
return FuncOf(func(this Value, args []Value) interface{} {
dispatch(func() {
if !src.Mounted() {
return
}
ctx := Context{
Context: src.context(),
Src: src,
JSSrc: src.JSValue(),
}
event := Event{
Value: args[0],
}
trackMousePosition(event)
h(ctx, event)
})
return nil
})
}
func trackMousePosition(e Event) {
x := e.Get("clientX")
if !x.Truthy() {
return
}
y := e.Get("clientY")
if !y.Truthy() {
return
}
Window().setCursorPosition(x.Int(), y.Int())
}
func isErrReplace(err error) bool {
_, replace := errors.Tag(err, "replace")
return replace
}
func mount(n UI) error {
n.setSelf(n)
return n.mount()
}
func dismount(n UI) {
n.dismount()
n.setSelf(nil)
}
func update(a, b UI) error {
a.setSelf(a)
b.setSelf(b)
return a.update(b)
}
type writableNode interface {
html(w io.Writer)
htmlWithIndent(w io.Writer, indent int)
}