Permalink
Browse files

escape closes quote mode

  • Loading branch information...
1 parent 62e1929 commit 0adc2536ce2b6a1891583115689a0ff664169fa3 Philip Kamenarsky committed May 3, 2012
Showing with 54 additions and 37 deletions.
  1. +29 −21 src/cljs/tellme/core.cljs
  2. +25 −16 src/cljs/tellme/quote.cljs
View
@@ -88,27 +88,32 @@
help (view :div.quote-help nil {:html quote-help-text})
help-container (view :div.quote-help-container [help button-container])
- qt (dm/add-class! (qt/create-quote text (fn [qt]
- (events/unlistenByKey @event-key)
-
- (swap! self fsm/send-message :go-to-dispatch)
- (swap! self fsm/send-message {:site :local
- :slide false
- :quotes (qt/get-quotes qt)})
-
- (dm/set-style! main-container :visibility "visible")
- (dm/set-style! table :bottom (* client-height 0.3) "px")
- (ui/resized table)
-
- (ui/animate [table :scroll-bottom 0 :duration 0]
- [main-container :style.opacity 1
- :onend #(do
- (dm/detach! qt)
- (dm/detach! help-container)
- (ui/select input)
- (ui/animate [table :style.bottom [(+ 31 bottom-padding) :px]]))]
- [quote-overlay :style.opacity 0
- :onend #(dm/set-style! quote-overlay :visibility "hidden")])))
+ qt (dm/add-class!
+ (qt/create-quote text (fn [qt]
+ (events/unlistenByKey @event-key)
+ (swap! self fsm/send-message :go-to-dispatch)
+
+ (when (qt/get-quotes qt)
+ ; if we actually got some quotes, send message to fsm &
+ ; set up chat table to match up with quotes (for cool slide-down
+ ; animation)
+ (swap! self fsm/send-message {:site :local
+ :slide false
+ :quotes (qt/get-quotes qt)})
+
+ (dm/set-style! table :bottom (* client-height 0.3) "px")
+ (ui/resized table))
+
+ (dm/set-style! main-container :visibility "visible")
+ (ui/animate [table :scroll-bottom 0 :duration 0]
+ [main-container :style.opacity 1
+ :onend #(do
+ (dm/detach! qt)
+ (dm/detach! help-container)
+ (ui/select input)
+ (ui/animate [table :style.bottom [(+ 28 bottom-padding) :px]]))]
+ [quote-overlay :style.opacity 0
+ :onend #(dm/set-style! quote-overlay :visibility "hidden")])))
"quote-table")
bottom (+ height (- (table/row-top table row) (table/scroll-top table)))
@@ -241,6 +246,9 @@
(let [code (.-keyCode event)]
(stop-defer @input-defer)
(if (or (and (>= code 48) (<= code 57)) (= code keycodes/BACKSPACE))
+ ; throttling should be done with defdep/defreaction in a FRP style,
+ ; but since those macros are pretty limited anyway (glitches), we do
+ ; it the old fashioned way here
(reset! input-defer
(defer-later 500
(when-not (js/isNaN (js/parseInt (dm/value number2)))
View
@@ -66,13 +66,19 @@
(ui/animate [input :style.height [(+ 20 sheight) :px]]
[table row (+ 20 sheight)]))))
-(defn- add-finished-listener [input callback quotable]
+(declare gen-quotes)
+
+(defn- add-finished-listener [input callback {:keys [table] :as quotable}]
(events/listen (events/KeyHandler. (dm/single-node input))
"key"
(fn [event]
(when (= (.-keyCode event) keycodes/ENTER)
- (callback quotable)
- (.preventDefault event)))))
+ (.preventDefault event)
+ (callback (assoc quotable :quotes (gen-quotes table))))
+ (when (= (.-keyCode event) keycodes/ESC)
+ (.preventDefault event)
+ (when (js/confirm "Your comment will be lost, is this OK?")
+ (callback quotable))))))
(defn- add-selection-listener [selection-timer dcontent f]
(dme/listen! dcontent
@@ -92,13 +98,19 @@
(def quote-ms 150)
(def quote-selection-timeout 800)
+(defn- gen-quotes [table]
+ ; partition quotes and retorts into an array of [quote retort] pairs
+ (map (fn [[q a]] [(dm/text q) (dm/value a)])
+ (partition 2 (map (partial table/row-contents table)
+ (range (table/row-count table))))))
+
(defprotocol IQuote
(add-quotable [this row content])
(slice-quotable [this row dcontent content])
(get-quotes [this]))
(defrecord Quote
- [callback table shadow retort-input selection-timer]
+ [callback table shadow retort-input selection-timer quotes]
dm/DomContent
(single-node [this] (dm/single-node table))
@@ -134,7 +146,8 @@
rest-row (table/add-row table (inc input-row))
drest (view :div.quote-text)
- input (view :textarea.retort-input)]
+ input (view :textarea.retort-input nil {:style.height [0 :px]
+ :style.padding [0 :px]})]
(dm/remove-class! dcontent "quote-text-inactive")
(dm/add-class! dcontent "quote-text-unselectable")
@@ -146,9 +159,6 @@
; FIXME: 31
(table/set-row-contents table input-row input)
- (dm/set-styles! input {:height (px 0)
- :padding (px 0)})
-
(ui/animate [input :style.height [38 :px] :duration quote-ms]
[input :style.paddingTop [10 :px] :duration quote-ms]
[input :style.paddingBottom [10 :px] :duration quote-ms]
@@ -207,12 +217,8 @@
(add-selection-listener selection-timer dcontent
(partial slice-quotable this row dcontent content))
dcontent))
-
- (get-quotes [this]
- ; partition quotes and retorts into an array of [quote retort] pairs
- (map (fn [[q a]] [(dm/text q) (dm/value a)])
- (partition 2 (map (partial table/row-contents table)
- (range (table/row-count table)))))))
+
+ (get-quotes [this] quotes))
; Constructor --------------------------------------------------------------
@@ -225,7 +231,7 @@
div))
retort-input (view :textarea.retort-input nil {:style.height [38 :px]})
- this (Quote. callback table shadow retort-input (atom nil))]
+ this (Quote. callback table shadow retort-input (atom nil) nil)]
; add initial quotable
(add-quotable this (table/add-row table) content)
@@ -249,7 +255,10 @@
; Tests --------------------------------------------------------------------
(defn test-quote []
- (let [qt (dm/add-class! (create-quote "There's one major problem. This doesn't fit into the monadic interface. Monads are (a -> m b), they're based around functions only. There's no way to attach static information. You have only one choice, throw in some input, and see if it passes or fails.") "chat-table")]
+ (let [qt (dm/add-class! (create-quote "There's one major problem. This doesn't fit into the monadic interface. Monads are (a -> m b), they're based around functions only. There's no way to attach static information. You have only one choice, throw in some input, and see if it passes or fails."
+ #(dm/log-debug (pr-str (if %
+ (get-quotes %)
+ "No quotes.")))) "chat-table")]
(dm/set-style! qt :bottom 200 "px")
(dm/append! (dmc/sel "body") qt)

0 comments on commit 0adc253

Please sign in to comment.