Add ActivePath as NavItems

This commit is contained in:
Achim Rohn
2025-08-11 23:25:44 +02:00
parent 740d145464
commit 96013fed62
3 changed files with 18 additions and 1 deletions
+6
View File
@@ -12,6 +12,7 @@ type HtmxContext interface {
GetActivePath() *ActivePath GetActivePath() *ActivePath
HasActivePath() bool HasActivePath() bool
SetActivePath(activePath *ActivePath) SetActivePath(activePath *ActivePath)
GetPath() string
} }
type HtmxContextImpl struct { type HtmxContextImpl struct {
@@ -21,6 +22,10 @@ type HtmxContextImpl struct {
activePath *ActivePath activePath *ActivePath
} }
func (c *HtmxContextImpl) GetPath() string {
return c.req.URL.Path
}
func (c *HtmxContextImpl) SetActivePath(activePath *ActivePath) { func (c *HtmxContextImpl) SetActivePath(activePath *ActivePath) {
c.activePath = activePath c.activePath = activePath
} }
@@ -65,6 +70,7 @@ type HtmxRoute interface {
ToUrl(queryParams ...HtmxPathParam) string ToUrl(queryParams ...HtmxPathParam) string
GetHtmx(queryParams ...HtmxPathParam) gomponents.Node GetHtmx(queryParams ...HtmxPathParam) gomponents.Node
SetActivePath(activePath *ActivePath) HtmxRoute SetActivePath(activePath *ActivePath) HtmxRoute
Add(server *http.ServeMux)
} }
type HtmxGetRoute struct { type HtmxGetRoute struct {
+11
View File
@@ -1,5 +1,7 @@
package ersteller_lib package ersteller_lib
import "strings"
type Language string type Language string
const ( const (
@@ -81,3 +83,12 @@ func NewActivePath(path string, langMap map[Language]string) ActivePath {
Path: path, Path: path,
} }
} }
func (a ActivePath) GetPath(language Language) string {
return "/" + string(language) + a.Path
}
func (a ActivePath) IsActive(c HtmxContext) bool {
pathWithLang := a.GetPath(c.GetLanguage())
return strings.HasPrefix(c.GetPath(), pathWithLang)
}
+1 -1
View File
@@ -23,7 +23,7 @@ type PageWebsiteMetaData struct {
Title string Title string
Lang Language Lang Language
Description string Description string
NavItems []NavItem NavItems []ActivePath
ScriptSrcs []string ScriptSrcs []string
StyleSrcs []string StyleSrcs []string
ActiveNavPath string ActiveNavPath string