-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-animal.go
198 lines (184 loc) · 8 KB
/
random-animal.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
package main
// DO NOT EDIT: This file was generated by vugu. Please regenerate instead of editing or add additional code in a separate file.
import "fmt"
import "reflect"
import "github.com/vugu/vugu"
import "encoding/json"
import "net/http"
import "log"
import "path/filepath"
type RandomAnimalData struct {
Animal Animal
ContentType string
IsLoading bool
ClickCount int
Target string
Url string
}
type Animal struct {
Url string `json:"url"`
}
func (comp *RandomAnimal) NewData(props vugu.Props) (interface{}, error) {
ret := &RandomAnimalData{}
ret.Target, _ = props["target"].(string)
ret.Url, _ = props["url"].(string)
return ret, nil
}
func (data *RandomAnimalData) HandleClick(event *vugu.DOMEvent) {
data.ClickCount = data.ClickCount + 1
data.Animal = Animal{}
ee := event.EventEnv()
go func() {
ee.Lock()
data.IsLoading = true
ee.UnlockRender()
res, err := http.Get(data.Url)
if err != nil {
log.Printf("Error fetch()ing: %v", err)
return
}
defer res.Body.Close()
var a Animal
err = json.NewDecoder(res.Body).Decode(&a)
if err != nil {
log.Printf("Error JSON decoding: %v", err)
return
}
ee.Lock()
defer ee.UnlockRender()
data.Animal = a
e := filepath.Ext(data.Animal.Url)
data.ContentType = getSourceType(e)
data.IsLoading = false
}()
}
func getSourceType(e string) string {
switch e {
case ".jpeg", ".png", ".jpg", ".JPG":
return "image"
case ".mp4", ".gif":
return "video"
default:
return "image"
}
}
var _ vugu.ComponentType = (*RandomAnimal)(nil)
func (comp *RandomAnimal) BuildVDOM(dataI interface{}) (vdom *vugu.VGNode, css *vugu.VGNode, reterr error) {
data := dataI.(*RandomAnimalData)
_ = data
_ = fmt.Sprint
_ = reflect.Value{}
event := vugu.DOMEventStub
_ = event
css = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "style", DataAtom: vugu.VGAtom(458501), Namespace: "", Attr: []vugu.VGAttribute(nil)}
css.AppendChild(&vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n img, video {\n width: 500px;\n height: 500px;\n object-fit: cover;\n margin: 50px 0;\n }\n button {\n font-size: 5em;\n font-weight: bold;\n margin: 50px 0;\n padding: 10px 30px;\n background-color: lightsalmon;\n color: #fff;\n border-style: none;\n }\n button:hover {\n background-color: darksalmon;\n color: #fff;\n cursor: pointer;\n }\n .random-animal {\n text-align: center;\n }\n .message {\n font-size: 4em;\n }\n", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)})
var n *vugu.VGNode
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "div", DataAtom: vugu.VGAtom(92931), Namespace: "", Attr: []vugu.VGAttribute{vugu.VGAttribute{Namespace: "", Key: "class", Val: "random-animal"}}}
vdom = n
{
parent := n
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "div", DataAtom: vugu.VGAtom(92931), Namespace: "", Attr: []vugu.VGAttribute{vugu.VGAttribute{Namespace: "", Key: "class", Val: "message"}}}
parent.AppendChild(n)
{
parent := n
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "どんな子がくるかな?", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
}
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "button", DataAtom: vugu.VGAtom(102662), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
// @click = { data.HandleClick(event) }
{
var i_ interface{} = data
idat_ := reflect.ValueOf(&i_).Elem().InterfaceData()
var i2_ interface{} = data.HandleClick
i2dat_ := reflect.ValueOf(&i2_).Elem().InterfaceData()
n.SetDOMEventHandler("click", vugu.DOMEventHandler{
ReceiverAndMethodHash: uint64(idat_[0]) ^ uint64(idat_[1]) ^ uint64(i2dat_[0]) ^ uint64(i2dat_[1]),
Method: reflect.ValueOf(data).MethodByName("HandleClick"),
Args: []interface{}{event},
})
}
if false {
// force compiler to check arguments for type safety
data.HandleClick(event)
}
{
parent := n
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "span", DataAtom: vugu.VGAtom(40708), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
n.InnerHTML = fmt.Sprint(data.Target)
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "げっと\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
}
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
if data.ClickCount > 0 {
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "div", DataAtom: vugu.VGAtom(92931), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
{
parent := n
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "span", DataAtom: vugu.VGAtom(40708), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
n.InnerHTML = fmt.Sprint(data.ClickCount)
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "回目\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
}
}
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
if data.IsLoading {
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "div", DataAtom: vugu.VGAtom(92931), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
{
parent := n
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "Loading...", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
}
}
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
if len(data.Animal.Url) > 0 {
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "div", DataAtom: vugu.VGAtom(92931), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
{
parent := n
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
if data.ContentType == "image" {
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "img", DataAtom: vugu.VGAtom(198403), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
n.Props = vugu.Props{
"src": data.Animal.Url,
}
}
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
if data.ContentType == "video" {
n = &vugu.VGNode{Type: vugu.VGNodeType(3), Data: "video", DataAtom: vugu.VGAtom(192773), Namespace: "", Attr: []vugu.VGAttribute{vugu.VGAttribute{Namespace: "", Key: "controls", Val: ""}}}
parent.AppendChild(n)
n.Props = vugu.Props{
"src": data.Animal.Url,
}
{
parent := n
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n ", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
}
}
}
}
n = &vugu.VGNode{Type: vugu.VGNodeType(1), Data: "\n", DataAtom: vugu.VGAtom(0), Namespace: "", Attr: []vugu.VGAttribute(nil)}
parent.AppendChild(n)
}
return
}
type RandomAnimal struct {}
func init() { vugu.RegisterComponentType("random-animal", &RandomAnimal{}) }