diff --git a/src/cljs/grub/view/app.cljs b/src/cljs/grub/view/app.cljs index db4a246..9e7f453 100644 --- a/src/cljs/grub/view/app.cljs +++ b/src/cljs/grub/view/app.cljs @@ -1,9 +1,9 @@ (ns grub.view.app (:require [om.core :as om :include-macros true] - [sablono.core :as html :refer-macros [html]] + [sablono.core :refer-macros [html]] [cljs.core.async :as a :refer [events (om/get-shared owner :>events)] diff --git a/src/cljs/grub/view/grub.cljs b/src/cljs/grub/view/grub.cljs index 9db3ff3..55636a0 100644 --- a/src/cljs/grub/view/grub.cljs +++ b/src/cljs/grub/view/grub.cljs @@ -29,7 +29,34 @@ :id id :completed (not completed)}) -(defn grub-view [{:keys [id grub completed] :as props} owner] +(def transitions + {:waiting {:mouse-down :pressed + :touch-start :pressed} + :pressed {:mouse-up :waiting + :mouse-leave :waiting + :mouse-out :waiting + :touch-cancel :waiting + :touch-end :waiting + :timeout :editing} + :editing {:enter :waiting + :body-mousedown :waiting}}) + +(defn transition-state [owner event] + (let [current (om/get-state owner :edit-state) + next (or (get-in transitions [current event]) current)] + (condp = [current next] + [:waiting :pressed] (let [timeout-fn #(transition-state owner :timeout) + timeout-id (js/setTimeout timeout-fn 500)] + (om/set-state! owner :timeout-id timeout-id)) + [:pressed :waiting] (js/clearTimeout (om/get-state owner :timeout-id)) + [:editing :waiting] (let [update-ch (om/get-shared owner :grub-update) + id (:id @(om/get-props owner)) + edit-event (edit-event id (om/get-state owner :grub))] + (put! update-ch edit-event)) + nil) + (om/set-state! owner :edit-state next))) + +(defn view [{:keys [id grub completed] :as props} owner] (reify om/IInitState (init-state [_] @@ -46,11 +73,15 @@ {:class [(when completed "completed") (when (= edit-state :pressed) "grub-active") (when (= edit-state :editing) "edit")] - :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 :grub-update) (complete-event @props)))} + (put! (om/get-shared owner :grub-update) (complete-event @props))) + :on-mouse-down #(transition-state owner :mouse-down) + :on-mouse-up #(transition-state owner :mouse-up) + :on-mouse-leave #(transition-state owner :mouse-leave) + :on-touch-start #(transition-state owner :touch-start) + :on-touch-move #(transition-state owner :touch-move) + :on-touch-cancel #(transition-state owner :touch-cancel) + :on-touch-end #(transition-state owner :touch-end)} [:span.grub-static (if completed [:span.glyphicon.glyphicon-check] @@ -60,89 +91,14 @@ {:type "text" :value (:grub state) :on-change #(om/set-state! owner :grub (.. % -target -value)) - :on-key-up #(when (dom/enter-pressed? %) (put! >local-events :enter))}]])) + :on-key-up #(when (dom/enter-pressed? %) (transition-state owner :enter))}]])) om/IWillMount - (will-mount [_] - (let [