playing around with storage
This commit is contained in:
parent
11b2c0f41f
commit
f4185b3c61
4
Makefile
4
Makefile
|
@ -44,8 +44,8 @@ cs019-doc:
|
|||
|
||||
|
||||
setup:
|
||||
raco setup --no-docs -P dyoo whalesong.plt 1 8
|
||||
raco setup --no-docs -P dyoo whalesong.plt 1 10
|
||||
|
||||
|
||||
planet-link:
|
||||
raco planet link dyoo whalesong.plt 1 8 .
|
||||
raco planet link dyoo whalesong.plt 1 10 .
|
||||
|
|
|
@ -139,7 +139,16 @@
|
|||
return plt.baselib.numbers.toFixnum(checkNumber(MACHINE, 'js-string?', 0));
|
||||
});
|
||||
|
||||
|
||||
|
||||
EXPORTS['js-null?'] =
|
||||
makePrimitiveProcedure(
|
||||
'js-null?',
|
||||
1,
|
||||
function(MACHINE) {
|
||||
return checkAny(MACHINE, 'js-null?', 0) === null;
|
||||
});
|
||||
|
||||
EXPORTS['js-null'] = null;
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -24,4 +24,7 @@
|
|||
viewport-width
|
||||
viewport-height
|
||||
in-javascript-context?
|
||||
|
||||
js-null?
|
||||
js-null
|
||||
))
|
|
@ -14,6 +14,9 @@
|
|||
js-number?
|
||||
number->js-number
|
||||
js-number->number
|
||||
|
||||
js-null?
|
||||
js-null
|
||||
)
|
||||
|
||||
(define (alert x)
|
||||
|
@ -56,6 +59,13 @@
|
|||
|
||||
|
||||
|
||||
(define (js-null? x)
|
||||
(error 'js-null? "Not available outside JavaScript context"))
|
||||
|
||||
(define js-null 'not-done-yet)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
;; in-javascript-context: -> boolean
|
||||
|
|
11
sandbox/test-storage.rkt
Normal file
11
sandbox/test-storage.rkt
Normal file
|
@ -0,0 +1,11 @@
|
|||
#lang planet dyoo/whalesong
|
||||
|
||||
(require (planet dyoo/whalesong/storage))
|
||||
|
||||
(storage-length)
|
||||
(storage-ref "whalesong test")
|
||||
(storage-set! "whalesong test" "hello world")
|
||||
(storage-ref "whalesong test")
|
||||
(storage-length)
|
||||
(storage-clear!)
|
||||
(storage-length)
|
14
sandbox/todo-storage/index.html
Normal file
14
sandbox/todo-storage/index.html
Normal file
|
@ -0,0 +1,14 @@
|
|||
<html>
|
||||
<head><title>TODO List</title></head>
|
||||
<body>
|
||||
<h1>TODO</h1>
|
||||
|
||||
<h2>Items</h2>
|
||||
<ul id="items"></ul>
|
||||
|
||||
|
||||
<h2>Adding an item</h2>
|
||||
<input type="text" id="next-item"/>
|
||||
<input type="button" id="add-button" value="Add!"/>
|
||||
</body>
|
||||
</html>
|
87
sandbox/todo-storage/todo.rkt
Normal file
87
sandbox/todo-storage/todo.rkt
Normal file
|
@ -0,0 +1,87 @@
|
|||
#lang planet dyoo/whalesong
|
||||
(require (planet dyoo/whalesong/web-world)
|
||||
(planet dyoo/whalesong/resource)
|
||||
(planet dyoo/whalesong/storage))
|
||||
|
||||
;; The world is our TODO list, represented as a list of strings.
|
||||
|
||||
(define-resource index.html)
|
||||
|
||||
|
||||
;; An item consists of a string id, the item's content, and a finished? flag.
|
||||
(define-struct item (id ;; string
|
||||
content ;; string
|
||||
finished? ;; boolean
|
||||
))
|
||||
|
||||
|
||||
;; new-item: string -> item
|
||||
(define (new-item content)
|
||||
(make-item (fresh-id) content #f))
|
||||
|
||||
|
||||
;; toggle-item-finished: world string -> world
|
||||
;; Mark the item with the given id so that it's finished, or reverse that change.
|
||||
(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))]))
|
||||
|
||||
|
||||
|
||||
;; world view -> world
|
||||
(define (on-add world view)
|
||||
(local [(define text (view-form-value (view-focus view "next-item")))]
|
||||
(cons (new-item text) world)))
|
||||
|
||||
|
||||
;; world view -> view
|
||||
(define (draw world view)
|
||||
(foldl refresh-item-in-view
|
||||
view
|
||||
world))
|
||||
|
||||
|
||||
|
||||
;; refresh-item-in-view: item view -> view
|
||||
(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)]))
|
||||
|
||||
|
||||
|
||||
;; when-item-clicked: world view -> world
|
||||
;; When an item is clicked, set its finished? flag.
|
||||
(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))
|
3
storage.rkt
Normal file
3
storage.rkt
Normal file
|
@ -0,0 +1,3 @@
|
|||
#lang s-exp "lang/base.rkt"
|
||||
(require "storage/storage.rkt")
|
||||
(provide (all-from-out "storage/storage.rkt"))
|
|
@ -23,8 +23,10 @@
|
|||
(js-string->string (call-method localStorage "key" (number->js-number i))))
|
||||
|
||||
(define (storage-ref name)
|
||||
(js-string->string
|
||||
(call-method localStorage "getItem" (string->js-string name))))
|
||||
(define val (call-method localStorage "getItem" (string->js-string name)))
|
||||
(if (js-null? val)
|
||||
#f
|
||||
(js-string->string val)))
|
||||
|
||||
(define (storage-set! name value)
|
||||
(void (call-method localStorage "setItem"
|
||||
|
|
|
@ -7,4 +7,4 @@
|
|||
(provide version)
|
||||
(: version String)
|
||||
|
||||
(define version "1.90")
|
||||
(define version "1.94")
|
||||
|
|
Loading…
Reference in New Issue
Block a user