    40 "down"
    46 "del"
    32 "space"
+   27 "escape"
    186 ";"})
  
 (defn event-modifiers

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

(defn now [] (new js/Date))
(def custom-formatter (time-format/formatter "dd/MM/yyyy"))
(def app-title (str "Notes " (time-format/unparse custom-formatter (time-core/date-time (.getFullYear (now)) (+ 1 (.getMonth (now))) (.getDate (now))))))
(def app-state (atom {:entered [] :current { :tab 0 :title "" }}))

(defn note-view  [note owner]
  (reify
    om/IRender
    (render [this]
      (indent-element (dom/li nil (:title note)) (:tab note))
      )))

(defn indent-element [content tab]
  (if (== 0 tab) content

    (do (.focus element)
       (.setSelectionRange element length length))))

(defn index-of [coll v]
  (let [i (count (take-while #(not= v %) coll))]
    (when (or (< i (count coll))
            (= v (last coll)))
      i)))

(defn go-up [app note]
  (let [existing-list (:notes @app)
        index (index-of (:notes @app) @note)
        note-above (get existing-list (- index 1))
        final-list (notes-with-editing-row-updated existing-list (:id note-above))]
    (if (> index 0)
      (om/update! app :notes final-list)
    )
  )
)

(defn go-down [app note]
  (let [existing-list (:notes @app)
        index (index-of (:notes @app) @note)
        last-row-index (- (.-length (clj->js existing-list)) 1)]
    (if (= index last-row-index)
      (let [n (new-note (:tab @note))]
        (save-note app n)
      )
      (let [note-above (get existing-list (+ index 1))
            final-list (notes-with-editing-row-updated existing-list (:id note-above))]
        (om/update! app :notes final-list)))
  )
)

(defn handle-event [type app val]
  (case type
    :destroy (destroy-note app val)
    :edit    (edit-note app val)
    :up      (go-up app val)
    :down    (go-down app val)
    :save    (save-note app val)
    :cancel  (destroy-note app val)
    nil))

(defn notes-app [app owner]
  (reify
    om/IWillMount
    (will-mount [_]
      (let [comm (chan)]
        (om/set-state! owner :comm comm)
        (go (while true
              (let [[type value] (<! comm)]
                (handle-event type app value))))))
    om/IRenderState
    (render-state [_ {:keys [comm]}]
      (dom/div nil
        (dom/h1 nil app-title)
        (apply dom/ul nil
          (om/build-all note-view (:notes app)
          {:init-state {:comm comm}
           :key :id }
          ))))))

(om/root
  notes-app
  app-state
  {:target (. js/document (getElementById "app"))})
