1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00
mycorrhiza/categories/views.go

108 lines
3.4 KiB
Go
Raw Normal View History

package categories
import (
"embed"
2022-03-29 20:59:36 +00:00
"github.com/bouncepaw/mycorrhiza/viewutil"
"log"
2022-07-11 08:54:31 +00:00
"sort"
"strings"
)
2022-04-01 21:59:47 +00:00
const ruTranslation = `
{{define "empty cat"}}Эта категория пуста.{{end}}
{{define "cat"}}Категория{{end}}
{{define "hypha name"}}Название гифы{{end}}
{{define "categories"}}Категории{{end}}
{{define "placeholder"}}Название категории...{{end}}
{{define "remove from category title"}}Убрать гифу из этой категории{{end}}
2022-03-23 12:46:08 +00:00
{{define "add to category title"}}Добавить гифу в эту категорию{{end}}
2022-04-01 21:59:47 +00:00
{{define "category list"}}Список категорий{{end}}
2022-03-23 12:46:08 +00:00
{{define "no categories"}}В этой вики нет категорий.{{end}}
{{define "category x"}}Категория {{. | beautifulName}}{{end}}
{{define "edit category x"}}Редактирование категории {{beautifulName .}}{{end}}
{{define "edit category heading"}}Редактирование категории <a href="/category/{{.}}">{{beautifulName .}}</a>{{end}}
{{define "add"}}Добавить{{end}}
{{define "remove hyphae"}}Убрать гифы из этой категории{{end}}
{{define "remove"}}Убрать{{end}}
{{define "edit"}}Редактировать{{end}}
`
2022-03-20 08:21:59 +00:00
var (
//go:embed *.html
fs embed.FS
viewListChain, viewPageChain, viewCardChain, viewEditChain viewutil.Chain
2022-03-20 08:21:59 +00:00
)
func prepareViews() {
viewCardChain = viewutil.CopyEnRuWith(fs, "view_card.html", ruTranslation)
viewListChain = viewutil.CopyEnRuWith(fs, "view_list.html", ruTranslation)
viewPageChain = viewutil.CopyEnRuWith(fs, "view_page.html", ruTranslation)
viewEditChain = viewutil.CopyEnRuWith(fs, "view_edit.html", ruTranslation)
}
type cardData struct {
HyphaName string
Categories []string
GivenPermissionToModify bool
2022-03-23 12:46:08 +00:00
}
2022-04-01 21:59:47 +00:00
// CategoryCard is the little sidebar that is shown nearby the hypha view.
func CategoryCard(meta viewutil.Meta, hyphaName string) string {
2022-03-23 12:46:08 +00:00
var buf strings.Builder
err := viewCardChain.Get(meta).ExecuteTemplate(&buf, "category card", cardData{
HyphaName: hyphaName,
Categories: CategoriesWithHypha(hyphaName),
GivenPermissionToModify: meta.U.CanProceed("add-to-category"),
})
if err != nil {
log.Println(err)
}
return buf.String()
}
2022-03-20 08:21:59 +00:00
type catData struct {
*viewutil.BaseData
CatName string
Hyphae []string
GivenPermissionToModify bool
2022-03-20 08:21:59 +00:00
}
func categoryEdit(meta viewutil.Meta, catName string) {
viewutil.ExecutePage(meta, viewEditChain, catData{
BaseData: &viewutil.BaseData{
Addr: "/edit-category/" + catName,
},
CatName: catName,
Hyphae: hyphaeInCategory(catName),
GivenPermissionToModify: meta.U.CanProceed("add-to-category"),
})
}
func categoryPage(meta viewutil.Meta, catName string) {
viewutil.ExecutePage(meta, viewPageChain, catData{
BaseData: &viewutil.BaseData{
Addr: "/category/" + catName,
},
CatName: catName,
2022-04-01 21:43:11 +00:00
Hyphae: hyphaeInCategory(catName),
GivenPermissionToModify: meta.U.CanProceed("add-to-category"),
})
}
type listData struct {
*viewutil.BaseData
Categories []string
}
func categoryList(meta viewutil.Meta) {
2022-07-11 08:54:31 +00:00
cats := listOfCategories()
sort.Strings(cats)
viewutil.ExecutePage(meta, viewListChain, listData{
BaseData: &viewutil.BaseData{
Addr: "/category",
},
2022-07-11 08:54:31 +00:00
Categories: cats,
})
}