add configuration: top nav order and settings
This commit is contained in:
1 parent
d300354725
commit
12decb7862
9 files changed
+386
-71
No files matched your search
@@ -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(),
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -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
@@ -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 {
|
||||
|
||||
|
||||
@@ -15,10 +15,13 @@
|
||||
<a href="/">Pages Wiki</a>
|
||||
</div>
|
||||
|
||||
|
||||
<nav>
|
||||
<a href="/">Home</a>
|
||||
<a href="/v/tree">Tree</a>
|
||||
{{ range .NavItems }}
|
||||
<a href="{{ .Href }}">{{ .Label }}</a>
|
||||
{{ end }}
|
||||
</nav>
|
||||
|
||||
</header>
|
||||
|
||||
<main class="config-layout">
|
||||
@@ -46,6 +49,10 @@ Home
|
||||
Recent Objects
|
||||
</a>
|
||||
|
||||
<a href="#" data-panel="nav" onclick="showConfigPanel('nav'); return false;">
|
||||
Navigation
|
||||
</a>
|
||||
|
||||
</nav>
|
||||
|
||||
</aside>
|
||||
@@ -66,6 +73,12 @@ Recent Objects
|
||||
action="/-/sys/config"
|
||||
>
|
||||
|
||||
<input
|
||||
type="hidden"
|
||||
name="section"
|
||||
value="home"
|
||||
>
|
||||
|
||||
<label>
|
||||
Home semantic path
|
||||
</label>
|
||||
@@ -143,6 +156,124 @@ Save
|
||||
|
||||
</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>
|
||||
|
||||
</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>
|
||||
|
||||
<style>
|
||||
|
||||
@@ -15,11 +15,13 @@
|
||||
<a href="/">Pages Wiki</a>
|
||||
</div>
|
||||
|
||||
|
||||
<nav>
|
||||
<a href="/v/tree">Tree</a>
|
||||
<a href="/-/sys/config">Config</a>
|
||||
<a href="/new">Draft</a>
|
||||
{{ range .NavItems }}
|
||||
<a href="{{ .Href }}">{{ .Label }}</a>
|
||||
{{ end }}
|
||||
</nav>
|
||||
|
||||
</header>
|
||||
|
||||
<main class="container">
|
||||
|
||||
@@ -15,11 +15,13 @@
|
||||
<a href="/">Pages Wiki</a>
|
||||
</div>
|
||||
|
||||
|
||||
<nav>
|
||||
<a href="/o/{{ .ObjectURI }}">View</a>
|
||||
<a href="/o/r/{{ .ObjectURI }}">History</a>
|
||||
<a href="/l?object_uuid={{ .ObjectUUID }}">Link</a>
|
||||
{{ range .NavItems }}
|
||||
<a href="{{ .Href }}">{{ .Label }}</a>
|
||||
{{ end }}
|
||||
</nav>
|
||||
|
||||
</header>
|
||||
|
||||
<main class="container">
|
||||
|
||||
@@ -15,12 +15,13 @@
|
||||
<a href="/">Pages Wiki</a>
|
||||
</div>
|
||||
|
||||
|
||||
<nav>
|
||||
<a href="/o/e/{{ .ObjectURI }}">Edit</a>
|
||||
<a href="/o/r/{{ .ObjectURI }}">History</a>
|
||||
<a href="/l?object_uuid={{ .ObjectUUID }}">Link</a>
|
||||
<a href="#" onclick="deleteObject('{{ .ObjectUUID }}'); return false;">Delete</a>
|
||||
{{ range .NavItems }}
|
||||
<a href="{{ .Href }}">{{ .Label }}</a>
|
||||
{{ end }}
|
||||
</nav>
|
||||
|
||||
</header>
|
||||
|
||||
<main class="container">
|
||||
|
||||
@@ -28,32 +28,14 @@
|
||||
<a href="/">Pages Wiki</a>
|
||||
</div>
|
||||
|
||||
|
||||
<nav>
|
||||
|
||||
<a href="/">Home</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>
|
||||
|
||||
{{ range .NavItems }}
|
||||
<a href="{{ .Href }}">{{ .Label }}</a>
|
||||
{{ end }}
|
||||
|
||||
</nav>
|
||||
|
||||
|
||||
</header>
|
||||
|
||||
<main class="container">
|
||||
|
||||
@@ -555,14 +555,14 @@ body {
|
||||
Pages Wiki
|
||||
</div>
|
||||
|
||||
|
||||
<nav>
|
||||
|
||||
<a href="/">
|
||||
Home
|
||||
</a>
|
||||
|
||||
{{ range .NavItems }}
|
||||
<a href="{{ .Href }}">{{ .Label }}</a>
|
||||
{{ end }}
|
||||
</nav>
|
||||
|
||||
|
||||
</header>
|
||||
|
||||
<main class="container">
|
||||
|
||||
Reference in new issue
Block a user