Fix creating urls with query string when not using fragment

This commit is contained in:
Juho Teperi 2018-07-12 22:46:25 +03:00
parent 5fd2f7b405
commit 6b8ebdebe6
2 changed files with 34 additions and 3 deletions

View file

@ -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)))

View file

@ -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))