-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
202 lines (168 loc) · 3.5 KB
/
base.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
package wasabi
import (
"fmt"
"strings"
"github.com/asaskevich/EventBus"
)
type Element interface {
ID() elementID
View() string
Attach(app App)
Detach()
Update()
Class(classes ...string) Element
Attr(name, value string) Element
GetAttr(name string) string
MergeAttrs(update map[string]string) Element
Style(name, value string) Element
GetStyle(name string) string
MergeStyles(update map[string]string) Element
OnClick(handler func()) Element
OnChange(handler func(ev Event)) Element
Modify(mods ...func(e Element) Element) Element
SetApp(app App)
}
type base struct {
id elementID
attached bool
app App
element Element
classes []string
attrs map[string]string
styles map[string]string
}
func newBase() base {
return base{
id: newElementID(),
attached: false,
attrs: map[string]string{},
styles: map[string]string{},
}
}
func (base *base) SetApp(app App) {
base.app = app
}
func (b *base) ID() elementID {
return b.id
}
func (b *base) View() string {
return ""
}
func (b *base) genHTML(tag string, body string) string {
return fmt.Sprintf(
`<%s id="%s" class="%s" %s style="%s">%s</%s>`,
tag,
b.ID(),
strings.Join(b.classes, " "),
b.getAttrsStr(),
b.getStyleStr(),
body,
tag,
)
}
func (b *base) Attach(a App) {
b.attached = true
b.app = a
}
func (b *base) Detach() {
b.attached = false
}
func (b *base) Class(classes ...string) Element {
b.classes = classes
return b.element
}
func (b *base) Attr(name, value string) Element {
b.attrs[name] = value
return b.element
}
func (b *base) GetAttr(name string) string {
return b.attrs[name]
}
func (b *base) MergeAttrs(update map[string]string) Element {
merge(b.attrs, update)
return b.element
}
func (b *base) Styles(styles map[string]string) Element {
b.styles = styles
return b.element
}
func merge(a, b map[string]string) {
for k, v := range b {
a[k] = v
}
}
func (b *base) MergeStyles(update map[string]string) Element {
merge(b.styles, update)
return b.element
}
func (b *base) Style(name, value string) Element {
b.styles[name] = value
return b.element
}
func (b *base) GetStyle(name string) string {
return b.styles[name]
}
func (b *base) getAttrsStr() string {
ret := ""
for name, value := range b.attrs {
ret += fmt.Sprintf("%s=\"%s\" ", name, value)
}
return ret
}
func (b *base) getStyleStr() string {
ret := ""
for name, value := range b.styles {
ret += fmt.Sprintf("%s: %s;", name, value)
}
return ret
}
func (b *base) SetElement(w Element) {
b.element = w
}
func (b *base) Update() {
if b.attached {
b.app.postUpdate(b.element)
}
}
func (b *base) OnClick(handler func()) Element {
b.app.addEventHandler(b.element, "click", func(_ Event) { handler() })
// TODO on detach, delete the handler
return b.element
}
func (b *base) OnChange(handler func(ev Event)) Element {
b.app.addEventHandler(b.element, "change", handler)
return b.element
}
func (b *base) Modify(mods ...func(Element) Element) Element {
for _, mod := range mods {
b.element = mod(b.element)
}
return b.element
}
type eventPublisher interface {
AddListener(w Element)
Notify()
}
type model struct {
bus EventBus.Bus
}
func newModel() model {
return model{
bus: EventBus.New(),
}
}
func (m *model) AddListener(b Element) {
m.bus.Subscribe("update", b.Update)
}
func (m *model) Notify() {
m.bus.Publish("update")
}
func (b *base) Build() Element {
return b.element
}
func init() {
SetTargetEvents([]TargetEvent{
{Name: "click", PropName: ""},
{Name: "change", PropName: "value"}},
)
}