mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-11-06 18:43:03 +00:00
Categories, views: Unexport stuff
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
// Package categories provides category management. All operations in this package are mutexed.
|
// Package categories provides category management.
|
||||||
//
|
//
|
||||||
// As per the long pondering, this is how categories (cats for short)
|
// As per the long pondering, this is how categories (cats for short)
|
||||||
// work in Mycorrhiza:
|
// work in Mycorrhiza:
|
||||||
@@ -12,12 +12,19 @@
|
|||||||
// cat operations are not mentioned on the recent changes page.
|
// cat operations are not mentioned on the recent changes page.
|
||||||
// - For cat A, if there are 0 hyphae in the cat, cat A does not
|
// - For cat A, if there are 0 hyphae in the cat, cat A does not
|
||||||
// exist. If there are 1 or more hyphae in the cat, cat A exists.
|
// exist. If there are 1 or more hyphae in the cat, cat A exists.
|
||||||
|
//
|
||||||
|
// List of things to do with categories later:
|
||||||
|
//
|
||||||
|
// - Forbid / in cat names.
|
||||||
|
// - Rename categories.
|
||||||
|
// - Delete categories.
|
||||||
|
// - Bind hyphae.
|
||||||
package categories
|
package categories
|
||||||
|
|
||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
// List returns names of all categories.
|
// listOfCategories returns names of all categories.
|
||||||
func List() (categoryList []string) {
|
func listOfCategories() (categoryList []string) {
|
||||||
mutex.RLock()
|
mutex.RLock()
|
||||||
for cat, _ := range categoryToHyphae {
|
for cat, _ := range categoryToHyphae {
|
||||||
categoryList = append(categoryList, cat)
|
categoryList = append(categoryList, cat)
|
||||||
@@ -26,8 +33,8 @@ func List() (categoryList []string) {
|
|||||||
return categoryList
|
return categoryList
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithHypha returns what categories have the given hypha. The hypha name must be canonical.
|
// categoriesWithHypha returns what categories have the given hypha. The hypha name must be canonical.
|
||||||
func WithHypha(hyphaName string) (categoryList []string) {
|
func categoriesWithHypha(hyphaName string) (categoryList []string) {
|
||||||
mutex.RLock()
|
mutex.RLock()
|
||||||
defer mutex.RUnlock()
|
defer mutex.RUnlock()
|
||||||
if node, ok := hyphaToCategories[hyphaName]; ok {
|
if node, ok := hyphaToCategories[hyphaName]; ok {
|
||||||
@@ -37,8 +44,8 @@ func WithHypha(hyphaName string) (categoryList []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contents returns what hyphae are in the category. If the returned slice is empty, the category does not exist, and vice versa. The category name must be canonical.
|
// hyphaeInCategory returns what hyphae are in the category. If the returned slice is empty, the category does not exist, and vice versa. The category name must be canonical.
|
||||||
func Contents(catName string) (hyphaList []string) {
|
func hyphaeInCategory(catName string) (hyphaList []string) {
|
||||||
mutex.RLock()
|
mutex.RLock()
|
||||||
defer mutex.RUnlock()
|
defer mutex.RUnlock()
|
||||||
if node, ok := categoryToHyphae[catName]; ok {
|
if node, ok := categoryToHyphae[catName]; ok {
|
||||||
@@ -50,8 +57,8 @@ func Contents(catName string) (hyphaList []string) {
|
|||||||
|
|
||||||
var mutex sync.RWMutex
|
var mutex sync.RWMutex
|
||||||
|
|
||||||
// AddHyphaToCategory adds the hypha to the category and updates the records on the disk. If the hypha is already in the category, nothing happens. Pass canonical names.
|
// addHyphaToCategory adds the hypha to the category and updates the records on the disk. If the hypha is already in the category, nothing happens. Pass canonical names.
|
||||||
func AddHyphaToCategory(hyphaName, catName string) {
|
func addHyphaToCategory(hyphaName, catName string) {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
if node, ok := hyphaToCategories[hyphaName]; ok {
|
if node, ok := hyphaToCategories[hyphaName]; ok {
|
||||||
node.storeCategory(catName)
|
node.storeCategory(catName)
|
||||||
@@ -68,8 +75,8 @@ func AddHyphaToCategory(hyphaName, catName string) {
|
|||||||
go saveToDisk()
|
go saveToDisk()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveHyphaFromCategory removes the hypha from the category and updates the records on the disk. If the hypha is not in the category, nothing happens. Pass canonical names.
|
// removeHyphaFromCategory removes the hypha from the category and updates the records on the disk. If the hypha is not in the category, nothing happens. Pass canonical names.
|
||||||
func RemoveHyphaFromCategory(hyphaName, catName string) {
|
func removeHyphaFromCategory(hyphaName, catName string) {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
if node, ok := hyphaToCategories[hyphaName]; ok {
|
if node, ok := hyphaToCategories[hyphaName]; ok {
|
||||||
node.removeCategory(catName)
|
node.removeCategory(catName)
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// InitCategoriesHandlers initializes HTTP handlers for the given router. Call somewhere in package web.
|
||||||
func InitCategoriesHandlers(r *mux.Router) {
|
func InitCategoriesHandlers(r *mux.Router) {
|
||||||
r.PathPrefix("/add-to-category").HandlerFunc(handlerAddToCategory).Methods("POST")
|
r.PathPrefix("/add-to-category").HandlerFunc(handlerAddToCategory).Methods("POST")
|
||||||
r.PathPrefix("/remove-from-category").HandlerFunc(handlerRemoveFromCategory).Methods("POST")
|
r.PathPrefix("/remove-from-category").HandlerFunc(handlerRemoveFromCategory).Methods("POST")
|
||||||
@@ -51,7 +52,7 @@ func handlerRemoveFromCategory(w http.ResponseWriter, rq *http.Request) {
|
|||||||
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
RemoveHyphaFromCategory(hyphaName, catName)
|
removeHyphaFromCategory(hyphaName, catName)
|
||||||
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +72,6 @@ func handlerAddToCategory(w http.ResponseWriter, rq *http.Request) {
|
|||||||
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
AddHyphaToCategory(hyphaName, catName)
|
addHyphaToCategory(hyphaName, catName)
|
||||||
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
http.Redirect(w, rq, redirectTo, http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ func CategoryCard(meta viewutil.Meta, hyphaName string) string {
|
|||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
err := viewCardChain.Get(meta).ExecuteTemplate(&buf, "category card", cardData{
|
err := viewCardChain.Get(meta).ExecuteTemplate(&buf, "category card", cardData{
|
||||||
hyphaName,
|
hyphaName,
|
||||||
WithHypha(hyphaName),
|
categoriesWithHypha(hyphaName),
|
||||||
meta.U.CanProceed("add-to-category"),
|
meta.U.CanProceed("add-to-category"),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -87,7 +87,7 @@ func categoryPage(meta viewutil.Meta, catName string) {
|
|||||||
CommonScripts: cfg.CommonScripts,
|
CommonScripts: cfg.CommonScripts,
|
||||||
},
|
},
|
||||||
CatName: catName,
|
CatName: catName,
|
||||||
Hyphae: Contents(catName),
|
Hyphae: hyphaeInCategory(catName),
|
||||||
GivenPermissionToModify: meta.U.CanProceed("add-to-category"),
|
GivenPermissionToModify: meta.U.CanProceed("add-to-category"),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@@ -106,7 +106,7 @@ func categoryList(meta viewutil.Meta) {
|
|||||||
HeaderLinks: cfg.HeaderLinks,
|
HeaderLinks: cfg.HeaderLinks,
|
||||||
CommonScripts: cfg.CommonScripts,
|
CommonScripts: cfg.CommonScripts,
|
||||||
},
|
},
|
||||||
Categories: List(),
|
Categories: listOfCategories(),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user