| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "html/template" |
| 6 | "net/http" |
| 7 | |
| 8 | "amadan.net/oficina/mapa/internal/mapa/icons" |
| 9 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 10 | ) |
| 11 | |
| 12 | // Drawing a page's look (columns and icons spec §2.4, §3.2). The icon |
| 13 | // is decoration: aria-hidden, alt="", so no accessible name changes. |
| 14 | |
| 15 | // look is a page's icon as drawn wherever its title is: team pages |
| 16 | // only (§2.1). |
| 17 | func (page *entry) look() *Icon { |
| 18 | if page == nil || !page.team() { |
| 19 | return nil |
| 20 | } |
| 21 | return page.record.Icon |
| 22 | } |
| 23 | |
| 24 | // pageIcon draws icon in a glyph cell, or glyph when there is none: an |
| 25 | // emoji in the system's emoji font, or the image, square. |
| 26 | func pageIcon(icon *Icon, glyph string) template.HTML { |
| 27 | switch { |
| 28 | case icon == nil: |
| 29 | return icons.Icon(glyph) |
| 30 | case icon.Emoji != "": |
| 31 | return template.HTML(`<span mapa-icon aria-hidden="true">` + |
| 32 | html.EscapeString(icon.Emoji) + `</span>`) |
| 33 | case markup.IsImageID(icon.Image): |
| 34 | return template.HTML(`<img mapa-icon src="` + markup.ImagePrefix + |
| 35 | icon.Image + `" alt="" width="16" height="16" ` + |
| 36 | `loading="lazy" decoding="async">`) |
| 37 | } |
| 38 | return icons.Icon(glyph) |
| 39 | } |
| 40 | |
| 41 | // tabTitle is a page's title for the browser's tab: an emoji icon |
| 42 | // prefixes it; an image icon does not (§2.4). |
| 43 | func tabTitle(icon *Icon, title string) string { |
| 44 | if icon != nil && icon.Emoji != "" { |
| 45 | return icon.Emoji + " " + title |
| 46 | } |
| 47 | return title |
| 48 | } |
| 49 | |
| 50 | // BannerView is the band at a page's top, as the server draws it. |
| 51 | type BannerView struct { |
| 52 | Src string |
| 53 | Width, Height int |
| 54 | Y int |
| 55 | } |
| 56 | |
| 57 | // banner is a team page's banner, when it has one. |
| 58 | func (page *entry) banner() *Banner { |
| 59 | if page == nil || !page.team() { |
| 60 | return nil |
| 61 | } |
| 62 | return page.record.Banner |
| 63 | } |
| 64 | |
| 65 | // chipIcon is the page's icon as markup draws it in a chip. |
| 66 | func (page *entry) chipIcon() markup.ChipIcon { |
| 67 | if icon := page.look(); icon != nil { |
| 68 | return markup.ChipIcon{Emoji: icon.Emoji, Image: icon.Image} |
| 69 | } |
| 70 | return markup.ChipIcon{} |
| 71 | } |
| 72 | |
| 73 | // bannerOf is the page's band as the server draws it, or nil: no |
| 74 | // banner, or one whose image it cannot draw, which draws no band |
| 75 | // (§3.5). The size comes from the image's sealed record, so the |
| 76 | // browser reserves the box before a byte arrives. |
| 77 | func (app *app) bannerOf(request *http.Request, viewer Viewer, |
| 78 | page PageView) *BannerView { |
| 79 | if page.Banner == nil { |
| 80 | return nil |
| 81 | } |
| 82 | id := page.Banner.Image |
| 83 | states := app.index.ImageStates(request.Context(), ScopeOf(viewer), |
| 84 | page.ID, []string{id}) |
| 85 | state := states[id] |
| 86 | if !state.Available { |
| 87 | return nil |
| 88 | } |
| 89 | return &BannerView{Src: markup.ImagePrefix + id, Width: state.Width, |
| 90 | Height: state.Height, Y: BannerY(page.Banner.Y)} |
| 91 | } |
| 92 | |
| 93 | // drawableIcon is icon when it can be drawn on page for viewer: an |
| 94 | // emoji, or an image the server can still draw there. An old version's |
| 95 | // icon whose picture has gone draws as nothing, never a broken image. |
| 96 | func (app *app) drawableIcon(request *http.Request, viewer Viewer, |
| 97 | pageID int64, icon *Icon) *Icon { |
| 98 | if icon == nil || icon.Image == "" { |
| 99 | return icon |
| 100 | } |
| 101 | states := app.index.ImageStates(request.Context(), ScopeOf(viewer), |
| 102 | pageID, []string{icon.Image}) |
| 103 | if !states[icon.Image].Available { |
| 104 | return nil |
| 105 | } |
| 106 | return icon |
| 107 | } |
| 108 | |