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

     1  package main
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/EngineerKamesh/gofullstack/volume4/section2/gopherface/client/handlers"
     7  
     8  	"github.com/EngineerKamesh/gofullstack/volume4/section2/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  	switch pageName {
    21  	case "feed":
    22  		handlers.InitializeFeedEventHandlers(env)
    23  	case "friends":
    24  		handlers.InitializeFriendsEventHandlers(env)
    25  	case "myprofile":
    26  		handlers.InitializeMyProfileEventHandlers(env)
    27  	}
    28  }
    29  
    30  func run() {
    31  	templateSetChannel := make(chan *isokit.TemplateSet)
    32  	go isokit.FetchTemplateBundle(templateSetChannel)
    33  	ts := <-templateSetChannel
    34  
    35  	env := common.Env{}
    36  	env.TemplateSet = ts
    37  	println("env.TemplateSet: ", env.TemplateSet)
    38  
    39  	env.Window = dom.GetWindow()
    40  	env.Document = dom.GetWindow().Document()
    41  	env.PrimaryContent = env.Document.GetElementByID("primaryContent")
    42  
    43  	r := isokit.NewRouter()
    44  	r.Handle("/feed", handlers.FeedHandler(&env))
    45  	r.Handle("/friends", handlers.FriendsHandler(&env))
    46  	r.Handle("/myprofile", handlers.MyProfileHandler(&env))
    47  	r.Listen()
    48  
    49  	initializeEventHandlers(&env)
    50  }
    51  
    52  func main() {
    53  	switch readyState := D.ReadyState(); readyState {
    54  	case "loading":
    55  		D.AddEventListener("DOMContentLoaded", false, func(dom.Event) {
    56  			go run()
    57  		})
    58  	case "interactive", "complete":
    59  		run()
    60  	default:
    61  		println("Unexpected document.ReadyState value!")
    62  	}
    63  }