Skip to content

Commit

Permalink
0.3.8 -> prevent-default-when-no-match? opt + replace recur-href with
Browse files Browse the repository at this point in the history
closest
  • Loading branch information
wavejumper committed Aug 28, 2017
1 parent 923581d commit 5a0f6f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject kibu/pushy "0.3.7"
(defproject kibu/pushy "0.3.8"
:description "HTML5 pushState for Clojurescript"
:url "https://github.com/kibu-australia/pushy"
:license {:name "Eclipse Public License"
Expand Down
29 changes: 13 additions & 16 deletions src/pushy/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
(defn- on-click [funk]
(events/listen js/document "click" funk))

(defn- recur-href
"Traverses up the DOM tree and returns the first node that contains a href attr"
[target]
(if (.-href target)
target
(when (.-parentNode target)
(recur-href (.-parentNode target)))))

(defn- update-history! [h]
(doto h
(.setUseFragment false)
Expand Down Expand Up @@ -68,8 +60,9 @@
* identity-fn: (optional) extract the route from value returned by match-fn"
[dispatch-fn match-fn &
{:keys [processable-url? identity-fn]
:or {processable-url? processable-url?
identity-fn identity}}]
:or {processable-url? processable-url?
identity-fn identity
prevent-default-when-no-match? (constantly false)}}]

(let [history (new-history)
event-keys (atom nil)]
Expand Down Expand Up @@ -104,7 +97,7 @@
(swap! event-keys conj
(on-click
(fn [e]
(when-let [el (recur-href (-> e .-target))]
(when-let [el (some-> e .-target (.closest "a"))]
(let [uri (.parse Uri (.-href el))]
;; Proceed if `identity-fn` returns a value and
;; the user did not trigger the event via one of the
Expand All @@ -123,12 +116,16 @@
;; Only dispatch on left button click
(= 0 (.-button e)))
(let [next-token (get-token-from-uri uri)]
(when (identity-fn (match-fn next-token))
(if (identity-fn (match-fn next-token))
;; Dispatch!
(if-let [title (-> el .-title)]
(set-token! this next-token title)
(set-token! this next-token))
(.preventDefault e)))))))))
(do
(if-let [title (-> el .-title)]
(set-token! this next-token title)
(set-token! this next-token))
(.preventDefault e))

(when (prevent-default-when-no-match? next-token)
(.preventDefault e))))))))))
nil)

(stop! [this]
Expand Down

0 comments on commit 5a0f6f0

Please sign in to comment.