package layout import ( "net/http" "net/url" "strings" "teruna.dev/yetano/yetano-webui-template/httpsec" ) // Values for PageHeader.Palette; each has its own block in // static/style.css. "" is PaletteDefault. const ( PaletteDefault = "default" PaletteGraphite = "graphite" // grey and violet, after nessajina.tv PaletteContrast = "contrast" // high contrast ) // Cookies AppearanceHandler sets and ApplyAppearance reads. Scoped to // the consumer's BasePath, so two dashboards on one host keep their own // choice. const ( ThemeCookie = "yw_theme" PaletteCookie = "yw_palette" ) // Choice is one entry of the appearance menu. type Choice struct { ID string Label string // English LabelDE string Symbol string } // Themes and Palettes are the choices ApplyAppearance and // AppearanceHandler accept, in menu order. var ( Themes = []Choice{ {ThemeSystem, "System", "System", "◐"}, {ThemeLight, "Light", "Hell", "☀"}, {ThemeDark, "Dark", "Dunkel", "☾"}, } Palettes = []Choice{ {PaletteDefault, "Yetano", "Yetano", ""}, {PaletteGraphite, "Graphite", "Graphit", ""}, {PaletteContrast, "High contrast", "Hoher Kontrast", ""}, } ) func known(choices []Choice, id string) bool { for _, c := range choices { if c.ID == id { return true } } return false } // ApplyAppearance overrides h.Theme and h.Palette with the visitor's // choice from the cookies AppearanceHandler sets (unknown values are // ignored, so the consumer's own defaults stay), and records the // current page as AppearanceBack for the menu's return trip. Call it // after BasePath is set. Resolving this on the server lets the page // arrive in the right colors without an inline script. func (h *PageHeader) ApplyAppearance(r *http.Request) { if c, err := r.Cookie(ThemeCookie); err == nil && known(Themes, c.Value) { h.Theme = c.Value } if c, err := r.Cookie(PaletteCookie); err == nil && known(Palettes, c.Value) { h.Palette = c.Value } path := r.URL.Path if h.BasePath != "" && !strippedPath(r) && (path == h.BasePath || strings.HasPrefix(path, h.BasePath+"/")) { path = strings.TrimPrefix(path, h.BasePath) } if path == "" { path = "/" } back := url.URL{Path: path, RawQuery: r.URL.RawQuery} h.AppearanceBack = WithoutFlash(back.String()) } // strippedPath reports whether r.URL.Path is already relative to the // UI's base path, i.e. a handler such as http.StripPrefix cut it down // from what the client asked for. Those keep RequestURI as sent, so a // path that no longer matches RequestURI's was stripped -- and must not // be stripped again: for base path "/feeds" the relative page "/feeds" // is a page of its own, not the start page. Without a RequestURI (a // request built in code) only a whole leading BasePath segment is cut, // so "/r" never eats the start of "/release-notes". func strippedPath(r *http.Request) bool { if r.RequestURI == "" { return false } u, err := url.ParseRequestURI(r.RequestURI) if err != nil { return false } return u.Path != r.URL.Path } // AppearanceHandler stores a theme and/or palette choice from a POST // form (fields "theme", "palette", "back") in cookies and redirects to // back, a BasePath-relative path inside this UI (anything else goes to // the start page, so it is no open redirect). Register it for POST only // at BasePath + PageHeader.AppearancePath; other methods get 405. // // No CSRF token: a foreign page could at worst change the colors. func AppearanceHandler(basePath string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { w.Header().Set("Allow", http.MethodPost) http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } theme, palette := r.FormValue("theme"), r.FormValue("palette") if (theme == "" && palette == "") || (theme != "" && !known(Themes, theme)) || (palette != "" && !known(Palettes, palette)) { http.Error(w, "unknown theme or palette", http.StatusBadRequest) return } set := func(name, value string) { http.SetCookie(w, &http.Cookie{Name: name, Value: value, Path: httpsec.CookiePath(basePath), MaxAge: 365 * 24 * 3600, HttpOnly: true, SameSite: http.SameSiteLaxMode}) } if theme != "" { set(ThemeCookie, theme) } if palette != "" { set(PaletteCookie, palette) } http.Redirect(w, r, basePath+httpsec.SafeNext(r.FormValue("back"), ""), http.StatusSeeOther) }) } // AppearanceItem is one button of the rendered appearance menu. type AppearanceItem struct { ID, Label, Symbol string Current bool } // AppearanceMenuData is what the navbar template renders the menu from. type AppearanceMenuData struct { Current AppearanceItem // the active theme, shown on the closed menu ThemeHeading, PaletteHeading string Themes, Palettes []AppearanceItem } // AppearanceMenu builds the menu's data, labeled in German when Lang // starts with "de", otherwise in English. The navbar template calls it. func (h PageHeader) AppearanceMenu() AppearanceMenuData { de := strings.HasPrefix(h.Lang, "de") items := func(choices []Choice, current string) []AppearanceItem { out := make([]AppearanceItem, len(choices)) for i, c := range choices { label := c.Label if de { label = c.LabelDE } out[i] = AppearanceItem{ID: c.ID, Label: label, Symbol: c.Symbol, Current: c.ID == current} } return out } theme, palette := h.Theme, h.Palette if theme == "" { theme = ThemeDark } if palette == "" { palette = PaletteDefault } m := AppearanceMenuData{ Themes: items(Themes, theme), Palettes: items(Palettes, palette), ThemeHeading: "Theme", PaletteHeading: "Palette", } if de { m.ThemeHeading, m.PaletteHeading = "Modus", "Farben" } for _, t := range m.Themes { if t.Current { m.Current = t } } return m }