oficina / fichas Public

Clone
git clone https://amadan.net/oficina/fichas

Plain git — no account needed to clone.

Download

Download this file

1package fichas
2
3// The frame (level-up §3): estilo's stacked bar, with Fichas' menus and
4// the save state in its slots, over the rail and the work.
5
6import (
7 "bytes"
8 "html/template"
9 "net/http"
10 "strconv"
11
12 "amadan.net/oficina/estilo.git/chrome"
13 "amadan.net/oficina/estilo.git/menu"
14 "amadan.net/oficina/estilo.git/savestatus"
15
16 "amadan.net/oficina/fichas/internal/fichas/icons"
17)
18
19// railCookie holds the rail's fold: "rail" (folded to icons) or "open".
20// Absent, CSS folds it from 800 to 1279px and opens it from 1280 (§3.3).
21const railCookie = "fichas-rail"
22
23func railState(r *http.Request) string {
24 if c, err := r.Cookie(railCookie); err == nil && (c.Value == "rail" || c.Value == "open") {
25 return c.Value
26 }
27 return ""
28}
29
30// foldRail is POST /view/rail: the fold is a preference, a cookie only.
31// It writes no row, logs no event, and returns only to a local path.
32func (a *app) foldRail(w http.ResponseWriter, r *http.Request) {
33 _ = r.ParseForm()
34 switch v := r.PostForm.Get("rail"); v {
35 case "rail", "open":
36 a.setPref(w, railCookie, v, prefMaxAge)
37 }
38 http.Redirect(w, r, localReturn(r.PostForm.Get("return_to"), "/"), http.StatusSeeOther)
39}
40
41// menuItem is one row of a menu: a link, a one-button form, a command a
42// script binds (disabled until it does), a dialog, or a divider.
43type menuItem struct {
44 Kind, Label, Href, Action, Key, Command, Dialog string
45 Fields map[string]string
46 Danger bool
47}
48
49type menuView struct {
50 ID, Title string
51 Items []menuItem
52}
53
54type menuBar struct {
55 Label string
56 Menus []menuView
57}
58
59// frameFacts is what the bar needs to know about a page.
60type frameFacts struct {
61 TypeKey, Singular, Title, Here string
62 CardID int64
63 Saving bool // an editing page: the lead shows the save state
64}
65
66// buildFrame puts the bar and the rail's state into a page's data.
67func (a *app) buildFrame(r *http.Request, name string, data map[string]any, nav []NavItem) error {
68 facts := frameFactsOf(r, name, data, nav)
69 menus, err := fragment(name, "fichas-menubar", menusFor(facts))
70 if err != nil {
71 return err
72 }
73 lead := template.HTML("")
74 if facts.Saving {
75 if lead, err = fragment(name, "estilo-save-status", saveState(r)); err != nil {
76 return err
77 }
78 }
79 // Search is the rail's first row and, below 800px, the Menu sheet's
80 // first row, before the types (the sheet draws the bar's nav).
81 search := searchHref(facts)
82 links := make([]chrome.Link, 0, len(nav)+1)
83 links = append(links, chrome.Link{Href: search, Label: "Search"})
84 for _, n := range nav {
85 links = append(links, chrome.Link{Href: n.Href, Label: n.Label, Current: n.Current})
86 }
87 me, _ := a.identity(r)
88 account := a.account(r, me)
89 account.Rail = false
90 data["Bar"] = chrome.Bar{
91 App: chrome.App{Name: "Fichas", Slug: "fichas", Home: "/", Icon: assets.Path("static/brand/icon.svg")},
92 Context: facts.Title, Nav: links, NavLabel: "Types",
93 Actions: barActions(facts), Account: account, Sticky: true,
94 Labels: chrome.BarLabels{Skip: "Skip to content", Menu: "Menu"},
95 Menubar: menus, Lead: lead,
96 }
97 data["Rail"] = railState(r)
98 data["Search"] = search
99 data["Here"] = orDefault(data["Here"], r.URL.RequestURI())
100 return nil
101}
102
103func orDefault(v any, def string) string {
104 if s, ok := v.(string); ok && s != "" {
105 return s
106 }
107 return def
108}
109
110// saveState is the lead's save state as the server knows it: "Saved"
111// after a scriptless save, blank otherwise; the script sets it as it
112// saves.
113func saveState(r *http.Request) savestatus.Data {
114 d := savestatus.Data{Saved: "Saved", Saving: "Saving…", Retrying: "Saving…", Failed: "Couldn't save", Label: "Save state"}
115 if r.URL.Query().Get("saved") != "" {
116 d.State = "saved"
117 }
118 return d
119}
120
121func frameFactsOf(r *http.Request, name string, data map[string]any, nav []NavItem) frameFacts {
122 f := frameFacts{Here: r.URL.RequestURI()}
123 f.TypeKey, _ = data["TypeKey"].(string)
124 for _, n := range nav {
125 if n.Current {
126 f.Singular, f.Title = n.Singular, n.Label
127 }
128 }
129 if c, ok := data["Card"].(CardView); ok && c.Card.ID != 0 {
130 f.CardID, f.Title, f.Singular = c.Card.ID, c.Title(), c.TypeLabel
131 }
132 if c, ok := data["Open"].(*CardView); ok && c != nil {
133 f.CardID = c.Card.ID
134 }
135 switch name {
136 case "fields":
137 f.Title = "Fields"
138 case "field_new":
139 f.Title = "New field"
140 case "field_edit":
141 f.Title = "Edit field"
142 case "type_new":
143 f.Title = "New type"
144 case "card_layout":
145 f.Title = "Card layout"
146 case "card_new":
147 f.Title = "New " + lower(f.Singular)
148 case "contract_prepare":
149 f.Title = "Contracts"
150 }
151 f.Saving = name == "index" || name == "card_show" || name == "card_edit" || name == "history"
152 return f
153}
154
155func menusFor(f frameFacts) menuBar {
156 newCard := menuItem{Kind: "link", Label: "New " + lower(orText(f.Singular, "card")), Href: "/cards/new"}
157 if f.TypeKey != "" {
158 newCard.Href += "?type=" + f.TypeKey
159 }
160 // File is first, as in Docs, Slides and Mapa (level-up build note
161 // 24). Its Import from Notion… comes with the Notion databases
162 // phase, after New and before the card's rows, as in Mapa; until
163 // Fichas has an importer there is no row for it.
164 file := []menuItem{newCard}
165 if f.CardID != 0 {
166 id := strconv.FormatInt(f.CardID, 10)
167 file = append(file,
168 menuItem{Kind: "link", Label: "Open full page", Href: "/cards/" + id},
169 menuItem{Kind: "link", Label: "History", Href: "/cards/" + id + "/history"},
170 menuItem{Kind: "divider"},
171 menuItem{Kind: "link", Label: "Delete…", Href: "/cards/" + id + "/delete", Danger: true})
172 }
173 edit := []menuItem{
174 {Kind: "command", Label: "Undo", Command: "undo", Key: "⌘Z"},
175 {Kind: "command", Label: "Redo", Command: "redo", Key: "⇧⌘Z"},
176 }
177 view := []menuItem{}
178 typ := []menuItem{}
179 if f.TypeKey != "" {
180 base := typeHref(f.TypeKey)
181 view = append(view,
182 menuItem{Kind: "link", Label: "Table", Href: base},
183 menuItem{Kind: "link", Label: "Cards", Href: base + "?view=cards"},
184 menuItem{Kind: "divider"},
185 menuItem{Kind: "form", Label: "Comfortable", Action: base + "/view", Fields: map[string]string{"density": DensityComfortable, "return_to": f.Here}},
186 menuItem{Kind: "form", Label: "Compact", Action: base + "/view", Fields: map[string]string{"density": DensityCompact, "return_to": f.Here}},
187 menuItem{Kind: "divider"})
188 typ = append(typ,
189 menuItem{Kind: "link", Label: "Fields", Href: base + "/fields"},
190 menuItem{Kind: "link", Label: "Card layout", Href: base + "/layout"},
191 menuItem{Kind: "divider"})
192 }
193 view = append(view, menuItem{Kind: "form", Label: "Narrow the side panel", Key: "⌘\\", Command: "fold", Action: "/view/rail", Fields: map[string]string{"rail": "rail", "return_to": f.Here}},
194 menuItem{Kind: "form", Label: "Widen the side panel", Command: "fold", Action: "/view/rail", Fields: map[string]string{"rail": "open", "return_to": f.Here}})
195 typ = append(typ, menuItem{Kind: "link", Label: "New type", Href: "/types/new"})
196 help := []menuItem{{Kind: "dialog", Label: "Keyboard shortcuts", Dialog: "fichas-keys", Key: "?"}}
197 return menuBar{Label: "Fichas", Menus: []menuView{
198 {ID: "file", Title: "File", Items: file},
199 {ID: "edit", Title: "Edit", Items: edit},
200 {ID: "view", Title: "View", Items: view},
201 {ID: "type", Title: "Type", Items: typ},
202 {ID: "help", Title: "Help", Items: help},
203 }}
204}
205
206func orText(s, def string) string {
207 if s == "" {
208 return def
209 }
210 return s
211}
212
213// searchHref is where Search goes: the list's Find box, the current
214// type's on a page that has one.
215func searchHref(f frameFacts) string {
216 if f.TypeKey != "" {
217 return typeHref(f.TypeKey) + "#find"
218 }
219 return "/"
220}
221
222// barActions: History on a card, and the primary New. The bar has no
223// Search (Paul, 2026-11-18: "I think we can just lose the search icon in
224// the header"): it is the rail's first row, the Menu sheet's below
225// 800px, and ⌘K (grid.js).
226func barActions(f frameFacts) []chrome.Action {
227 var actions []chrome.Action
228 if f.CardID != 0 {
229 actions = append(actions, chrome.Action{Item: menu.Item{Href: "/cards/" + strconv.FormatInt(f.CardID, 10) + "/history", Label: "History"}, Icon: icons.Icon("history")})
230 }
231 if f.TypeKey != "" && f.Singular != "" {
232 actions = append(actions, chrome.Action{Item: menu.Item{Href: "/cards/new?type=" + f.TypeKey, Label: "New " + lower(f.Singular)}, Primary: true, Icon: icons.Icon("plus")})
233 }
234 return actions
235}
236
237// fragment executes one named template of a page into HTML for a slot.
238func fragment(page, name string, data any) (template.HTML, error) {
239 var buf bytes.Buffer
240 if err := pages[page].ExecuteTemplate(&buf, name, data); err != nil {
241 return "", err
242 }
243 return template.HTML(buf.String()), nil
244}
245