From d754955694ce689743cf694339b8673ff3313a60 Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Sun, 25 Aug 2013 13:53:47 +0300 Subject: [PATCH] Clear all button --- public/css/styles.css | 9 +++++++++ spec/clj/grub/db_spec.clj | 12 +++++++++++- spec/cljs/state_spec.cljs | 8 ++++++++ src/clj/grub/db.clj | 9 ++++++--- src/cljs/grub/state.cljs | 3 +++ src/cljs/grub/view.cljs | 25 +++++++++++++++++++++++-- 6 files changed, 60 insertions(+), 6 deletions(-) diff --git a/public/css/styles.css b/public/css/styles.css index 0906faa..81472c5 100644 --- a/public/css/styles.css +++ b/public/css/styles.css @@ -2,6 +2,10 @@ h3 { margin-top: 10px; } +.hidden { + display: none; +} + .grub-close { display: none; } @@ -12,6 +16,7 @@ tr:hover .grub-close { #grub-list { margin-top: 10px; + margin-bottom: 10px; } .grub-item { @@ -31,3 +36,7 @@ tr:hover .grub-close { text-decoration: line-through; color: #a9a9a9; } + +#clear-all-btn { + float: right; +} diff --git a/spec/clj/grub/db_spec.clj b/spec/clj/grub/db_spec.clj index 40458af..2737a65 100644 --- a/spec/clj/grub/db_spec.clj +++ b/spec/clj/grub/db_spec.clj @@ -59,4 +59,14 @@ (short-delay) (should= nil - (mc/find-one-as-map db/grub-collection {:_id (:_id test-grub)})))))) + (mc/find-one-as-map db/grub-collection {:_id (:_id test-grub)}))))) + + (describe "Clear all" + (it "should delete all grubs" + (let [test-grub {:_id 123456 :completed true}] + (mc/insert db/grub-collection test-grub) + (>!! @db/incoming-events {:event :clear-all}) + (short-delay) + (should + (empty? + (mc/find-maps db/grub-collection))))))) diff --git a/spec/cljs/state_spec.cljs b/spec/cljs/state_spec.cljs index d0ea1f7..cdb0a69 100644 --- a/spec/cljs/state_spec.cljs +++ b/spec/cljs/state_spec.cljs @@ -48,6 +48,14 @@ delete-event {:_id (:_id test-grub) :event :delete}] (reset! state/grubs [test-grub]) (state/handle-event delete-event) + (should= [] @state/grubs)))) + + (describe "Clear all" + (it "should delete all grubs" + (let [test-grub {:_id 234243 :grub "testgrub" :completed true} + clear-all-event {:event :clear-all}] + (reset! state/grubs [test-grub]) + (state/handle-event clear-all-event) (should= [] @state/grubs))))) (describe diff --git a/src/clj/grub/db.clj b/src/clj/grub/db.clj index 5af7efa..93cae87 100644 --- a/src/clj/grub/db.clj +++ b/src/clj/grub/db.clj @@ -7,6 +7,9 @@ (def grub-collection "grubs") +(defn clear-grubs [] + (mc/drop grub-collection)) + (def incoming-events (atom nil)) (defmulti handle-event :event :default :unknown-event) @@ -29,6 +32,9 @@ (defmethod handle-event :delete [event] (mc/remove grub-collection {:_id (:_id event)})) +(defmethod handle-event :clear-all [event] + (clear-grubs)) + (defmethod handle-event :unknown-event [event] (println "Cannot handle unknown event:" event)) @@ -48,9 +54,6 @@ (>! out grub-event)))) out)) -(defn clear-grubs [] - (mc/drop grub-collection)) - (def default-db "grub") (defn connect-and-handle-events diff --git a/src/cljs/grub/state.cljs b/src/cljs/grub/state.cljs index 4c3dbc3..e412ef0 100644 --- a/src/cljs/grub/state.cljs +++ b/src/cljs/grub/state.cljs @@ -43,6 +43,9 @@ (fn [current] (vec (remove #(= (:_id %) (:_id event)) current))))) +(defmethod handle-event :clear-all [event] + (reset! grubs [])) + (defmethod handle-event :unknown-event [event] (logs "Cannot handle unknown event:" event)) diff --git a/src/cljs/grub/view.cljs b/src/cljs/grub/view.cljs index 4bd76ff..44100c8 100644 --- a/src/cljs/grub/view.cljs +++ b/src/cljs/grub/view.cljs @@ -26,7 +26,8 @@ add-grub-text [:span.input-group-btn add-grub-btn]] - [:ul#grub-list.list-group]] + [:ul#grub-list.list-group] + [:button.btn.hidden {:id "clear-all-btn" :type "button"} "Clear all"]] [:div.col-lg-4] [:div.col-lg-2]]]) @@ -42,6 +43,17 @@ (defn render-body [] (dommy/prepend! (sel1 :body) (main-template))) +(defprotocol IHideable + (-hide! [view]) + (-show! [view])) + +(extend-type js/HTMLElement + IHideable + (-hide! [view] + (dommy/add-class! view :hidden)) + (-show! [view] + (dommy/remove-class! view :hidden))) + (defn get-add-grub-text [] (let [text (dommy/value add-grub-text)] (dommy/set-value! add-grub-text "") @@ -86,6 +98,11 @@ grub-events (map-chan (fn [id] {:event :delete :_id id}) ids)] grub-events))) +(defn get-clear-all-events [] + (let [events (chan)] + (dommy/listen! (sel1 :#clear-all-btn) :click #(go (>! events {:event :clear-all}))) + events)) + (defn render-grub-list [grubs] (let [grub-list (sel1 :#grub-list) @@ -98,12 +115,16 @@ (defn push-outgoing-events [] (fan-in outgoing-events [(get-added-events) - (get-deleted-events)])) + (get-deleted-events) + (get-clear-all-events)])) (defn watch-for-state-changes [] (add-watch state/grubs :grub-add-watch (fn [key ref old new] + (if (empty? new) + (-hide! (sel1 :#clear-all-btn)) + (-show! (sel1 :#clear-all-btn))) (render-grub-list new)))) (defn init []