Expand top layer of data structures

This commit is contained in:
chris (daiyi) 2017-09-29 11:04:28 +02:00
parent a2ece4b9fc
commit 762077ac0b
1 changed files with 41 additions and 33 deletions

View File

@ -18,47 +18,55 @@
(declare jsonml->hiccup) (declare jsonml->hiccup)
(defn data-structure [jsonml] (defn data-structure
(let [expanded? (r/atom false)] ([jsonml]
(fn [jsonml] (data-structure jsonml 0))
[:span ([jsonml depth]
{:class (str/join " " ["re-frame-trace--object" (let [expanded? (r/atom false)]
(when @expanded? "expanded")])} (fn [jsonml]
[:span {:class "toggle" [:div
:on-click #(swap! expanded? not)} {:class (str/join " " ["re-frame-trace--object"
[:button (if @expanded? "▼" "▶")]] (when @expanded? "expanded")])}
(jsonml->hiccup (if @expanded? [:span {:class "toggle"
(cljs-devtools/body-api-call :on-click #(swap! expanded? not)}
(.-object (get jsonml 1)) [:button (if @expanded? "▼" "▶")]]
(.-config (get jsonml 1))) (jsonml->hiccup (if @expanded?
(cljs-devtools/header-api-call (cljs-devtools/body-api-call
(.-object (get jsonml 1)) (.-object (get jsonml 1))
(.-config (get jsonml 1)))))]))) (.-config (get jsonml 1)))
(cljs-devtools/header-api-call
(.-object (get jsonml 1))
(.-config (get jsonml 1))))
(+ depth 1))]))))
(defn jsonml->hiccup [jsonml] (defn jsonml->hiccup
(if (number? jsonml) ([jsonml]
jsonml (jsonml->hiccup jsonml 0))
(let [[head & args] jsonml ([jsonml depth]
tagnames #{"div" "span" "ol" "li" "table" "tr" "td"}] (if (number? jsonml)
(cond jsonml
(contains? tagnames head) (let [[style & children] args] (let [[head & args] jsonml
(into tagnames #{"div" "span" "ol" "li" "table" "tr" "td"}]
[(keyword head) {:style (-> (js->clj style) (cond
(get "style") (contains? tagnames head) (let [[style & children] args]
(string->css))}] (into
(map jsonml->hiccup children))) [(keyword head) {:style (-> (js->clj style)
(get "style")
(string->css))}]
(map jsonml->hiccup children)))
(= head "object") [data-structure jsonml] (= head "object") [data-structure jsonml (+ depth 1)]
(= jsonml ", ") " " (= jsonml ", ") " "
:else jsonml)))) :else jsonml)))))
(defn subtree [data title] (defn subtree [data title]
(let [expanded? (r/atom false)] (let [expanded? (r/atom true)]
(fn [data] (fn [data]
[:div [:div
{:class (str/join " " ["re-frame-trace--object" {:class (str/join " " ["re-frame-trace--object"
(when @expanded? "expanded")])} (when @expanded? "expanded")])}
[:span {:class "toggle" [:span {:style {:margin-left 1}
:class "toggle"
:on-click #(swap! expanded? not)} :on-click #(swap! expanded? not)}
[:button (if @expanded? "▼ " "▶ ")]] [:button (if @expanded? "▼ " "▶ ")]]
(or title "data") (or title "data")