add configuration: top nav order and settings

This commit is contained in:
Anya Tokugawa committed 2026-05-31 21:10:01 +09:00
1 parent d300354725
commit 12decb7862
9 files changed
+386 -71

No files matched your search

+97 -18
View File
@@ -1,7 +1,9 @@
package server
import (
"encoding/json"
"net/http"
"strings"
)
func (s *Server) handleConfig(
@@ -11,22 +13,102 @@ func (s *Server) handleConfig(
if r.Method == http.MethodPost {
homePath := r.FormValue("home_path")
err := s.store.SetConfig(
"home_path",
homePath,
)
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
section :=
r.FormValue("section")
switch section {
case "home":
err = s.store.SetConfig(
"home_path",
r.FormValue("home_path"),
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
case "nav":
labels :=
r.Form["nav_label"]
hrefs :=
r.Form["nav_href"]
var items []NavItem
for i := range labels {
label :=
strings.TrimSpace(labels[i])
if label == "" {
continue
}
if i >= len(hrefs) {
continue
}
href :=
strings.TrimSpace(hrefs[i])
if href == "" {
continue
}
items = append(
items,
NavItem{
Label: label,
Href: href,
},
)
}
raw, err :=
json.Marshal(items)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = s.store.SetConfig(
"nav_items",
string(raw),
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
default:
http.Error(
w,
"unknown config section",
http.StatusBadRequest,
)
return
}
http.Redirect(
w,
r,
"/-/sys/config",
"/-/sys/config?panel="+section,
http.StatusFound,
)
@@ -34,22 +116,18 @@ func (s *Server) handleConfig(
}
homePath, err :=
s.store.GetConfig(
"home_path",
)
s.store.GetConfig("home_path")
if err != nil {
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
recent, err :=
s.store.ListRecentObjects(
50,
)
s.store.ListRecentObjects(50)
if err != nil {
http.Error(w, err.Error(), 500)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -57,8 +135,9 @@ func (s *Server) handleConfig(
w,
"config.gohtml",
map[string]any{
"HomePath": homePath,
"Objects": recent,
"HomePath": homePath,
"Objects": recent,
"NavItemsConfig": s.getNavItems(),
},
)
}
+57
View File
@@ -0,0 +1,57 @@
package server
import (
"encoding/json"
)
type NavItem struct {
Label string `json:"label"`
Href string `json:"href"`
}
func defaultNavItems() []NavItem {
return []NavItem{
{
Label: "Home",
Href: "/",
},
{
Label: "Tree",
Href: "/v/tree",
},
{
Label: "Config",
Href: "/-/sys/config",
},
}
}
func (s *Server) getNavItems() []NavItem {
raw, err :=
s.store.GetConfig(
"nav_items",
)
if err != nil ||
raw == "" {
return defaultNavItems()
}
var items []NavItem
err = json.Unmarshal(
[]byte(raw),
&items,
)
if err != nil ||
len(items) == 0 {
return defaultNavItems()
}
return items
}
+36 -14
View File
@@ -1,29 +1,51 @@
package server
import (
"html/template"
"net/http"
)
func mergeTemplateData(
data any,
nav []NavItem,
) map[string]any {
out := map[string]any{
"NavItems": nav,
}
if data == nil {
return out
}
m, ok := data.(map[string]any)
if !ok {
out["Data"] = data
return out
}
for k, v := range m {
out[k] = v
}
return out
}
func (s *Server) render(
w http.ResponseWriter,
name string,
data any,
) {
s.templates.Funcs(
template.FuncMap{
"sub": func(a, b int) int {
return a - b
},
},
)
err := s.templates.ExecuteTemplate(
w,
name,
data,
)
err :=
s.templates.ExecuteTemplate(
w,
name,
mergeTemplateData(
data,
s.getNavItems(),
),
)
if err != nil {