reagent/demo/reagentdemo/common.cljs

59 lines
1.6 KiB
Plaintext
Raw Normal View History

2014-01-17 14:22:49 +00:00
(ns reagentdemo.common
(:require [reagent.core :as reagent :refer [atom]]
2014-01-18 10:43:56 +00:00
[reagent.debug :refer-macros [dbg println]]
2014-01-17 14:22:49 +00:00
[clojure.string :as string]
[reagentdemo.page :as rpage]
2014-01-18 10:43:56 +00:00
[reagentdemo.syntax :as syntax]))
2014-01-17 14:22:49 +00:00
2014-01-18 10:43:56 +00:00
(def syntaxify (memoize syntax/syntaxify))
2014-01-17 14:22:49 +00:00
(defn src-parts [src]
(string/split src #"\n(?=[(])"))
(defn src-defs [parts]
(let [ws #"[^ \t]+"]
(into {} (for [x parts]
[(->> x (re-seq ws) second keyword) x]))))
2014-02-15 16:29:37 +00:00
(def ns-src
2014-01-17 14:22:49 +00:00
"(ns example
(:require [reagent.core :as reagent :refer [atom]]))
")
2014-02-15 16:29:37 +00:00
(def nsr-src
"(ns example
(:require [reagent.core :as r :refer [atom]]))
")
2014-01-17 14:22:49 +00:00
(defn src-for-names [srcmap names]
2014-01-30 21:29:42 +00:00
(string/join "\n" (map srcmap names)))
2014-01-17 14:22:49 +00:00
(defn fun-map [src]
2014-02-15 16:29:37 +00:00
(-> src src-parts src-defs (assoc :ns ns-src :nsr nsr-src)))
2014-01-17 14:22:49 +00:00
(defn src-for [funmap defs]
[:pre (-> funmap (src-for-names defs) syntaxify)])
(defn demo-component [{:keys [comp src complete]}]
2014-01-18 10:43:56 +00:00
(let [showing (atom true)]
2014-01-17 14:22:49 +00:00
(fn []
[:div
(when comp
2014-01-30 21:29:42 +00:00
[:div.demo-example.clearfix
2014-01-17 14:22:49 +00:00
[:a.demo-example-hide {:on-click (fn [e]
(.preventDefault e)
(swap! showing not)
false)}
2014-01-17 14:22:49 +00:00
(if @showing "hide" "show")]
[:h3.demo-heading "Example "]
(when @showing
(if-not complete
[:div.simple-demo [comp]]
[comp]))])
2014-02-15 16:29:37 +00:00
(if @showing
(if src
[:div.demo-source.clearfix
[:h3.demo-heading "Source"]
src]
[:div.clearfix]))])))