Set new recipe to edit mode

This commit is contained in:
Nicholas Kariniemi 2014-07-27 10:20:00 +03:00
parent 608ef993d3
commit a22a32d996
3 changed files with 55 additions and 18 deletions

View file

@ -25,18 +25,20 @@
(dom/on-body-mousedown #(put! >events {:type :body-mousedown :event %}))))))
(defn render-app [state]
(let [add (chan)
update (chan)
clear-all (chan)
out (a/merge [add update clear-all])
(let [grub-add (chan)
grub-update (chan)
grub-clear-all (chan)
recipe-add (chan)
out (a/merge [grub-add grub-update grub-clear-all recipe-add])
>events (chan)
<events (a/pub >events :type)]
(om/root app-view
state
{:target (.getElementById js/document "container")
:shared {:add add
:update update
:clear-all clear-all
:shared {:grub-add grub-add
:grub-update grub-update
:grub-clear-all grub-clear-all
:recipe-add recipe-add
:>events >events
:<events <events}})
out))

View file

@ -43,7 +43,7 @@
:on-mouse-up #(put! >local-events :mouse-up)
:on-mouse-leave #(put! >local-events :mouse-leave)
:on-click #(when (#{:waiting :pressed} edit-state)
(put! (om/get-shared owner :update) (complete-event @props)))}
(put! (om/get-shared owner :grub-update) (complete-event @props)))}
[:span.grub-static
(if completed
[:span.glyphicon.glyphicon-check]
@ -85,7 +85,7 @@
(recur))))
(a/unsub <events :body-mousedown subscriber)
(a/unsub <local-events :enter subscriber)
(put! (om/get-shared owner :update)
(put! (om/get-shared owner :grub-update)
(edit-event id (om/get-state owner :grub))))
(om/set-state! owner :edit-state :waiting)))
(recur))))))
@ -114,7 +114,7 @@
{:new-grub ""})
om/IRenderState
(render-state [this {:keys [new-grub] :as state}]
(let [add (om/get-shared owner :add)]
(let [add (om/get-shared owner :grub-add)]
(html
[:div
[:h3 "Grub List"]
@ -139,5 +139,6 @@
{:id "clear-all-btn"
:class (when (empty? props) "hidden")
:type "button"
:on-click #(put! (om/get-shared owner :clear-all) {:event :clear-all-grubs})}
:on-click #(put! (om/get-shared owner :grub-clear-all)
{:event :clear-all-grubs})}
"Clear all"]])))))

View file

@ -1,7 +1,8 @@
(ns grub.view.recipe
(:require [om.core :as om :include-macros true]
[sablono.core :as html :refer-macros [html]]
[cljs.core.async :as a :refer [<! put! chan]])
[cljs.core.async :as a :refer [<! put! chan]]
[grub.view.dom :as dom])
(:require-macros [grub.macros :refer [log logs]]
[cljs.core.async.macros :refer [go go-loop]]))
@ -34,19 +35,27 @@
(defn recipes-view [recipes owner]
(reify
om/IRender
(render [this]
om/IInitState
(init-state [_]
(let [publisher (chan)]
{:editing false
:>local-events publisher
:<local-events (a/pub publisher identity)}))
om/IRenderState
(render-state [this {:keys [editing >local-events]}]
(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
{:id "recipe-name"
:type "text"
:placeholder "Grub pie"}]
[:button.btn.btn-primary.btn-sm.recipe-add-grubs-btn {:type "button"} "Add Grubs"]]
[:div.panel-body.recipe-grubs.hidden
:placeholder "Grub pie"}]]
[:div.panel-body.recipe-grubs
{:class (when (not editing) "hidden")}
[:textarea.form-control.recipe-grubs-input
{:id "recipe-grubs"
:rows 3
@ -55,4 +64,29 @@
{:type "button"} "Done"]]]
[:ul#recipe-list.list-group.recipe-list
(for [recipe (vals recipes)]
(om/build recipe-view recipe {:key :id}))]]))))
(om/build recipe-view recipe {:key :id}))]]))
om/IWillMount
(will-mount [_]
(let [<local-events (om/get-state owner :<local-events)
<events (om/get-shared owner :<events)]
(go-loop []
(log "waiting")
(let [subscriber (chan)]
(a/sub <local-events :click subscriber)
(<! subscriber)
(a/unsub <local-events :click subscriber)
(a/close! subscriber))
(om/set-state! owner :editing true)
(log "editing")
(let [subscriber (chan)]
(a/sub <events :body-mousedown subscriber)
(loop []
(let [event (<! subscriber)]
(when (and (= (:type event) :body-mousedown)
(dom/click-on-self? (:event event) (om/get-node owner)))
(recur))))
(a/unsub <events :body-mousedown subscriber)
(a/close! subscriber))
(om/set-state! owner :editing false)
(recur))))))