Simplify link handling

This commit is contained in:
Dan Holmsand 2014-01-20 11:37:57 +01:00
parent ebade509bf
commit f5c80e820e
3 changed files with 27 additions and 22 deletions

View File

@ -2,15 +2,14 @@
(:require [reagent.core :as reagent :refer [atom]]
[clojure.string :as string]
[reagentdemo.common :as common
:refer [demo-component page link reverse-page-map prefix]]
:refer [demo-component page link page-map prefix]]
[reagentdemo.intro :as intro]
[reagentdemo.news :as news]
[reagent.debug :refer-macros [dbg println]]))
(common/set-page-map {:index ["index.html" [intro/main]]
:news ["news/index.html" [news/main]]
:undo-demo ["news/cloact-reagent-undo-demo.html"
[news/main]]})
(swap! page-map assoc
"index.html" intro/main
"news/index.html" news/main)
(defn github-badge []
[:a.github-badge
@ -23,13 +22,11 @@
[:div
[:div.nav
[:ul.nav
[:li.brand [link {:href :index} "Reagent:"]]
[:li [link {:href :index} "Introduction"]]
[:li [link {:href :news} "News"]]]]
(let [p @page
[_ comp] (get @reverse-page-map p
(:index @common/page-map))]
comp)
[:li.brand [link {:href intro/main} "Reagent:"]]
[:li [link {:href intro/main} "Introduction"]]
[:li [link {:href news/main} "News"]]]]
(let [comp (get @page-map @page intro/main)]
[comp])
[github-badge]])
(defn ^:export mountdemo [p]
@ -38,6 +35,7 @@
(defn gen-page [p timestamp]
(reset! page p)
(dbg @page)
(let [body (reagent/render-component-to-string [demo])
title @common/title-atom]
(str "<!doctype html>
@ -60,7 +58,7 @@
(defn ^:export genpages []
(let [timestamp (str "?" (.now js/Date))]
(->> (keys @reverse-page-map)
(->> (keys @page-map)
(map #(vector % (gen-page % timestamp)))
(into {})
clj->js)))

View File

@ -13,10 +13,11 @@
(def page-map (atom nil))
(def reverse-page-map (atom nil))
(defn set-page-map [m]
(reset! page-map m)
(reset! reverse-page-map (into {} (for [[k [p c]] m]
[p [k c]]))))
(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)
@ -24,9 +25,9 @@
(str pref href)))
(defn link [props children]
(let [pm @page-map
href (-> props :href pm first)]
(assert href)
(let [pm @reverse-page-map
href (-> props :href pm)]
(assert (string? href))
(apply vector :a (assoc props
:href (prefix href)
:on-click (if rpage/history

View File

@ -43,9 +43,15 @@
(reset! undo-list nil)
(remove-watch state ::undo-watcher))}))
(defn main []
(defn undo-example []
(let [head "This should become news"]
[:div.reagent-demo
[:h1 [link {:href :undo-demo} head]]
[:h1 [link {:href undo-example} head]]
[title head]
[undo-demo-cleanup]]))
(defn main []
[undo-example])
(swap! common/page-map assoc
"news/cloact-reagent-undo-demo.html" undo-example)