/
main.go
80 lines (69 loc) · 2.36 KB
/
main.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
package main
import (
"log"
"syscall/js"
"github.com/flosch/pongo2"
"github.com/pexarkh/weathassembly/consts"
"github.com/pexarkh/weathassembly/pongoutils"
"github.com/pexarkh/weathassembly/weather"
)
func main() {
js.Global().Set("WssNavToggleOnClick", js.FuncOf(WssNavToggleOnClick))
cb0 := js.FuncOf(WssSearchOnSubmit)
wssSearch := js.Global().Get("document").Call("getElementById", "WssSearch")
wssSearch.Set("onsubmit", cb0)
wssSearch.Set("onclick", cb0)
cb1 := js.FuncOf(WssCurrentLocation)
wssLoc := js.Global().Get("document").Call("getElementById", "WssCurrentLocation")
wssLoc.Set("onsubmit", cb1)
wssLoc.Set("onclick", cb1)
_ = pongo2.RegisterFilter("TopForecast", pongoutils.TopForecast)
_ = pongo2.RegisterFilter("TopTemperature", pongoutils.TopTemperature)
_ = pongo2.RegisterFilter("TopStartTime", pongoutils.TopStartTime)
forever := make(chan bool)
retrieveAndRenderWeb("10001", "")
<-forever
}
func retrieveAndRenderWeb(zip, ip string) {
pfc, err := weather.RetrieveForecastForPlace(zip, ip)
if err != nil {
log.Printf("ERROR: %s", err)
return
}
htmlView, err := weather.Render(consts.ForecastFragment, pfc)
if err != nil {
log.Printf("ERROR: %s", err)
return
}
js.Global().Get("document").Call("getElementById", "root").Set("innerHTML", htmlView)
}
func WssCurrentLocation(this js.Value, args []js.Value) interface{} {
log.Printf("WssCurrentLocation: this: %v", this)
log.Printf("WssCurrentLocation: args: %v", args)
go func() {
retrieveAndRenderWeb("10001", "")
}()
f := js.Global().Get("document").Call("querySelector", "#WssNav")
f.Get("classList").Call("toggle", "is-open")
return nil
}
func WssSearchOnSubmit(this js.Value, args []js.Value) interface{} {
log.Printf("WssSearchOnSubmit: this: %v", this)
log.Printf("WssSearchOnSubmit: args: %v", args)
wssZip := js.Global().Get("document").Call("getElementById", "WssZip")
if zip := wssZip.Get("value").String(); weather.IsValidZip(zip) {
go func() {
retrieveAndRenderWeb(zip, "")
}()
f := js.Global().Get("document").Call("querySelector", "#WssNav")
f.Get("classList").Call("toggle", "is-open")
} else {
js.Global().Call("alert", "ERROR: invalid zip: "+zip)
}
return nil
}
func WssNavToggleOnClick(this js.Value, args []js.Value) interface{} {
f := js.Global().Get("document").Call("querySelector", "#WssNav")
f.Get("classList").Call("toggle", "is-open")
return nil
}