Refactoring
This commit is contained in:
parent
30d2d8a3b1
commit
608ef993d3
5 changed files with 78 additions and 76 deletions
|
@ -5,7 +5,7 @@
|
|||
[clj-webdriver.core :as webdriver]
|
||||
[clojure.test :as test]))
|
||||
|
||||
;; Hard-code path to chromedriver
|
||||
;; Hard-coded path to chromedriver
|
||||
(defn set-chromedriver-path! []
|
||||
(System/setProperty "webdriver.chrome.driver" "bin/chromedriver"))
|
||||
|
||||
|
|
|
@ -2,12 +2,13 @@
|
|||
(:require [om.core :as om :include-macros true]
|
||||
[sablono.core :as html :refer-macros [html]]
|
||||
[cljs.core.async :as a :refer [<! put! chan]]
|
||||
[grub.view.dom :as dom]
|
||||
[grub.view.grub :as grub]
|
||||
[grub.view.recipe :as recipe])
|
||||
(:require-macros [grub.macros :refer [log logs]]
|
||||
[cljs.core.async.macros :refer [go go-loop]]))
|
||||
|
||||
(defn app-view [state owner]
|
||||
(defn app-view [props owner]
|
||||
(reify
|
||||
om/IRender
|
||||
(render [this]
|
||||
|
@ -15,30 +16,27 @@
|
|||
[:div.container
|
||||
[:div.row
|
||||
[:div.col-sm-6.leftmost-column
|
||||
(om/build grub/grubs-view (:grubs state))]
|
||||
(om/build grub/grubs-view (:grubs props))]
|
||||
[:div.col-sm-6
|
||||
(om/build recipe/recipes-view (:recipes state))]]]))
|
||||
(om/build recipe/recipes-view (:recipes props))]]]))
|
||||
om/IWillMount
|
||||
(will-mount [_]
|
||||
(let [body-elem (aget (.getElementsByTagName js/document "body") 0)]
|
||||
(log "body:" body-elem)
|
||||
(.addEventListener body-elem "mousedown"
|
||||
#(put! (om/get-shared owner :events-in) {:type :body-mousedown
|
||||
:event %}))))))
|
||||
(let [>events (om/get-shared owner :>events)]
|
||||
(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])
|
||||
events-in (chan)
|
||||
events (a/pub events-in :type)]
|
||||
>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
|
||||
:events events
|
||||
:events-in events-in}})
|
||||
:>events >events
|
||||
:<events <events}})
|
||||
out))
|
||||
|
|
12
src/cljs/grub/view/dom.cljs
Normal file
12
src/cljs/grub/view/dom.cljs
Normal file
|
@ -0,0 +1,12 @@
|
|||
(ns grub.view.dom)
|
||||
|
||||
(defn enter-pressed? [event]
|
||||
(let [enter-keycode 13]
|
||||
(= (.-which event) enter-keycode)))
|
||||
|
||||
(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)))
|
|
@ -1,51 +1,49 @@
|
|||
(ns grub.view.grub
|
||||
(: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]]))
|
||||
|
||||
(defn enter-pressed? [event]
|
||||
(let [enter-keycode 13]
|
||||
(= (.-which event) enter-keycode)))
|
||||
|
||||
(defn complete-event [{:keys [id completed]}]
|
||||
{:event :update-grub
|
||||
:id id
|
||||
:completed (not completed)})
|
||||
|
||||
(defn click-on-self? [{:keys [event]} node]
|
||||
(or (= (.-target event) node) (.contains node (.-target event))))
|
||||
(defn add-event [grub]
|
||||
{:event :add-grub
|
||||
:id (str "grub-" (.now js/Date))
|
||||
:grub grub
|
||||
:completed false})
|
||||
|
||||
(defn edit-event [id grub]
|
||||
{:event :update-grub
|
||||
:id id
|
||||
:grub grub})
|
||||
|
||||
(defn grub-view [{:keys [id grub completed] :as grub-state} owner]
|
||||
(defn complete-event [{:keys [id completed]}]
|
||||
{:event :update-grub
|
||||
:id id
|
||||
:completed (not completed)})
|
||||
|
||||
(defn grub-view [{:keys [id grub completed] :as props} owner]
|
||||
(reify
|
||||
om/IInitState
|
||||
(init-state [_]
|
||||
(let [publisher (chan)]
|
||||
{:edit-state :waiting
|
||||
:events-in publisher
|
||||
:events (a/pub publisher identity)
|
||||
:>local-events publisher
|
||||
:<local-events (a/pub publisher identity)
|
||||
:grub grub}))
|
||||
|
||||
om/IRenderState
|
||||
(render-state [_ {:keys [edit-state events-in events pressed] :as state}]
|
||||
(logs id edit-state)
|
||||
(render-state [_ {:keys [edit-state >local-events] :as state}]
|
||||
(html
|
||||
[:li.list-group-item.grub-item
|
||||
{:id id
|
||||
:class [(when completed "completed")
|
||||
{:class [(when completed "completed")
|
||||
(when (= edit-state :pressed) "grub-active")
|
||||
(when (= edit-state :editing) "edit")]
|
||||
:on-mouse-down #(put! events-in :mouse-down)
|
||||
:on-mouse-up #(put! events-in :mouse-up)
|
||||
:on-mouse-leave #(put! events-in :mouse-leave)
|
||||
:on-mouse-down #(put! >local-events :mouse-down)
|
||||
: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 @grub-state)))}
|
||||
(put! (om/get-shared owner :update) (complete-event @props)))}
|
||||
[:span.grub-static
|
||||
(if completed
|
||||
[:span.glyphicon.glyphicon-check]
|
||||
|
@ -55,37 +53,40 @@
|
|||
{:type "text"
|
||||
:value (:grub state)
|
||||
:on-change #(om/set-state! owner :grub (.. % -target -value))
|
||||
:on-key-up #(when (enter-pressed? %) (put! events-in :enter))}]]))
|
||||
:on-key-up #(when (dom/enter-pressed? %) (put! >local-events :enter))}]]))
|
||||
|
||||
om/IWillMount
|
||||
(will-mount [_]
|
||||
(let [local-events (om/get-state owner :events)
|
||||
events (om/get-shared owner :events)
|
||||
(let [<local-events (om/get-state owner :<local-events)
|
||||
<events (om/get-shared owner :<events)
|
||||
subscriber (chan)]
|
||||
(go-loop []
|
||||
(om/set-state! owner :edit-state :waiting)
|
||||
(a/sub local-events :mouse-down subscriber)
|
||||
(<! subscriber)
|
||||
(a/unsub local-events :mouse-down subscriber)
|
||||
(let [subscriber (chan)]
|
||||
(a/sub <local-events :mouse-down subscriber)
|
||||
(<! subscriber)
|
||||
(a/unsub <local-events :mouse-down subscriber)
|
||||
(a/close! subscriber))
|
||||
(om/set-state! owner :edit-state :pressed)
|
||||
(a/sub local-events :mouse-leave subscriber)
|
||||
(a/sub local-events :mouse-up subscriber)
|
||||
(a/sub <local-events :mouse-leave subscriber)
|
||||
(a/sub <local-events :mouse-up subscriber)
|
||||
(let [timeout (a/timeout 500)
|
||||
[event c] (a/alts! [timeout subscriber])]
|
||||
(a/unsub local-events :mouse-leave subscriber)
|
||||
(a/unsub local-events :mouse-up subscriber)
|
||||
(a/unsub <local-events :mouse-leave subscriber)
|
||||
(a/unsub <local-events :mouse-up subscriber)
|
||||
(if (= c timeout)
|
||||
(do (om/set-state! owner :edit-state :editing)
|
||||
(a/sub events :body-mousedown subscriber)
|
||||
(a/sub local-events :enter subscriber)
|
||||
(a/sub <events :body-mousedown subscriber)
|
||||
(a/sub <local-events :enter subscriber)
|
||||
(loop []
|
||||
(let [event (<! subscriber)]
|
||||
(when (and (= (:type event) :body-mousedown)
|
||||
(click-on-self? event (om/get-node owner)))
|
||||
(dom/click-on-self? (:event event) (om/get-node owner)))
|
||||
(recur))))
|
||||
(a/unsub events :body-mousedown subscriber)
|
||||
(a/unsub local-events :enter subscriber)
|
||||
(put! (om/get-shared owner :update) (edit-event id (om/get-state owner :grub))))
|
||||
(a/unsub <events :body-mousedown subscriber)
|
||||
(a/unsub <local-events :enter subscriber)
|
||||
(put! (om/get-shared owner :update)
|
||||
(edit-event id (om/get-state owner :grub))))
|
||||
(om/set-state! owner :edit-state :waiting)))
|
||||
(recur))))))
|
||||
|
||||
|
@ -98,26 +99,15 @@
|
|||
(defn sort-grubs [grubs]
|
||||
(sort-by (juxt :completed get-grub-ingredient :grub) (vals grubs)))
|
||||
|
||||
(defn add-grub-event [grub]
|
||||
{:event :add-grub
|
||||
:id (str "grub-" (.now js/Date))
|
||||
:grub grub
|
||||
:completed false})
|
||||
|
||||
(defn add-grub [add {:keys [new-grub]} owner]
|
||||
(defn add-grub [add new-grub owner]
|
||||
(when (not (empty? new-grub))
|
||||
(let [new-grub-event (add-grub-event new-grub)]
|
||||
(om/set-state! owner :new-grub "")
|
||||
(put! add (add-grub-event new-grub)))))
|
||||
(om/set-state! owner :new-grub "")
|
||||
(put! add (add-event new-grub))))
|
||||
|
||||
(defn add-grub-on-enter [event add state owner]
|
||||
(when (enter-pressed? event)
|
||||
(add-grub add state owner)))
|
||||
|
||||
(defn handle-new-grub-change [event owner]
|
||||
(defn update-new-grub-value [event owner]
|
||||
(om/set-state! owner :new-grub (.. event -target -value)))
|
||||
|
||||
(defn grubs-view [grubs owner]
|
||||
(defn grubs-view [props owner]
|
||||
(reify
|
||||
om/IInitState
|
||||
(init-state [_]
|
||||
|
@ -134,19 +124,20 @@
|
|||
{:type "text"
|
||||
:placeholder "2 grubs"
|
||||
:value new-grub
|
||||
:on-key-up #(add-grub-on-enter % add state owner)
|
||||
:on-change #(handle-new-grub-change % owner)}]]
|
||||
:on-key-up #(when (dom/enter-pressed? %)
|
||||
(add-grub add new-grub owner))
|
||||
:on-change #(update-new-grub-value % owner)}]]
|
||||
[:button.btn.btn-primary
|
||||
{:id "add-grub-btn"
|
||||
:type "button"
|
||||
:on-click #(add-grub (om/get-shared owner :add) new-grub owner)}
|
||||
:on-click #(add-grub add new-grub owner)}
|
||||
"Add"]]
|
||||
[:ul#grub-list.list-group
|
||||
(for [grub (sort-grubs grubs)]
|
||||
(om/build grub-view grub))]
|
||||
(for [grub (sort-grubs props)]
|
||||
(om/build grub-view grub {:key :id}))]
|
||||
[:button.btn.pull-right
|
||||
{:id "clear-all-btn"
|
||||
:class (when (empty? grubs) "hidden")
|
||||
:class (when (empty? props) "hidden")
|
||||
:type "button"
|
||||
:on-click #(put! (om/get-shared owner :clear-all) {:event :clear-all-grubs})}
|
||||
"Clear all"]])))))
|
||||
|
|
|
@ -12,7 +12,8 @@
|
|||
(let [{:keys [id name grubs]} recipe]
|
||||
(html
|
||||
[:div.panel.panel-default.recipe-panel
|
||||
{:id id}
|
||||
{:id id
|
||||
:key id}
|
||||
[:div.panel-heading.recipe-header
|
||||
[:input.form-control.recipe-header-input
|
||||
{:id "recipe-name"
|
||||
|
@ -54,4 +55,4 @@
|
|||
{:type "button"} "Done"]]]
|
||||
[:ul#recipe-list.list-group.recipe-list
|
||||
(for [recipe (vals recipes)]
|
||||
(om/build recipe-view recipe))]]))))
|
||||
(om/build recipe-view recipe {:key :id}))]]))))
|
||||
|
|
Loading…
Reference in a new issue