From 701437b7253cf2393d059ec8e90e0f37f41b3d24 Mon Sep 17 00:00:00 2001 From: eyelidlessness Date: Fri, 27 Nov 2015 19:52:37 -0800 Subject: [PATCH] Correctly handle query params --- project.clj | 2 +- src/accountant/core.cljs | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/project.clj b/project.clj index 1c473a8..b23b6d6 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject venantius/accountant "0.1.5" +(defproject venantius/accountant "0.1.6" :description "Navigation for Single-Page Applications Made Easy." :url "http://github.com/venantius/accountant" :license {:name "Eclipse Public License" diff --git a/src/accountant/core.cljs b/src/accountant/core.cljs index d32490c..6f1ca4b 100644 --- a/src/accountant/core.cljs +++ b/src/accountant/core.cljs @@ -6,7 +6,8 @@ [secretary.core :as secretary] [goog.events :as events] [goog.history.EventType :as EventType]) - (:import goog.history.Html5History + (:import goog.history.Event + goog.history.Html5History goog.Uri)) (defonce history (Html5History.)) @@ -34,6 +35,31 @@ (when-let [parent (.-parentNode e)] (recur parent)))) +(defn- get-url + "Gets the URL for a history token, but without preserving the query string + as Google's version incorrectly does. (See https://goo.gl/xwgUos)" + [history token] + (str (.-pathPrefix_ history) token)) + +(defn- set-token! + "Sets a history token, but without preserving the query string as Google's + version incorrectly does. (See https://goo.gl/xwgUos)" + [history token title] + (let [js-history (.. history -window_ -history) + url (get-url history token)] + (.pushState js-history nil (or title js/document.title "") url) + (.dispatchEvent history (Event. token)))) + +(defn- uri->query [uri] + (let [query (.getQuery uri)] + (when-not (empty? query) + (str "?" query)))) + +(defn- uri->fragment [uri] + (let [fragment (.getFragment uri)] + (when-not (empty? fragment) + (str "#" fragment)))) + (defn- prevent-reload-on-known-path "Create a click handler that blocks page reloads for known routes in Secretary." @@ -50,10 +76,14 @@ shift-key (.-shiftKey e) any-key (or meta-key alt-key ctrl-key shift-key) href (find-href target) - path (.getPath (.parse Uri href)) + uri (.parse Uri href) + path (.getPath uri) + query (uri->query uri) + fragment (uri->fragment uri) + relative-href (str path query fragment) title (.-title target)] (when (and (not any-key) (= button 0) (secretary/locate-route path)) - (. history (setToken path title)) + (set-token! history relative-href title) (.preventDefault e)))))) (defn configure-navigation!