/
client.go
60 lines (45 loc) · 1.39 KB
/
client.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
package main
import (
"strings"
"github.com/EngineerKamesh/gofullstack/volume3/section4/gopherface/client/handlers"
"github.com/EngineerKamesh/gofullstack/volume3/section4/gopherface/client/common"
"go.isomorphicgo.org/go/isokit"
"honnef.co/go/js/dom"
)
var D = dom.GetWindow().Document().(dom.HTMLDocument)
func initializeEventHandlers(env *common.Env) {
println("location: ", env.Window.Location().Href)
l := strings.Split(env.Window.Location().Href, "/")
pageName := l[len(l)-1]
switch pageName {
}
}
func run() {
println("GopherFace is on the client-side, thanks to GopherJS!")
templateSetChannel := make(chan *isokit.TemplateSet)
go isokit.FetchTemplateBundle(templateSetChannel)
ts := <-templateSetChannel
env := common.Env{}
env.TemplateSet = ts
env.Window = dom.GetWindow()
env.Document = dom.GetWindow().Document()
env.PrimaryContent = env.Document.GetElementByID("primaryContent")
r := isokit.NewRouter()
r.Handle("/feed", handlers.FeedHandler(&env))
r.Handle("/friends", handlers.FriendsHandler(&env))
r.Handle("/profile", handlers.MyProfileHandler(&env))
r.Listen()
initializeEventHandlers(&env)
}
func main() {
switch readyState := D.ReadyState(); readyState {
case "loading":
D.AddEventListener("DOMContentLoaded", false, func(dom.Event) {
go run()
})
case "interactive", "complete":
run()
default:
println("Unexpected document.ReadyState value!")
}
}