| 1 | package fichas |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/rastrillo/rastrillo/ui" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "sort" |
| 9 | "strconv" |
| 10 | ) |
| 11 | |
| 12 | // listState is everything about a list page that lives in its URL: the |
| 13 | // view, the search, the filters, the sort and the page. It lives in the URL |
| 14 | // so that every state is a link — bookmarkable, shareable, and working with |
| 15 | // scripts off, where the search box is an ordinary GET form. |
| 16 | type listState struct { |
| 17 | base string |
| 18 | View string |
| 19 | Q string |
| 20 | Sort string |
| 21 | Desc bool |
| 22 | Filters map[string]string |
| 23 | Page int |
| 24 | // Card is the card open beside the list (§5.4): navigation, not part |
| 25 | // of what the list shows. |
| 26 | Card int64 |
| 27 | } |
| 28 | |
| 29 | // parseList reads a list URL. Filters are read only for the type's choice |
| 30 | // fields, so an arbitrary query parameter cannot become a filter. |
| 31 | func parseList(r *http.Request, base string, fields []FieldView) listState { |
| 32 | return parseListValues(r.URL.Query(), base, fields) |
| 33 | } |
| 34 | |
| 35 | // parseListValues is parseList over a query already parsed: the list a |
| 36 | // cell edit was posted from (§5.1) reads its state this way. |
| 37 | func parseListValues(q url.Values, base string, fields []FieldView) listState { |
| 38 | l := listState{base: base, View: q.Get("view"), Q: q.Get("q"), Sort: q.Get("sort"), Desc: q.Get("dir") == "desc", Filters: map[string]string{}} |
| 39 | if l.View != "cards" { |
| 40 | l.View = "table" |
| 41 | } |
| 42 | l.Page, _ = strconv.Atoi(q.Get("page")) |
| 43 | l.Card, _ = strconv.ParseInt(q.Get("card"), 10, 64) |
| 44 | for _, f := range fields { |
| 45 | if f.Def.Kind == KindEnum { |
| 46 | if v := q.Get(f.Def.Key); v != "" { |
| 47 | l.Filters[f.Def.Key] = v |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | return l |
| 52 | } |
| 53 | |
| 54 | func (l listState) query() Query { |
| 55 | return Query{Text: l.Q, Filters: l.Filters, Sort: l.Sort, Desc: l.Desc, Page: l.Page, PerPage: 100} |
| 56 | } |
| 57 | |
| 58 | // href renders a state as a URL. Parameters are written in a fixed order so |
| 59 | // the same state is always the same link. |
| 60 | func (l listState) href() string { |
| 61 | v := url.Values{} |
| 62 | if l.View == "cards" { |
| 63 | v.Set("view", "cards") |
| 64 | } |
| 65 | if l.Q != "" { |
| 66 | v.Set("q", l.Q) |
| 67 | } |
| 68 | keys := make([]string, 0, len(l.Filters)) |
| 69 | for k := range l.Filters { |
| 70 | keys = append(keys, k) |
| 71 | } |
| 72 | sort.Strings(keys) |
| 73 | for _, k := range keys { |
| 74 | v.Set(k, l.Filters[k]) |
| 75 | } |
| 76 | if l.Sort != "" { |
| 77 | v.Set("sort", l.Sort) |
| 78 | if l.Desc { |
| 79 | v.Set("dir", "desc") |
| 80 | } |
| 81 | } |
| 82 | if l.Page > 1 { |
| 83 | v.Set("page", strconv.Itoa(l.Page)) |
| 84 | } |
| 85 | if l.Card > 0 { |
| 86 | v.Set("card", strconv.FormatInt(l.Card, 10)) |
| 87 | } |
| 88 | if len(v) == 0 { |
| 89 | return l.base |
| 90 | } |
| 91 | return l.base + "?" + v.Encode() |
| 92 | } |
| 93 | |
| 94 | // with returns a copy after a change. Any change except paging itself goes |
| 95 | // back to page 1 — a new search on page 7 of the old one is a page of |
| 96 | // nothing. |
| 97 | func (l listState) with(change func(*listState)) listState { |
| 98 | c := l |
| 99 | c.Filters = map[string]string{} |
| 100 | for k, v := range l.Filters { |
| 101 | c.Filters[k] = v |
| 102 | } |
| 103 | c.Page, c.Card = 0, 0 |
| 104 | change(&c) |
| 105 | return c |
| 106 | } |
| 107 | |
| 108 | // hidden is the state a search form must carry through, so typing a search |
| 109 | // does not throw away the filters and sort. |
| 110 | func (l listState) hidden() [][2]string { |
| 111 | var out [][2]string |
| 112 | if l.View == "cards" { |
| 113 | out = append(out, [2]string{"view", "cards"}) |
| 114 | } |
| 115 | keys := make([]string, 0, len(l.Filters)) |
| 116 | for k := range l.Filters { |
| 117 | keys = append(keys, k) |
| 118 | } |
| 119 | sort.Strings(keys) |
| 120 | for _, k := range keys { |
| 121 | out = append(out, [2]string{k, l.Filters[k]}) |
| 122 | } |
| 123 | if l.Sort != "" { |
| 124 | out = append(out, [2]string{"sort", l.Sort}) |
| 125 | if l.Desc { |
| 126 | out = append(out, [2]string{"dir", "desc"}) |
| 127 | } |
| 128 | } |
| 129 | return out |
| 130 | } |
| 131 | |
| 132 | // column is one sortable table header. |
| 133 | type column struct { |
| 134 | Label string |
| 135 | Href string |
| 136 | Sort string // "ascending", "descending" or "" |
| 137 | } |
| 138 | |
| 139 | // columns are the table's headers: every field that is not long text, then |
| 140 | // the latest call, less those this reader hid (listprefs.go). Clicking a |
| 141 | // header sorts by it; clicking it again flips. |
| 142 | func (l listState) columns(fields []FieldView, shown map[string]bool) []column { |
| 143 | var out []column |
| 144 | add := func(label, key string) { |
| 145 | if !shown[key] { |
| 146 | return |
| 147 | } |
| 148 | c := column{Label: label} |
| 149 | first := key == SortLastCall // most recent call first is the useful default |
| 150 | if l.Sort == key { |
| 151 | if l.Desc { |
| 152 | c.Sort = "descending" |
| 153 | } else { |
| 154 | c.Sort = "ascending" |
| 155 | } |
| 156 | } |
| 157 | c.Href = l.with(func(s *listState) { |
| 158 | s.Sort = key |
| 159 | if l.Sort == key { |
| 160 | s.Desc = !l.Desc |
| 161 | } else { |
| 162 | s.Desc = first |
| 163 | } |
| 164 | }).href() |
| 165 | out = append(out, c) |
| 166 | } |
| 167 | for _, f := range fields { |
| 168 | if !isLong(f.Def) { |
| 169 | add(f.Label, f.Def.Key) |
| 170 | } |
| 171 | } |
| 172 | add("Last call", SortLastCall) |
| 173 | return out |
| 174 | } |
| 175 | |
| 176 | // filterMenus is one dropdown per choice field, for the ui dropdown partial. |
| 177 | func (l listState) filterMenus(fields []FieldView) []map[string]any { |
| 178 | var out []map[string]any |
| 179 | for _, f := range fields { |
| 180 | if f.Def.Kind != KindEnum { |
| 181 | continue |
| 182 | } |
| 183 | key, current := f.Def.Key, l.Filters[f.Def.Key] |
| 184 | items := []map[string]any{{ |
| 185 | "Label": "All", "Current": current == "", |
| 186 | "Href": l.with(func(s *listState) { delete(s.Filters, key) }).href(), |
| 187 | }} |
| 188 | for _, opt := range f.Options { |
| 189 | opt := opt |
| 190 | items = append(items, map[string]any{ |
| 191 | "Label": opt, "Current": current == opt, |
| 192 | "Href": l.with(func(s *listState) { s.Filters[key] = opt }).href(), |
| 193 | }) |
| 194 | } |
| 195 | label := f.Label + ": All" |
| 196 | if current != "" { |
| 197 | label = f.Label + ": " + current |
| 198 | } |
| 199 | out = append(out, map[string]any{"Label": label, "Aria": "Filter by " + label, "Items": items}) |
| 200 | } |
| 201 | return out |
| 202 | } |
| 203 | |
| 204 | // pages is the ui pagination partial's items: first, last, the current page |
| 205 | // and its neighbours, with gaps between. |
| 206 | func (l listState) pages(res Result) []map[string]any { |
| 207 | if res.Pages <= 1 { |
| 208 | return nil |
| 209 | } |
| 210 | at := func(p int) string { c := l; c.Page = p; return c.href() } |
| 211 | var out []map[string]any |
| 212 | if res.Page > 1 { |
| 213 | out = append(out, map[string]any{"Label": "Previous", "Href": at(res.Page - 1)}) |
| 214 | } else { |
| 215 | out = append(out, map[string]any{"Label": "Previous", "Disabled": true}) |
| 216 | } |
| 217 | last := 0 |
| 218 | for p := 1; p <= res.Pages; p++ { |
| 219 | if p != 1 && p != res.Pages && (p < res.Page-1 || p > res.Page+1) { |
| 220 | continue |
| 221 | } |
| 222 | if last != 0 && p > last+1 { |
| 223 | out = append(out, map[string]any{"Gap": true}) |
| 224 | } |
| 225 | if p == res.Page { |
| 226 | out = append(out, map[string]any{"Label": strconv.Itoa(p), "Current": true}) |
| 227 | } else { |
| 228 | out = append(out, map[string]any{"Label": strconv.Itoa(p), "Href": at(p)}) |
| 229 | } |
| 230 | last = p |
| 231 | } |
| 232 | if res.Page < res.Pages { |
| 233 | out = append(out, map[string]any{"Label": "Next", "Href": at(res.Page + 1)}) |
| 234 | } else { |
| 235 | out = append(out, map[string]any{"Label": "Next", "Disabled": true}) |
| 236 | } |
| 237 | return out |
| 238 | } |
| 239 | |
| 240 | // countLine says what the list is showing, in words. |
| 241 | func countLine(L *ui.Localizer, res Result, filtered bool) string { |
| 242 | num := func(n int) string { return count(L, n) } |
| 243 | noun := res.Plural |
| 244 | if res.All == 1 && !filtered { |
| 245 | noun = res.Label // "1 customer", not "1 customers" |
| 246 | } |
| 247 | switch { |
| 248 | case res.All == 0: |
| 249 | return "" |
| 250 | case filtered && res.Total == 0: |
| 251 | return fmt.Sprintf("No %s match. %s in all.", lower(noun), num(res.All)) |
| 252 | case filtered && res.Pages > 1: |
| 253 | return fmt.Sprintf("%s–%s of %s matching %s (%s in all)", num(res.First), num(res.Last), num(res.Total), lower(noun), num(res.All)) |
| 254 | case filtered: |
| 255 | return fmt.Sprintf("%s of %s %s", num(res.Total), num(res.All), lower(noun)) |
| 256 | case res.Pages > 1: |
| 257 | return fmt.Sprintf("%s–%s of %s %s", num(res.First), num(res.Last), num(res.All), lower(noun)) |
| 258 | default: |
| 259 | return fmt.Sprintf("%s %s", num(res.All), lower(noun)) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | func lower(s string) string { |
| 264 | if s == "" { |
| 265 | return s |
| 266 | } |
| 267 | b := []rune(s) |
| 268 | if b[0] >= 'A' && b[0] <= 'Z' { |
| 269 | b[0] += 'a' - 'A' |
| 270 | } |
| 271 | return string(b) |
| 272 | } |
| 273 | |