7c69f163b6
This reverts commit a1b93c0eba.
165 lines
3.6 KiB
Go
165 lines
3.6 KiB
Go
package ersteller_lib
|
|
|
|
import (
|
|
"github.com/labstack/echo/v4"
|
|
. "maragu.dev/gomponents"
|
|
. "maragu.dev/gomponents/html"
|
|
)
|
|
|
|
const DeIndexUrl = "/de/index"
|
|
const EnIndexUrl = "/en/index"
|
|
const DefaultLanguage = En
|
|
|
|
type NavItem struct {
|
|
Label string
|
|
Url string
|
|
}
|
|
|
|
type WebsiteMetaData struct {
|
|
AppTitle string
|
|
Title string
|
|
Lang Language
|
|
Description string
|
|
NavItems []NavItem
|
|
}
|
|
|
|
type PageWebsiteMetaData struct {
|
|
AppTitle string
|
|
Title I18nText
|
|
Lang Language
|
|
Description I18nText
|
|
NavItems []ActivePath
|
|
FooterNavItems []ActivePath
|
|
AllRoutes []HtmxRoute
|
|
ScriptSrcs []string
|
|
StyleSrcs []string
|
|
ActiveNavPath string
|
|
HideNavigation bool
|
|
UserEmail string
|
|
}
|
|
|
|
type CreatePageFunc func(c echo.Context, metadata PageWebsiteMetaData, content ...Node) error
|
|
|
|
type CreateHtmxPageFunc func(req HtmxContext, metadata PageWebsiteMetaData, content ...Node)
|
|
|
|
func AddLanguageSelectScript(metadata PageWebsiteMetaData) Node {
|
|
script := InlineTemplate(`
|
|
(function() {
|
|
const langs = {
|
|
de: "$.DeUrl$",
|
|
en: "$.EnUrl$",
|
|
};
|
|
const currentLang = "$.CurrentLang$";
|
|
const defaultLang = "$.DefaultLang$";
|
|
selectWebsiteLanguage(currentLang, langs, defaultLang);
|
|
})();`, struct {
|
|
CurrentLang string
|
|
DeUrl string
|
|
EnUrl string
|
|
DefaultLang string
|
|
}{
|
|
CurrentLang: string(metadata.Lang),
|
|
DeUrl: DeIndexUrl,
|
|
EnUrl: EnIndexUrl,
|
|
DefaultLang: string(DefaultLanguage),
|
|
})
|
|
return Script(Type("text/javascript"), Raw(script))
|
|
}
|
|
|
|
func GetNav(c HtmxContext, metadata PageWebsiteMetaData) Node {
|
|
// Create navigation items from metadata
|
|
navItems := Map(metadata.NavItems, func(path ActivePath) Node {
|
|
return CreateNavItem(c, path)
|
|
})
|
|
|
|
// Check if user is authenticated and add logout button if so
|
|
isAuthenticated, authCtx := c.GetAuthContext()
|
|
if isAuthenticated {
|
|
logoutButton := A(
|
|
Href("/logout"),
|
|
Text("Logout ("+authCtx.Email+")"),
|
|
Class("logout-button"),
|
|
)
|
|
navItems = append(navItems, logoutButton)
|
|
}
|
|
|
|
return Nav(Class("nav"), Aria("label", "Main Menu"),
|
|
Group(navItems),
|
|
)
|
|
}
|
|
|
|
func CreateNavItem(c HtmxContext, activePath ActivePath) Node {
|
|
isActive := activePath.IsActive(c)
|
|
return A(
|
|
Href(activePath.GetPath(c.GetLanguage())),
|
|
Text(activePath.From(c)),
|
|
If(isActive, Attr("aria-current", "page")),
|
|
If(isActive, Class("selected")),
|
|
)
|
|
}
|
|
|
|
func GetLanguageSwitcher(c HtmxContext, metadata PageWebsiteMetaData) Node {
|
|
currentLang := c.GetLanguage()
|
|
|
|
var currentRoute HtmxRoute
|
|
for _, route := range c.GetAllRoutes() {
|
|
if route.IsCurrentRoute(c) {
|
|
currentRoute = route
|
|
break
|
|
}
|
|
}
|
|
|
|
return Div(Class("language-switcher"),
|
|
CreateLanguageButton(En, currentLang, currentRoute, c),
|
|
CreateLanguageButton(De, currentLang, currentRoute, c),
|
|
)
|
|
}
|
|
|
|
func GetFooterMenu(c HtmxContext, metadata PageWebsiteMetaData) Node {
|
|
lang := c.GetLanguage()
|
|
return Div(
|
|
Map(metadata.FooterNavItems, func(path ActivePath) Node {
|
|
return Span(
|
|
A(Href(path.GetPath(lang)), Class("footer-link"), Text(path.From(c))),
|
|
)
|
|
}),
|
|
)
|
|
}
|
|
|
|
func CreateLanguageButton(lang Language, currentLang Language, route HtmxRoute, c HtmxContext) Node {
|
|
isActive := lang == currentLang
|
|
var href string
|
|
|
|
if route != nil {
|
|
href = route.ToUrlFromContext(c, lang)
|
|
} else {
|
|
// Fallback to index page if no active path found
|
|
if lang == En {
|
|
href = EnIndexUrl
|
|
} else {
|
|
href = DeIndexUrl
|
|
}
|
|
}
|
|
|
|
var langText string
|
|
if lang == En {
|
|
langText = "EN"
|
|
} else {
|
|
langText = "DE"
|
|
}
|
|
|
|
// Combine classes properly
|
|
var classes string
|
|
if isActive {
|
|
classes = "language-button active"
|
|
} else {
|
|
classes = "language-button inactive"
|
|
}
|
|
|
|
return A(
|
|
Href(href),
|
|
Text(langText),
|
|
Class(classes),
|
|
)
|
|
}
|