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 PaletteSlate = "slate" // dark slate and lavender, made for StyleModern ) // Values for PageHeader.Style, the shape of the chrome, rendered as // . "" is StyleModern, the default since v0.5.0 // (floating sidebar card with group headings and icons, rounded // components); StyleClassic is the original look, still selectable // from the appearance menu. const ( StyleClassic = "classic" StyleModern = "modern" ) // 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" StyleCookie = "yw_style" ) // Choice is one entry of the appearance menu. type Choice struct { ID string Label string // English LabelDE string Symbol string } // Themes, Palettes and Styles 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", ""}, {PaletteSlate, "Slate", "Schiefer", ""}, } Styles = []Choice{ {StyleModern, "Modern", "Modern", ""}, {StyleClassic, "Classic", "Klassisch", ""}, } ) func known(choices []Choice, id string) bool { for _, c := range choices { if c.ID == id { return true } } return false } // ApplyAppearance overrides h.Theme, h.Palette and h.Style 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 } if c, err := r.Cookie(StyleCookie); err == nil && known(Styles, c.Value) { h.Style = 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, palette and/or style choice from a // POST form (fields "theme", "palette", "style", "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, style := r.FormValue("theme"), r.FormValue("palette"), r.FormValue("style") if (theme == "" && palette == "" && style == "") || (theme != "" && !known(Themes, theme)) || (palette != "" && !known(Palettes, palette)) || (style != "" && !known(Styles, style)) { http.Error(w, "unknown theme, palette or style", 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) } if style != "" { set(StyleCookie, style) } 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, StyleHeading string Themes, Palettes, Styles []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, style := h.Theme, h.Palette, h.Style if theme == "" { theme = ThemeDark } if palette == "" { palette = PaletteDefault } if style == "" { style = StyleModern } m := AppearanceMenuData{ Themes: items(Themes, theme), Palettes: items(Palettes, palette), Styles: items(Styles, style), ThemeHeading: "Theme", PaletteHeading: "Palette", StyleHeading: "Style", } if de { m.ThemeHeading, m.PaletteHeading, m.StyleHeading = "Modus", "Farben", "Stil" } for _, t := range m.Themes { if t.Current { m.Current = t } } return m }