import vdom, karax, karaxdsl, jstrutils, components type Filter = enum all, active, completed var selectedEntry = -1 filter: Filter entriesLen: int proc getItem(key : cstring ): cstring {.importc: "localStorage.getItem"} proc setItem(key, value : cstring ) {.importc: "localStorage.setItem"} proc clearEntries() {.importc: "localStorage.clear"} proc getEntryCstring(pos: int): cstring = result = getItem(&pos & cstring"cstring") if result == cstring"null": result = nil proc isCompleted(pos: int): bool = var value = getItem(cstring($pos & "bool")) result = value == cstring"true" proc setEntryCstring(pos: int, value: cstring) = setItem(cstring($pos & "cstring"), value) proc markAsCompleted(pos: int, value : bool) = var val = cstring"true" if not value: val = cstring"false" setItem(cstring($pos & "bool"), val) proc addEntry(str: cstring, bval : bool) = setEntryCstring(entriesLen, str) markAsCompleted(entriesLen, bval) inc entriesLen proc updateEntry(pos: int, str: cstring, bval: bool) = setEntryCstring(pos, str) markAsCompleted(pos, bval) proc onTodoEnter(ev: Event; n: VNode) = addEntry(n.value, false) n.value = "" proc removeHandler(ev: Event; n: VNode) = updateEntry(n.key, cstring(nil), false) proc editHandler(ev: Event; n: VNode) = selectedEntry = n.key proc focusLost(ev: Event; n: VNode) = selectedEntry = -1 proc editEntry(ev: Event; n: VNode) = setEntryCstring(n.key, n.value) selectedEntry = -1 proc toggleEntry(ev: Event; n: VNode) = let id = n.key markAsCompleted(id, not isCompleted(id)) proc onAllDone(ev: Event; n: VNode) = clearEntries() selectedEntry = -1 proc clearCompleted(ev: Event, n: VNode) = for i in 0..