Advertisement
Patasuss

ToDo-App

Apr 6th, 2019
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 1.88 KB | None | 0 0
  1. include karax/prelude
  2. import random, sugar, strutils, algorithm
  3.  
  4. type
  5.   Entry = object
  6.     addedAt: int
  7.     entry: kstring
  8.  
  9.   AddEntryComponent = object
  10.     currentText: kstring
  11.     placeholder: kstring
  12.     callback: (kstring)->void
  13.  
  14. proc initAddEntryComponent(): AddEntryComponent =
  15.   return AddEntryComponent(currentText: "",
  16.                            placeholder: "Good vibes only",
  17.                            callback: (s: kstring) => (discard))
  18.  
  19. proc render(comp: var AddEntryComponent): VNode =
  20.   result = buildHtml():
  21.     form(class="pure-form"):
  22.       fieldset():
  23.         input(id="entry", placeholder=comp.placeholder):
  24.           proc onkeyup(ev: Event, n: VNode) =
  25.             discard
  26.         button(type="button", class="pure-button pure-button-primary"):
  27.           text "ADD"
  28.           proc onclick() =
  29.             let inputField = getVNodeById("entry")
  30.             comp.callback(inputField.text)
  31.   return result
  32.  
  33. var entries : seq[Entry]
  34.  
  35. proc addEntry(e: kstring) =
  36.   entries.add(Entry(entry: e, addedAt: entries.len()))
  37.  
  38. var
  39.   entryComponent = initAddEntryComponent()
  40.  
  41. entryComponent.callback = addEntry
  42.  
  43. proc createDom(): VNode =
  44.   result = buildHtml():
  45.     tdiv():
  46.       link("rel"="stylesheet", "href"="../style.css")
  47.       link("rel"="stylesheet", "href"="../pure-min.css")
  48.       tdiv("class"="content"):
  49.         h1:
  50.           text "TODO"
  51.         entryComponent.render()
  52.         hr()
  53.         for i, e in entries:
  54.           tdiv(class="todoentry"):
  55.             button(type="button", id="" & $i, class="pure-button"):
  56.               text "DONE"
  57.               proc onclick(e: Event, n: VNode) =
  58.                 let id = n.id.parseInt()
  59.                 entries.del(id)
  60.                 entries.sort((a: Entry, b: Entry) => (system.cmp(a.addedAt, b.addedAt)))
  61.             echo "Entry: ", e.entry
  62.             text " " & e.entry
  63. setRenderer createDom
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement