preferences.nim 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # SPDX-License-Identifier: AGPL-3.0-only
  2. import tables, macros, strutils
  3. import karax/[karaxdsl, vdom]
  4. import renderutils
  5. import ../types, ../prefs_impl
  6. macro renderPrefs*(): untyped =
  7. result = nnkCall.newTree(
  8. ident("buildHtml"), ident("tdiv"), nnkStmtList.newTree())
  9. for header, options in prefList:
  10. result[2].add nnkCall.newTree(
  11. ident("legend"),
  12. nnkStmtList.newTree(
  13. nnkCommand.newTree(ident("text"), newLit(header))))
  14. for pref in options:
  15. let procName = ident("gen" & capitalizeAscii($pref.kind))
  16. let state = nnkDotExpr.newTree(ident("prefs"), ident(pref.name))
  17. var stmt = nnkStmtList.newTree(
  18. nnkCall.newTree(procName, newLit(pref.name), newLit(pref.label), state))
  19. case pref.kind
  20. of checkbox: discard
  21. of input: stmt[0].add newLit(pref.placeholder)
  22. of select:
  23. if pref.name == "theme":
  24. stmt[0].add ident("themes")
  25. else:
  26. stmt[0].add newLit(pref.options)
  27. result[2].add stmt
  28. proc renderPreferences*(prefs: Prefs; path: string; themes: seq[string];
  29. prefsUrl: string): VNode =
  30. buildHtml(tdiv(class="overlay-panel")):
  31. fieldset(class="preferences"):
  32. form(`method`="post", action="/saveprefs", autocomplete="off"):
  33. refererField path
  34. renderPrefs()
  35. legend: text "Bookmark"
  36. p(class="bookmark-note"):
  37. text "Save this URL to restore your preferences (?prefs works on all pages)"
  38. pre(class="prefs-code"):
  39. text prefsUrl
  40. p(class="bookmark-note"):
  41. verbatim "You can override preferences with query parameters (e.g. <code>?hlsPlayback=on</code>). These overrides aren't saved to cookies, and links won't retain the parameters. Intended for configuring RSS feeds and other cookieless environments. Hover over a preference to see its name."
  42. h4(class="note"):
  43. text "Preferences are stored client-side using cookies without any personal information."
  44. button(`type`="submit", class="pref-submit"):
  45. text "Save preferences"
  46. buttonReferer "/resetprefs", "Reset preferences", path, class="pref-reset"