From 740d14546406538be4d81b04bae5b39e2dcde5a0 Mon Sep 17 00:00:00 2001 From: Achim Rohn Date: Mon, 11 Aug 2025 22:34:50 +0200 Subject: [PATCH] Add ActivePath --- htmx_route.go | 35 +++++++++++++++++++++++++++++++---- i18n.go | 12 ++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/htmx_route.go b/htmx_route.go index dc2e3d2..0e83917 100644 --- a/htmx_route.go +++ b/htmx_route.go @@ -9,12 +9,28 @@ import ( type HtmxContext interface { Render(node gomponents.Node) GetLanguage() Language + GetActivePath() *ActivePath + HasActivePath() bool + SetActivePath(activePath *ActivePath) } type HtmxContextImpl struct { - req *http.Request - res http.ResponseWriter - language Language + req *http.Request + res http.ResponseWriter + 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 { @@ -48,6 +64,7 @@ type HtmxRoute interface { WithPathParams(params ...HtmxPathParam) HtmxRoute ToUrl(queryParams ...HtmxPathParam) string GetHtmx(queryParams ...HtmxPathParam) gomponents.Node + SetActivePath(activePath *ActivePath) HtmxRoute } type HtmxGetRoute struct { @@ -55,6 +72,12 @@ type HtmxGetRoute struct { RouteFunc HtmxRouteFunc PathParams []HtmxPathParam 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 { @@ -64,7 +87,11 @@ func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc, languages ...Language func (h HtmxGetRoute) Add(server *http.ServeMux) { for _, language := range h.Languages { 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) }) } } diff --git a/i18n.go b/i18n.go index feabfb8..8a61c18 100644 --- a/i18n.go +++ b/i18n.go @@ -69,3 +69,15 @@ func (t I18nText) GetKey() string { } return "" } + +type ActivePath struct { + I18nText + Path string +} + +func NewActivePath(path string, langMap map[Language]string) ActivePath { + return ActivePath{ + I18nText: NewI18nText(langMap), + Path: path, + } +}