nitter/src/views/preferences.nim

67 lines
2.2 KiB
Nim
Raw Normal View History

2019-08-15 12:52:34 +00:00
import tables, macros, strformat, xmltree
2019-08-13 17:44:29 +00:00
import karax/[karaxdsl, vdom, vstyles]
2019-09-05 20:53:23 +00:00
import renderutils
2019-08-17 19:49:41 +00:00
import ../types, ../prefs_impl
2019-08-13 17:44:29 +00:00
2019-08-15 12:38:14 +00:00
proc genCheckbox(pref, label: string; state: bool): VNode =
2019-08-13 17:44:29 +00:00
buildHtml(tdiv(class="pref-group")):
2019-08-15 20:44:11 +00:00
label(class="checkbox-container"):
text label
if state: input(name=pref, `type`="checkbox", checked="")
else: input(name=pref, `type`="checkbox")
span(class="checkbox")
2019-08-13 17:44:29 +00:00
2019-08-15 12:38:14 +00:00
proc genSelect(pref, label, state: string; options: seq[string]): VNode =
2019-08-13 17:44:29 +00:00
buildHtml(tdiv(class="pref-group")):
2019-08-15 20:44:11 +00:00
label(`for`=pref): text label
2019-08-13 17:44:29 +00:00
select(name=pref):
for opt in options:
if opt == state:
option(value=opt, selected=""): text opt
else:
option(value=opt): text opt
2019-08-15 12:38:14 +00:00
proc genInput(pref, label, state, placeholder: string): VNode =
2019-08-15 12:52:34 +00:00
let s = xmltree.escape(state)
let p = xmltree.escape(placeholder)
2019-08-15 20:44:11 +00:00
buildHtml(tdiv(class="pref-group pref-input")):
2019-08-13 17:44:29 +00:00
label(`for`=pref): text label
2019-08-15 20:44:11 +00:00
verbatim &"<input name={pref} type=\"text\" placeholder=\"{p}\" value=\"{s}\"/>"
2019-08-13 17:44:29 +00:00
macro renderPrefs*(): untyped =
result = nnkCall.newTree(
ident("buildHtml"), ident("tdiv"), nnkStmtList.newTree())
for header, options in prefList:
result[2].add nnkCall.newTree(
ident("legend"),
nnkStmtList.newTree(
nnkCommand.newTree(ident("text"), newLit(header))))
for pref in options:
2019-08-15 12:38:14 +00:00
let procName = ident("gen" & capitalizeAscii($pref.kind))
let state = nnkDotExpr.newTree(ident("prefs"), ident(pref.name))
var stmt = nnkStmtList.newTree(
nnkCall.newTree(procName, newLit(pref.name), newLit(pref.label), state))
2019-08-13 17:44:29 +00:00
case pref.kind
2019-08-15 12:38:14 +00:00
of checkbox: discard
of select: stmt[0].add newLit(pref.options)
of input: stmt[0].add newLit(pref.placeholder)
result[2].add stmt
2019-08-13 17:44:29 +00:00
proc renderPreferences*(prefs: Prefs; path: string): VNode =
2019-08-13 17:44:29 +00:00
buildHtml(tdiv(class="preferences-container")):
2019-08-15 17:13:54 +00:00
fieldset(class="preferences"):
form(`method`="post", action="/saveprefs"):
2019-09-05 20:53:23 +00:00
refererField path
2019-08-13 17:44:29 +00:00
renderPrefs()
2019-08-15 23:57:36 +00:00
button(`type`="submit", class="pref-submit"):
2019-08-13 17:44:29 +00:00
text "Save preferences"
2019-08-15 17:13:54 +00:00
2019-09-05 20:53:23 +00:00
buttonReferer "/resetprefs", "Reset Preferences", path, class="pref-reset"