Add ActivePath
This commit is contained in:
+28
-1
@@ -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
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user