You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.6 KiB
Go

// Copyright (c) 2021 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
//go:build wasm
package main
import (
"log"
"github.com/vugu/vgrouter"
"github.com/vugu/vugu"
)
// urlPathPrefix contains the path prefix for the router.
//
// If the application's URL is `example.com/suburl/`, use `go build -ldflags="-X 'main.urlPathPrefix=/suburl'"`.
var urlPathPrefix = ""
// vuguSetup sets up overall wiring and routing.
func vuguSetup(buildEnv *vugu.BuildEnv, eventEnv vugu.EventEnv) vugu.Builder {
// Create new router instance.
router := vgrouter.New(eventEnv)
// Set prefix if available.
router.SetPathPrefix(urlPathPrefix)
// Create root object.
root := &Root{}
buildEnv.SetWireFunc(func(b vugu.Builder) {
// Make our wire function populate anything that wants a "Navigator".
if c, ok := b.(vgrouter.NavigatorSetter); ok {
c.NavigatorSet(router)
}
})
// Wire the root component. (Not sure if that is really needed)
buildEnv.WireComponent(root)
// Add routes.
router.MustAddRouteExact("/",
vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = nil
}))
router.MustAddRouteExact("/icons/",
vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = &PageIcons{}
}))
router.SetNotFound(vgrouter.RouteHandlerFunc(
func(rm *vgrouter.RouteMatch) {
root.Body = nil
}))
// Tell the router to listen to the browser changing URLs.
err := router.ListenForPopState()
if err != nil {
log.Panic(err)
}
// Grab the current browser URL and process it as a route.
err = router.Pull()
if err != nil {
log.Panic(err)
}
return root
}