first commit
This commit is contained in:
commit
8c4dcae475
89 changed files with 24623 additions and 0 deletions
262
prefs.go
Normal file
262
prefs.go
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
// prefs.go - per-browser UI preferences stored in cookies, plus shared
|
||||
// cookie helpers used by the pins and history features.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// setCookie / delCookie are the single place cookie attributes are decided.
|
||||
|
||||
func setCookie(w http.ResponseWriter, name, val string) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: name,
|
||||
Value: val,
|
||||
Path: "/",
|
||||
MaxAge: 365 * 24 * 3600,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
HttpOnly: true,
|
||||
})
|
||||
}
|
||||
|
||||
func delCookie(w http.ResponseWriter, name string) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: name,
|
||||
Value: "",
|
||||
Path: "/",
|
||||
MaxAge: -1,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
HttpOnly: true,
|
||||
})
|
||||
}
|
||||
|
||||
const cookieValueMax = 3800
|
||||
|
||||
func writeListCookie(w http.ResponseWriter, name string, entries []string) []string {
|
||||
n, size := 0, 0
|
||||
for _, e := range entries {
|
||||
add := len(e)
|
||||
if n > 0 {
|
||||
add++ // the "|" separator
|
||||
}
|
||||
if size+add > cookieValueMax {
|
||||
break
|
||||
}
|
||||
size += add
|
||||
n++
|
||||
}
|
||||
entries = entries[:n]
|
||||
if len(entries) == 0 {
|
||||
delCookie(w, name)
|
||||
return entries
|
||||
}
|
||||
setCookie(w, name, strings.Join(entries, "|"))
|
||||
return entries
|
||||
}
|
||||
|
||||
type Prefs struct {
|
||||
DocView string // "images" (default), "split", "text"
|
||||
Theme string // "auto" (default), "light", "dark"
|
||||
PerPage int // docs per page (default 20); 0 means "all", which cardsPerPage bounds
|
||||
|
||||
ShowClassification bool // classification/dissemination marking on document cards (default true)
|
||||
ShowJournalists bool // journalist names on document cards (default true)
|
||||
ShowPeople bool // people line on document cards (default true)
|
||||
ShowPrograms bool // program names on document cards (default true)
|
||||
ShowSummary bool // short summary (description) on document cards (default true)
|
||||
ShowThumbs bool // thumbnails on search results & cards (default true)
|
||||
ShowCollCols bool // category tables: per-collection count columns always visible (default false, toggle on the page)
|
||||
TextOnly bool // suppress thumbnails and page images (default false)
|
||||
CardPagePreview bool // desktop: full page preview on card-thumb hover (default false, opt-in)
|
||||
ScrollDoc bool // doc view: cap pane height and scroll (default false)
|
||||
InfoSidebar bool // doc view: metadata beside the document instead of above (default false); cookie is "above"/"beside", not a 0/1 flag
|
||||
ProgramsAsList bool // doc view: programs one-per-line (default false, comma-separated)
|
||||
TagsAsList bool // doc view: tags one-per-line (default false, comma-separated)
|
||||
OpenInfo bool // doc view: "More info" expanded by default (default false)
|
||||
CollapsePubTogether bool // doc view: collapse "published in the same report" (default false)
|
||||
CollapseRelated bool // doc view: collapse related documents (default false)
|
||||
TrackRecent bool // record recently viewed documents (default false)
|
||||
TrackSearches bool // record recent searches (default false)
|
||||
IncludeFOIA bool // FOIA releases in search, category totals, /random and related documents when the URL names no collection (default false, opt-in)
|
||||
}
|
||||
|
||||
func (p Prefs) thumbsOn() bool { return p.ShowThumbs && !p.TextOnly }
|
||||
|
||||
const maxCardsPerPage = 500
|
||||
|
||||
func (p Prefs) cardsPerPage() int {
|
||||
if p.PerPage <= 0 || p.PerPage > maxCardsPerPage {
|
||||
return maxCardsPerPage
|
||||
}
|
||||
return p.PerPage
|
||||
}
|
||||
|
||||
func newCardView(p Prefs, docs []*Document) cardView {
|
||||
return cardView{
|
||||
Docs: docs,
|
||||
ShowThumbs: p.thumbsOn(),
|
||||
CardPagePreview: p.CardPagePreview,
|
||||
HideClassification: !p.ShowClassification,
|
||||
HideJournalists: !p.ShowJournalists,
|
||||
HidePeople: !p.ShowPeople,
|
||||
HidePrograms: !p.ShowPrograms,
|
||||
HideSummary: !p.ShowSummary,
|
||||
FOIAQS: p.foiaLinkQS(),
|
||||
}
|
||||
}
|
||||
|
||||
func (p Prefs) foiaLinkQS() string {
|
||||
return pageScope(nil, p, false).including(collectionFOIA).suffix()
|
||||
}
|
||||
|
||||
var boolPrefs = []struct {
|
||||
cookie string
|
||||
form string
|
||||
invert bool
|
||||
field func(*Prefs) *bool
|
||||
}{
|
||||
{"pref_hide_classification", "hide_classification", true, func(p *Prefs) *bool { return &p.ShowClassification }},
|
||||
{"pref_hide_journalists", "hide_journalists", true, func(p *Prefs) *bool { return &p.ShowJournalists }},
|
||||
{"pref_hide_people", "hide_people", true, func(p *Prefs) *bool { return &p.ShowPeople }},
|
||||
{"pref_hide_programs", "hide_programs", true, func(p *Prefs) *bool { return &p.ShowPrograms }},
|
||||
{"pref_hide_summary", "hide_summary", true, func(p *Prefs) *bool { return &p.ShowSummary }},
|
||||
{"pref_hide_thumbs", "hide_thumbs", true, func(p *Prefs) *bool { return &p.ShowThumbs }},
|
||||
{"pref_show_coll_cols", "show_coll_cols", false, func(p *Prefs) *bool { return &p.ShowCollCols }},
|
||||
{"pref_text_only", "text_only", false, func(p *Prefs) *bool { return &p.TextOnly }},
|
||||
{"pref_page_preview", "page_preview", false, func(p *Prefs) *bool { return &p.CardPagePreview }},
|
||||
{"pref_scroll_doc", "scroll_doc", false, func(p *Prefs) *bool { return &p.ScrollDoc }},
|
||||
{"pref_programs_list", "programs_list", false, func(p *Prefs) *bool { return &p.ProgramsAsList }},
|
||||
{"pref_tags_list", "tags_list", false, func(p *Prefs) *bool { return &p.TagsAsList }},
|
||||
{"pref_open_info", "open_info", false, func(p *Prefs) *bool { return &p.OpenInfo }},
|
||||
{"pref_collapse_pub", "collapse_pub", false, func(p *Prefs) *bool { return &p.CollapsePubTogether }},
|
||||
{"pref_collapse_related", "collapse_related", false, func(p *Prefs) *bool { return &p.CollapseRelated }},
|
||||
{"pref_track_recent", "track_recent", false, func(p *Prefs) *bool { return &p.TrackRecent }},
|
||||
{"pref_track_searches", "track_searches", false, func(p *Prefs) *bool { return &p.TrackSearches }},
|
||||
{"pref_include_foia", "include_foia", false, func(p *Prefs) *bool { return &p.IncludeFOIA }},
|
||||
}
|
||||
|
||||
var valuePrefs = []struct {
|
||||
cookie string
|
||||
form string
|
||||
allowed []string
|
||||
def string
|
||||
apply func(*Prefs, string)
|
||||
}{
|
||||
{"pref_doc_view", "doc_view", []string{"images", "split", "text"}, "images",
|
||||
func(p *Prefs, v string) { p.DocView = v }},
|
||||
{"pref_theme", "theme", []string{"auto", "light", "dark"}, "auto",
|
||||
func(p *Prefs, v string) { p.Theme = v }},
|
||||
{"pref_per_page", "per_page", []string{"10", "20", "50", "100", "all"}, "20",
|
||||
func(p *Prefs, v string) {
|
||||
if v == "all" {
|
||||
p.PerPage = 0
|
||||
return
|
||||
}
|
||||
if n, err := strconv.Atoi(v); err == nil {
|
||||
p.PerPage = n
|
||||
}
|
||||
}},
|
||||
{"pref_info_position", "info_position", []string{"above", "beside"}, "above",
|
||||
func(p *Prefs, v string) { p.InfoSidebar = v == "beside" }},
|
||||
}
|
||||
|
||||
var prefCookies = func() []string {
|
||||
var out []string
|
||||
for _, vp := range valuePrefs {
|
||||
out = append(out, vp.cookie)
|
||||
}
|
||||
for _, bp := range boolPrefs {
|
||||
out = append(out, bp.cookie)
|
||||
}
|
||||
return append(out, recentDocsCookie, recentSearchesCookie)
|
||||
}()
|
||||
|
||||
func (p Prefs) ThemeAttr() string {
|
||||
if p.Theme == "auto" {
|
||||
return ""
|
||||
}
|
||||
return p.Theme
|
||||
}
|
||||
|
||||
func readPrefs(r *http.Request) Prefs {
|
||||
var p Prefs
|
||||
cookie := map[string]string{}
|
||||
for _, c := range r.Cookies() {
|
||||
cookie[c.Name] = c.Value
|
||||
}
|
||||
|
||||
for i := range boolPrefs {
|
||||
bp := &boolPrefs[i]
|
||||
v := cookie[bp.cookie] == "1"
|
||||
if bp.invert {
|
||||
v = !v
|
||||
}
|
||||
*bp.field(&p) = v
|
||||
}
|
||||
|
||||
for i := range valuePrefs {
|
||||
vp := &valuePrefs[i]
|
||||
v := cookie[vp.cookie]
|
||||
if !slices.Contains(vp.allowed, v) {
|
||||
v = vp.def
|
||||
}
|
||||
vp.apply(&p, v)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func handlePreferences(w http.ResponseWriter, r *http.Request) {
|
||||
noStore(w)
|
||||
q := r.URL.Query()
|
||||
render(w, r, &preferencesPage{
|
||||
Base: pageWith(readPrefs(r)),
|
||||
MaxCards: maxCardsPerPage,
|
||||
Saved: q.Get("saved") == "1",
|
||||
Reset: q.Get("reset") == "1",
|
||||
})
|
||||
}
|
||||
|
||||
func handlePreferencesSave(w http.ResponseWriter, r *http.Request) {
|
||||
if !readPOSTForm(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
if r.FormValue("reset") == "1" {
|
||||
for _, name := range prefCookies {
|
||||
delCookie(w, name)
|
||||
}
|
||||
http.Redirect(w, r, "/preferences?reset=1", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
for _, vp := range valuePrefs {
|
||||
v := r.FormValue(vp.form)
|
||||
switch {
|
||||
case v == vp.def:
|
||||
delCookie(w, vp.cookie)
|
||||
case slices.Contains(vp.allowed, v):
|
||||
setCookie(w, vp.cookie, v)
|
||||
}
|
||||
}
|
||||
|
||||
for _, bp := range boolPrefs {
|
||||
if r.FormValue(bp.form) == "1" {
|
||||
setCookie(w, bp.cookie, "1")
|
||||
} else {
|
||||
delCookie(w, bp.cookie)
|
||||
}
|
||||
}
|
||||
|
||||
if r.FormValue("track_recent") != "1" {
|
||||
delCookie(w, recentDocsCookie)
|
||||
}
|
||||
if r.FormValue("track_searches") != "1" {
|
||||
delCookie(w, recentSearchesCookie)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/preferences?saved=1", http.StatusSeeOther)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue