Also exit editing new recipe on click on other recipes

This commit is contained in:
Nicholas Kariniemi 2014-07-28 20:51:00 +03:00
parent 3a919a5c4c
commit c208d9a625
3 changed files with 37 additions and 30 deletions

View file

@ -22,7 +22,7 @@
om/IWillMount
(will-mount [_]
(let [>events (om/get-shared owner :>events)]
(dom/on-body-mousedown #(put! >events {:type :body-mousedown :event %}))))))
(dom/on-document-mousedown #(put! >events {:type :body-mousedown :event %}))))))
(defn render-app [state]
(let [grub-add (chan)

View file

@ -7,9 +7,8 @@
(defn click-on-self? [event node]
(.contains node (.-target event)))
(defn on-body-mousedown [f]
(let [body-elem (aget (.getElementsByTagName js/document "body") 0)]
(.addEventListener body-elem "mousedown" f)))
(defn on-document-mousedown [f]
(.addEventListener js/document "mousedown" f))
(defn event-val [event]
(.. event -target -value))

View file

@ -59,9 +59,10 @@
(not (empty? grubs)))
(om/set-state! owner :new-recipe-name "")
(om/set-state! owner :new-recipe-grubs "")
(om/set-state! owner :editing false)
(put! ch (add-event name grubs))))
(defn recipes-view [recipes owner]
(defn new-recipe-view [_ owner]
(reify
om/IInitState
(init-state [_]
@ -76,31 +77,26 @@
(render-state [this {:keys [editing >local-events new-recipe-name new-recipe-grubs]}]
(let [add (om/get-shared owner :recipe-add)]
(html
[:div
[:h3.recipes-title "Recipes"]
[:div.panel.panel-default.recipe-panel
{:on-click #(put! >local-events :click)}
[:div.panel-heading.recipe-header
[:input.form-control.recipe-header-input
{:type "text"
:placeholder "New recipe"
:value new-recipe-name
:on-change #(om/set-state! owner :new-recipe-name (dom/event-val %))}]]
[:div.panel-body.recipe-grubs
{:class (when (not editing) "hidden")}
[:textarea.form-control.recipe-grubs-input
{:id "recipe-grubs"
:rows 3
:placeholder "Recipe ingredients"
:value new-recipe-grubs
:on-change #(om/set-state! owner :new-recipe-grubs (dom/event-val %))}]
[:button.btn.btn-primary.pull-right.recipe-btn.recipe-done-btn
{:type "button"
:on-click #(add-recipe add new-recipe-name new-recipe-grubs owner)}
"Done"]]]
[:ul#recipe-list.list-group.recipe-list
(for [recipe (vals recipes)]
(om/build recipe-view recipe {:key :id}))]])))
[:div.panel.panel-default.recipe-panel
{:on-click #(put! >local-events :click)}
[:div.panel-heading.recipe-header
[:input.form-control.recipe-header-input
{:type "text"
:placeholder "New recipe"
:value new-recipe-name
:on-change #(om/set-state! owner :new-recipe-name (dom/event-val %))}]]
[:div.panel-body.recipe-grubs
{:class (when (not editing) "hidden")}
[:textarea.form-control.recipe-grubs-input
{:id "recipe-grubs"
:rows 3
:placeholder "Recipe ingredients"
:value new-recipe-grubs
:on-change #(om/set-state! owner :new-recipe-grubs (dom/event-val %))}]
[:button.btn.btn-primary.pull-right.recipe-btn.recipe-done-btn
{:type "button"
:on-click #(add-recipe add new-recipe-name new-recipe-grubs owner)}
"Done"]]])))
om/IWillMount
(will-mount [_]
@ -124,3 +120,15 @@
(a/close! subscriber))
(om/set-state! owner :editing false)
(recur))))))
(defn recipes-view [recipes owner]
(reify
om/IRender
(render [this]
(html
[:div
[:h3.recipes-title "Recipes"]
(om/build new-recipe-view recipes)
[:ul#recipe-list.list-group.recipe-list
(for [recipe (vals recipes)]
(om/build recipe-view recipe {:key :id}))]]))))