diff --git a/modules/reitit-core/src/reitit/impl.cljc b/modules/reitit-core/src/reitit/impl.cljc index c5fca01e..d5a41969 100644 --- a/modules/reitit-core/src/reitit/impl.cljc +++ b/modules/reitit-core/src/reitit/impl.cljc @@ -11,6 +11,7 @@ ; You must not remove this notice, or any other, from this software. (ns ^:no-doc reitit.impl + #?(:cljs (:require-macros [reitit.impl])) (:require [clojure.string :as str] [clojure.set :as set]) #?(:clj @@ -222,3 +223,15 @@ "=" (url-encode (into-string v))))) (str/join "&"))) + +(defmacro goog-extend [type base-type ctor & methods] + `(do + (def ~type (fn ~@ctor)) + + (goog/inherits ~type ~base-type) + + ~@(map + (fn [method] + `(set! (.. ~type -prototype ~(symbol (str "-" (first method)))) + (fn ~@(rest method)))) + methods))) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index 7e178723..3ec91abe 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -5,7 +5,8 @@ [goog.events :as e] [goog.dom :as dom] [reitit.core :as r] - [reitit.frontend :as rf]) + [reitit.frontend :as rf] + [reitit.impl :as impl]) (:import goog.history.Html5History goog.Uri)) @@ -54,7 +55,24 @@ (= 0 (.-button e)) (reitit/match-by-path router (.getPath uri))) (.preventDefault e) - (.replaceToken history (path->token history (.getPath uri))))))) + (.replaceToken history (path->token history (str (.getPath uri) + (if (seq (.getQuery uri)) + (str "?" (.getQuery uri)))))))))) + +(impl/goog-extend + ^{:jsdoc ["@constructor" + "@extends {Html5History.TokenTransformer}"]} + TokenTransformer + Html5History.TokenTransformer + ([] + (this-as this + (.call Html5History.TokenTransformer this))) + (retrieveToken [path-prefix location] + (subs (.-pathname location) (count path-prefix))) + (createUrl [token path-prefix location] + ;; Code in Closure also adds current query params + ;; from location. + (str path-prefix token))) (defn start! "This registers event listeners on either haschange or HTML5 history. @@ -75,7 +93,7 @@ :or {path-prefix "/" use-fragment true}}] (let [history - (doto (Html5History.) + (doto (Html5History. nil (TokenTransformer.)) (.setEnabled true) (.setPathPrefix path-prefix) (.setUseFragment use-fragment))