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 package server
import ( import (
"encoding/json"
"net/http" "net/http"
"strings"
) )
func (s *Server) handleConfig( func (s *Server) handleConfig(
@@ -11,22 +13,102 @@ func (s *Server) handleConfig(
if r.Method == http.MethodPost { if r.Method == http.MethodPost {
homePath := r.FormValue("home_path") err := r.ParseForm()
err := s.store.SetConfig(
"home_path",
homePath,
)
if err != nil { 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 return
} }
http.Redirect( http.Redirect(
w, w,
r, r,
"/-/sys/config", "/-/sys/config?panel="+section,
http.StatusFound, http.StatusFound,
) )
@@ -34,22 +116,18 @@ func (s *Server) handleConfig(
} }
homePath, err := homePath, err :=
s.store.GetConfig( s.store.GetConfig("home_path")
"home_path",
)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
recent, err := recent, err :=
s.store.ListRecentObjects( s.store.ListRecentObjects(50)
50,
)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
@@ -57,8 +135,9 @@ func (s *Server) handleConfig(
w, w,
"config.gohtml", "config.gohtml",
map[string]any{ map[string]any{
"HomePath": homePath, "HomePath": homePath,
"Objects": recent, "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 package server
import ( import (
"html/template"
"net/http" "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( func (s *Server) render(
w http.ResponseWriter, w http.ResponseWriter,
name string, name string,
data any, data any,
) { ) {
s.templates.Funcs( err :=
template.FuncMap{ s.templates.ExecuteTemplate(
"sub": func(a, b int) int { w,
return a - b name,
}, mergeTemplateData(
}, data,
) s.getNavItems(),
),
err := s.templates.ExecuteTemplate( )
w,
name,
data,
)
if err != nil { if err != nil {
+172 -2
View File
@@ -15,10 +15,13 @@
<a href="/">Pages Wiki</a> <a href="/">Pages Wiki</a>
</div> </div>
<nav> <nav>
<a href="/">Home</a> {{ range .NavItems }}
<a href="/v/tree">Tree</a> <a href="{{ .Href }}">{{ .Label }}</a>
{{ end }}
</nav> </nav>
</header> </header>
<main class="config-layout"> <main class="config-layout">
@@ -46,6 +49,10 @@ Home
Recent Objects Recent Objects
</a> </a>
<a href="#" data-panel="nav" onclick="showConfigPanel('nav'); return false;">
Navigation
</a>
</nav> </nav>
</aside> </aside>
@@ -66,6 +73,12 @@ Recent Objects
action="/-/sys/config" action="/-/sys/config"
> >
<input
type="hidden"
name="section"
value="home"
>
<label> <label>
Home semantic path Home semantic path
</label> </label>
@@ -143,6 +156,124 @@ Save
</section> </section>
<section
id="panel-nav"
class="config-section config-panel"
>
<h2>Navigation</h2>
<form
method="post"
action="/-/sys/config"
>
<input
type="hidden"
name="section"
value="nav"
>
<table class="recent-table">
<thead>
<tr>
<th>Label</th>
<th>Href</th>
<th>Order</th>
</tr>
</thead>
<tbody>
{{ range .NavItemsConfig }}
<tr>
<td>
<input
name="nav_label"
type="text"
value="{{ .Label }}"
>
</td>
<td>
<input
name="nav_href"
type="text"
value="{{ .Href }}"
>
</td>
<td>
<button
type="button"
onclick="moveNavRow(this, -1)"
>
↑
</button>
<button
type="button"
onclick="moveNavRow(this, 1)"
>
↓
</button>
</td>
</tr>
{{ end }}
<tr>
<td>
<input
name="nav_label"
type="text"
placeholder="Label"
>
</td>
<td>
<input
name="nav_href"
type="text"
placeholder="/path"
>
</td>
<td>
<button
type="button"
onclick="moveNavRow(this, -1)"
>
↑
</button>
<button
type="button"
onclick="moveNavRow(this, 1)"
>
↓
</button>
</td>
</tr>
</tbody>
</table>
<button type="submit">
Save Navigation
</button>
</form>
</section>
</section> </section>
</main> </main>
@@ -220,6 +351,45 @@ document.addEventListener(
) )
}, },
) )
function moveNavRow(button, direction) {
const row =
button.closest(
"tr"
)
const tbody =
row.parentElement
if (direction < 0) {
const prev =
row.previousElementSibling
if (prev) {
tbody.insertBefore(
row,
prev
)
}
return
}
const next =
row.nextElementSibling
if (next) {
tbody.insertBefore(
next,
row
)
}
}
</script> </script>
<style> <style>
+5 -3
View File
@@ -15,11 +15,13 @@
<a href="/">Pages Wiki</a> <a href="/">Pages Wiki</a>
</div> </div>
<nav> <nav>
<a href="/v/tree">Tree</a> {{ range .NavItems }}
<a href="/-/sys/config">Config</a> <a href="{{ .Href }}">{{ .Label }}</a>
<a href="/new">Draft</a> {{ end }}
</nav> </nav>
</header> </header>
<main class="container"> <main class="container">
+5 -3
View File
@@ -15,11 +15,13 @@
<a href="/">Pages Wiki</a> <a href="/">Pages Wiki</a>
</div> </div>
<nav> <nav>
<a href="/o/{{ .ObjectURI }}">View</a> {{ range .NavItems }}
<a href="/o/r/{{ .ObjectURI }}">History</a> <a href="{{ .Href }}">{{ .Label }}</a>
<a href="/l?object_uuid={{ .ObjectUUID }}">Link</a> {{ end }}
</nav> </nav>
</header> </header>
<main class="container"> <main class="container">
+5 -4
View File
@@ -15,12 +15,13 @@
<a href="/">Pages Wiki</a> <a href="/">Pages Wiki</a>
</div> </div>
<nav> <nav>
<a href="/o/e/{{ .ObjectURI }}">Edit</a> {{ range .NavItems }}
<a href="/o/r/{{ .ObjectURI }}">History</a> <a href="{{ .Href }}">{{ .Label }}</a>
<a href="/l?object_uuid={{ .ObjectUUID }}">Link</a> {{ end }}
<a href="#" onclick="deleteObject('{{ .ObjectUUID }}'); return false;">Delete</a>
</nav> </nav>
</header> </header>
<main class="container"> <main class="container">
+4 -22
View File
@@ -28,32 +28,14 @@
<a href="/">Pages Wiki</a> <a href="/">Pages Wiki</a>
</div> </div>
<nav> <nav>
{{ range .NavItems }}
<a href="/">Home</a> <a href="{{ .Href }}">{{ .Label }}</a>
{{ if .Path }}
<a href="/e{{ .Path }}">
Edit
</a>
<a href="/r/history/{{ .ObjectUUID }}">
History
</a>
<a href="/l?object_uuid={{ .ObjectUUID }}">
Links
</a>
<a href="/raw{{ .Path }}">
Raw
</a>
{{ end }} {{ end }}
</nav> </nav>
</header> </header>
<main class="container"> <main class="container">
+5 -5
View File
@@ -555,14 +555,14 @@ body {
Pages Wiki Pages Wiki
</div> </div>
<nav> <nav>
{{ range .NavItems }}
<a href="/"> <a href="{{ .Href }}">{{ .Label }}</a>
Home {{ end }}
</a>
</nav> </nav>
</header> </header>
<main class="container"> <main class="container">