Created
February 16, 2014 22:37
-
-
Save selfsame/9041649 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn collapse-node [start] | |
(loop [data start result []] | |
(let [d (if (sequential? data) data [data]) | |
sorted (map (fn [v] | |
{:id (:uid v) :babies (:children v)}) d) | |
uids (flatten (filter #(not (nil? %)) (map :id sorted))) | |
babies (vec (flatten (filter #(not (nil? %)) (map :babies sorted))))] | |
(if-not (pos? (count babies)) | |
(concat result uids) | |
(recur babies (concat result uids)))))) | |
(sug/defcomp dom-node [data owner opts] | |
{:render-state | |
(fn [_ state] | |
(let [tag (:tag data) | |
uid (:uid data) | |
targeted (= (:mouse-target state) uid) | |
selected ((:selection state) uid) | |
selected-class (str (when selected "selected ") | |
(when targeted "targeted ") "") | |
node-class (str | |
"outliner_node " | |
(when (expanded? data) "open ")) | |
exp-class (cond (children? data) (cond (expanded? data) "exp-box expanded" :else "exp-box collapsed") :else "exp-box")] | |
(apply dom/div #js {:className node-class :onClick (fn [e] (sug/fire! owner :select-node {:uid @uid}) false)} | |
(dom/span #js {:className (str "outliner_background " selected-class)}) | |
(dom/span nil | |
(dom/div #js {:className exp-class :onClick (fn [e] (when (:expanded @data) | |
(sug/fire! owner :collapsing-nodes | |
{:nodes (collapse-node (:children @data))})) | |
(toggle-expansion e data owner opts) false)})) | |
(dom/span #js {:className (str "tag-name " tag)} tag) | |
(when (:id data) (dom/span #js {:className "id-name"} (:id data))) | |
(when (and (children? data) (expanded? data)) | |
(sug/make-all dom-node (:children data) {:state {:selection (:selection state)}}) | |
; (conj {} (when (= (first (:mouse-target state)) (:uid data)) | |
; {:state {:mouse-target (rest (:mouse-target state))}})) | |
))))}) | |
(sug/defcomp outliner [data owner opts] | |
{:render-state | |
(fn [_ state] | |
(let [app-state data | |
selection (:selection app-state) | |
mouse-target (:mouse-target app-state) | |
cname (str "select-" (:active (:element-filter app-state)))] | |
(apply dom/div #js {:className cname :id "outliner"} | |
(sug/make-all dom-node (:dom app-state) {:state {:selection selection :mouse-target mouse-target} })))) | |
:on {:select-node | |
(fn [e] (om/transact! data [:selection] #(toggle % (:uid e)))) | |
:collapsing-nodes | |
(fn [e] (let [nodes (:nodes e)] | |
(om/transact! data [:selection] #(apply disj % nodes) ))) }}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment