renderutils.nim 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import strutils, xmltree
  2. import karax/[karaxdsl, vdom, vstyles]
  3. import ../types, ../utils
  4. proc icon*(icon: string; text=""; title=""; class=""; href=""): VNode =
  5. var c = "icon-" & icon
  6. if class.len > 0: c = c & " " & class
  7. buildHtml(tdiv(class="icon-container")):
  8. if href.len > 0:
  9. a(class=c, title=title, href=href)
  10. else:
  11. span(class=c, title=title)
  12. if text.len > 0:
  13. text " " & text
  14. proc linkUser*(profile: Profile, class=""): VNode =
  15. let
  16. isName = "username" notin class
  17. href = "/" & profile.username
  18. nameText = if isName: profile.fullname
  19. else: "@" & profile.username
  20. buildHtml(a(href=href, class=class, title=nameText)):
  21. text nameText
  22. if isName and profile.verified:
  23. icon "ok", class="verified-icon", title="Verified account"
  24. if isName and profile.protected:
  25. text " "
  26. icon "lock-circled", title="Protected account"
  27. proc linkText*(text: string; class=""): VNode =
  28. let url = if "http" notin text: "http://" & text else: text
  29. buildHtml():
  30. a(href=url, class=class): text text
  31. proc hiddenField*(name, value: string): VNode =
  32. buildHtml():
  33. input(name=name, style={display: "none"}, value=value)
  34. proc refererField*(path: string): VNode =
  35. hiddenField("referer", path)
  36. proc iconReferer*(icon, action, path: string, title=""): VNode =
  37. buildHtml(form(`method`="get", action=action, class="icon-button")):
  38. refererField path
  39. button(`type`="submit"):
  40. icon icon, title=title
  41. proc buttonReferer*(action, text, path: string; class=""; `method`="post"): VNode =
  42. buildHtml(form(`method`=`method`, action=action, class=class)):
  43. refererField path
  44. button(`type`="submit"):
  45. text text
  46. proc genCheckbox*(pref, label: string; state: bool): VNode =
  47. buildHtml(label(class="pref-group checkbox-container")):
  48. text label
  49. if state: input(name=pref, `type`="checkbox", checked="")
  50. else: input(name=pref, `type`="checkbox")
  51. span(class="checkbox")
  52. proc genInput*(pref, label, state, placeholder: string; class=""; autofocus=false): VNode =
  53. let s = xmltree.escape(state)
  54. let p = xmltree.escape(placeholder)
  55. let a = if autofocus: "autofocus" else: ""
  56. buildHtml(tdiv(class=("pref-group pref-input " & class))):
  57. if label.len > 0:
  58. label(`for`=pref): text label
  59. input(name=pref, `type`="text", placeholder=p, value=s): text a
  60. proc genSelect*(pref, label, state: string; options: seq[string]): VNode =
  61. buildHtml(tdiv(class="pref-group pref-input")):
  62. label(`for`=pref): text label
  63. select(name=pref):
  64. for opt in options:
  65. if opt == state:
  66. option(value=opt, selected=""): text opt
  67. else:
  68. option(value=opt): text opt
  69. proc genDate*(pref, state: string): VNode =
  70. buildHtml(span(class="date-input")):
  71. input(name=pref, `type`="date", value=state)
  72. icon "calendar"
  73. proc genImg*(url: string; class=""): VNode =
  74. buildHtml():
  75. img(src=getPicUrl(url), class=class, alt="Image")
  76. proc getTabClass*(query: Query; tab: QueryKind): string =
  77. result = "tab-item"
  78. if query.kind == tab:
  79. result &= " active"