2014-01-17 11:35:40 +00:00
|
|
|
(ns reagentdemo.page
|
|
|
|
(:require [reagent.core :as reagent :refer [atom partial]]
|
2014-04-01 17:50:28 +00:00
|
|
|
[reagent.interop :refer-macros [.' .! fvar fvar?]]
|
2014-01-17 11:35:40 +00:00
|
|
|
[reagent.debug :refer-macros [dbg]]
|
|
|
|
[clojure.string :as string]
|
2014-02-03 13:40:37 +00:00
|
|
|
[goog.events :as events]
|
|
|
|
[goog.history.EventType :as hevt])
|
2014-01-17 11:35:40 +00:00
|
|
|
(:import [goog History]
|
2014-02-03 13:40:37 +00:00
|
|
|
[goog.history Html5History]))
|
2014-01-17 11:35:40 +00:00
|
|
|
|
|
|
|
(def page (atom ""))
|
2014-01-27 21:30:42 +00:00
|
|
|
(def base-path (atom nil))
|
|
|
|
(def html5-history false)
|
2014-01-17 11:35:40 +00:00
|
|
|
|
|
|
|
(defn create-history []
|
|
|
|
(when reagent/is-client
|
|
|
|
(let [proto (-> js/window .-location .-protocol)]
|
|
|
|
(if (and (.isSupported Html5History)
|
|
|
|
(case proto "http:" true "https:" true false))
|
2014-01-27 21:30:42 +00:00
|
|
|
(do (set! html5-history true)
|
|
|
|
(doto (Html5History.)
|
|
|
|
(.setUseFragment false)))
|
2014-01-17 11:35:40 +00:00
|
|
|
(History.)))))
|
|
|
|
|
2014-02-21 06:45:40 +00:00
|
|
|
(def history (create-history))
|
|
|
|
|
2014-01-17 11:35:40 +00:00
|
|
|
(defn setup-history []
|
2014-02-21 06:45:40 +00:00
|
|
|
(when-let [h history]
|
2014-02-03 13:40:37 +00:00
|
|
|
(events/listen h hevt/NAVIGATE
|
2014-01-29 10:53:45 +00:00
|
|
|
(fn [e]
|
|
|
|
(reset! page (subs (.-token e)
|
|
|
|
(count @base-path)))
|
|
|
|
(reagent/flush)))
|
2014-01-17 11:35:40 +00:00
|
|
|
(add-watch page ::history (fn [_ _ oldp newp]
|
2014-02-21 06:45:40 +00:00
|
|
|
(when-not (= oldp newp)
|
|
|
|
(.setToken h (str @base-path newp)))))
|
|
|
|
(.setEnabled h true)))
|
2014-01-17 11:35:40 +00:00
|
|
|
|
2014-02-21 06:45:40 +00:00
|
|
|
(js/setTimeout setup-history 100)
|
2014-01-17 11:35:40 +00:00
|
|
|
|
2014-01-27 21:30:42 +00:00
|
|
|
(defn set-start-page [p]
|
|
|
|
(when html5-history
|
|
|
|
;; Find base-path for html5 history
|
|
|
|
(let [loc (-> js/window .-location .-pathname)
|
|
|
|
split #".[^/]*"
|
|
|
|
loc-parts (re-seq split loc)
|
|
|
|
page-parts (re-seq split (case p "" "." p))
|
|
|
|
base (str (apply str
|
|
|
|
(drop-last (count page-parts) loc-parts))
|
|
|
|
"/")]
|
|
|
|
(reset! base-path (string/replace base #"^/" ""))))
|
|
|
|
(reset! page p))
|
|
|
|
|
2014-01-21 10:50:08 +00:00
|
|
|
(def title-atom (atom ""))
|
2014-01-17 11:35:40 +00:00
|
|
|
|
2014-01-21 10:50:08 +00:00
|
|
|
(def page-map (atom nil))
|
|
|
|
|
|
|
|
(def reverse-page-map (atom nil))
|
|
|
|
|
|
|
|
(add-watch page-map ::page-map-watch
|
|
|
|
(fn [_ _ _ new-map]
|
|
|
|
(reset! reverse-page-map
|
|
|
|
(into {} (for [[k v] new-map]
|
|
|
|
[v k])))))
|
|
|
|
|
|
|
|
(defn prefix [href]
|
|
|
|
(let [depth (-> #"/" (re-seq @page) count)]
|
|
|
|
(str (->> "../" (repeat depth) (apply str)) href)))
|
|
|
|
|
2014-02-16 07:40:52 +00:00
|
|
|
(defn link [props child]
|
2014-01-21 10:50:08 +00:00
|
|
|
(let [rpm @reverse-page-map
|
|
|
|
href (-> props :href rpm)]
|
|
|
|
(assert (string? href))
|
2014-02-16 07:40:52 +00:00
|
|
|
[:a (assoc props
|
|
|
|
:href (prefix href)
|
|
|
|
:on-click (if history
|
|
|
|
(fn [e]
|
|
|
|
(.preventDefault e)
|
|
|
|
(reset! page href)
|
2014-02-21 06:45:40 +00:00
|
|
|
(reagent/next-tick
|
2014-02-22 07:37:30 +00:00
|
|
|
#(set! (.-scrollTop (.-body js/document))
|
2014-02-22 07:21:21 +00:00
|
|
|
0)))
|
2014-02-16 07:40:52 +00:00
|
|
|
identity))
|
|
|
|
child]))
|
2014-01-21 10:50:08 +00:00
|
|
|
|
2014-01-30 21:29:42 +00:00
|
|
|
(add-watch page ::title-watch
|
|
|
|
(fn [_ _ _ p]
|
|
|
|
;; First title on a page wins
|
|
|
|
(reset! title-atom "")))
|
|
|
|
|
2014-02-10 22:08:20 +00:00
|
|
|
(defn title [name]
|
|
|
|
(when (= @title-atom "")
|
|
|
|
(if reagent/is-client
|
2014-02-22 07:21:21 +00:00
|
|
|
(set! (.-title js/document) name))
|
2014-02-10 22:08:20 +00:00
|
|
|
(reset! title-atom name))
|
|
|
|
[:div])
|