                              :overflow "hidden"
                              :text-overflow "ellipsis"})))
 
-(defn condense! [elem container]
+(defn arrange-element! [state elem container]
   (remove-all-styles! elem)
   (d/set-styles! elem {:white-space "pre"})
-  (d/remove-class! elem "arranged")
   (when (overflow? elem container)
-    (when (d/has-class? elem "collection")
-      (condense-collection! elem container))))
+    (cond
+     (d/has-class? elem "collection")
+     (if (d/has-class? elem "condensed")
+       (condense-collection! elem container)
+       (arrange-collection! state elem container))
+     (d/has-class? elem "keyval")
+     (arrange-keyval! state elem container))))
+
+(defn arrange! [elem container]
+  (arrange-element! initial-arrange-state elem container))
 
 (defn find-arranged-parent [elem container]
   (cond (= container elem)

(def next-state rest)

(defn arrange-keyval! [state elem container]
  (d/add-class! elem "arranged")
  (let [[key separator val] (d/children elem)]
    (arrange-element! state key container)
    (arrange-element! state val container)

        :else
          (recur (.-parentNode elem) container)))

(defn toggle! [target-elem arranged-elem container]
  (if (d/has-class? target-elem "condensed")
    (d/remove-class! target-elem "condensed")
    (d/add-class! target-elem "condensed"))
  (arrange! arranged-elem container))

(defn set-toggle-on-click! [elem container]
  (events/listen (d/single-node elem) "click"
                 (fn [event]
                   (loop [t (.-target event)]
                     (when t
                       (if (and (gdom/isElement t)
                                (d/has-class? t "collection")
                                (or (d/has-class? t "condensed")
                                    (d/has-class? t "arranged")))
                         (do (.stopPropagation event)
                             (.preventDefault event)
                             (toggle! t elem container))
                         (recur (.-parentNode t))))))))
