Permalink
Browse files

more robust event handling, now with really awesome delegation

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
1 parent 0018f43 commit 53a86eb37bfa899fad9d601ffb34d39ccf679d00 @ibdknox committed Aug 21, 2011
Showing with 47 additions and 14 deletions.
  1. +1 −0 .gitignore
  2. 0 src/todo/core.cljs → examples/todo.cljs
  3. +31 −9 src/pinot/events.cljs
  4. +8 −2 src/pinot/html.cljs
  5. +7 −3 src/pinot/macros.clj
View
@@ -6,3 +6,4 @@ pom.xml
.lein-failures
autodoc/
docs/
+resources/public/cljs
File renamed without changes.
View
@@ -8,19 +8,41 @@
(defn get-body []
(if-not @body
- (swap! body (first (html/dom-find "body")))
+ (reset! body (first (html/dom-find "body")))
@body))
+;;TODO: this is ugly.
+(defn ->target [elem]
+ (cond
+ (fn? elem) {:group-id (:pinot-group (meta elem))}
+ (html/attr elem :pinot-id) {:elem elem :pinot-id (html/attr elem :pinot-id)}
+ :else {:elem elem }))
+
+(defn match? [{:keys [elem group-id pinot-id]} init-target]
+ (loop [target init-target]
+ (let [target-group (html/attr target :group-id)
+ target-pinot (html/attr target :pinot-id)]
+ (when (not= target (html/parent (get-body)))
+ (if (or
+ (and elem (= elem target))
+ (and group-id (= group-id target-group))
+ (and pinot-id (= pinot-id target-pinot)))
+ target
+ (recur (html/parent target)))))))
+
(defn on [elem event func]
- (let [ev-name (string/upper-case (name event))]
+ (let [ev-name (string/upper-case (name event))
+ event (aget events/EventType ev-name)
+ body-elem (get-body)]
(doseq [el (pclj/->coll elem)]
- (events/listen (get-body)
- (aget events/EventType ev-name)
- (fn [e]
- (let [elem-id (html/attr el :pinotId)
- target-id (html/attr (.target e) :pinotId)]
- (when (= elem-id target-id)
- (func (.target e) e))))))
+ (let [parsed (->target el)]
+ (events/listen body-elem
+ event
+ (fn [e]
+ (let [target (.target e)]
+ (if-let [match (match? parsed target)]
+ (func match e)
+ true))))))
elem))
(defn prevent [e]
View
@@ -84,7 +84,7 @@
(defn elem-factory [tag-def]
(let [[tag attrs content] (normalize-element tag-def)
elem (dom/createElement tag (pjs/map->js attrs))]
- (attr elem {:pinotId (swap! elem-id inc)})
+ (attr elem {:pinot-id (swap! elem-id inc)})
(as-content elem content)
elem))
@@ -95,9 +95,15 @@
;; Dom interaction functions
;; ********************************************
+(defn parent [elem]
+ (.parentNode elem))
+
+(defn is-dom? [elem]
+ (dom/isNodeLike elem))
+
(defn dom-clone [elem]
(let [neue (. elem (cloneNode true))]
- (attr neue {:pinotId (.pinotId elem)})
+ (attr neue {:pinot-id (.pinotId elem)})
neue))
;;TODO: for a collection of elements it appends the same DOM
View
@@ -2,9 +2,13 @@
(defmacro defpartial
[name params & body]
- `(defn ~name ~params
- (pinot.html/html
- ~@body)))
+ `(let [group# (swap! inc pinot.html/group-counter)]
+ (defn ^{:pinot-group group#}
+ ~name ~params
+ (pinot.html/attr
+ (pinot.html/html
+ ~@body)
+ {:pinot-group group#}))))
(defmacro defelem
"Defines a function that will return a tag vector. If the first argument

0 comments on commit 53a86eb

Please sign in to comment.