| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "slices" |
| 7 | |
| 8 | "github.com/go-chi/chi/v5" |
| 9 | ) |
| 10 | |
| 11 | // The sidebar and its rail (level-up spec §3.2), and the two view |
| 12 | // cookies. A view preference is not a write: it sets a cookie, touches |
| 13 | // no row and logs no event, so gate 8 lists these routes as view-only. |
| 14 | |
| 15 | const ( |
| 16 | sidebarCookie = "mapa-sidebar" |
| 17 | linksCookie = "mapa-links" |
| 18 | // cookieYear is how long a view preference is kept. |
| 19 | cookieYear = 365 * 24 * 60 * 60 |
| 20 | ) |
| 21 | |
| 22 | // sideView is the sidebar's data. |
| 23 | type sideView struct { |
| 24 | // State is the mapa-sidebar cookie: "open", "rail", or "" for the |
| 25 | // default, which CSS decides by width (open from 80rem; §11 Q4, |
| 26 | // proposed, awaiting Paul). |
| 27 | State string |
| 28 | Places []NavItem |
| 29 | // Pinned and Recent are the viewer's own (§3.2): pins (§11 Q1) and |
| 30 | // the pages they last wrote in. |
| 31 | Pinned, Recent []sideLink |
| 32 | Return string |
| 33 | } |
| 34 | |
| 35 | // sideLink is one page row. |
| 36 | type sideLink struct { |
| 37 | Label, Href, Icon string |
| 38 | // Look is the page's own icon, drawn in place of Icon (columns and |
| 39 | // icons spec §2.4). |
| 40 | Look *Icon |
| 41 | // Here is the page on screen; Current, whether this row carries the |
| 42 | // one full highlight (light): the first row that is Here. |
| 43 | Here, Current bool |
| 44 | // Private is a note of the viewer's own, pinned: it shows a lock. |
| 45 | Private bool |
| 46 | // Menu is the row's ⋯ (build note 88), from what page allows. |
| 47 | Menu []menuEntry |
| 48 | page PageView |
| 49 | id int64 |
| 50 | // Gutter is a Pinned row, which keeps a cell for an arrow; Arrow, |
| 51 | // that its page holds pages the viewer may open; Open, that they are |
| 52 | // shown, as Inside; Depth, its indent; More and All, how many more |
| 53 | // there are than are listed, and all of them (side_inside.go). |
| 54 | Gutter, Arrow, Open bool |
| 55 | Inside []sideLink |
| 56 | Depth, More, All int |
| 57 | // Return is where the arrow's form comes back to; Public, the |
| 58 | // page's public id; ListID, the open list's own id, unique however |
| 59 | // often a page is shown. |
| 60 | Return, Public, ListID string |
| 61 | } |
| 62 | |
| 63 | func (app *app) side(request *http.Request, page *view) sideView { |
| 64 | // The places are copied: the bar's Menu sheet, below 800px, has no |
| 65 | // page rows, so its places keep their own highlight. |
| 66 | side := sideView{State: sidebarState(request), |
| 67 | Places: slices.Clone(page.Nav), Return: page.Return} |
| 68 | side.Pinned, side.Recent = app.sideLists(request, page) |
| 69 | if len(side.Pinned) > 0 { |
| 70 | current := "" |
| 71 | if page.Chrome != nil && page.Chrome.Page != nil { |
| 72 | current = page.Chrome.Page.Href |
| 73 | } |
| 74 | app.index.insideRows(request.Context(), ScopeOf(page.Viewer), |
| 75 | side.Pinned, openRows(request), current) |
| 76 | setReturn(side.Pinned, side.Return) |
| 77 | } |
| 78 | side.light() |
| 79 | side.menus() |
| 80 | return side |
| 81 | } |
| 82 | |
| 83 | // menus gives each page row its ⋯ (build note 88): a row in Recently |
| 84 | // edited is pinned too when Pinned holds its page. |
| 85 | func (side *sideView) menus() { |
| 86 | back, pinned := withoutNotice(side.Return), map[string]bool{} |
| 87 | for index := range side.Pinned { |
| 88 | pinned[side.Pinned[index].Href] = true |
| 89 | } |
| 90 | menuRows(side.Pinned, pinned, back) |
| 91 | menuRows(side.Recent, pinned, back) |
| 92 | } |
| 93 | |
| 94 | // withoutNotice is a return path less its ?done= notice, which said |
| 95 | // what the last action did and must not be said again after a pin. |
| 96 | func withoutNotice(path string) string { |
| 97 | parsed, err := url.Parse(path) |
| 98 | if err != nil || !parsed.Query().Has("done") { |
| 99 | return path |
| 100 | } |
| 101 | query := parsed.Query() |
| 102 | query.Del("done") |
| 103 | parsed.RawQuery = query.Encode() |
| 104 | return parsed.RequestURI() |
| 105 | } |
| 106 | |
| 107 | // light leaves exactly one full highlight (Paul, 2026-11-16; build |
| 108 | // note 80): on the page's own row when it has one, the first of |
| 109 | // Pinned and Recently edited, and otherwise on its place. The rail |
| 110 | // hides the rows, so there the place takes it: a place that holds the |
| 111 | // page is drawn twice, unlit for the open sidebar and lit for the rail, |
| 112 | // and CSS shows the one that fits. The cookie, the width, and a fold in |
| 113 | // place all choose by CSS alone, with or without script. |
| 114 | func (side *sideView) light() { |
| 115 | var here *sideLink |
| 116 | for _, list := range [][]sideLink{side.Pinned, side.Recent} { |
| 117 | lightRows(list, &here) |
| 118 | } |
| 119 | if here == nil { |
| 120 | return |
| 121 | } |
| 122 | for index := range side.Places { |
| 123 | place := &side.Places[index] |
| 124 | place.Holds, place.Current = place.Current, false |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // sidebarState is the cookie, or "" when it holds nothing we know. |
| 129 | func sidebarState(request *http.Request) string { |
| 130 | cookie, err := request.Cookie(sidebarCookie) |
| 131 | if err != nil { |
| 132 | return "" |
| 133 | } |
| 134 | switch cookie.Value { |
| 135 | case "open", "rail": |
| 136 | return cookie.Value |
| 137 | } |
| 138 | return "" |
| 139 | } |
| 140 | |
| 141 | // linksState is the Links panel's cookie: open unless exactly "closed", |
| 142 | // like Docs' docs-inspector. |
| 143 | func linksState(request *http.Request) string { |
| 144 | if cookie, err := request.Cookie(linksCookie); err == nil && |
| 145 | cookie.Value == "closed" { |
| 146 | return "closed" |
| 147 | } |
| 148 | return "open" |
| 149 | } |
| 150 | |
| 151 | func (app *app) viewRoutes(router chi.Router) { |
| 152 | router.Post("/view/sidebar", app.viewSidebar) |
| 153 | router.Post("/view/links", app.viewLinks) |
| 154 | router.Post("/view/open", app.viewOpen) |
| 155 | router.Get("/p/{id}/inside", app.insidePage) |
| 156 | router.Get("/help/markdown", app.helpMarkdown) |
| 157 | } |
| 158 | |
| 159 | // helpMarkdown is Help → Markdown in Mapa (review M1): the subset a page |
| 160 | // is written in, and nothing it does not read. |
| 161 | func (app *app) helpMarkdown(writer http.ResponseWriter, |
| 162 | request *http.Request) { |
| 163 | viewer, err := app.viewer(request) |
| 164 | if err != nil { |
| 165 | app.fail(writer, request, err) |
| 166 | return |
| 167 | } |
| 168 | app.show(writer, request, http.StatusOK, "help_markdown", view{ |
| 169 | Title: "Markdown in Mapa", Nav: nav(""), Viewer: viewer}) |
| 170 | } |
| 171 | |
| 172 | // viewSidebar folds or opens the sidebar. "state" names the new state; |
| 173 | // without it the form toggles from what the page showed ("shown"). |
| 174 | func (app *app) viewSidebar(writer http.ResponseWriter, |
| 175 | request *http.Request) { |
| 176 | state := request.PostFormValue("state") |
| 177 | if state != "open" && state != "rail" { |
| 178 | state = "rail" |
| 179 | if request.PostFormValue("shown") == "rail" { |
| 180 | state = "open" |
| 181 | } |
| 182 | } |
| 183 | setViewCookie(writer, sidebarCookie, state) |
| 184 | seeOther(writer, request, localReturn(request.PostFormValue("return"))) |
| 185 | } |
| 186 | |
| 187 | // viewLinks shows or hides the Links panel. |
| 188 | func (app *app) viewLinks(writer http.ResponseWriter, |
| 189 | request *http.Request) { |
| 190 | state := request.PostFormValue("state") |
| 191 | if state != "open" && state != "closed" { |
| 192 | state = "closed" |
| 193 | if linksState(request) == "closed" { |
| 194 | state = "open" |
| 195 | } |
| 196 | } |
| 197 | setViewCookie(writer, linksCookie, state) |
| 198 | seeOther(writer, request, localReturn(request.PostFormValue("return"))) |
| 199 | } |
| 200 | |
| 201 | func setViewCookie(writer http.ResponseWriter, name, value string) { |
| 202 | http.SetCookie(writer, &http.Cookie{Name: name, Value: value, |
| 203 | Path: "/", MaxAge: cookieYear, SameSite: http.SameSiteLaxMode, |
| 204 | Secure: true}) |
| 205 | } |
| 206 | |
| 207 | // localReturn is a path on this site, or "/". It is safeReturn's rule, |
| 208 | // the stronger one (review L): no scheme, no host, no "//", no |
| 209 | // backslash a browser might read as a slash, and no control character |
| 210 | // at all, which some clients drop, turning "/\v/evil" into "//evil". |
| 211 | func localReturn(target string) string { |
| 212 | if safe := safeReturn(target); safe != "" { |
| 213 | return safe |
| 214 | } |
| 215 | return "/" |
| 216 | } |
| 217 | |
| 218 | // pinned reports whether the page on screen is one of the viewer's pins. |
| 219 | func (side sideView) pinned() bool { |
| 220 | for _, link := range side.Pinned { |
| 221 | if link.Here { |
| 222 | return true |
| 223 | } |
| 224 | } |
| 225 | return false |
| 226 | } |
| 227 | |