Created
June 7, 2013 11:40
-
-
Save brentonashworth/5728698 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns hammock-cafe.ui.history | |
(:require [io.pedestal.app.protocols :as p] | |
[io.pedestal.app.util.log :as log] | |
[io.pedestal.app.messages :as msg])) | |
(def last-page (atom nil)) | |
(def dispatchers (atom {})) | |
(defn navigate [token] | |
(when-let [d (get @dispatchers token)] | |
(p/put-message d {msg/topic msg/app-model | |
msg/type :navigate | |
:name (keyword (str "emitter-" (name token)))}))) | |
(def supported? (and js/history (.-pushState js/history))) | |
(defn navigated [d token] | |
(when supported? | |
(let [current-token (.-state js/history)] | |
(when (not= current-token token) | |
(if (nil? @last-page) | |
(.replaceState js/history token nil nil) | |
(.pushState js/history token nil nil)))) | |
(reset! last-page token) | |
(swap! dispatchers assoc token d))) | |
(if supported? | |
(set! (.-onpopstate js/window) (fn [e] (navigate (.-state e)))) | |
(log/warn :in :hammock-cafe.ui.history | |
:message "HTML 5 History is not supported in this browser!")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment