   (let [k (key-event->keycode e)]
     (when (legal-key k) 
       (condp = k
-        ENTER (do 
-                (when (not (empty? (:text state)))
-                  (set! js/window.location.hash ((-> @data :routes :search) 
-                                                 {:query (:text state)}))
-                  (om/set-state! owner :text "")))
+        ENTER (maybe-search data state owner)
         ESC   (om/set-state! owner :text "")))))
 
 ; render ---


(defn async-window-scroll [data ajax-chan]
  (.addEventListener js/window "scroll" 
                     (fn []
                       (when
                         (and
                           (= :loaded (:current-state @data))
                           (let [body (.. js/document -body)
                                 html  (.. js/document -documentElement)
                                 height (max (.. body -scrollHeight)
                                             (.. body -offsetHeight)
                                             (.. html -clientHeight)
                                             (.. html -scrollHeight)
                                             (.. html -offsetHeight)
                                             )]
                             (> 100
                                (- height
                                   (+ (.. js/window -innerHeight)
                                      (.. js/window -scrollY))))))
                         (om/transact! data #(assoc % :current-state :loading))
                         (go (async-search @data ajax-chan))))))

(defn async-window-resize [data]
  (.addEventListener 
    js/window "resize"
    (fn []
      (om/transact! data #(assoc % :window-width (.. js/window -innerWidth))))))


(defn setup-navigation [data ajax-chan]

  (secretary/set-config! :prefix "#")

  (defroute search-route "/search/:query" [query]
    (new-search data query ajax-chan))

  (defroute index-route "/" []
    (om/transact! data reset-state))

  (om/transact! data #(assoc % :routes
                             {:index index-route
                              :search search-route}))

  (let [h (History.)]
    (goog.events/listen h EventType/NAVIGATE #(secretary/dispatch! (.-token %)))

               (dom/button 
                 #js {:onClick (fn [e]
                                 (.preventDefault e)
                                 (maybe-search data state owner)
                                 )}
                 "Search")
               ; clear button
