2022-04-01 20:52:56 +00:00
|
|
|
|
package categories
|
2022-03-19 20:57:33 +00:00
|
|
|
|
|
|
|
|
|
import (
|
2022-04-01 20:52:56 +00:00
|
|
|
|
"embed"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
2022-03-19 20:57:33 +00:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
2022-03-29 20:59:36 +00:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/viewutil"
|
2022-03-19 20:57:33 +00:00
|
|
|
|
"log"
|
|
|
|
|
"strings"
|
2022-04-01 20:52:56 +00:00
|
|
|
|
"text/template" // TODO: Fight
|
2022-03-19 20:57:33 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-03-20 19:24:40 +00:00
|
|
|
|
const categoriesRu = `
|
|
|
|
|
{{define "empty cat"}}Эта категория пуста.{{end}}
|
|
|
|
|
{{define "add hypha"}}Добавить в категорию{{end}}
|
|
|
|
|
{{define "cat"}}Категория{{end}}
|
|
|
|
|
{{define "hypha name"}}Имя гифы{{end}}
|
2022-03-22 13:57:25 +00:00
|
|
|
|
{{define "categories"}}Категории{{end}}
|
2022-03-23 12:46:08 +00:00
|
|
|
|
{{define "placeholder"}}Имя категории...{{end}}
|
2022-03-22 13:57:25 +00:00
|
|
|
|
{{define "remove from category title"}}Убрать гифу из этой категории{{end}}
|
2022-03-23 12:46:08 +00:00
|
|
|
|
{{define "add to category title"}}Добавить гифу в эту категорию{{end}}
|
|
|
|
|
{{define "category list heading"}}Список категорий{{end}}
|
|
|
|
|
{{define "no categories"}}В этой вики нет категорий.{{end}}
|
|
|
|
|
{{define "category x"}}Категория {{. | beautifulName}}{{end}}
|
2022-03-20 19:24:40 +00:00
|
|
|
|
`
|
|
|
|
|
|
2022-03-20 08:21:59 +00:00
|
|
|
|
var (
|
2022-04-01 20:52:56 +00:00
|
|
|
|
//go:embed *.html
|
2022-04-01 21:18:37 +00:00
|
|
|
|
fs embed.FS
|
|
|
|
|
m = template.Must
|
|
|
|
|
baseEn, baseRu *template.Template
|
|
|
|
|
viewListChain, viewPageChain viewutil.Chain
|
|
|
|
|
categoryTemplatesEn *template.Template
|
|
|
|
|
categoryTemplatesRu *template.Template
|
2022-03-20 08:21:59 +00:00
|
|
|
|
)
|
2022-03-19 20:57:33 +00:00
|
|
|
|
|
2022-04-01 20:52:56 +00:00
|
|
|
|
func prepareViews() {
|
2022-03-23 12:46:08 +00:00
|
|
|
|
categoryTemplatesEn = template.Must(template.
|
2022-03-20 09:04:08 +00:00
|
|
|
|
New("category").
|
|
|
|
|
Funcs(
|
|
|
|
|
template.FuncMap{
|
|
|
|
|
"beautifulName": util.BeautifulName,
|
|
|
|
|
}).
|
2022-03-22 13:40:40 +00:00
|
|
|
|
ParseFS(fs, "categories.html"))
|
2022-03-23 12:46:08 +00:00
|
|
|
|
categoryTemplatesRu = template.Must(template.Must(categoryTemplatesEn.Clone()).Parse(categoriesRu))
|
2022-04-01 20:52:56 +00:00
|
|
|
|
|
|
|
|
|
baseEn = m(viewutil.BaseEn.Clone())
|
2022-04-01 21:07:28 +00:00
|
|
|
|
baseRu = m(viewutil.BaseRu.Clone())
|
2022-04-01 21:18:37 +00:00
|
|
|
|
|
|
|
|
|
viewListChain = viewutil.
|
|
|
|
|
En(
|
|
|
|
|
m(m(baseEn.Clone()).ParseFS(fs, "view_list.html"))).
|
|
|
|
|
Ru(
|
|
|
|
|
m(m(m(baseRu.Clone()).ParseFS(fs, "view_list.html")).Parse(categoriesRu)))
|
|
|
|
|
viewPageChain = viewutil.
|
|
|
|
|
En(
|
|
|
|
|
m(m(baseEn.Clone()).ParseFS(fs, "view_page.html"))).
|
|
|
|
|
Ru(
|
|
|
|
|
m(m(m(baseRu.Clone()).ParseFS(fs, "view_page.html")).Parse(categoriesRu)))
|
2022-03-19 20:57:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 20:59:36 +00:00
|
|
|
|
func localizedCatTemplates(meta viewutil.Meta) *template.Template {
|
2022-03-23 12:46:08 +00:00
|
|
|
|
if meta.Lc.Locale == "ru" {
|
|
|
|
|
return categoryTemplatesRu
|
|
|
|
|
}
|
|
|
|
|
return categoryTemplatesEn
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 20:59:36 +00:00
|
|
|
|
func localizedCatTemplateAsString(meta viewutil.Meta, name string, datum ...interface{}) string {
|
2022-03-19 20:57:33 +00:00
|
|
|
|
var buf strings.Builder
|
2022-03-23 12:46:08 +00:00
|
|
|
|
var err error
|
|
|
|
|
if len(datum) == 1 {
|
2022-03-26 15:26:14 +00:00
|
|
|
|
err = localizedCatTemplates(meta).ExecuteTemplate(&buf, name, datum[0])
|
2022-03-23 12:46:08 +00:00
|
|
|
|
} else {
|
2022-03-26 15:26:14 +00:00
|
|
|
|
err = localizedCatTemplates(meta).ExecuteTemplate(&buf, name, nil)
|
2022-03-23 12:46:08 +00:00
|
|
|
|
}
|
2022-03-22 13:40:40 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2022-03-23 12:46:08 +00:00
|
|
|
|
return buf.String()
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:52:56 +00:00
|
|
|
|
func CategoryCard(meta viewutil.Meta, hyphaName string) string {
|
2022-03-23 12:46:08 +00:00
|
|
|
|
var buf strings.Builder
|
2022-03-26 15:26:14 +00:00
|
|
|
|
err := localizedCatTemplates(meta).ExecuteTemplate(&buf, "category card", struct {
|
2022-03-26 15:39:33 +00:00
|
|
|
|
HyphaName string
|
|
|
|
|
Categories []string
|
|
|
|
|
GivenPermissionToModify bool
|
2022-03-19 20:57:33 +00:00
|
|
|
|
}{
|
|
|
|
|
hyphaName,
|
2022-04-01 20:52:56 +00:00
|
|
|
|
WithHypha(hyphaName),
|
2022-03-26 15:39:33 +00:00
|
|
|
|
meta.U.CanProceed("add-to-category"),
|
2022-03-19 20:57:33 +00:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
}
|
|
|
|
|
return buf.String()
|
|
|
|
|
}
|
2022-03-20 08:21:59 +00:00
|
|
|
|
|
2022-04-01 21:07:28 +00:00
|
|
|
|
type pageData struct {
|
|
|
|
|
viewutil.BaseData
|
|
|
|
|
CatName string
|
|
|
|
|
Hyphae []string
|
|
|
|
|
GivenPermissionToModify bool
|
2022-03-20 08:21:59 +00:00
|
|
|
|
}
|
2022-03-20 18:24:54 +00:00
|
|
|
|
|
2022-04-01 21:07:28 +00:00
|
|
|
|
func categoryPage(meta viewutil.Meta, catName string) {
|
2022-04-01 21:18:37 +00:00
|
|
|
|
if err := viewPageChain.Get(meta).ExecuteTemplate(meta.W, "page", pageData{
|
2022-04-01 21:07:28 +00:00
|
|
|
|
BaseData: viewutil.BaseData{
|
|
|
|
|
Meta: meta,
|
|
|
|
|
Title: localizedCatTemplateAsString(meta, "category x", catName),
|
|
|
|
|
HeaderLinks: cfg.HeaderLinks,
|
|
|
|
|
CommonScripts: cfg.CommonScripts,
|
|
|
|
|
},
|
|
|
|
|
CatName: catName,
|
|
|
|
|
Hyphae: Contents(catName),
|
|
|
|
|
GivenPermissionToModify: meta.U.CanProceed("add-to-category"),
|
|
|
|
|
}); err != nil {
|
|
|
|
|
log.Println(err)
|
2022-03-20 18:24:54 +00:00
|
|
|
|
}
|
2022-04-01 20:52:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type listData struct {
|
|
|
|
|
viewutil.BaseData
|
|
|
|
|
Categories []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func categoryList(meta viewutil.Meta) {
|
2022-04-01 21:18:37 +00:00
|
|
|
|
if err := viewListChain.Get(meta).ExecuteTemplate(meta.W, "page", listData{
|
2022-04-01 20:52:56 +00:00
|
|
|
|
BaseData: viewutil.BaseData{
|
|
|
|
|
Meta: meta,
|
|
|
|
|
Title: localizedCatTemplateAsString(meta, "category list heading"),
|
|
|
|
|
HeaderLinks: cfg.HeaderLinks,
|
|
|
|
|
CommonScripts: cfg.CommonScripts,
|
|
|
|
|
},
|
|
|
|
|
Categories: List(),
|
|
|
|
|
}); err != nil {
|
2022-03-20 18:24:54 +00:00
|
|
|
|
log.Println(err)
|
|
|
|
|
}
|
|
|
|
|
}
|