|
10 | 10 | (defn- on-click [funk]
|
11 | 11 | (events/listen js/document "click" funk))
|
12 | 12 |
|
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 |
| - |
21 | 13 | (defn- update-history! [h]
|
22 | 14 | (doto h
|
23 | 15 | (.setUseFragment false)
|
|
68 | 60 | * identity-fn: (optional) extract the route from value returned by match-fn"
|
69 | 61 | [dispatch-fn match-fn &
|
70 | 62 | {: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)}}] |
73 | 66 |
|
74 | 67 | (let [history (new-history)
|
75 | 68 | event-keys (atom nil)]
|
|
104 | 97 | (swap! event-keys conj
|
105 | 98 | (on-click
|
106 | 99 | (fn [e]
|
107 |
| - (when-let [el (recur-href (-> e .-target))] |
| 100 | + (when-let [el (some-> e .-target (.closest "a"))] |
108 | 101 | (let [uri (.parse Uri (.-href el))]
|
109 | 102 | ;; Proceed if `identity-fn` returns a value and
|
110 | 103 | ;; the user did not trigger the event via one of the
|
|
123 | 116 | ;; Only dispatch on left button click
|
124 | 117 | (= 0 (.-button e)))
|
125 | 118 | (let [next-token (get-token-from-uri uri)]
|
126 |
| - (when (identity-fn (match-fn next-token)) |
| 119 | + (if (identity-fn (match-fn next-token)) |
127 | 120 | ;; 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)))))))))) |
132 | 129 | nil)
|
133 | 130 |
|
134 | 131 | (stop! [this]
|
|
0 commit comments