major api change: view focus now only treats its input as an id, rather than a general selector. We'll have extension to this later.

This commit is contained in:
Danny Yoo 2011-09-02 13:56:34 -04:00
parent 086a5dc61f
commit d6759935cd
13 changed files with 42 additions and 48 deletions

View File

@ -165,7 +165,7 @@ Write a file called @filepath{tick-tock.rkt} with the following content.
;; draw: world view -> view ;; draw: world view -> view
(define (draw world dom) (define (draw world dom)
(update-view-text (view-focus dom "#counter") world)) (update-view-text (view-focus dom "counter") world))
;; tick: world view -> world ;; tick: world view -> world
@ -333,7 +333,7 @@ function will be called every time an event occurs.
;; draw: world view -> view ;; draw: world view -> view
(define (draw world dom) (define (draw world dom)
(update-view-text (view-focus dom "#name-span") (update-view-text (view-focus dom "name-span")
(world-name world))) (world-name world)))
... ...
(big-bang ... (big-bang ...
@ -359,19 +359,13 @@ Common values for @racket[x] include @tech{resource}s.
@defproc[(view-focus? [v view] [selector String]) boolean]{ @defproc[(view-focus? [v view] [id String]) boolean]{
Return true if the view can be focused using the given selector. Return true if the view can be focused using the given id.
Selectors are currently restricted to @litchar{#id} selectors for the
moment.
} }
@defproc[(view-focus [v view] [selector String]) view]{ @defproc[(view-focus [v view] [id String]) view]{
Focuses the view on an element, given the @racket[selector]. The view Focuses the view on an element, given the @racket[id]. The view
will be searched starting from the toplevelmost node. will be searched starting from the toplevelmost node.
Selectors are currently restricted to @litchar{#id} selectors for the
moment.
} }

View File

@ -561,7 +561,7 @@ Write a file called @filepath{tick-tock.rkt} with the following content.
;; draw: world view -> view ;; draw: world view -> view
(define (draw world dom) (define (draw world dom)
(update-view-text (view-focus dom "#counter") world)) (update-view-text (view-focus dom "counter") world))
;; tick: world view -> world ;; tick: world view -> world
@ -709,7 +709,7 @@ function will be called every time an event occurs.
;; draw: world view -> view ;; draw: world view -> view
(define (draw world dom) (define (draw world dom)
(update-view-text (view-focus dom "#name-span") (update-view-text (view-focus dom "name-span")
(world-name world))) (world-name world)))
... ...
(big-bang ... (big-bang ...
@ -734,12 +734,14 @@ Common values for @racket[x] include @tech{resource}s.
} }
@defproc[(view-focus [v view] [selector String]) view]{ @defproc[(view-focus? [v view] [id String]) boolean]{
Focuses the view on an element, given the @racket[selector]. The view Return true if the view can be focused using the given id.
will be searched starting from the toplevelmost node. }
Selectors are currently restricted to @litchar{#id} selectors for the
moment. @defproc[(view-focus [v view] [id String]) view]{
Focuses the view on an element, given the @racket[id]. The view
will be searched starting from the toplevelmost node.
} }

View File

@ -59,7 +59,7 @@
(define-struct person (name friends) #:mutable) (define-struct person (name friends))
(let-values ([(a b c) (let-values ([(a b c)
(shared ([a (make-person "jill" (list b c))] (shared ([a (make-person "jill" (list b c))]
[b (make-person "jack" (list a c))] [b (make-person "jack" (list a c))]

View File

@ -10,10 +10,10 @@
(modulo (add1 w) 10)) (modulo (add1 w) 10))
;; pick-block: world view -> view ;; pick-block: view number -> view
;; Focus the view on block i. ;; Focus the view on block i.
(define (pick-block v i) (define (pick-block v i)
(view-focus v (format "#~a" i))) (view-focus v (format "~a" i)))
(define (draw w v) (define (draw w v)

View File

@ -39,7 +39,7 @@ The program for this is:
;; pick-block: world view -> view ;; pick-block: world view -> view
;; Focus the view on block i. ;; Focus the view on block i.
(define (pick-block v i) (define (pick-block v i)
(view-focus v (format "#~a" i))) (view-focus v (format "~a" i)))
(define (draw w v) (define (draw w v)

View File

@ -26,8 +26,8 @@
;; until we're all done. ;; until we're all done.
(define (draw w dom-view) (define (draw w dom-view)
(foldl (lambda (name view) (foldl (lambda (name view)
(cond [(view-focus? view (format "#~a" name)) (cond [(view-focus? view name)
(define focused (view-focus view (format "#~a" name))) (define focused (view-focus view name))
(cond (cond
[(member name w) [(member name w)
view] view]
@ -44,7 +44,7 @@
;; to each name here. ;; to each name here.
(define my-view (define my-view
(foldl (lambda (name view) (foldl (lambda (name view)
(view-bind (view-focus view (format "#~a" name)) (view-bind (view-focus view name)
"click" "click"
hide-on-click)) hide-on-click))
(->view index.html) (->view index.html)

View File

@ -26,7 +26,7 @@
;; until we're all done. ;; until we're all done.
(define (draw w dom-view) (define (draw w dom-view)
(foldl (lambda (name view) (foldl (lambda (name view)
(define focused (view-focus view (format "#~a" name))) (define focused (view-focus view name))
(cond (cond
[(member name w) [(member name w)
(view-show focused)] (view-show focused)]
@ -41,7 +41,7 @@
;; to each name here. ;; to each name here.
(define my-view (define my-view
(foldl (lambda (name view) (foldl (lambda (name view)
(view-bind (view-focus view (format "#~a" name)) (view-bind (view-focus view name)
"click" "click"
hide-on-click)) hide-on-click))
(->view index.html) (->view index.html)

View File

@ -11,18 +11,18 @@
;; When the user clicks on the button, grab at the text of the ;; When the user clicks on the button, grab at the text of the
;; text-field. ;; text-field.
(define (on-click w button-view) (define (on-click w button-view)
(view-form-value (view-focus button-view "#text-field"))) (view-form-value (view-focus button-view "text-field")))
;; draw: world view -> view ;; draw: world view -> view
;; Take the view, and replace the template with the world value. ;; Take the view, and replace the template with the world value.
(define (draw w dom) (define (draw w dom)
(update-view-text (view-focus dom "#template") (update-view-text (view-focus dom "template")
w)) w))
(define my-view (view-bind (view-focus (->view index.html) (define my-view (view-bind (view-focus (->view index.html)
"#button") "button")
"click" "click"
on-click)) on-click))

View File

@ -9,7 +9,7 @@
;; draw: world view -> view ;; draw: world view -> view
(define (draw w v) (define (draw w v)
(update-view-text (view-focus v "#counter") w)) (update-view-text (view-focus v "counter") w))

View File

@ -7,7 +7,7 @@
;; draw: world view -> view ;; draw: world view -> view
(define (draw w v) (define (draw w v)
(update-view-text (view-focus v "#counter") w)) (update-view-text (view-focus v "counter") w))

View File

@ -36,7 +36,7 @@
;; world view -> world ;; world view -> world
(define (on-add world view) (define (on-add world view)
(local [(define text (view-form-value (view-focus view "#next-item")))] (local [(define text (view-form-value (view-focus view "next-item")))]
(cons (new-item text) world))) (cons (new-item text) world)))
@ -50,8 +50,8 @@
;; refresh-item-in-view: item view -> view ;; refresh-item-in-view: item view -> view
(define (refresh-item-in-view item view) (define (refresh-item-in-view item view)
(cond (cond
[(view-focus? view (format "#~a" (item-id item))) [(view-focus? view (item-id item))
(update-view-css (view-focus view (format "#~a" (item-id item))) (update-view-css (view-focus view (item-id item))
"text-decoration" "text-decoration"
(cond [(item-finished? item) (cond [(item-finished? item)
"line-through"] "line-through"]
@ -59,7 +59,7 @@
"none"]))] "none"]))]
[else [else
(view-bind (view-bind
(view-append-child (view-focus view "#items") (view-append-child (view-focus view "items")
(xexp->dom `(li (@ (id ,(item-id item))) (xexp->dom `(li (@ (id ,(item-id item)))
,(item-content item)))) ,(item-content item))))
"click" "click"
@ -74,7 +74,7 @@
(define the-view (define the-view
(view-bind (view-focus (->view index.html) "#add-button") (view-bind (view-focus (->view index.html) "add-button")
"click" "click"
on-add)) on-add))

View File

@ -23,13 +23,13 @@
(define (draw world dom) (define (draw world dom)
(define v1 (if (coord? (world-real world)) (define v1 (if (coord? (world-real world))
(update-view-text (view-focus dom "#real-location") (update-view-text (view-focus dom "real-location")
(format "lat=~a, lng=~a" (format "lat=~a, lng=~a"
(coord-lat (world-real world)) (coord-lat (world-real world))
(coord-lng (world-real world)))) (coord-lng (world-real world))))
dom)) dom))
(define v2 (if (coord? (world-mock world)) (define v2 (if (coord? (world-mock world))
(update-view-text (view-focus v1 "#mock-location") (update-view-text (view-focus v1 "mock-location")
(format "lat=~a, lng=~a" (format "lat=~a, lng=~a"
(coord-lat (world-mock world)) (coord-lat (world-mock world))
(coord-lng (world-mock world)))) (coord-lng (world-mock world))))

View File

@ -72,15 +72,12 @@
// For the moment, we only support selection by id. // For the moment, we only support selection by id.
var idRegexp = new RegExp("^#");
var selectorMatches = function(selector, node) { var selectorMatches = function(selector, node) {
if (selector.match(idRegexp)) {
if (node.nodeType === 1) { if (node.nodeType === 1) {
return node.getAttribute('id') === selector.substring(1); return node.getAttribute('id') === selector;
} else { } else {
return false; return false;
} }
}
return false; return false;
}; };
@ -107,6 +104,7 @@
this.nonce); this.nonce);
}; };
MockView.prototype.updateFocus = function(selector) { MockView.prototype.updateFocus = function(selector) {
selector = selector.toString(); selector = selector.toString();
return this.act( return this.act(
@ -125,7 +123,7 @@
}, },
function(eventHandlers) { return eventHandlers; }, function(eventHandlers) { return eventHandlers; },
function(view) { function(view) {
view.focus = view.top.find(selector); view.focus = $(document.getElementById(selector));
} }
); );
}; };