Merge pull request #107 from metosin/frontend-routing-2

Frontend routing fixes
This commit is contained in:
Juho Teperi 2018-07-12 22:59:30 +03:00 committed by GitHub
commit 88c16bd64d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 3 deletions

View file

@ -11,6 +11,7 @@
; You must not remove this notice, or any other, from this software. ; You must not remove this notice, or any other, from this software.
(ns ^:no-doc reitit.impl (ns ^:no-doc reitit.impl
#?(:cljs (:require-macros [reitit.impl]))
(:require [clojure.string :as str] (:require [clojure.string :as str]
[clojure.set :as set]) [clojure.set :as set])
#?(:clj #?(:clj
@ -222,3 +223,15 @@
"=" "="
(url-encode (into-string v))))) (url-encode (into-string v)))))
(str/join "&"))) (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.events :as e]
[goog.dom :as dom] [goog.dom :as dom]
[reitit.core :as r] [reitit.core :as r]
[reitit.frontend :as rf]) [reitit.frontend :as rf]
[reitit.impl :as impl])
(:import goog.history.Html5History (:import goog.history.Html5History
goog.Uri)) goog.Uri))
@ -54,7 +55,24 @@
(= 0 (.-button e)) (= 0 (.-button e))
(reitit/match-by-path router (.getPath uri))) (reitit/match-by-path router (.getPath uri)))
(.preventDefault e) (.preventDefault e)
(.replaceToken history (path->token history (.getPath uri))))))) (.setToken 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! (defn start!
"This registers event listeners on either haschange or HTML5 history. "This registers event listeners on either haschange or HTML5 history.
@ -75,7 +93,7 @@
:or {path-prefix "/" :or {path-prefix "/"
use-fragment true}}] use-fragment true}}]
(let [history (let [history
(doto (Html5History.) (doto (Html5History. nil (TokenTransformer.))
(.setEnabled true) (.setEnabled true)
(.setPathPrefix path-prefix) (.setPathPrefix path-prefix)
(.setUseFragment use-fragment)) (.setUseFragment use-fragment))
@ -118,7 +136,17 @@
token (match->token history match k params query)] token (match->token history match k params query)]
(token->href history token)))) (token->href history token))))
(defn set-token
"Sets the new route, leaving previous route in history."
([state k params]
(set-token state k params nil))
([{:keys [router history]} k params query]
(let [match (rf/match-by-name! router k params)
token (match->token history match k params query)]
(.setToken history token))))
(defn replace-token (defn replace-token
"Replaces current route. I.e. current route is not left on history."
([state k params] ([state k params]
(replace-token state k params nil)) (replace-token state k params nil))
([{:keys [router history]} k params query] ([{:keys [router history]} k params query]