reagent/demo/reagentdemo/common.cljs

60 lines
1.7 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]
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 []
2014-01-18 10:43:56 +00:00
(let [showing (atom true)]
(fn [{:keys [comp src complete no-heading]}]
2014-01-17 14:22:49 +00:00
[: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")]
2014-02-22 16:24:22 +00:00
(when-not no-heading
[:h3.demo-heading "Example "])
2014-01-17 14:22:49 +00:00
(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
2014-02-22 16:24:22 +00:00
(when-not no-heading
[:h3.demo-heading "Source"])
2014-02-15 16:29:37 +00:00
src]
[:div.clearfix]))])))