1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-04 18:19:54 +00:00

Categories, views: Give the card view its own file, make title a block

This commit is contained in:
Timur Ismagilov 2022-04-02 00:35:40 +03:00
parent cbf416d7bb
commit 686c329615
7 changed files with 58 additions and 84 deletions

View File

@ -1,38 +0,0 @@
{{define "category x"}}Category {{. | beautifulName}}{{end}}
{{define "category list heading"}}Category list{{end}}
{{define "category card"}}
{{$hyphaName := .HyphaName}}
{{$givenPermission := .GivenPermissionToModify}}
<aside class="layout-card categories-card">
<h2 class="layout-card__title">{{block `categories` .}}Categories{{end}}</h2>
<ul class="categories-card__entries">
{{range .Categories}}
<li class="categories-card__entry">
<a class="categories-card__link" href="/category/{{.}}">{{beautifulName .}}</a>
<form method="POST" action="/remove-from-category" class="categories-card__remove-form">
<input type="hidden" name="cat" value="{{.}}">
<input type="hidden" name="hypha" value="{{$hyphaName}}">
<input type="hidden" name="redirect-to" value="/hypha/{{$hyphaName}}">
{{if $givenPermission}}
<input type="submit" value="x" class="btn categories-card__btn"
title="{{block `remove from category title` .}}Remove the hypha from this category{{end}}">
{{end}}
</form>
</li>
{{end}}
{{if .GivenPermissionToModify}}
<li class="categories-card__entry categories-card__add-to-cat">
<form method="POST" action="/add-to-category" class="categories-card__add-form">
<input type="text" name="cat" id="_cat-name"
placeholder="{{block `placeholder` .}}Category name...{{end}}">
<input type="hidden" name="hypha" value="{{$hyphaName}}">
<input type="hidden" name="redirect-to" value="/hypha/{{$hyphaName}}">
<input type="submit" class="btn categories-card__btn" value="+"
title="{{block `add to category title` .}}Add the hypha to this category{{end}}">
</form>
</li>
{{end}}
</ul>
</aside>
{{end}}

35
categories/view_card.html Normal file
View File

@ -0,0 +1,35 @@
{{define "category card"}}
{{$hyphaName := .HyphaName}}
{{$givenPermission := .GivenPermissionToModify}}
<aside class="layout-card categories-card">
<h2 class="layout-card__title">{{block `categories` .}}Categories{{end}}</h2>
<ul class="categories-card__entries">
{{range .Categories}}
<li class="categories-card__entry">
<a class="categories-card__link" href="/category/{{.}}">{{beautifulName .}}</a>
<form method="POST" action="/remove-from-category" class="categories-card__remove-form">
<input type="hidden" name="cat" value="{{.}}">
<input type="hidden" name="hypha" value="{{$hyphaName}}">
<input type="hidden" name="redirect-to" value="/hypha/{{$hyphaName}}">
{{if $givenPermission}}
<input type="submit" value="x" class="btn categories-card__btn"
title="{{block `remove from category title` .}}Remove the hypha from this category{{end}}">
{{end}}
</form>
</li>
{{end}}
{{if .GivenPermissionToModify}}
<li class="categories-card__entry categories-card__add-to-cat">
<form method="POST" action="/add-to-category" class="categories-card__add-form">
<input type="text" name="cat" id="_cat-name"
placeholder="{{block `placeholder` .}}Category name...{{end}}">
<input type="hidden" name="hypha" value="{{$hyphaName}}">
<input type="hidden" name="redirect-to" value="/hypha/{{$hyphaName}}">
<input type="submit" class="btn categories-card__btn" value="+"
title="{{block `add to category title` .}}Add the hypha to this category{{end}}">
</form>
</li>
{{end}}
</ul>
</aside>
{{end}}

View File

@ -1,6 +1,7 @@
{{define "title"}}Category list{{end}}
{{define "body"}}
<main class="main-width category-list">
<h1>{{block `category list heading` .}}Category list{{end}}</h1>
<h1>{{template "title"}}</h1>
{{if len .Categories}}
<ul class="category-list__entries">
{{range .Categories}}

View File

@ -1,3 +1,4 @@
{{define "title"}}Category {{.CatName | beautifulName}}{{end}}
{{define "body"}}
{{$catName := .CatName}}
<main class="main-width category">

View File

@ -3,7 +3,6 @@ package categories
import (
"embed"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/viewutil"
"log"
"strings"
@ -26,27 +25,22 @@ const categoriesRu = `
var (
//go:embed *.html
fs embed.FS
m = template.Must
baseEn, baseRu *template.Template
viewListChain, viewPageChain viewutil.Chain
categoryTemplatesEn *template.Template
categoryTemplatesRu *template.Template
fs embed.FS
m = template.Must
baseEn, baseRu *template.Template
viewListChain, viewPageChain, viewCardChain viewutil.Chain
)
func prepareViews() {
categoryTemplatesEn = template.Must(template.
New("category").
Funcs(
template.FuncMap{
"beautifulName": util.BeautifulName,
}).
ParseFS(fs, "categories.html"))
categoryTemplatesRu = template.Must(template.Must(categoryTemplatesEn.Clone()).Parse(categoriesRu))
baseEn = m(viewutil.BaseEn.Clone())
baseRu = m(viewutil.BaseRu.Clone())
viewCardChain = viewutil.
En(
m(m(baseEn.Clone()).ParseFS(fs, "view_card.html"))).
Ru(
m(m(m(baseRu.Clone()).ParseFS(fs, "view_card.html")).Parse(categoriesRu)))
viewListChain = viewutil.
En(
m(m(baseEn.Clone()).ParseFS(fs, "view_list.html"))).
@ -59,35 +53,15 @@ func prepareViews() {
m(m(m(baseRu.Clone()).ParseFS(fs, "view_page.html")).Parse(categoriesRu)))
}
func localizedCatTemplates(meta viewutil.Meta) *template.Template {
if meta.Lc.Locale == "ru" {
return categoryTemplatesRu
}
return categoryTemplatesEn
}
func localizedCatTemplateAsString(meta viewutil.Meta, name string, datum ...interface{}) string {
var buf strings.Builder
var err error
if len(datum) == 1 {
err = localizedCatTemplates(meta).ExecuteTemplate(&buf, name, datum[0])
} else {
err = localizedCatTemplates(meta).ExecuteTemplate(&buf, name, nil)
}
if err != nil {
log.Println(err)
return ""
}
return buf.String()
type cardData struct {
HyphaName string
Categories []string
GivenPermissionToModify bool
}
func CategoryCard(meta viewutil.Meta, hyphaName string) string {
var buf strings.Builder
err := localizedCatTemplates(meta).ExecuteTemplate(&buf, "category card", struct {
HyphaName string
Categories []string
GivenPermissionToModify bool
}{
err := viewCardChain.Get(meta).ExecuteTemplate(&buf, "category card", cardData{
hyphaName,
WithHypha(hyphaName),
meta.U.CanProceed("add-to-category"),
@ -109,7 +83,6 @@ func categoryPage(meta viewutil.Meta, catName string) {
if err := viewPageChain.Get(meta).ExecuteTemplate(meta.W, "page", pageData{
BaseData: viewutil.BaseData{
Meta: meta,
Title: localizedCatTemplateAsString(meta, "category x", catName),
HeaderLinks: cfg.HeaderLinks,
CommonScripts: cfg.CommonScripts,
},
@ -130,7 +103,6 @@ func categoryList(meta viewutil.Meta) {
if err := viewListChain.Get(meta).ExecuteTemplate(meta.W, "page", listData{
BaseData: viewutil.BaseData{
Meta: meta,
Title: localizedCatTemplateAsString(meta, "category list heading"),
HeaderLinks: cfg.HeaderLinks,
CommonScripts: cfg.CommonScripts,
},

View File

@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{.Title}}</title>
<title>{{block "title" .}}{{end}}</title>
<link rel="shortcut icon" href="/static/favicon.ico">
<link rel="stylesheet" href="/static/style.css">
<script src="/static/shortcuts.js"></script>

View File

@ -52,15 +52,18 @@ func localizedBaseWithWeirdBody(meta Meta) *template.Template {
}
return BaseEn
}()
return m(m(t.Clone()).Parse(`{{define "body"}}{{.Body}}{{end}}`))
return m(m(t.Clone()).Parse(`
{{define "body"}}{{.Body}}{{end}}
{{define "title"}}{{.Title}}{{end}}
`))
}
type BaseData struct {
Meta Meta
Title string
HeadElements []string
HeaderLinks []cfg.HeaderLink
CommonScripts []string
Title string // TODO: remove
Body string // TODO: remove
}