github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume3/section4/gopherface/client/client.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/EngineerKamesh/gofullstack/volume3/section4/gopherface/client/handlers"
     7  
     8  	"github.com/EngineerKamesh/gofullstack/volume3/section4/gopherface/client/common"
     9  
    10  	"go.isomorphicgo.org/go/isokit"
    11  	"honnef.co/go/js/dom"
    12  )
    13  
    14  var D = dom.GetWindow().Document().(dom.HTMLDocument)
    15  
    16  func initializeEventHandlers(env *common.Env) {
    17  	println("location: ", env.Window.Location().Href)
    18  	l := strings.Split(env.Window.Location().Href, "/")
    19  	pageName := l[len(l)-1]
    20  
    21  	switch pageName {
    22  
    23  	}
    24  }
    25  
    26  func run() {
    27  	println("GopherFace is on the client-side, thanks to GopherJS!")
    28  
    29  	templateSetChannel := make(chan *isokit.TemplateSet)
    30  	go isokit.FetchTemplateBundle(templateSetChannel)
    31  	ts := <-templateSetChannel
    32  
    33  	env := common.Env{}
    34  	env.TemplateSet = ts
    35  	env.Window = dom.GetWindow()
    36  	env.Document = dom.GetWindow().Document()
    37  	env.PrimaryContent = env.Document.GetElementByID("primaryContent")
    38  
    39  	r := isokit.NewRouter()
    40  	r.Handle("/feed", handlers.FeedHandler(&env))
    41  	r.Handle("/friends", handlers.FriendsHandler(&env))
    42  	r.Handle("/profile", handlers.MyProfileHandler(&env))
    43  	r.Listen()
    44  
    45  	initializeEventHandlers(&env)
    46  
    47  }
    48  
    49  func main() {
    50  	switch readyState := D.ReadyState(); readyState {
    51  	case "loading":
    52  		D.AddEventListener("DOMContentLoaded", false, func(dom.Event) {
    53  			go run()
    54  		})
    55  	case "interactive", "complete":
    56  		run()
    57  	default:
    58  		println("Unexpected document.ReadyState value!")
    59  	}
    60  }