| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "log/slog" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // The sidebar's arrows (columns and icons spec §5.3): a Pinned row whose |
| 11 | // page holds pages the viewer may open has a disclosure arrow, and each |
| 12 | // row it opens has its own, so the pages inside show one level at a |
| 13 | // time. Which rows are open is the cookie mapa-open, a view preference |
| 14 | // like the fold: the server draws open rows at first paint, POST |
| 15 | // /view/open sets it with no script, and it writes no row. |
| 16 | |
| 17 | const ( |
| 18 | openCookie = "mapa-open" |
| 19 | // maxOpen is how many open rows the cookie keeps, newest first. |
| 20 | maxOpen = 64 |
| 21 | // insideShown is how many pages one open row lists before its link |
| 22 | // to all of them; insideTotal caps the open rows the sidebar draws. |
| 23 | insideShown = 50 |
| 24 | insideTotal = 200 |
| 25 | // insideDepth is how many levels are indented; deeper ones keep the |
| 26 | // last indent. |
| 27 | insideDepth = 4 |
| 28 | ) |
| 29 | |
| 30 | // openRows is the cookie's set of open page public ids, in its order. |
| 31 | func openRows(request *http.Request) []string { |
| 32 | cookie, err := request.Cookie(openCookie) |
| 33 | if err != nil { |
| 34 | return nil |
| 35 | } |
| 36 | var out []string |
| 37 | for _, id := range strings.Fields(cookie.Value) { |
| 38 | if len(id) <= 20 && len(out) < maxOpen { |
| 39 | out = append(out, id) |
| 40 | } |
| 41 | } |
| 42 | return out |
| 43 | } |
| 44 | |
| 45 | // insideRows fills each Pinned row's arrow and, for an open one, the |
| 46 | // rows inside it, through the scoped accessor (§5.4). |
| 47 | func (index *Index) insideRows(ctx context.Context, scope Scope, |
| 48 | rows []sideLink, open []string, current string) { |
| 49 | if err := index.Sync(ctx); err != nil { |
| 50 | // The rows draw closed, with no arrows: the sidebar is still |
| 51 | // whole, and the next page tries again. |
| 52 | slog.Warn("sidebar: index sync", "err", err) |
| 53 | return |
| 54 | } |
| 55 | index.mutex.RLock() |
| 56 | defer index.mutex.RUnlock() |
| 57 | expand := opener{index: index, scope: scope, current: current, |
| 58 | open: map[string]bool{}} |
| 59 | for _, id := range open { |
| 60 | expand.open[id] = true |
| 61 | } |
| 62 | for at := range rows { |
| 63 | expand.row(&rows[at], 0, nil) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // opener draws the rows inside rows, counting what it has drawn. |
| 68 | type opener struct { |
| 69 | index *Index |
| 70 | scope Scope |
| 71 | current string |
| 72 | open map[string]bool |
| 73 | drawn int |
| 74 | } |
| 75 | |
| 76 | // row draws one row's arrow and, when it is open, the rows inside it. |
| 77 | // path is the row's ancestors in this sidebar, outermost first: a page |
| 78 | // on it is listed without an arrow (a cycle), and it names the open |
| 79 | // list, so a page shown twice has two ids (§5.3: inside-<row>-<page>). |
| 80 | func (expand *opener) row(row *sideLink, depth int, path []*sideLink) { |
| 81 | row.Gutter, row.Depth = true, min(depth, insideDepth) |
| 82 | for _, above := range path { |
| 83 | if above.id == row.id { |
| 84 | return |
| 85 | } |
| 86 | } |
| 87 | if !expand.open[row.page.Public] { |
| 88 | row.Arrow = expand.index.holdsAny(expand.scope, row.id) |
| 89 | return |
| 90 | } |
| 91 | // An open row past the cap is drawn open with its link to all its |
| 92 | // pages and no rows, so its arrow still closes it (Fable L5). |
| 93 | children := expand.index.insideLocked(expand.scope, row.id) |
| 94 | row.Arrow = len(children) > 0 |
| 95 | row.Open = row.Arrow |
| 96 | if !row.Open { |
| 97 | return |
| 98 | } |
| 99 | row.ListID = listID(path, row) |
| 100 | inner := append(append([]*sideLink{}, path...), row) |
| 101 | shown := children[:min(len(children), insideShown)] |
| 102 | for _, child := range shown { |
| 103 | if expand.drawn >= insideTotal { |
| 104 | break |
| 105 | } |
| 106 | expand.drawn++ |
| 107 | link := sideLinkOf(child, expand.scope, expand.current) |
| 108 | expand.row(&link, depth+1, inner) |
| 109 | row.Inside = append(row.Inside, link) |
| 110 | } |
| 111 | if len(row.Inside) < len(children) { |
| 112 | row.More, row.All = len(children)-len(row.Inside), len(children) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // listID is an open list's id: its row's path of public ids. |
| 117 | func listID(path []*sideLink, row *sideLink) string { |
| 118 | parts := []string{"inside"} |
| 119 | for _, above := range path { |
| 120 | parts = append(parts, above.page.Public) |
| 121 | } |
| 122 | return strings.Join(append(parts, row.page.Public), "-") |
| 123 | } |
| 124 | |
| 125 | // sideLinkOf is a page as a sidebar row. |
| 126 | func sideLinkOf(found *entry, scope Scope, current string) sideLink { |
| 127 | return sideLink{Label: found.label(scope.Locale), Href: found.href(), |
| 128 | Icon: sideIcon(found.kind), Look: found.look(), id: found.id, |
| 129 | Public: publicID(found.nonce), |
| 130 | Here: found.href() == current, Private: !found.team(), |
| 131 | page: PageView{Public: publicID(found.nonce), Kind: found.kind, |
| 132 | Rung: found.rung}} |
| 133 | } |
| 134 | |
| 135 | // viewOpen opens or closes one row's pages inside: POST /view/open with |
| 136 | // id and open (1 or 0). It sets the cookie and goes back, with script or |
| 137 | // without: script reads the sidebar from the page it goes back to |
| 138 | // (build note P2). |
| 139 | func (app *app) viewOpen(writer http.ResponseWriter, request *http.Request) { |
| 140 | id := request.PostFormValue("id") |
| 141 | open := openRows(request) |
| 142 | kept := []string{} |
| 143 | if request.PostFormValue("open") == "1" && id != "" && len(id) <= 20 { |
| 144 | kept = append(kept, id) |
| 145 | } |
| 146 | for _, each := range open { |
| 147 | if each != id && len(kept) < maxOpen { |
| 148 | kept = append(kept, each) |
| 149 | } |
| 150 | } |
| 151 | setViewCookie(writer, openCookie, strings.Join(kept, " ")) |
| 152 | seeOther(writer, request, localReturn(request.PostFormValue("return"))) |
| 153 | } |
| 154 | |
| 155 | // setReturn gives every row, open ones' rows included, the return path. |
| 156 | func setReturn(rows []sideLink, back string) { |
| 157 | for at := range rows { |
| 158 | rows[at].Return = back |
| 159 | setReturn(rows[at].Inside, back) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // lightRows lights the first row that is Here, in document order: each |
| 164 | // row, then the rows open inside it (build note 80's one highlight). |
| 165 | func lightRows(rows []sideLink, here **sideLink) { |
| 166 | for at := range rows { |
| 167 | rows[at].Here = rows[at].Here && *here == nil |
| 168 | rows[at].Current = rows[at].Here |
| 169 | if rows[at].Here { |
| 170 | *here = &rows[at] |
| 171 | } |
| 172 | lightRows(rows[at].Inside, here) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // menuRows gives each row, open ones' rows included, its ⋯. |
| 177 | func menuRows(rows []sideLink, pinned map[string]bool, back string) { |
| 178 | for at := range rows { |
| 179 | rows[at].Menu = rowMenu(rows[at].page, pinned[rows[at].Href], back) |
| 180 | menuRows(rows[at].Inside, pinned, back) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // insidePage is GET /p/{id}/inside: every page inside a team page, in |
| 185 | // its order, for a list the sidebar cuts at insideShown. Scoped as the |
| 186 | // arrow is; anything else is the app's 404. |
| 187 | func (app *app) insidePage(writer http.ResponseWriter, |
| 188 | request *http.Request) { |
| 189 | page, ok := app.teamOnly(writer, request) |
| 190 | if !ok { |
| 191 | return |
| 192 | } |
| 193 | viewer, err := app.viewer(request) |
| 194 | if err != nil { |
| 195 | app.fail(writer, request, err) |
| 196 | return |
| 197 | } |
| 198 | pages, err := app.index.Inside(request.Context(), ScopeOf(viewer), |
| 199 | page.ID) |
| 200 | if err != nil { |
| 201 | app.fail(writer, request, err) |
| 202 | return |
| 203 | } |
| 204 | rows := make([]sideLink, 0, len(pages)) |
| 205 | for _, found := range pages { |
| 206 | rows = append(rows, sideLinkOf(found, ScopeOf(viewer), "")) |
| 207 | } |
| 208 | app.show(writer, request, http.StatusOK, "inside", view{ |
| 209 | Title: "Pages inside " + page.Label, Nav: nav("/pages"), |
| 210 | Viewer: viewer, Data: map[string]any{"Page": page, "Rows": rows}}) |
| 211 | } |
| 212 | |