-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarkup.go
More file actions
108 lines (85 loc) · 1.74 KB
/
markup.go
File metadata and controls
108 lines (85 loc) · 1.74 KB
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
package spago
import (
"fmt"
"syscall/js"
)
type none struct{}
func (m none) apply(n *Node) {}
// If ...
func If(cond bool, m Markup) Markup {
if cond {
return m
}
return none{}
}
// attribute attribute
type attribute struct {
Key string
Value interface{}
}
func (a attribute) apply(n *Node) {
n.attributes = append(n.attributes, a)
}
// A attribute markup
func A(k string, v interface{}) Markup {
return attribute{Key: k, Value: v}
}
// AttrMap map sttyle attribute markup
type AttrMap map[string]interface{}
func (a AttrMap) apply(n *Node) {
for k, v := range a {
attribute{k, v}.apply(n)
}
}
// listener ...
type listener struct {
Name string
Func func(ev js.Value)
}
func (e listener) apply(n *Node) {
n.listeners = append(n.listeners, e)
}
// Event event markup
func Event(name string, fn func(ev js.Value)) Markup {
return &listener{name, fn}
}
// Markups List of Markup type
type Markups []Markup
func (c Markups) apply(n *Node) {
for _, v := range c {
v.apply(n)
}
}
type components []Component
func (c components) apply(n *Node) {
for _, v := range c {
n.children = append(n.children, v)
}
}
// C make Components
func C(c ...Component) Markup {
return components(c)
}
// S make string from Stringer objects
func S(s ...interface{}) string {
return fmt.Sprint(s...)
}
type unsafeHTML struct {
Core
content string
}
func (m *unsafeHTML) apply(n *Node) {
n.children = append(n.children, m)
}
func (m *unsafeHTML) html(bind bool) js.Value {
div := document.Call("createElement", "div")
div.Set("innerHTML", m.content)
return div.Get("childNodes")
}
func (m *unsafeHTML) Render() HTML {
return m
}
// UnsafeHTML make DOM-Elements from HTML string
func UnsafeHTML(s string) Markup {
return &unsafeHTML{content: s}
}