This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.scm
339 lines (287 loc) · 12.3 KB
/
main.scm
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
;;; scheme helpers
(define (pk . args)
(apply console-log (cons ";;;" args))
(car (reverse args)))
(define (acons a b alist)
(cons (cons a b) alist))
(define (alist-delete alist key)
(let loop ((alist alist)
(out '()))
(if (null? alist)
out
(if (equal? (caar alist) key)
(loop (cdr alist) out)
(loop (cdr alist) (cons (car alist) out))))))
(define (alist-set alist key value)
(acons key value (alist-delete alist key)))
(define (alist-ref alist key)
(let loop ((alist alist))
(cond
((null? alist) #f)
((equal? (caar alist) key) (cdar alist))
(else (loop (cdr alist))))))
(define (string-prefix? prefix string)
(let ((l (string-length prefix)))
(if (< (string-length string) l)
#f
(let ((other (substring string 0 l)))
(equal? other prefix)))))
;;; async ajax bindings
(define $ (js-eval "jQuery"))
(define (ajax url settings k)
(let ((promise (js-invoke $ "ajax" url (alist->js-obj settings))))
(js-invoke promise "always" (js-closure k))))
;;; snabbdom bindings
(define %patch (js-eval "patch"))
(define (patch old new)
(js-call %patch old new)
new)
(define %html (js-eval "h"))
(define (events->js-obj make-action events)
(alist->js-obj (map (lambda (name+proc)
(cons (car name+proc) (js-closure (make-action (cdr name+proc)))))
events)))
(define (html tag events children)
(js-call %html tag events (list->js-array children)))
;;; navigo bindings
(define (make-navigo root)
(if (null? root)
(js-new "Navigo")
(js-new "Navigo" root)))
(define (navigo-on navigo pattern proc)
(js-invoke navigo "on" pattern (js-closure (lambda (param) (proc (js-obj->alist param))))))
(define (navigo-default navigo proc)
(js-invoke navigo "on" (js-closure proc)))
(define (navigo-not-found navigo proc)
(js-invoke navigo "notFound" (js-closure proc)))
(define (navigo-navigate navigo path)
(js-invoke navigo "navigate" path))
(define (navigo-resolve navigo)
(js-invoke navigo "resolve"))
;;; FIXME: use biwascheme bindings
(define (event-target event)
(js-ref event "target"))
(define (event-target-value event)
(js-ref (event-target event) "value"))
(define (event-target-checked event)
(js-ref (event-target event) "checked"))
(define (event-key event)
(js-ref event "key"))
(define (event-prevent-default event)
(js-invoke event "preventDefault"))
;;; framework-ish stuff
(define (attrs->js-obj make-action attrs)
(let loop ((attrs attrs)
(on '())
(out '()))
(if (null? attrs)
(if (alist-ref out "key")
(alist->js-obj `(("on" . ,(events->js-obj make-action on))
("attrs" . ,(alist->js-obj out))
("key" . ,(alist-ref out "key"))))
(alist->js-obj `(("on" . ,(events->js-obj make-action on))
("attrs" . ,(alist->js-obj out)))))
(let ((name (symbol->string (caar attrs)))
(value (cdar attrs)))
(if (string-prefix? "on-" name)
(loop (cdr attrs)
(cons (cons (substring name 3 (string-length name)) value)
on)
out)
(loop (cdr attrs)
on
(cons (cons name value) out)))))))
(define (sxml->h* make-action)
(lambda (element)
(if (not (list? element))
element
(let ((tag (symbol->string (car element))))
(let ((attrs (cadr element)))
(if (and (list? attrs) (eq? (car attrs) '@))
(html (string-append tag "#")
(attrs->js-obj make-action (cdr attrs))
(map (sxml->h* make-action) (cddr element)))
(html (string-append tag "#")
(alist->js-obj '())
(map (sxml->h* make-action) (cdr element)))))))))
(define (app container init view)
(let ((state (init)))
(letrec ((make-action (lambda (action)
(lambda args
(receive (new effect) (apply (action state) args)
(set! state new)
(render)
;; apply effect
(unless (null? effect)
(let ((proc (car effect))
(args (cadr effect))
(k (caddr effect)))
(apply proc (append args (list (make-action k)))))))))))
(lambda ()
(set! container (patch container
((sxml->h* make-action) (view state))))))))
;;; app
;; prepare container
(define body (car (js-array->list ($ "body"))))
(define container (element-new '(div "getting started")))
(element-append-child! body container)
(define (init)
`((page . home) (todos . ((0 . ("Learn Scheme" . #f))))))
(define (todo:checked todo-uid)
(lambda (state)
(lambda (event)
(let ((checked (event-target-checked event)))
(let* ((todos (alist-ref state 'todos))
(todo (alist-ref todos todo-uid)))
(values (alist-set state 'todos (alist-set todos todo-uid (cons (car todo) checked)))
'()))))))
(define (todo:destroy-clicked todo-uid)
(lambda (state)
(lambda (event)
(let ((todos (alist-ref state 'todos)))
(values (alist-set state 'todos (alist-delete todos todo-uid)) '())))))
(define (todo:view todo)
`(li (@ (class . ,(if (cddr todo) "completed" ""))
(key . ,(car todo)))
(div (@ (class . "view"))
(input (@ (class . "toggle")
(type . "checkbox")
(checked . ,(if (cddr todo) "t" ""))
(on-click . ,(todo:checked (car todo)))))
(label ,(cadr todo))
(button (@ (class . "destroy")
(on-click . ,(todo:destroy-clicked (car todo))))))))
(define (todos:active state)
(filter (lambda (pair) (not (cddr pair))) (alist-ref state 'todos)))
(define (todos:completed state)
(filter cddr (alist-ref state 'todos)))
(define (todos:clear-completed-clicked state)
(lambda (event)
(event-prevent-default event)
(values (alist-set state 'todos (todos:active state))
(list ajax (list "/message.txt" '()) (lambda (state)
(lambda (data status _)
(pk data)
(values state '())))))))
(define (new-todo:keypressed state)
(lambda (event)
(let ((key (event-key event)))
(if (equal? key "Enter")
(let ((new (event-target-value event))
(todos (list-sort (alist-ref state 'todos))))
(if (null? todos)
(begin
(element-empty! (event-target event)) ;; Y U NO CLEAR MY INPUT?!
(values (alist-set state 'todos (acons 0 (cons new #f) '())) '()))
(let ((uid (+ 1 (caar (reverse todos)))))
(element-empty! (event-target event)) ;; Y U NO CLEAR MY INPUT?!
(values (alist-set state 'todos (acons uid (cons new #f) todos)) '()))))
(values state '())))))
(define (filters:all state)
(lambda (event)
(event-prevent-default event)
(values (alist-set state 'page 'home) '())))
(define (filters:active state)
(lambda (event)
(event-prevent-default event)
(values (alist-set state 'page 'active) '())))
(define (filters:completed state)
(lambda (event)
(event-prevent-default event)
(values (alist-set state 'page 'completed) '())))
;; home
(define (home:view state)
`(section (@ (class . "todoapp"))
(header (@ (class . "header"))
(h1 "todos")
(input (@ (class . "new-todo")
(placeholder . "What needs to be done?")
(autofocus . "t")
(on-keypress . ,new-todo:keypressed))))
(section (@ (class . "main"))
(ul (@ (class . "todo-list"))
,@(map todo:view (list-sort (alist-ref state 'todos)))))
(footer (@ (class . "footer"))
(span (@ (class . "todo-count"))
,(length (todos:active state))
" items left")
(ul (@ (class . "filters"))
(li (a (@ (href . "#")
(class . "selected")
(on-click . ,filters:all))
"All"))
(li (a (@ (href . "#")
(on-click . ,filters:active))
"Active"))
(li (a (@ (href . "#")
(on-click . ,filters:completed))
"Completed")))
(button (@ (class . "clear-completed")
(on-click . ,todos:clear-completed-clicked))
"Clear completed"))))
(define (active:view state)
`(section (@ (class . "todoapp"))
(header (@ (class . "header"))
(h1 "todos")
(input (@ (class . "new-todo")
(placeholder . "What needs to be done?")
(autofocus . "t")
(on-keypress . ,new-todo:keypressed))))
(section (@ (class . "main"))
(ul (@ (class . "todo-list"))
,@(map todo:view (list-sort (todos:active state)))))
(footer (@ (class . "footer"))
(span (@ (class . "todo-count"))
,(length (todos:active state))
" items left")
(ul (@ (class . "filters"))
(li (a (@ (href . "#")
(on-click . ,filters:all))
"All"))
(li (a (@ (href . "#")
(class . "selected")
(on-click . ,filters:active))
"Active"))
(li (a (@ (href . "#")
(on-click . ,filters:completed))
"Completed")))
(button (@ (class . "clear-completed")
(on-click . ,todos:clear-completed-clicked))
"Clear completed"))))
(define (completed:view state)
`(section (@ (class . "todoapp"))
(header (@ (class . "header"))
(h1 "todos")
(input (@ (class . "new-todo")
(placeholder . "What needs to be done?")
(autofocus . "t")
(on-keypress . ,new-todo:keypressed))))
(section (@ (class . "main"))
(ul (@ (class . "todo-list"))
,@(map todo:view (list-sort (todos:completed state)))))
(footer (@ (class . "footer"))
(span (@ (class . "todo-count"))
,(length (todos:active state))
" items left")
(ul (@ (class . "filters"))
(li (a (@ (href . "#")
(on-click . ,filters:all))
"All"))
(li (a (@ (href . "#")
(on-click . ,filters:active))
"Active"))
(li (a (@ (href . "#")
(class . "selected")
(on-click . ,filters:completed))
"Completed")))
(button (@ (class . "clear-completed")
(on-click . ,todos:clear-completed-clicked))
"Clear completed"))))
(define (view state)
(case (alist-ref state 'page)
((home) (home:view state))
((active) (active:view state))
((completed) (completed:view state))
(else `(h1 "unknown page state"))))
(define render (app container init view))
(render)