forked from jcubic/lips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.lips
254 lines (205 loc) · 7.83 KB
/
todo.lips
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
;; -*- scheme -*-
;;
;; TODO app using LIPS - Scheme based Powerful lips in JavaScript
;; Copyright (C) 2018-2020 Jakub T. Jankiewicz <https://jcubic.pl/me>
;; Released under the MIT license
;;
;; code based on jQuery TODO MVC
;; https://github.com/tastejs/todomvc/blob/gh-pages/examples/jquery/js/app.js
;;
;; ----------------------------------------------------------------------------------
;; Helper functions
;; ----------------------------------------------------------------------------------
(define (filter-todos completed)
"return todos where completed fields is equal to argument"
(filter (lambda (item)
(eq? (cdr (assoc 'completed item)) completed)) todos))
(define (element-id element)
"return data-id attribute value"
(--> ($ element) (closest "li") (data "id")))
(define (find-todo element)
"return single todo representation of a given element"
(let ((id (element-id element)))
(find (lambda (item) (eq? (cdr (assoc 'id item)) id)) todos)))
(define (on-todo fn element)
"Invoke function for each todo"
(let* ((todo (find-todo element)))
(if (not (null? todo))
(fn todo))))
(define (get-todos)
"return selected todo"
(if (eq? selected-filter "active")
(filter-todos false)
(if (eq? selected-filter "completed")
(filter-todos true)
todos)))
(define (uuid)
"Generatates unique ID (comments are because of scheme highligting in emacs (| |) is commnent)"
(let ((i 0) (rnd) (uuid ""))
(while (< i 32)
(set! rnd (| (* (--> Math (random)) 16) 0)) ;;|)
(if (or (== i 8) (== i 12) (== i 16) (== i 20))
(set! uuid (concat uuid "-")))
(let ((value (if (== i 1)
4
(if (== i 16)
(| (& rnd 3) 8) ;;|)
rnd))))
(set! uuid (concat uuid (--> value (toString 16)))))
(++ i))
uuid))
(define (pluralize count word)
(if (or (> count 1) (== count 0)) (concat word "s") word))
(define (store namespace . data)
"set or get value from localStorage"
(if (not (null? data))
(--> localStorage (setItem namespace (--> (car data) (toString))))
(let ((store (--> localStorage (getItem namespace))))
(if (null? store)
'()
(read store)))))
;; ref: https://www.programming-idioms.org/idiom/12/check-if-list-contains-a-value/154/scheme
(define (contains x list)
(if (not list)
false
(if (eq? (car list) x)
true
(contains x (cdr list)))))
(define log (. console "log"))
;; ----------------------------------------------------------------------------------
;; Global variables
;; ----------------------------------------------------------------------------------
(define todos (store "todos-jquery"))
(define ENTER_KEY 13)
(define ESCAPE_KEY 27)
(define selected-filter "all")
;; ----------------------------------------------------------------------------------
;; Templates
;; ----------------------------------------------------------------------------------
(define (template selector)
(let ((html (--> ($ selector) (html))))
(--> Handlebars (compile html))))
(define todo-template (template "#todo-template"))
(define footer-template (template "#footer-template"))
;; ----------------------------------------------------------------------------------
;; Event handles
;; ----------------------------------------------------------------------------------
(define (toggle-completed)
"remove completed todos"
(let ((ids (--> ($ ".toggle:checked")
(closest "li")
(map (lambda (i item)
(--> ($ item) (data "id"))))
(get))))
(set! todos (filter (lambda (item)
(not (--> ids (includes (cdr (assoc 'id item))))))
todos))
(render)))
(define (add-todo e)
"create new todo"
(let* ((input (. e "target"))
(which (. e "which"))
(val (--> (. input "value") (trim))))
(if (and (not (eq? val "")) (eq? which ENTER_KEY))
(begin
(append! todos (list `((id . ,(uuid))
(title . ,val)
(completed . false))))
(set! (. input "value") "")
(render)))))
(define (destroy event)
"remove todo"
(let ((id (element-id (. event "target"))))
(set! todos (filter (lambda (item)
(not (eq? (cdr (assoc 'id item)) id))) todos))
(render)))
(define (toggle event)
"flip completed on todo and render todos"
(on-todo (lambda (todo)
(let ((completed (cdr (assoc 'completed todo))))
(set-cdr! (assoc 'completed todo) (not completed))
(render)))
(. event "target")))
(define (toggle-all event)
(let ((is-checked (--> ($ (. event "target")) (prop "checked"))))
(for-each (lambda (item) (set-cdr! (assoc 'completed item) is-checked)) todos)
(render)))
(define (edit-mode event)
(let* (($input (--> ($ (. event "target"))
(closest "li")
(addClass "editing")
(find ".edit")))
(tmp-str (--> $input (val))))
(--> $input (val ""))
(--> $input (val tmp-str))
(--> $input (focus))))
(define (edit-keyup event)
(let ((key (. event "which"))
(element (. event "target")))
(if (eq? key ENTER_KEY)
(--> element (blur)))
(if (eq? key ESCAPE_KEY)
(--> ($ element) (data "abort" true) (blur)))))
(define (update event)
"update title of todo but not when user press ESC"
(let* ((element (. event "target"))
($el ($ element))
(val (--> $el (val) (trim))))
(if (--> $el (data "abort"))
(begin
(--> $el (data "abort" false))
(render))
(if (not val)
(detroy event)
(begin
(on-todo (lambda (todo)
(set-cdr! (assoc 'title todo) val))
element)
(render))))))
;; ----------------------------------------------------------------------------------
;; Main code
;; ----------------------------------------------------------------------------------
(define (render-footer)
"get data about toods and render the app footer"
(let* ((todo-count (length todos))
(active-count (length (filter-todos false)))
(data `((activeTodoCount . ,active-count)
(activeTodoWord . ,(pluralize active-count "item"))
(completedTodos . ,(- todo-count active-count))))
(template (footer-template (--> data (toObject)))))
(--> ($ ".footer")
(toggle (> todo-count 0))
(html template))))
(define (render)
"render the app"
(let* ((todos (get-todos))
(data (--> (map (lambda (e) (--> e (toObject))) todos) (toArray))))
(--> ($ ".todo-list")
(html (todo-template data)))
(--> ($ ".main") (toggle (> (length data) 0)))
(--> ($ ".new-todo") (focus))
(render-footer)
(let ((todos (filter-todos false)))
(--> ($ ".toggle-all") (prop "checked" (== (length todos) 0))))
(store "todos-jquery" todos)))
(define (bind)
"bind event handlers"
(--> ($ ".new-todo") (on "keyup" add-todo))
(--> ($ ".toggle-all") (on "change" toggle-all))
(--> ($ ".todoapp")
(on "click" ".clear-completed" toggle-completed))
(--> ($ ".todo-list")
(on "dblclick" "label" edit-mode)
(on "keyup" ".edit" edit-keyup)
(on "focusout" ".edit" update)
(on "change" ".toggle" toggle)
(on "click" ".destroy" destroy)))
(define (init)
"initialization function"
(bind)
(let* ((routes `(("/:filter" . ,(lambda (filter)
(set! selected-filter filter)
(render)))))
(router (new Router (--> routes (toObject)))))
(--> router (init "/all"))))
(init)