| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "embed" |
| 7 | "html/template" |
| 8 | "io/fs" |
| 9 | "net/http" |
| 10 | "testing/fstest" |
| 11 | |
| 12 | "amadan.net/oficina/estilo.git/base" |
| 13 | "amadan.net/oficina/estilo.git/busy" |
| 14 | "amadan.net/oficina/estilo.git/chrome" |
| 15 | "amadan.net/oficina/estilo.git/credits" |
| 16 | "amadan.net/oficina/estilo.git/menu" |
| 17 | "amadan.net/oficina/estilo.git/savestatus" |
| 18 | "amadan.net/oficina/estilo.git/sky" |
| 19 | "amadan.net/oficina/estilo.git/theme" |
| 20 | "amadan.net/rastrillo/rastrillo" |
| 21 | "amadan.net/rastrillo/rastrillo/ui" |
| 22 | |
| 23 | // The en pack: rastrillo refuses to start without the pack for its |
| 24 | // default locale, and Mapa speaks English. |
| 25 | _ "amadan.net/rastrillo/rastrillo/locales/en" |
| 26 | |
| 27 | "amadan.net/oficina/mapa/internal/mapa/icons" |
| 28 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 29 | ) |
| 30 | |
| 31 | //go:embed templates static |
| 32 | var appFS embed.FS |
| 33 | |
| 34 | // themeSlug is Mapa's entry on estilo's ring: hue 125 and its own mark, |
| 35 | // placed by Paul on 2026-11-13 (estilo f724edb). |
| 36 | const themeSlug = "mapa" |
| 37 | |
| 38 | // assets is every static file, fingerprinted: the suite's files taken |
| 39 | // straight from the estilo and rastrillo this module pins, as Portero |
| 40 | // does, so nothing is copied and nothing can drift; then Mapa's own. |
| 41 | var assets = rastrillo.NewAssets(func() fs.FS { |
| 42 | accent, ok := theme.ForApp(themeSlug) |
| 43 | if !ok { |
| 44 | panic("estilo's theme ring has no " + themeSlug) |
| 45 | } |
| 46 | files := fstest.MapFS{ |
| 47 | "static/tokens.css": {Data: ui.TokensCSS()}, |
| 48 | "static/theme.css": {Data: accent}, |
| 49 | "static/base.css": {Data: base.CSS()}, |
| 50 | "static/shell.css": {Data: ui.ShellCSS()}, |
| 51 | "static/shell.js": {Data: ui.ShellJS()}, |
| 52 | "static/rastrillo.js": {Data: busy.ShimJS()}, |
| 53 | "static/busy.js": {Data: busy.JS()}, |
| 54 | "static/busy.css": {Data: busy.CSS()}, |
| 55 | "static/chrome.css": {Data: chrome.CSS()}, |
| 56 | "static/chrome.js": {Data: chrome.JS()}, |
| 57 | "static/menu.css": {Data: menu.CSS()}, |
| 58 | "static/sky.css": {Data: sky.CSS()}, |
| 59 | "static/savestatus.css": {Data: savestatus.CSS()}, |
| 60 | "static/credits.css": {Data: credits.CSS()}, |
| 61 | "static/brand/icon.svg": {Data: brandIcon()}, |
| 62 | } |
| 63 | own, err := fs.Glob(appFS, "static/*") |
| 64 | if err != nil { |
| 65 | panic(err) |
| 66 | } |
| 67 | for _, name := range own { |
| 68 | data, err := appFS.ReadFile(name) |
| 69 | if err != nil { |
| 70 | panic(err) |
| 71 | } |
| 72 | files[name] = &fstest.MapFile{Data: data} |
| 73 | } |
| 74 | return files |
| 75 | }()) |
| 76 | |
| 77 | // editorBundle is the editor's script (level-up §6.2): the one |
| 78 | // editor.<hash>.js make editor builds and commits, loaded only on pages |
| 79 | // the viewer may edit. |
| 80 | var editorBundle = func() func() string { |
| 81 | found, err := fs.Glob(appFS, "static/editor.*.js") |
| 82 | if err != nil || len(found) != 1 { |
| 83 | panic("static/ must hold exactly one editor bundle") |
| 84 | } |
| 85 | path := assets.Path(found[0]) |
| 86 | return func() string { return path } |
| 87 | }() |
| 88 | |
| 89 | // pages is one template tree per page: its shell, the shared partials and |
| 90 | // the page's own file, so every page can define "content". It writes |
| 91 | // dates in the default locale; pagesIn holds a clone of it for each |
| 92 | // offered locale, each bound to that locale's functions (locales.go). |
| 93 | var ( |
| 94 | pages = map[string]*template.Template{} |
| 95 | pagesIn = map[string]map[string]*template.Template{} |
| 96 | ) |
| 97 | |
| 98 | // Two shells. The app's own pages use rastrillo's sidebar shell; sign-in |
| 99 | // and errors keep the plain column, because Calma keeps app chrome out of |
| 100 | // the place the suite meets you. |
| 101 | var shellOf = map[string]string{ |
| 102 | "signin": "layout", "errors": "layout", |
| 103 | "pages": "shell", "page": "shell", |
| 104 | "page_new": "shell", "page_edit": "shell", "rename": "shell", |
| 105 | "links": "shell", "alias_free": "shell", "delete": "shell", |
| 106 | "image_remove": "shell", "look": "shell", "inside": "shell", |
| 107 | "deleted": "shell", "restore": "shell", "day": "shell", |
| 108 | "scratch": "shell", "scratch_new": "shell", "share": "shell", |
| 109 | "settings": "shell", "search": "shell", "conflict": "shell", |
| 110 | "revision": "shell", "blocks": "shell", "help_markdown": "shell", |
| 111 | "colophon": "shell", "colophon_public": "layout", |
| 112 | "import": "shell", "import_confirm": "shell", |
| 113 | } |
| 114 | |
| 115 | func init() { |
| 116 | for name, shell := range shellOf { |
| 117 | page := template.New("layout"). |
| 118 | Funcs(ui.Funcs(ui.WithIcons(icons.Icon, icons.Assets))). |
| 119 | Funcs(templateFuncs).Funcs(localeFuncs(nil)) |
| 120 | page = template.Must(page.ParseFS(ui.Templates(), "*.html")) |
| 121 | page = template.Must(page.ParseFS(menu.Templates(), |
| 122 | "templates/*.html")) |
| 123 | page = template.Must(page.ParseFS(chrome.Templates(), |
| 124 | "templates/*.html")) |
| 125 | page = template.Must(page.ParseFS(credits.Templates(), |
| 126 | "templates/*.html")) |
| 127 | page = template.Must(page.Parse(savestatus.Template())) |
| 128 | pages[name] = template.Must(page.ParseFS(appFS, |
| 129 | "templates/"+shell+".html", "templates/partials.html", |
| 130 | "templates/frame.html", "templates/history.html", |
| 131 | "templates/"+name+".html")) |
| 132 | } |
| 133 | for _, code := range locale.Offered() { |
| 134 | pagesIn[code] = map[string]*template.Template{} |
| 135 | for name, page := range pages { |
| 136 | clone := template.Must(page.Clone()) |
| 137 | pagesIn[code][name] = clone.Funcs(localeFuncs(locale.Get(code))) |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // templateIn is a page's templates in the request's locale. |
| 143 | func templateIn(request *http.Request, name string) *template.Template { |
| 144 | if chosen := localeIn(request); chosen != nil { |
| 145 | return pagesIn[chosen.Code()][name] |
| 146 | } |
| 147 | return pages[name] |
| 148 | } |
| 149 | |
| 150 | // render executes a page into a buffer before anything reaches the wire, |
| 151 | // so a template error is a clean 500, not half a page. |
| 152 | func render(writer http.ResponseWriter, request *http.Request, |
| 153 | name string, data any) { |
| 154 | var buffer bytes.Buffer |
| 155 | if err := templateIn(request, name).ExecuteTemplate(&buffer, "layout", |
| 156 | data); err != nil { |
| 157 | http.Error(writer, "something went wrong", |
| 158 | http.StatusInternalServerError) |
| 159 | return |
| 160 | } |
| 161 | _, _ = buffer.WriteTo(writer) |
| 162 | } |
| 163 | |
| 164 | // ErrorPage draws the error page: a panic and a handled failure draw the |
| 165 | // same page. Built.ErrorPage is rastrillo's ErrorPageFunc. |
| 166 | func ErrorPage(writer http.ResponseWriter, request *http.Request, |
| 167 | status int, ref string) { |
| 168 | writer.WriteHeader(status) |
| 169 | render(writer, request, "errors", map[string]any{"Status": status, |
| 170 | "Ref": ref, |
| 171 | "Theme": themeAttr(request)}) |
| 172 | } |
| 173 | |
| 174 | // ErrorPage is rastrillo's ErrorPageFunc, which runs outside the router: |
| 175 | // in the team's locale, since no request locale was chosen there. |
| 176 | func (built Built) ErrorPage(writer http.ResponseWriter, |
| 177 | request *http.Request, status int, ref string) { |
| 178 | if localeIn(request) == nil { |
| 179 | request = request.WithContext(context.WithValue(request.Context(), |
| 180 | localeKey{}, built.app.teamLocale())) |
| 181 | } |
| 182 | ErrorPage(writer, request, status, ref) |
| 183 | } |
| 184 | |
| 185 | // notFound is every "this is not here", including a page you may not |
| 186 | // see and a route you may not use. |
| 187 | func notFound(writer http.ResponseWriter, request *http.Request) { |
| 188 | ErrorPage(writer, request, http.StatusNotFound, "") |
| 189 | } |
| 190 | |
| 191 | // themeAttr is <html data-theme>: Light, Dark, or empty for System. |
| 192 | func themeAttr(request *http.Request) string { |
| 193 | return chrome.ThemeAttr(chrome.Appearance(request)) |
| 194 | } |
| 195 | |