Use html5 history

This commit is contained in:
Dan Holmsand 2015-09-08 09:35:55 +02:00
parent f5ad999593
commit d7cfb82dd3
1 changed files with 23 additions and 15 deletions

View File

@ -51,6 +51,9 @@
(defonce history nil) (defonce history nil)
(defn as-relative [f]
(clojure.string/replace f #"^/" ""))
(defn demo-handler [state [id v1 v2 :as event]] (defn demo-handler [state [id v1 v2 :as event]]
(case id (case id
:content (do :content (do
@ -63,7 +66,7 @@
:main-content v1 :main-content v1
:title title))) :title title)))
:set-page (do (secretary/dispatch! v1) :set-page (do (secretary/dispatch! v1)
(assoc state :page v1)) (assoc state :page-name v1))
:goto-page (do :goto-page (do
(when r/is-client (when r/is-client
(.setToken history v1 false) (.setToken history v1 false)
@ -72,7 +75,7 @@
state)) state))
(defn dispatch [event] (defn dispatch [event]
;; (dbg event) (dbg event)
(rswap! config demo-handler event) (rswap! config demo-handler event)
nil) nil)
@ -81,9 +84,22 @@
(defn init-history [] (defn init-history []
(when-not history (when-not history
(doto (set! history (History.)) (let [page (:page-name @config)
(evt/listen hevt/NAVIGATE #(dispatch [:set-page (.-token %)])) html5 (let [proto js/location.protocol]
(.setEnabled true)))) (and page
(.isSupported Html5History)
(#{"http:" "https:"} proto)))]
(doto (set! history
(if html5
(doto (Html5History.)
(.setUseFragment false)
(.setPathPrefix js/location.origin)
(.setEnabled true))
(History.)))
(evt/listen hevt/NAVIGATE #(dispatch [:set-page (str (.-token %))]))
(.setEnabled true))
(when html5
(.setToken history page)))))
;; (defn register-page ;; (defn register-page
@ -286,8 +302,6 @@
(write-file (path-join dir (:css-file @config)) (write-file (path-join dir (:css-file @config))
(read-css))) (read-css)))
(defn as-relative [f]
(clojure.string/replace f #"^/" ""))
;;; Main entry points ;;; Main entry points
@ -295,7 +309,7 @@
(log "Generating site") (log "Generating site")
(swap! config merge (js->clj opts :keywordize-keys true)) (swap! config merge (js->clj opts :keywordize-keys true))
(let [dir (:site-dir @config) (let [dir (:site-dir @config)
timestamp (str "?" (.' js/Date now))] timestamp (str "?" js/Date.now)]
(doseq [f (:pages @config)] (doseq [f (:pages @config)]
(write-file (path-join dir (dbg (as-relative f))) (write-file (path-join dir (dbg (as-relative f)))
(gen-page f timestamp))) (gen-page f timestamp)))
@ -309,13 +323,7 @@
(js->clj js/pageConfig :keywordize-keys true)) (js->clj js/pageConfig :keywordize-keys true))
page-name (:page-name conf)] page-name (:page-name conf)]
(swap! config merge conf) (swap! config merge conf)
(when (nil? history)
;; (when page-name
;; (set-start-page page-name))
(init-history) (init-history)
;; (setup-history page-name)
;; (set! (.-title js/document) (get-title))
)
(r/render-component (body) (r/render-component (body)
(.' js/document getElementById (.' js/document getElementById
(:main-div @config)))))) (:main-div @config))))))