    [:#url-input] (do-> (set-attr :value (:url-input-text state))
                        (listen :on-change #(handle-text-change % owner :url-input-text)
                                :on-key-down #(if (= (.-keyCode %) 10)
-                                               (send-bookmark state owner)
+                                               (send-bookmark app owner)
                                                (when (= (.-which %) 13)
                                                  (when (.-ctrlKey %)
-                                                   (send-bookmark state owner))))))
+                                                   (send-bookmark app owner))))))
    [:#search-input] (do-> (set-attr :value (:search-text state))
                           (listen :on-change #(handle-text-change % owner :search-text)))
-   [:#url-list] (content (map #(url %) (sort-by :ts > app)))
-   [:#bookmark-btn] (listen :on-click (fn [e]
-                                        (send-bookmark state owner)))})
+   [:#url-list] (content (let [bms (if (blank? (:search-text state))
+                                     (:bookmarks app)
+                                     (remove
+                                      (fn [bookmark]
+                                        (nil?
+                                         (re-find
+                                          (-> state :search-text trim lower-case re-pattern)
+                                          (-> bookmark :title lower-case))))
+                                      (:bookmarks app)))]
+                           (map #(url %) (sort-by :ts > bms))))
+   [:#bookmark-btn] (listen :on-click (fn [e] (send-bookmark app owner)))})
 
 
-;; --- INIT ---
-(defn bookmark-view
-  "Central view containing bookmarks and url input field"
+;; --- VIEWS ---
+(defn nav-view
+  "Navbar view handling sin-up and log-in menu"
   [app owner]
   (reify
     om/IInitState
     (init-state [_]
-      {:url-input-text ""
-       :search-text ""
-       :ws-in (chan)})
+      {:signup-text ""})
     om/IWillMount
     (will-mount [_]
       (go

(ns lesezeichen.core
  (:require [figwheel.client :as figw :include-macros true]
            [weasel.repl :as ws-repl]
            [cljs.core.async :refer [put! chan <! >! alts! timeout close!] :as async]
            [cljs.reader :refer [read-string] :as read]
            [kioo.om :refer [content set-attr do-> substitute listen]]


(defn send-bookmark
  "Send bookmark via websocket to server"
  [app owner]
  (let [url (om/get-state owner :url-input-text)
        {[in _] :ws user :user} (-> app deref)]
    (if (clojure.string/blank? url)
      (println "INFO: no input")
      (if user
        (do
          (go (>! in {:topic :add-bookmark :data {:email user :url url}}))
          (om/set-state! owner :url-input-text ""))
        (println "not registered yet!")))))


(defn send-registry
  "Send mail for sign up"
  [app owner]
  (let [email (om/get-state owner :signup-text)
        [in _] (-> app deref :ws)]
    (if (clojure.string/blank? email)
      (println "INFO: no mail")
      (do
        (go (>! in {:topic :sign-up :data {:email email}}))
        (om/transact! app :user (fn [old] email))
        (om/set-state! owner :signup-text "")))))


;; --- NAVBAR ---

(deftemplate nav "templates/navbar.html"
  [app owner state]
  {[:#brand] (content "Lesezeichen")
   [:#sign-up-input] (do-> (set-attr :value (:signup-text state))
                           (listen :on-change #(handle-text-change % owner :signup-text)
                                   :on-key-down #(if (= (.-keyCode %) 10)
                                                   (send-registry app owner)
                                                   (when (= (.-which %) 13)
                                                     (when (.-ctrlKey %)
                                                       (send-registry app owner))))))
   [:#modal-signup-btn] (listen :on-click (fn [e] (send-registry app owner)))})


;; --- MAIN VIEW ---
