Add ActivePath

This commit is contained in:
Achim Rohn
2025-08-11 22:34:50 +02:00
parent 37439305d3
commit 740d145464
2 changed files with 43 additions and 4 deletions
+31 -4
View File
@@ -9,12 +9,28 @@ import (
type HtmxContext interface { type HtmxContext interface {
Render(node gomponents.Node) Render(node gomponents.Node)
GetLanguage() Language GetLanguage() Language
GetActivePath() *ActivePath
HasActivePath() bool
SetActivePath(activePath *ActivePath)
} }
type HtmxContextImpl struct { type HtmxContextImpl struct {
req *http.Request req *http.Request
res http.ResponseWriter res http.ResponseWriter
language Language language Language
activePath *ActivePath
}
func (c *HtmxContextImpl) SetActivePath(activePath *ActivePath) {
c.activePath = activePath
}
func (c *HtmxContextImpl) GetActivePath() *ActivePath {
return c.activePath
}
func (c *HtmxContextImpl) HasActivePath() bool {
return c.activePath != nil
} }
func (c *HtmxContextImpl) GetLanguage() Language { func (c *HtmxContextImpl) GetLanguage() Language {
@@ -48,6 +64,7 @@ type HtmxRoute interface {
WithPathParams(params ...HtmxPathParam) HtmxRoute WithPathParams(params ...HtmxPathParam) HtmxRoute
ToUrl(queryParams ...HtmxPathParam) string ToUrl(queryParams ...HtmxPathParam) string
GetHtmx(queryParams ...HtmxPathParam) gomponents.Node GetHtmx(queryParams ...HtmxPathParam) gomponents.Node
SetActivePath(activePath *ActivePath) HtmxRoute
} }
type HtmxGetRoute struct { type HtmxGetRoute struct {
@@ -55,6 +72,12 @@ type HtmxGetRoute struct {
RouteFunc HtmxRouteFunc RouteFunc HtmxRouteFunc
PathParams []HtmxPathParam PathParams []HtmxPathParam
Languages []Language Languages []Language
ActivePath *ActivePath
}
func (h HtmxGetRoute) SetActivePath(activePath *ActivePath) HtmxRoute {
h.ActivePath = activePath
return h
} }
func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc, languages ...Language) *HtmxGetRoute { func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc, languages ...Language) *HtmxGetRoute {
@@ -64,7 +87,11 @@ func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc, languages ...Language
func (h HtmxGetRoute) Add(server *http.ServeMux) { func (h HtmxGetRoute) Add(server *http.ServeMux) {
for _, language := range h.Languages { for _, language := range h.Languages {
server.HandleFunc("GET /"+string(language)+h.Path, func(res http.ResponseWriter, req *http.Request) { server.HandleFunc("GET /"+string(language)+h.Path, func(res http.ResponseWriter, req *http.Request) {
h.RouteFunc(NewHtmxContext(req, res, language)) context := NewHtmxContext(req, res, language)
if h.ActivePath != nil {
context.SetActivePath(h.ActivePath)
}
h.RouteFunc(context)
}) })
} }
} }
+12
View File
@@ -69,3 +69,15 @@ func (t I18nText) GetKey() string {
} }
return "" return ""
} }
type ActivePath struct {
I18nText
Path string
}
func NewActivePath(path string, langMap map[Language]string) ActivePath {
return ActivePath{
I18nText: NewI18nText(langMap),
Path: path,
}
}