From f7d94ade13ea517ccb888085d51f71b228c2d0c3 Mon Sep 17 00:00:00 2001 From: Baptiste Dupuch Date: Mon, 9 Oct 2017 11:25:10 +0200 Subject: [PATCH 1/4] - add recur for other attrs than href --- src/pushy/core.cljs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/pushy/core.cljs b/src/pushy/core.cljs index 10c0d17..051eb26 100644 --- a/src/pushy/core.cljs +++ b/src/pushy/core.cljs @@ -10,6 +10,14 @@ (defn- on-click [funk] (events/listen js/document "click" funk)) +(defn- recur-attr + "Traverses up the DOM tree and returns the first node that contains a href, action or formaction attr" + [target] + (if (some #(goog.object/get target %) #{"href" "action" "formaction"}) + target + (when (.-parentNode target) + (recur-attr (.-parentNode target))))) + (defn- update-history! [h] (doto h (.setUseFragment false) @@ -97,7 +105,7 @@ (swap! event-keys conj (on-click (fn [e] - (when-let [el (some-> e .-target (.closest "a"))] + (when-let [el (recur-attr (-> e .-target))] (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 From 822aef02a14e2ef8b76e8e9f651775f311dc01be Mon Sep 17 00:00:00 2001 From: Baptiste Dupuch Date: Mon, 9 Oct 2017 13:20:04 +0200 Subject: [PATCH 2/4] Correct good behavior --- src/pushy/core.cljs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pushy/core.cljs b/src/pushy/core.cljs index 051eb26..2ba9c98 100644 --- a/src/pushy/core.cljs +++ b/src/pushy/core.cljs @@ -11,9 +11,9 @@ (events/listen js/document "click" funk)) (defn- recur-attr - "Traverses up the DOM tree and returns the first node that contains a href, action or formaction attr" + "Traverses up the DOM tree and returns the first node that contains a href, action or formAction attr" [target] - (if (some #(goog.object/get target %) #{"href" "action" "formaction"}) + (if (some #(goog.object/get target %) #{"href" "formAction"}) target (when (.-parentNode target) (recur-attr (.-parentNode target))))) @@ -106,7 +106,7 @@ (on-click (fn [e] (when-let [el (recur-attr (-> e .-target))] - (let [uri (.parse Uri (.-href el))] + (let [uri (.parse Uri (some #(goog.object/get el %) #{"href" "formAction"}))] ;; Proceed if `identity-fn` returns a value and ;; the user did not trigger the event via one of the ;; keys we should bypass From 8befb2679bb08a45aa4f4d6fd27647c9253184c0 Mon Sep 17 00:00:00 2001 From: Baptiste Dupuch Date: Tue, 10 Oct 2017 12:02:28 +0200 Subject: [PATCH 3/4] Working submit event dispatch form forms action - Need to rework as it is highly un-optimized --- src/pushy/core.cljs | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/src/pushy/core.cljs b/src/pushy/core.cljs index 2ba9c98..fadb4ef 100644 --- a/src/pushy/core.cljs +++ b/src/pushy/core.cljs @@ -10,13 +10,22 @@ (defn- on-click [funk] (events/listen js/document "click" funk)) -(defn- recur-attr +(defn- on-submit [funk] + (events/listen js/document "submit" funk)) + +(defn- recur-href + "Traverses up the DOM tree and returns the first node that contains a href, action or formAction attr" + [target] + (when (goog.object/get target "href") + target)) + +(defn- recur-action "Traverses up the DOM tree and returns the first node that contains a href, action or formAction attr" [target] - (if (some #(goog.object/get target %) #{"href" "formAction"}) + (if (goog.object/get target "action") target (when (.-parentNode target) - (recur-attr (.-parentNode target))))) + (recur-action (.-parentNode target))))) (defn- update-history! [h] (doto h @@ -47,7 +56,8 @@ (replace-token! [this token] [this token title]) (get-token [this]) (start! [this]) - (stop! [this])) + (stop! [this]) + (get-event-keys [this])) (defn- processable-url? [uri] (and (not (clojure.string/blank? uri)) ;; Blank URLs are not processable. @@ -86,6 +96,9 @@ (replace-token! [_ token title] (. history (replaceToken token title))) + (get-event-keys [_] + event-keys) + (get-token [_] (.getToken history)) @@ -105,8 +118,8 @@ (swap! event-keys conj (on-click (fn [e] - (when-let [el (recur-attr (-> e .-target))] - (let [uri (.parse Uri (some #(goog.object/get el %) #{"href" "formAction"}))] + (when-let [el (recur-href (-> e .-target))] + (let [uri (.parse Uri (goog.object/get el "href"))] ;; Proceed if `identity-fn` returns a value and ;; the user did not trigger the event via one of the ;; keys we should bypass @@ -134,6 +147,23 @@ (when (prevent-default-when-no-match? next-token) (.preventDefault e)))))))))) + (swap! event-keys conj + (on-submit + (fn [e] + (when-let [el (recur-action (-> e .-target))] + (let [uri (.parse Uri (goog.object/get el "action"))] + (when (processable-url? uri) + (let [next-token (get-token-from-uri uri)] + (if (identity-fn (match-fn next-token)) + ;; Dispatch! + (do + (if-let [title (-> el .-title)] + (set-token! this next-token title) + (set-token! this next-token)) + (doto e (.preventDefault) #_(.stopPropagation))) + + (when (prevent-default-when-no-match? next-token) + (doto e (.preventDefault) #_(.stopPropagation))))))))))) nil) (stop! [this] From e9725a9088bee81077f7386c58b953d86605e7b5 Mon Sep 17 00:00:00 2001 From: Baptiste Dupuch Date: Tue, 17 Oct 2017 17:27:40 +0200 Subject: [PATCH 4/4] - add the right recur --- src/pushy/core.cljs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/pushy/core.cljs b/src/pushy/core.cljs index fadb4ef..006acab 100644 --- a/src/pushy/core.cljs +++ b/src/pushy/core.cljs @@ -16,8 +16,10 @@ (defn- recur-href "Traverses up the DOM tree and returns the first node that contains a href, action or formAction attr" [target] - (when (goog.object/get target "href") - target)) + (if (goog.object/get target "href") + target + (when (.-parentNode target) + (recur-href (.-parentNode target))))) (defn- recur-action "Traverses up the DOM tree and returns the first node that contains a href, action or formAction attr" @@ -147,6 +149,7 @@ (when (prevent-default-when-no-match? next-token) (.preventDefault e)))))))))) + (swap! event-keys conj (on-submit (fn [e] @@ -160,10 +163,10 @@ (if-let [title (-> el .-title)] (set-token! this next-token title) (set-token! this next-token)) - (doto e (.preventDefault) #_(.stopPropagation))) + (.preventDefault e)) (when (prevent-default-when-no-match? next-token) - (doto e (.preventDefault) #_(.stopPropagation))))))))))) + (.preventDefault e)))))))))) nil) (stop! [this]