Skip to content

Commit

Permalink
Merge pull request #16 from eyelidlessness/master
Browse files Browse the repository at this point in the history
Correctly handle query params
  • Loading branch information
venantius committed Dec 3, 2015
2 parents 500b66f + 701437b commit b7982d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
36 changes: 33 additions & 3 deletions src/accountant/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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.))
Expand Down Expand Up @@ -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."
Expand All @@ -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!
Expand Down

0 comments on commit b7982d8

Please sign in to comment.