D3vugu-components/router.go
David Vogel e722ae1d2b Add pagination component
- Add and update example pages
- Add example address component
- Add vugu data tags to struct fields
- Fix some button padding values
- Remove debug log line
- Fix background color of the sidebar menu
2023-05-16 18:10:24 +02:00

89 lines
2.3 KiB
Go

//go:build wasm
package main
import (
"log"
"git.d3nexus.de/Dadido3/D3vugu-components/components/navigation"
"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{}
var pageInfo navigation.PageInfo
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)
}
})
// Call wire function on root component.
buildEnv.WireComponent(root)
// Add routes.
router.MustAddRouteExact("/", vgrouter.RouteHandlerFunc(func(rm *vgrouter.RouteMatch) {
root.Body = &PageHome{}
}))
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("/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)
}))
// 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
}