From f4185b3c61ca42150d7af3938f062d378d5bf87f Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Wed, 30 Nov 2011 16:03:39 -0500 Subject: [PATCH] playing around with storage --- Makefile | 4 +- js/js-impl.js | 11 ++++- js/main.rkt | 3 ++ js/racket-impl.rkt | 10 ++++ sandbox/test-storage.rkt | 11 +++++ sandbox/todo-storage/index.html | 14 ++++++ sandbox/todo-storage/todo.rkt | 87 +++++++++++++++++++++++++++++++++ storage.rkt | 3 ++ storage/storage.rkt | 6 ++- version.rkt | 2 +- 10 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 sandbox/test-storage.rkt create mode 100644 sandbox/todo-storage/index.html create mode 100644 sandbox/todo-storage/todo.rkt create mode 100644 storage.rkt diff --git a/Makefile b/Makefile index c0a607a..50be87c 100644 --- a/Makefile +++ b/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 . diff --git a/js/js-impl.js b/js/js-impl.js index a392a5a..83430f7 100644 --- a/js/js-impl.js +++ b/js/js-impl.js @@ -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; diff --git a/js/main.rkt b/js/main.rkt index 9ee0f1e..b731f61 100644 --- a/js/main.rkt +++ b/js/main.rkt @@ -24,4 +24,7 @@ viewport-width viewport-height in-javascript-context? + + js-null? + js-null )) \ No newline at end of file diff --git a/js/racket-impl.rkt b/js/racket-impl.rkt index 5629b60..0272f29 100644 --- a/js/racket-impl.rkt +++ b/js/racket-impl.rkt @@ -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 diff --git a/sandbox/test-storage.rkt b/sandbox/test-storage.rkt new file mode 100644 index 0000000..25f14eb --- /dev/null +++ b/sandbox/test-storage.rkt @@ -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) \ No newline at end of file diff --git a/sandbox/todo-storage/index.html b/sandbox/todo-storage/index.html new file mode 100644 index 0000000..7c6b5f7 --- /dev/null +++ b/sandbox/todo-storage/index.html @@ -0,0 +1,14 @@ + +TODO List + +

TODO

+ +

Items

+ + + +

Adding an item

+ + + + diff --git a/sandbox/todo-storage/todo.rkt b/sandbox/todo-storage/todo.rkt new file mode 100644 index 0000000..3dd5554 --- /dev/null +++ b/sandbox/todo-storage/todo.rkt @@ -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)) \ No newline at end of file diff --git a/storage.rkt b/storage.rkt new file mode 100644 index 0000000..f00cbf1 --- /dev/null +++ b/storage.rkt @@ -0,0 +1,3 @@ +#lang s-exp "lang/base.rkt" +(require "storage/storage.rkt") +(provide (all-from-out "storage/storage.rkt")) \ No newline at end of file diff --git a/storage/storage.rkt b/storage/storage.rkt index a835b3b..c273a1e 100644 --- a/storage/storage.rkt +++ b/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" diff --git a/version.rkt b/version.rkt index b22d90f..b279385 100644 --- a/version.rkt +++ b/version.rkt @@ -7,4 +7,4 @@ (provide version) (: version String) -(define version "1.90") +(define version "1.94")