#lang planet dyoo/whalesong
(require (planet dyoo/whalesong/web-world)
(planet dyoo/whalesong/resource))
(define-resource index.html)
(define-struct item (id content finished? ))
(define (new-item content)
(make-item (fresh-id) content #f))
(define (toggle-item-finished world id)
(cond
[(empty? world)
'()]
[(string=? id (item-id (first world)))
(cons (make-item id (item-content (first world)) (not (item-finished? (first world))))
(rest world))]
[else
(cons (first world)
(toggle-item-finished (rest world) id))]))
(define (on-add world view)
(local [(define text (view-form-value (view-focus view "next-item")))]
(cons (new-item text) world)))
(define (draw world view)
(foldl refresh-item-in-view
view
world))
(define (refresh-item-in-view item view)
(cond
[(view-focus? view (item-id item))
(update-view-css (view-focus view (item-id item))
"text-decoration"
(cond [(item-finished? item)
"line-through"]
[else
"none"]))]
[else
(view-bind
(view-append-child (view-focus view "items")
(xexp->dom `(li (@ (id ,(item-id item)))
,(item-content item))))
"click"
when-item-clicked)]))
(define (when-item-clicked world view)
(toggle-item-finished world (view-attr view "id")))
(define the-view
(view-bind (view-focus (->view index.html) "add-button")
"click"
on-add))
(big-bang (list (new-item "milk")
(new-item "eggs"))
(initial-view the-view)
(to-draw draw))