Skip to content

Commit 5a0f6f0

Browse files
committed
0.3.8 -> prevent-default-when-no-match? opt + replace recur-href with
closest
1 parent 923581d commit 5a0f6f0

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject kibu/pushy "0.3.7"
1+
(defproject kibu/pushy "0.3.8"
22
:description "HTML5 pushState for Clojurescript"
33
:url "https://github.com/kibu-australia/pushy"
44
:license {:name "Eclipse Public License"

src/pushy/core.cljs

+13-16
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010
(defn- on-click [funk]
1111
(events/listen js/document "click" funk))
1212

13-
(defn- recur-href
14-
"Traverses up the DOM tree and returns the first node that contains a href attr"
15-
[target]
16-
(if (.-href target)
17-
target
18-
(when (.-parentNode target)
19-
(recur-href (.-parentNode target)))))
20-
2113
(defn- update-history! [h]
2214
(doto h
2315
(.setUseFragment false)
@@ -68,8 +60,9 @@
6860
* identity-fn: (optional) extract the route from value returned by match-fn"
6961
[dispatch-fn match-fn &
7062
{:keys [processable-url? identity-fn]
71-
:or {processable-url? processable-url?
72-
identity-fn identity}}]
63+
:or {processable-url? processable-url?
64+
identity-fn identity
65+
prevent-default-when-no-match? (constantly false)}}]
7366

7467
(let [history (new-history)
7568
event-keys (atom nil)]
@@ -104,7 +97,7 @@
10497
(swap! event-keys conj
10598
(on-click
10699
(fn [e]
107-
(when-let [el (recur-href (-> e .-target))]
100+
(when-let [el (some-> e .-target (.closest "a"))]
108101
(let [uri (.parse Uri (.-href el))]
109102
;; Proceed if `identity-fn` returns a value and
110103
;; the user did not trigger the event via one of the
@@ -123,12 +116,16 @@
123116
;; Only dispatch on left button click
124117
(= 0 (.-button e)))
125118
(let [next-token (get-token-from-uri uri)]
126-
(when (identity-fn (match-fn next-token))
119+
(if (identity-fn (match-fn next-token))
127120
;; Dispatch!
128-
(if-let [title (-> el .-title)]
129-
(set-token! this next-token title)
130-
(set-token! this next-token))
131-
(.preventDefault e)))))))))
121+
(do
122+
(if-let [title (-> el .-title)]
123+
(set-token! this next-token title)
124+
(set-token! this next-token))
125+
(.preventDefault e))
126+
127+
(when (prevent-default-when-no-match? next-token)
128+
(.preventDefault e))))))))))
132129
nil)
133130

134131
(stop! [this]

0 commit comments

Comments
 (0)