Move all page handling to page.cljs

This commit is contained in:
Dan Holmsand 2014-01-21 11:50:08 +01:00
parent f5c80e820e
commit 97da42101a
5 changed files with 44 additions and 47 deletions

View File

@ -1,8 +1,8 @@
(ns demo
(:require [reagent.core :as reagent :refer [atom]]
[clojure.string :as string]
[reagentdemo.common :as common
:refer [demo-component page link page-map prefix]]
[reagentdemo.page :as page :refer [page-map page link prefix]]
[reagentdemo.common :as common :refer [demo-component]]
[reagentdemo.intro :as intro]
[reagentdemo.news :as news]
[reagent.debug :refer-macros [dbg println]]))
@ -35,9 +35,8 @@
(defn gen-page [p timestamp]
(reset! page p)
(dbg @page)
(let [body (reagent/render-component-to-string [demo])
title @common/title-atom]
title @page/title-atom]
(str "<!doctype html>
<html>
<head>

View File

@ -5,46 +5,6 @@
[reagentdemo.page :as rpage]
[reagentdemo.syntax :as syntax]))
(def page rpage/page)
(def title-atom (atom "Reagent: Minimalistic React for ClojureScript"))
(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)
pref (->> "../" (repeat depth) (apply str))]
(str pref href)))
(defn link [props children]
(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
(fn [e]
(.preventDefault e)
(reset! page href))
identity))
children)))
(defn title [props children]
(let [name (first children)]
(if reagent/is-client
(let [title (aget (.getElementsByTagName js/document "title") 0)]
(set! (.-innerHTML title) (dbg name))))
(reset! title-atom name)
[:div]))
(def syntaxify (memoize syntax/syntaxify))
(defn src-parts [src]

View File

@ -4,7 +4,8 @@
[reagent.debug :refer-macros [dbg println]]
[clojure.string :as string]
[reagentdemo.syntax :refer-macros [get-source]]
[reagentdemo.common :as common :refer [demo-component title]]))
[reagentdemo.page :refer [link title]]
[reagentdemo.common :as common :refer [demo-component]]))
(defn simple-component []
[:div

View File

@ -2,7 +2,8 @@
(:require [reagent.core :as reagent :refer [atom]]
[reagent.debug :refer-macros [dbg println]]
[reagentdemo.syntax :refer-macros [get-source]]
[reagentdemo.common :as common :refer [demo-component title link]]
[reagentdemo.page :refer [title link page-map]]
[reagentdemo.common :as common :refer [demo-component]]
[todomvc :as todomvc]))
(def funmap (-> "reagentdemo/news.cljs" get-source common/fun-map))
@ -53,5 +54,5 @@
(defn main []
[undo-example])
(swap! common/page-map assoc
(swap! page-map assoc
"news/cloact-reagent-undo-demo.html" undo-example)

View File

@ -29,4 +29,40 @@
(def history (setup-history))
(def title-atom (atom ""))
(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)))
(defn link [props children]
(let [rpm @reverse-page-map
href (-> props :href rpm)]
(assert (string? href))
(apply vector
:a (assoc props
:href (prefix href)
:on-click (if history
(fn [e]
(.preventDefault e)
(reset! page href))
identity))
children)))
(defn title [props children]
(let [name (first children)]
(if reagent/is-client
(let [title (aget (.getElementsByTagName js/document "title") 0)]
(set! (.-innerHTML title) name)))
(reset! title-atom name)
[:div]))