~repos /gromer
git clone https://pyrossh.dev/repos/gromer.git
gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.
add svg tags
- app_common.go +1 -1
- app_wasm.go +2 -2
- attributes.go +13 -1
- component.go +1 -1
- element.go +28 -28
- html.go +37 -25
app_common.go
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
package app
|
|
2
2
|
|
|
3
3
|
var (
|
|
4
|
-
body *
|
|
4
|
+
body *Element
|
|
5
5
|
content UI
|
|
6
6
|
renderFunc RenderFunc
|
|
7
7
|
helmet = &Helmet{}
|
app_wasm.go
CHANGED
|
@@ -47,7 +47,7 @@ func Route(path string, render RenderFunc) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
func initBody() {
|
|
50
|
-
body = &
|
|
50
|
+
body = &Element{
|
|
51
51
|
jsvalue: js.Window.Get("document").Get("body"),
|
|
52
52
|
tag: "body",
|
|
53
53
|
}
|
|
@@ -55,7 +55,7 @@ func initBody() {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
func initContent() {
|
|
58
|
-
content := &
|
|
58
|
+
content := &Element{
|
|
59
59
|
jsvalue: body.JSValue().Get("firstElementChild"),
|
|
60
60
|
tag: "div",
|
|
61
61
|
}
|
attributes.go
CHANGED
|
@@ -55,6 +55,18 @@ func Src(v string) Attribute {
|
|
|
55
55
|
return Attribute{"src", v}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
func ViewBox(v string) Attribute {
|
|
59
|
+
return Attribute{"viewBox", v}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
func X(v string) Attribute {
|
|
63
|
+
return Attribute{"x", v}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
func Y(v string) Attribute {
|
|
67
|
+
return Attribute{"y", v}
|
|
68
|
+
}
|
|
69
|
+
|
|
58
70
|
type CssAttribute struct {
|
|
59
71
|
classes string
|
|
60
72
|
}
|
|
@@ -99,7 +111,7 @@ type HelmetDescription string
|
|
|
99
111
|
type HelmetAuthor string
|
|
100
112
|
type HelmetKeywords string
|
|
101
113
|
|
|
102
|
-
func mergeAttributes(parent *
|
|
114
|
+
func mergeAttributes(parent *Element, uis ...interface{}) {
|
|
103
115
|
elems := []UI{}
|
|
104
116
|
for _, v := range uis {
|
|
105
117
|
switch c := v.(type) {
|
component.go
CHANGED
|
@@ -182,7 +182,7 @@ func (r RenderFunc) replaceRoot(n UI) error {
|
|
|
182
182
|
var parent UI
|
|
183
183
|
for {
|
|
184
184
|
parent = r.parent()
|
|
185
|
-
_, isElem := parent.(*
|
|
185
|
+
_, isElem := parent.(*Element)
|
|
186
186
|
if parent == nil || isElem {
|
|
187
187
|
break
|
|
188
188
|
}
|
element.go
CHANGED
|
@@ -7,7 +7,7 @@ import (
|
|
|
7
7
|
"github.com/pyros2097/wapp/js"
|
|
8
8
|
)
|
|
9
9
|
|
|
10
|
-
type
|
|
10
|
+
type Element struct {
|
|
11
11
|
attrs map[string]string
|
|
12
12
|
body []UI
|
|
13
13
|
events map[string]js.EventHandler
|
|
@@ -18,48 +18,48 @@ type elem struct {
|
|
|
18
18
|
this UI
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
func (e *
|
|
21
|
+
func (e *Element) JSValue() js.Value {
|
|
22
22
|
return e.jsvalue
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
func (e *
|
|
25
|
+
func (e *Element) Mounted() bool {
|
|
26
26
|
return e.self() != nil &&
|
|
27
27
|
e.jsvalue != nil
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
func (e *
|
|
30
|
+
func (e *Element) name() string {
|
|
31
31
|
return e.tag
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
func (e *
|
|
34
|
+
func (e *Element) self() UI {
|
|
35
35
|
return e.this
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
func (e *
|
|
38
|
+
func (e *Element) setSelf(n UI) {
|
|
39
39
|
e.this = n
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
func (e *
|
|
42
|
+
func (e *Element) attributes() map[string]string {
|
|
43
43
|
return e.attrs
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
func (e *
|
|
46
|
+
func (e *Element) eventHandlers() map[string]js.EventHandler {
|
|
47
47
|
return e.events
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
func (e *
|
|
50
|
+
func (e *Element) parent() UI {
|
|
51
51
|
return e.parentElem
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
func (e *
|
|
54
|
+
func (e *Element) setParent(p UI) {
|
|
55
55
|
e.parentElem = p
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
func (e *
|
|
58
|
+
func (e *Element) children() []UI {
|
|
59
59
|
return e.body
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
func (e *
|
|
62
|
+
func (e *Element) mount() error {
|
|
63
63
|
if e.Mounted() {
|
|
64
64
|
panic("mounting elem failed already mounted " + e.name())
|
|
65
65
|
}
|
|
@@ -87,7 +87,7 @@ func (e *elem) mount() error {
|
|
|
87
87
|
return nil
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
func (e *
|
|
90
|
+
func (e *Element) dismount() {
|
|
91
91
|
for _, c := range e.children() {
|
|
92
92
|
dismount(c)
|
|
93
93
|
}
|
|
@@ -99,7 +99,7 @@ func (e *elem) dismount() {
|
|
|
99
99
|
e.jsvalue = nil
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
func (e *
|
|
102
|
+
func (e *Element) update(n UI) error {
|
|
103
103
|
if !e.Mounted() {
|
|
104
104
|
return nil
|
|
105
105
|
}
|
|
@@ -157,7 +157,7 @@ func (e *elem) update(n UI) error {
|
|
|
157
157
|
return nil
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
-
func (e *
|
|
160
|
+
func (e *Element) appendChild(c UI, onlyJsValue bool) error {
|
|
161
161
|
if err := mount(c); err != nil {
|
|
162
162
|
panic("appending child failed child-name: " + c.name() + " name: " + e.name())
|
|
163
163
|
}
|
|
@@ -171,7 +171,7 @@ func (e *elem) appendChild(c UI, onlyJsValue bool) error {
|
|
|
171
171
|
return nil
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
func (e *
|
|
174
|
+
func (e *Element) replaceChildAt(idx int, new UI) error {
|
|
175
175
|
old := e.body[idx]
|
|
176
176
|
|
|
177
177
|
if err := mount(new); err != nil {
|
|
@@ -186,7 +186,7 @@ func (e *elem) replaceChildAt(idx int, new UI) error {
|
|
|
186
186
|
return nil
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
func (e *
|
|
189
|
+
func (e *Element) removeChildAt(idx int) error {
|
|
190
190
|
body := e.body
|
|
191
191
|
if idx < 0 || idx >= len(body) {
|
|
192
192
|
panic("removing child failed index out of range name: " + e.name() + " index: " + strconv.Itoa(idx))
|
|
@@ -204,7 +204,7 @@ func (e *elem) removeChildAt(idx int) error {
|
|
|
204
204
|
return nil
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
-
func (e *
|
|
207
|
+
func (e *Element) updateAttrs(attrs map[string]string) {
|
|
208
208
|
for k := range e.attrs {
|
|
209
209
|
if _, exists := attrs[k]; !exists {
|
|
210
210
|
e.delAttr(k)
|
|
@@ -223,7 +223,7 @@ func (e *elem) updateAttrs(attrs map[string]string) {
|
|
|
223
223
|
}
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
-
func (e *
|
|
226
|
+
func (e *Element) setAttr(k string, v string) {
|
|
227
227
|
if e.attrs == nil {
|
|
228
228
|
e.attrs = make(map[string]string)
|
|
229
229
|
}
|
|
@@ -253,16 +253,16 @@ func (e *elem) setAttr(k string, v string) {
|
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
-
func (e *
|
|
256
|
+
func (e *Element) setJsAttr(k, v string) {
|
|
257
257
|
e.JSValue().Call("setAttribute", k, v)
|
|
258
258
|
}
|
|
259
259
|
|
|
260
|
-
func (e *
|
|
260
|
+
func (e *Element) delAttr(k string) {
|
|
261
261
|
e.JSValue().Call("removeAttribute", k)
|
|
262
262
|
delete(e.attrs, k)
|
|
263
263
|
}
|
|
264
264
|
|
|
265
|
-
func (e *
|
|
265
|
+
func (e *Element) updateEventHandler(handlers map[string]js.EventHandler) {
|
|
266
266
|
for k, current := range e.events {
|
|
267
267
|
if _, exists := handlers[k]; !exists {
|
|
268
268
|
e.delJsEventHandler(k, current)
|
|
@@ -285,7 +285,7 @@ func (e *elem) updateEventHandler(handlers map[string]js.EventHandler) {
|
|
|
285
285
|
}
|
|
286
286
|
}
|
|
287
287
|
|
|
288
|
-
func (e *
|
|
288
|
+
func (e *Element) setEventHandler(k string, h js.EventHandlerFunc) {
|
|
289
289
|
if e.events == nil {
|
|
290
290
|
e.events = make(map[string]js.EventHandler)
|
|
291
291
|
}
|
|
@@ -293,7 +293,7 @@ func (e *elem) setEventHandler(k string, h js.EventHandlerFunc) {
|
|
|
293
293
|
e.events[k] = js.NewEventHandler(k, h)
|
|
294
294
|
}
|
|
295
295
|
|
|
296
|
-
func (e *
|
|
296
|
+
func (e *Element) setJsEventHandler(k string, h js.EventHandler) {
|
|
297
297
|
jshandler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
298
298
|
dispatch(func() {
|
|
299
299
|
if !e.self().Mounted() {
|
|
@@ -313,24 +313,24 @@ func (e *elem) setJsEventHandler(k string, h js.EventHandler) {
|
|
|
313
313
|
e.JSValue().Call("addEventListener", k, jshandler)
|
|
314
314
|
}
|
|
315
315
|
|
|
316
|
-
func (e *
|
|
316
|
+
func (e *Element) delJsEventHandler(k string, h js.EventHandler) {
|
|
317
317
|
e.JSValue().Call("removeEventListener", k, h.JSvalue)
|
|
318
318
|
h.JSvalue.Release()
|
|
319
319
|
delete(e.events, k)
|
|
320
320
|
}
|
|
321
321
|
|
|
322
|
-
func (e *
|
|
322
|
+
func (e *Element) setBody(body []UI) {
|
|
323
323
|
if e.selfClosing {
|
|
324
324
|
panic("setting html element body failed: self closing element can't have children" + e.name())
|
|
325
325
|
}
|
|
326
326
|
e.body = body
|
|
327
327
|
}
|
|
328
328
|
|
|
329
|
-
func (e *
|
|
329
|
+
func (e *Element) Html(w io.Writer) {
|
|
330
330
|
e.HtmlWithIndent(w, 0)
|
|
331
331
|
}
|
|
332
332
|
|
|
333
|
-
func (e *
|
|
333
|
+
func (e *Element) HtmlWithIndent(w io.Writer, indent int) {
|
|
334
334
|
writeIndent(w, indent)
|
|
335
335
|
w.Write(stob("<"))
|
|
336
336
|
w.Write(stob(e.tag))
|
html.go
CHANGED
|
@@ -2,29 +2,29 @@ package app
|
|
|
2
2
|
|
|
3
3
|
import "reflect"
|
|
4
4
|
|
|
5
|
-
func Html(elems ...UI) *
|
|
5
|
+
func Html(elems ...UI) *Element {
|
|
6
|
-
return &
|
|
6
|
+
return &Element{tag: "html", body: elems}
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
func Head(elems ...UI) *
|
|
9
|
+
func Head(elems ...UI) *Element {
|
|
10
10
|
basic := []UI{
|
|
11
|
-
&
|
|
11
|
+
&Element{tag: "meta", selfClosing: true, attrs: map[string]string{"charset": "UTF-8"}},
|
|
12
|
-
&
|
|
12
|
+
&Element{tag: "meta", selfClosing: true, attrs: map[string]string{"http-equiv": "Content-Type", "content": "text/html;charset=utf-8"}},
|
|
13
|
-
&
|
|
13
|
+
&Element{tag: "meta", selfClosing: true, attrs: map[string]string{"http-equiv": "encoding", "content": "utf-8"}},
|
|
14
14
|
}
|
|
15
|
-
return &
|
|
15
|
+
return &Element{tag: "head", body: append(basic, elems...)}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
func Body(elems ...UI) *
|
|
18
|
+
func Body(elems ...UI) *Element {
|
|
19
|
-
return &
|
|
19
|
+
return &Element{tag: "body", body: elems}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
func Title(v string) *
|
|
22
|
+
func Title(v string) *Element {
|
|
23
|
-
return &
|
|
23
|
+
return &Element{tag: "title", body: []UI{Text(v)}}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
func Meta(name, content string) *
|
|
26
|
+
func Meta(name, content string) *Element {
|
|
27
|
-
e := &
|
|
27
|
+
e := &Element{
|
|
28
28
|
tag: "meta",
|
|
29
29
|
selfClosing: true,
|
|
30
30
|
}
|
|
@@ -33,8 +33,8 @@ func Meta(name, content string) *elem {
|
|
|
33
33
|
return e
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
func Link(rel, href string) *
|
|
36
|
+
func Link(rel, href string) *Element {
|
|
37
|
-
e := &
|
|
37
|
+
e := &Element{
|
|
38
38
|
tag: "link",
|
|
39
39
|
selfClosing: true,
|
|
40
40
|
}
|
|
@@ -43,37 +43,49 @@ func Link(rel, href string) *elem {
|
|
|
43
43
|
return e
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
func Script(str string) *
|
|
46
|
+
func Script(str string) *Element {
|
|
47
|
-
return &
|
|
47
|
+
return &Element{
|
|
48
48
|
tag: "script",
|
|
49
49
|
body: []UI{Text(str)},
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
func Div(uis ...interface{}) *
|
|
53
|
+
func Div(uis ...interface{}) *Element {
|
|
54
|
-
e := &
|
|
54
|
+
e := &Element{tag: "div"}
|
|
55
55
|
mergeAttributes(e, uis...)
|
|
56
56
|
return e
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
func Input(uis ...interface{}) *
|
|
59
|
+
func Input(uis ...interface{}) *Element {
|
|
60
|
-
e := &
|
|
60
|
+
e := &Element{tag: "input"}
|
|
61
61
|
mergeAttributes(e, uis...)
|
|
62
62
|
return e
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
func Image(uis ...interface{}) *
|
|
65
|
+
func Image(uis ...interface{}) *Element {
|
|
66
|
-
e := &
|
|
66
|
+
e := &Element{tag: "image"}
|
|
67
67
|
mergeAttributes(e, uis...)
|
|
68
68
|
return e
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
func Button(uis ...interface{}) *
|
|
71
|
+
func Button(uis ...interface{}) *Element {
|
|
72
|
-
e := &
|
|
72
|
+
e := &Element{tag: "button"}
|
|
73
73
|
mergeAttributes(e, uis...)
|
|
74
74
|
return e
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
func Svg(elems ...interface{}) *Element {
|
|
78
|
+
e := &Element{tag: "svg"}
|
|
79
|
+
mergeAttributes(e, elems...)
|
|
80
|
+
return e
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
func SvgText(elems ...interface{}) *Element {
|
|
84
|
+
e := &Element{tag: "text"}
|
|
85
|
+
mergeAttributes(e, elems...)
|
|
86
|
+
return e
|
|
87
|
+
}
|
|
88
|
+
|
|
77
89
|
func Row(uis ...interface{}) UI {
|
|
78
90
|
return Div(append([]interface{}{Css("flex flex-row justify-center items-center")}, uis...)...)
|
|
79
91
|
}
|