                   (om/build-all outline-body (:children node) {:state state}))
                  ))) 
 
-(defn listen [el type]
-  (let [out (chan)]
-    (events/listen el type (fn [e]
-                             ; ahh.. haxx
-                             (when (= (.-keyCode e) 8) (.preventDefault e))
-                             (put! out e)))
-    out))
-
-(defn get-indexless [current-path]
-  (subvec current-path 0 (dec (count current-path))))
-
-(defn get-parent [current-path] (subvec current-path 0 (- (count current-path) 2)))
-
-(def keyevent->command
-  ; takes keycode, shift / alt / ctrl-state
-  ; returns action
-  {[37] :cursor-left
-   [38] :cursor-up
-   [39] :cursor-right
-   [40] :cursor-down
-   [38 :alt] :node-up
-   [40 :alt] :node-down
-   [8] :destroy-node
-   })
-
-(defn prev-depth-path [parent-path body]
-  (loop [p parent-path]
-    (let [current (get-in body p)
-          children (:children current)]
-      (if (empty? children) p
-          (recur (conj p :children (dec (count children))))))))
-
-
-(defn gen-left-path [current-child-idx parent-path
-                     up-path data]
-    (cond
-        ; 0th sibling? -> parent
-        (zero? current-child-idx) parent-path
-        ; else, deepest of parent sibling recursively
-        :else (prev-depth-path up-path @data)
-        ))
-
-(defn next-depth-path [parent-path body]
-  (loop [p parent-path]
-    (let [current-p-idx  (last p)
-          maybe-new-p    (conj (get-indexless p) (inc current-p-idx))
-          maybe-new-node (get-in body maybe-new-p false)
-          ]
-      (cond
-       ; at the root node -> bottomed out..
-       (= [:body 0] p) false
-       ; found a new node -> return it
-       maybe-new-node maybe-new-p
-       ; else, try again a level up
-       :else (recur (get-parent p))))))
-
-(defn gen-right-path [current-path current current-child-idx
-                      parent-path parent-children
-                      down-path
-                      data]
-  (cond
-   ; current node has children? -> first child
-   (not (empty? (:children current))) (conj current-path :children 0)
-   ; not max sibling? -> go to next sibling
-   (and parent-children (< current-child-idx (dec (count parent-children)))) down-path
-   ; else, find next depth wise.
-   :else (or (next-depth-path parent-path @data) current-path)))
-
 (defcomponent component [data owner]
   (init-state [_]
               (let [key-down-chan (listen js/window "keydown")
-                    click-chan (chan)
-                    root-path (om/path data)]
+                    click-chan    (chan)
+                    root-path     (om/path data)]
                 (go (loop []
                       (alt!
+                        ; key down events
+                        ; -------------------------------------------------------------------------------
                         key-down-chan
                         ([e c]
                          

                                                  ))
                               (om/transact! data
                                             (fn [d]
                                               (let [new-parent-children (vec (concat (subvec parent-children 0 current-child-idx)
                                                                                      (subvec parent-children (inc current-child-idx))))]
                                                 (print (pr-str (conj parent-path :children)))
                                                 
                                                 (assoc-in d (conj parent-path :children) new-parent-children)
                                                 ))))
                             
                             )))
                        click-chan ([e c]
                                    (om/set-state! owner :selected (subvec (:new-path e)
                                                                           (count root-path)))))
                      (recur)))
                {:selected   [:body 0]
                 :click-chan click-chan
                 :root-path  root-path}))
  (render-state [_ state]
                (print (pr-str data))
                (dom/div {:class "yeah"}
                         (dom/h2 (pr-str(:selected state)))
                         (om/build outline-head (:head data))
                         (dom/ol {:class "root"}
                                 (om/build-all outline-body (:body data) {:state state})
                                 ))))

   [om-tools.dom :as dom :include-macros true]
   ))



(def root-path root-node)


(defn listen [el type]
  (let [out (chan)]
    (events/listen el type (fn [e]
                             ; ahh.. haxx
                             (when (or (= (.-keyCode e) 8)
                                       (= (.-keyCode e) 9))
                               (.preventDefault e))
                             (put! out e)))
    out))

(defn get-indexless [current-path]
  (subvec current-path 0 (dec (count current-path))))

(defn get-parent [current-path] (subvec current-path 0 (- (count current-path) 2)))

(defn remove-node-from-parent [parent-children current-child-idx]
  (vec (concat (subvec parent-children 0 current-child-idx)
               (subvec parent-children (inc current-child-idx)))))

(def keyevent->command
  {; cursor movement ---------------------------
   [37]        :cursor-left      ;left
   [38]        :cursor-up        ;up
   [39]        :cursor-right     ;right
   [40]        :cursor-down      ;down
   ; node movement -----------------------------
   [38 :alt]   :node-up          ;alt-up
   [40 :alt]   :node-down        ;alt-down
   [9]         :right-shift-node ;tab
   [39 :alt]   :right-shift-node ;alt-right
   [9  :shift] :left-shift-node  ;shift-tab
   [37 :alt]   :left-shift-node  ;alt-left
   ; delete ------------------------------------
   [8]         :destroy-node     ;bkspace
   ; visibility of children --------------------
   [32]        :toggle-collapse  ;space
   })

(defn prev-depth-path [parent-path body]
  (loop [p parent-path]
    (let [current (get-in body p)
          children (:children current)]
      (if (empty? children) p
          (recur (conj p :children (dec (count children))))))))


(defn gen-left-path [current-child-idx parent-path
                     up-path data]
    (cond
        ; 0th sibling? -> parent
        (zero? current-child-idx) parent-path
        ; else, deepest of parent sibling recursively
        :else (prev-depth-path up-path @data)
        ))

(defn next-depth-path [parent-path body]
  (loop [p parent-path]
    (let [current-p-idx  (last p)
          maybe-new-p    (conj (get-indexless p) (inc current-p-idx))
          maybe-new-node (get-in body maybe-new-p false)
          ]
      (cond
       ; at the root node -> bottomed out..
       (= root-node p) false
       ; found a new node -> return it
       maybe-new-node maybe-new-p
       ; else, try again a level up
       :else (recur (get-parent p))))))

(defn gen-right-path [current-path current current-child-idx
                      parent-path parent-children
                      down-path
                      data]
  (cond
   ; current node has children? -> first child
   (not (empty? (:children current))) (conj current-path :children 0)
   ; not max sibling? -> go to next sibling
   (and parent-children (< current-child-idx (dec (count parent-children)))) down-path
   ; else, find next depth wise.
   :else (or (next-depth-path parent-path @data) current-path)))

(defcomponent outline-head [head owner]
  (render [_]
