Monorepo for Tangled
tangled.org
1package pages
2
3import (
4 "fmt"
5 "html"
6 "net/http"
7 "strings"
8)
9
10// Notice performs a hx-oob-swap to replace the content of an element with a message.
11// Pass the id of the element and the message to display.
12func (s *Pages) Notice(w http.ResponseWriter, id, msg string) {
13 escaped := html.EscapeString(msg)
14 markup := fmt.Sprintf(`<span id="%s" hx-swap-oob="innerHTML">%s</span>`, id, escaped)
15
16 w.Header().Set("Content-Type", "text/html")
17 w.WriteHeader(http.StatusOK)
18 w.Write([]byte(markup))
19}
20
21func (s *Pages) NoticeHTML(w http.ResponseWriter, id string, trustedHTML string) {
22 markup := fmt.Sprintf(`<span id="%s" hx-swap-oob="innerHTML">%s</span>`, id, trustedHTML)
23
24 w.Header().Set("Content-Type", "text/html")
25 w.WriteHeader(http.StatusOK)
26 w.Write([]byte(markup))
27}
28
29func (s *Pages) NoticeHTMLWithClears(w http.ResponseWriter, id string, trustedHTML string, clearIDs ...string) {
30 var b strings.Builder
31 b.WriteString(fmt.Sprintf(`<span id="%s" hx-swap-oob="innerHTML">%s</span>`, id, trustedHTML))
32 for _, clearID := range clearIDs {
33 b.WriteString(fmt.Sprintf(`<span id="%s" hx-swap-oob="innerHTML"></span>`, clearID))
34 }
35
36 w.Header().Set("Content-Type", "text/html")
37 w.WriteHeader(http.StatusOK)
38 w.Write([]byte(b.String()))
39}
40
41// HxRefresh is a client-side full refresh of the page.
42func (s *Pages) HxRefresh(w http.ResponseWriter) {
43 w.Header().Set("HX-Refresh", "true")
44 w.WriteHeader(http.StatusOK)
45}
46
47// HxRedirect is a full page reload with a new location.
48func (s *Pages) HxRedirect(w http.ResponseWriter, location string) {
49 w.Header().Set("HX-Redirect", location)
50 w.WriteHeader(http.StatusOK)
51}
52
53// HxLocation is an SPA-style navigation to a new location.
54func (s *Pages) HxLocation(w http.ResponseWriter, location string) {
55 w.Header().Set("HX-Location", location)
56 w.WriteHeader(http.StatusOK)
57}