D3vugu-components/router.go

107 lines
2.8 KiB
Go
Raw Normal View History

2023-05-08 18:43:07 +00:00
//go:build wasm
package main
import (
"log"
"git.d3nexus.de/Dadido3/D3vugu-components/components/navigation"
"git.d3nexus.de/Dadido3/D3vugu-components/components/overlay"
2023-05-08 18:43:07 +00:00
"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{
OverlayContainer: &overlay.Container{},
}
2023-05-08 18:43:07 +00:00
var pageInfo navigation.PageInfo
2023-05-08 18:43:07 +00:00
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)
}
if c, ok := b.(navigation.PageInfoSetter); ok {
c.PageInfoSet(&pageInfo)
}
if c, ok := b.(overlay.OverlayContainerSetter); ok {
c.OverlayContainerSet(root.OverlayContainer)
}
2023-05-08 18:43:07 +00:00
})
// Call wire function on root component.
2023-05-08 18:43:07 +00:00
buildEnv.WireComponent(root)
// Add routes.
router.MustAddRouteExact("/", vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = &PageHome{}
}))
router.MustAddRouteExact("/input/", vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = &PageInput{}
}))
router.MustAddRouteExact("/icons/", vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = &PageIcons{}
}))
router.MustAddRouteExact("/layout/", vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = &PageLayout{}
}))
router.MustAddRouteExact("/colors/", vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = &PageColors{}
}))
router.MustAddRouteExact("/overlays/", vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = &PageOverlays{}
}))
router.MustAddRouteExact("/user/", vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = &PageUser{}
}))
router.MustAddRouteExact("/settings/", vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = &PageSettings{}
}))
router.SetNotFound(vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = nil
}))
// add another route at the end that always runs and handles the page info
router.MustAddRoute("/", vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
pageInfo = navigation.NewPageInfo(rm.Path, root.Body)
}))
2023-05-08 18:43:07 +00:00
// 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
}