mirror of
https://github.com/status-im/reagent.git
synced 2025-01-12 21:05:20 +00:00
Move all page handling to page.cljs
This commit is contained in:
parent
f5c80e820e
commit
97da42101a
@ -1,8 +1,8 @@
|
|||||||
(ns demo
|
(ns demo
|
||||||
(:require [reagent.core :as reagent :refer [atom]]
|
(:require [reagent.core :as reagent :refer [atom]]
|
||||||
[clojure.string :as string]
|
[clojure.string :as string]
|
||||||
[reagentdemo.common :as common
|
[reagentdemo.page :as page :refer [page-map page link prefix]]
|
||||||
:refer [demo-component page link page-map prefix]]
|
[reagentdemo.common :as common :refer [demo-component]]
|
||||||
[reagentdemo.intro :as intro]
|
[reagentdemo.intro :as intro]
|
||||||
[reagentdemo.news :as news]
|
[reagentdemo.news :as news]
|
||||||
[reagent.debug :refer-macros [dbg println]]))
|
[reagent.debug :refer-macros [dbg println]]))
|
||||||
@ -35,9 +35,8 @@
|
|||||||
|
|
||||||
(defn gen-page [p timestamp]
|
(defn gen-page [p timestamp]
|
||||||
(reset! page p)
|
(reset! page p)
|
||||||
(dbg @page)
|
|
||||||
(let [body (reagent/render-component-to-string [demo])
|
(let [body (reagent/render-component-to-string [demo])
|
||||||
title @common/title-atom]
|
title @page/title-atom]
|
||||||
(str "<!doctype html>
|
(str "<!doctype html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
@ -5,46 +5,6 @@
|
|||||||
[reagentdemo.page :as rpage]
|
[reagentdemo.page :as rpage]
|
||||||
[reagentdemo.syntax :as syntax]))
|
[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))
|
(def syntaxify (memoize syntax/syntaxify))
|
||||||
|
|
||||||
(defn src-parts [src]
|
(defn src-parts [src]
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
[reagent.debug :refer-macros [dbg println]]
|
[reagent.debug :refer-macros [dbg println]]
|
||||||
[clojure.string :as string]
|
[clojure.string :as string]
|
||||||
[reagentdemo.syntax :refer-macros [get-source]]
|
[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 []
|
(defn simple-component []
|
||||||
[:div
|
[:div
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
(:require [reagent.core :as reagent :refer [atom]]
|
(:require [reagent.core :as reagent :refer [atom]]
|
||||||
[reagent.debug :refer-macros [dbg println]]
|
[reagent.debug :refer-macros [dbg println]]
|
||||||
[reagentdemo.syntax :refer-macros [get-source]]
|
[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]))
|
[todomvc :as todomvc]))
|
||||||
|
|
||||||
(def funmap (-> "reagentdemo/news.cljs" get-source common/fun-map))
|
(def funmap (-> "reagentdemo/news.cljs" get-source common/fun-map))
|
||||||
@ -53,5 +54,5 @@
|
|||||||
(defn main []
|
(defn main []
|
||||||
[undo-example])
|
[undo-example])
|
||||||
|
|
||||||
(swap! common/page-map assoc
|
(swap! page-map assoc
|
||||||
"news/cloact-reagent-undo-demo.html" undo-example)
|
"news/cloact-reagent-undo-demo.html" undo-example)
|
||||||
|
@ -29,4 +29,40 @@
|
|||||||
|
|
||||||
(def history (setup-history))
|
(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]))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user