oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1// Package busy carries rastrillo's busy rule to the Oficina apps (Paul,
2// 2026-09-29): a submit button's label gives way to a spinner, the
3// button disables, its form refuses a second submit, and Back hands the
4// form back.
5//
6// When the spinner shows depends on the method (Paul, 2026-11-10: "The
7// busy 650ms should only really be for POST-style requests. For GET
8// style requests (like updating a view), it should either be instant if
9// it makes sense, or loading state only if the request takes more than
10// 500ms."). A POST's shows at once and for at least 650ms, the submit
11// held until then, because a save should read as a deliberate beat. A
12// GET goes at once, and its button shows nothing unless the page is
13// still there 500ms later: holding a search or a filter made every look
14// slower for a spinner nobody needed. Both refuse a second submit from
15// the click.
16//
17// The rule is rastrillo's, and rastrillo now takes it from Fiesta
18// (js/busy.js). The apps vendor older rastrillo files, and moving every
19// app to a newer rastrillo would carry far more than this, so estilo
20// carries three byte-exact copies instead, each recorded in
21// provenance.json and checked against it:
22//
23// - ShimJS: rastrillo.js as it is now the busy rule has left it, at the
24// rastrillo Docs pins (v0.29.1-0.20261027164201-054787952dd2), so the
25// rst-switch drag it carries and the switch CSS in that rastrillo's
26// tokens.css come from one tree. It also applies rastrillo's
27// colour-scheme menu ([rst-scheme]) in place, its only submit
28// listener, which no other form reaches. An app replaces its vendored
29// static/rastrillo.js with these bytes — its old copy still carries
30// the old busy rule, and two busy rules on one page fight over every
31// submit.
32// - JS: busy.js, the rule itself, from Fiesta.
33// - CSS: the spinner-in-place block from rastrillo's tokens.css, for a
34// tokens.css that predates it. Link it after tokens.css.
35//
36// When an app next re-vendors rastrillo for real, it takes all three from
37// there and drops this package.
38package busy
39
40import (
41 "crypto/sha256"
42 _ "embed"
43 "encoding/hex"
44 "net/http"
45 "strconv"
46 "strings"
47)
48
49//go:embed rastrillo.js
50var shimJS []byte
51
52//go:embed busy.js
53var busyJS []byte
54
55//go:embed busy.css
56var busyCSS []byte
57
58// ShimJS is rastrillo.js without the busy rule, for an app's static/.
59func ShimJS() []byte { return append([]byte(nil), shimJS...) }
60
61// JS is busy.js.
62func JS() []byte { return append([]byte(nil), busyJS...) }
63
64// CSS is the spinner-in-place block.
65func CSS() []byte { return append([]byte(nil), busyCSS...) }
66
67// Prefix is the URL path Handler serves under. Its version segment is the
68// two files' own digest, so new bytes are always a new URL: both are
69// cached for a year, immutably, and no one has to remember to bump it.
70var Prefix = func() string {
71 sum := sha256.Sum256(append(append([]byte(nil), busyJS...), busyCSS...))
72 return "/static/estilo/busy/" + hex.EncodeToString(sum[:6]) + "/"
73}()
74
75// Handler serves GET for Prefix + "busy.js" and Prefix + "busy.css", and
76// nothing else. Public and sessionless: the sign-in form needs it too.
77func Handler() http.Handler {
78 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
79 var body []byte
80 var kind string
81 switch strings.TrimPrefix(r.URL.Path, Prefix) {
82 case "busy.js":
83 body, kind = busyJS, "text/javascript; charset=utf-8"
84 case "busy.css":
85 body, kind = busyCSS, "text/css; charset=utf-8"
86 }
87 if body == nil || !strings.HasPrefix(r.URL.Path, Prefix) {
88 http.NotFound(w, r)
89 return
90 }
91 if r.Method != http.MethodGet && r.Method != http.MethodHead {
92 w.Header().Set("Allow", "GET, HEAD")
93 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
94 return
95 }
96 w.Header().Set("Content-Type", kind)
97 w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
98 w.Header().Set("X-Content-Type-Options", "nosniff")
99 w.Header().Set("Content-Length", strconv.Itoa(len(body)))
100 if r.Method == http.MethodHead {
101 return
102 }
103 _, _ = w.Write(body)
104 })
105}
106