definition of header menu

This commit is contained in:
Niko Abeler 2022-12-03 16:21:51 +01:00
parent c3563ce5ab
commit 1f0856602a
4 changed files with 57 additions and 1 deletions

View File

@ -88,6 +88,17 @@
</div> </div>
</div> </div>
<nav>
<ul>
{{ range $link := .User.Config.HeaderMenu }}
{{ if $link.List }}
<li><a href="{{ listUrl $.User $link.List }}">{{ $link.Title }}</a></li>
{{ else }}
<li><a href="{{ $link.Url }}">{{ $link.Title }}</a></li>
{{ end }}
{{ end }}
</ul>
</nav>
</div> </div>
</header> </header>
<main class="container"> <main class="container">

View File

@ -49,6 +49,12 @@ func noescape(str string) template.HTML {
return template.HTML(str) return template.HTML(str)
} }
func listUrl(user User, id string) string {
return user.ListUrl(PostList{
Id: id,
})
}
func renderEmbedTemplate(templateFile string, data interface{}) (string, error) { func renderEmbedTemplate(templateFile string, data interface{}) (string, error) {
templateStr, err := embed_files.ReadFile(templateFile) templateStr, err := embed_files.ReadFile(templateFile)
if err != nil { if err != nil {
@ -60,6 +66,7 @@ func renderEmbedTemplate(templateFile string, data interface{}) (string, error)
func renderTemplateStr(templateStr []byte, data interface{}) (string, error) { func renderTemplateStr(templateStr []byte, data interface{}) (string, error) {
t, err := template.New("_").Funcs(template.FuncMap{ t, err := template.New("_").Funcs(template.FuncMap{
"noescape": noescape, "noescape": noescape,
"listUrl": listUrl,
}).Parse(string(templateStr)) }).Parse(string(templateStr))
if err != nil { if err != nil {
return "", err return "", err
@ -74,7 +81,10 @@ func renderTemplateStr(templateStr []byte, data interface{}) (string, error) {
func renderIntoBaseTemplate(user User, data PageContent) (string, error) { func renderIntoBaseTemplate(user User, data PageContent) (string, error) {
baseTemplate, _ := user.Template() baseTemplate, _ := user.Template()
t, err := template.New("index").Parse(baseTemplate) t, err := template.New("index").Funcs(template.FuncMap{
"noescape": noescape,
"listUrl": listUrl,
}).Parse(baseTemplate)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -406,3 +406,25 @@ func TestRenderUserAuthHiddenFields(t *testing.T) {
assertions.AssertContains(t, result, "name=\"response_type\" value=\"code\"") assertions.AssertContains(t, result, "name=\"response_type\" value=\"code\"")
assertions.AssertContains(t, result, "name=\"state\" value=\"teststate\"") assertions.AssertContains(t, result, "name=\"state\" value=\"teststate\"")
} }
func TestRenderHeaderMenuListItem(t *testing.T) {
user := getTestUser()
user.AddHeaderMenuItem(owl.MenuItem{
Title: "Test Entry",
List: "test",
})
result, _ := owl.RenderIndexPage(user)
assertions.AssertContains(t, result, "Test Entry")
assertions.AssertContains(t, result, "/lists/test")
}
func TestRenderHeaderMenuUrlItem(t *testing.T) {
user := getTestUser()
user.AddHeaderMenuItem(owl.MenuItem{
Title: "Test Entry",
Url: "https://example.com",
})
result, _ := owl.RenderIndexPage(user)
assertions.AssertContains(t, result, "Test Entry")
assertions.AssertContains(t, result, "https://example.com")
}

13
user.go
View File

@ -28,6 +28,7 @@ type UserConfig struct {
PassworHash string `yaml:"password_hash"` PassworHash string `yaml:"password_hash"`
Lists []PostList `yaml:"lists"` Lists []PostList `yaml:"lists"`
PrimaryListInclude []string `yaml:"primary_list_include"` PrimaryListInclude []string `yaml:"primary_list_include"`
HeaderMenu []MenuItem `yaml:"header_menu"`
} }
type PostList struct { type PostList struct {
@ -36,6 +37,12 @@ type PostList struct {
Include []string `yaml:"include"` Include []string `yaml:"include"`
} }
type MenuItem struct {
Title string `yaml:"title"`
List string `yaml:"list"`
Url string `yaml:"url"`
}
func (l *PostList) ContainsType(t string) bool { func (l *PostList) ContainsType(t string) bool {
for _, t2 := range l.Include { for _, t2 := range l.Include {
if t2 == t { if t2 == t {
@ -390,6 +397,12 @@ func (user User) AddPostList(list PostList) error {
return user.SetConfig(config) return user.SetConfig(config)
} }
func (user User) AddHeaderMenuItem(link MenuItem) error {
config := user.Config()
config.HeaderMenu = append(config.HeaderMenu, link)
return user.SetConfig(config)
}
func (user User) ResetPassword(password string) error { func (user User) ResetPassword(password string) error {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 10) bytes, err := bcrypt.GenerateFromPassword([]byte(password), 10)
if err != nil { if err != nil {