| 1 | package fichas |
| 2 | |
| 3 | // The table's view preferences (Paul, 2026-11-12): how dense its rows are, |
| 4 | // and which columns it shows. Both are one person's, on one device, in |
| 5 | // cookies, as Appearance is: they change how a list looks to its reader |
| 6 | // and nothing about the records. Columns are remembered per card type; |
| 7 | // density is one choice for every list. |
| 8 | // |
| 9 | // The columns cookie holds what is HIDDEN, not what is shown, so a field |
| 10 | // added later appears in every reader's table without anyone opting in. |
| 11 | // Keys are opaque (f_ plus hex), so the cookie names no field. |
| 12 | |
| 13 | import ( |
| 14 | "net/http" |
| 15 | "strings" |
| 16 | ) |
| 17 | |
| 18 | const ( |
| 19 | densityCookie = "fichas-density" |
| 20 | // DensityComfortable is the default: room to read a row. |
| 21 | DensityComfortable = "comfortable" |
| 22 | // DensityCompact fits more rows on a screen. |
| 23 | DensityCompact = "compact" |
| 24 | // a year: a preference outlives a session. |
| 25 | prefMaxAge = 365 * 24 * 3600 |
| 26 | ) |
| 27 | |
| 28 | // densityOf is the request's density. |
| 29 | func densityOf(r *http.Request) string { |
| 30 | if c, err := r.Cookie(densityCookie); err == nil && c.Value == DensityCompact { |
| 31 | return DensityCompact |
| 32 | } |
| 33 | return DensityComfortable |
| 34 | } |
| 35 | |
| 36 | func columnsCookie(typeKey string) string { return "fichas-hide-" + typeKey } |
| 37 | |
| 38 | // ColumnChoice is one row of the Columns checklist. |
| 39 | type ColumnChoice struct { |
| 40 | Key, Label string |
| 41 | Shown bool |
| 42 | // Locked is the title column: every row is named by it and opens |
| 43 | // its card from it, so it cannot be hidden. |
| 44 | Locked bool |
| 45 | } |
| 46 | |
| 47 | // listColumns are the keys a table can show, in order, with their labels: |
| 48 | // every field that is not long text, then the latest call. |
| 49 | func listColumns(fields []FieldView) (keys []string, labels map[string]string) { |
| 50 | labels = map[string]string{} |
| 51 | for _, f := range fields { |
| 52 | if !isLong(f.Def) { |
| 53 | keys = append(keys, f.Def.Key) |
| 54 | labels[f.Def.Key] = f.Label |
| 55 | } |
| 56 | } |
| 57 | keys = append(keys, SortLastCall) |
| 58 | labels[SortLastCall] = "Last call" |
| 59 | return keys, labels |
| 60 | } |
| 61 | |
| 62 | // typeTitleKey is the field a type's cards are named by: Name where the |
| 63 | // type has it, otherwise its first field (CardView.titleKey's rule). |
| 64 | func typeTitleKey(fields []FieldView) string { |
| 65 | for _, f := range fields { |
| 66 | if f.Def.Key == FieldName { |
| 67 | return FieldName |
| 68 | } |
| 69 | } |
| 70 | if len(fields) > 0 { |
| 71 | return fields[0].Def.Key |
| 72 | } |
| 73 | return "" |
| 74 | } |
| 75 | |
| 76 | // shownColumns is the set of column keys this reader sees for a type. |
| 77 | func shownColumns(r *http.Request, typeKey string, fields []FieldView) map[string]bool { |
| 78 | hidden := map[string]bool{} |
| 79 | if c, err := r.Cookie(columnsCookie(typeKey)); err == nil { |
| 80 | for _, k := range strings.Split(c.Value, ".") { |
| 81 | hidden[k] = true |
| 82 | } |
| 83 | } |
| 84 | title := typeTitleKey(fields) |
| 85 | keys, _ := listColumns(fields) |
| 86 | shown := map[string]bool{} |
| 87 | for _, k := range keys { |
| 88 | if !hidden[k] || k == title { |
| 89 | shown[k] = true |
| 90 | } |
| 91 | } |
| 92 | return shown |
| 93 | } |
| 94 | |
| 95 | // columnChoices is the Columns checklist for a type. |
| 96 | func columnChoices(fields []FieldView, shown map[string]bool) []ColumnChoice { |
| 97 | keys, labels := listColumns(fields) |
| 98 | title := typeTitleKey(fields) |
| 99 | out := make([]ColumnChoice, 0, len(keys)) |
| 100 | for _, k := range keys { |
| 101 | out = append(out, ColumnChoice{Key: k, Label: labels[k], Shown: shown[k], Locked: k == title}) |
| 102 | } |
| 103 | return out |
| 104 | } |
| 105 | |
| 106 | // saveView is POST /t/{type}/view: a density, or the columns to show. |
| 107 | // It answers with the list it came from. |
| 108 | func (a *app) saveView(w http.ResponseWriter, r *http.Request) { |
| 109 | ti, err := a.currentType(r) |
| 110 | if err != nil { |
| 111 | a.notFoundOrFail(w, r, err) |
| 112 | return |
| 113 | } |
| 114 | fields, err := a.ix.Fields(ti.Key) |
| 115 | if err != nil { |
| 116 | a.fail(w, r, err) |
| 117 | return |
| 118 | } |
| 119 | _ = r.ParseForm() |
| 120 | switch d := r.PostForm.Get("density"); d { |
| 121 | case DensityComfortable, DensityCompact: |
| 122 | a.setPref(w, densityCookie, d, prefMaxAge) |
| 123 | } |
| 124 | if r.PostForm.Get("columns") == "1" { |
| 125 | show := map[string]bool{} |
| 126 | for _, k := range r.PostForm["show"] { |
| 127 | show[k] = true |
| 128 | } |
| 129 | keys, _ := listColumns(fields) |
| 130 | title := typeTitleKey(fields) |
| 131 | var hide []string |
| 132 | for _, k := range keys { |
| 133 | if !show[k] && k != title { |
| 134 | hide = append(hide, k) |
| 135 | } |
| 136 | } |
| 137 | if len(hide) == 0 { |
| 138 | a.setPref(w, columnsCookie(ti.Key), "", -1) |
| 139 | } else { |
| 140 | a.setPref(w, columnsCookie(ti.Key), strings.Join(hide, "."), prefMaxAge) |
| 141 | } |
| 142 | } |
| 143 | http.Redirect(w, r, listReturn(r.PostForm.Get("return_to"), typeHref(ti.Key)), http.StatusSeeOther) |
| 144 | } |
| 145 | |
| 146 | // setPref writes a preference cookie (maxAge -1 clears it). Secure |
| 147 | // whenever the app is served over HTTPS, as every other cookie it and |
| 148 | // estilo set is; derived from the origin, as the session cookie is, so a |
| 149 | // plain-http local preview still keeps it in Safari. |
| 150 | func (a *app) setPref(w http.ResponseWriter, name, value string, maxAge int) { |
| 151 | http.SetCookie(w, &http.Cookie{ |
| 152 | Name: name, Value: value, Path: "/", MaxAge: maxAge, |
| 153 | HttpOnly: true, SameSite: http.SameSiteLaxMode, |
| 154 | Secure: strings.HasPrefix(a.cfg.Origin, "https://"), |
| 155 | }) |
| 156 | } |
| 157 | |
| 158 | // listReturn is where a view change lands: the list it was made on (its |
| 159 | // search, sort and filters kept), or the type's list. Only this type's |
| 160 | // list or the home list: never another path, and never another origin. |
| 161 | func listReturn(to, base string) string { |
| 162 | if strings.ContainsAny(to, "\\\r\n\t") { |
| 163 | return base |
| 164 | } |
| 165 | path, _, _ := strings.Cut(to, "?") |
| 166 | if path == base || path == "/" { |
| 167 | return to |
| 168 | } |
| 169 | return base |
| 170 | } |
| 171 | |