Permalink
Browse files

extended TreeControl to intercept ENTER

  • Loading branch information...
netguy204 committed Jul 8, 2012
1 parent 5a980a5 commit b93e512fa5d74e26c48b81f58c77e12b42251d0c
Showing with 56 additions and 13 deletions.
  1. +1 −1 src-cljs/move/models.cljs
  2. +2 −0 src-cljs/move/utils.cljs
  3. +34 −10 src-cljs/move/views.cljs
  4. +19 −2 src/move/macros.clj
@@ -94,7 +94,7 @@
update-in [:lists list]
insert datum pos)
(doseq [m (list-markers state list)]
- (when (>= (position m) pos)
+ (when (> (position m) pos)
(update-marker m inc)))))
(events/fire [:add-item list] datum)
View
@@ -6,3 +6,5 @@
(defn index-of [coll value]
(first (indices-of coll value)))
+
+
View
@@ -3,10 +3,15 @@
[goog.ui.Button :as Button]
[goog.ui.Dialog :as Dialog]
[goog.ui.LabelInput :as LabelInput]
+ [goog.ui.tree.BaseNode :as BaseNode]
[goog.dom :as dom]
[goog.events :as ge]
+ [goog.Timer :as Timer]
[move.events :as events]
- [move.utils :as utils]))
+ [move.utils :as utils])
+
+ (:use-macros [move.macros :only [goog-extend]]
+ [cljs.core :only [this-as]]))
(defn content-view []
(dom/getElement "content"))
@@ -19,10 +24,6 @@
(set-state [view view-state])
(with-dialog-input [view message default callback]))
-(defn- make-list-view [name]
- (let [config goog/ui.tree.TreeControl.defaultConfig]
- (goog/ui.tree.TreeControl. name config)))
-
(defn- get-children [view]
(or (.getChildren view) nil))
@@ -50,6 +51,27 @@
(let [entry (nth (get-children view) idx)]
(.setSelectedItem view entry)))
+(goog-extend
+ MyTree goog/ui.tree.TreeControl
+ ([name config]
+ (this-as this
+ (goog/base this name config)))
+
+ (handleKeyEvent
+ [e]
+ (this-as this
+ (goog/base this "handleKeyEvent" e)
+ (let [view (.-hiddenView this)]
+ (when (#{(.-ENTER goog/events.KeyCodes)
+ (.-MAC_ENTER goog/events.KeyCodes)} (.-keyCode e))
+ (events/fire [:edit view] (get-selected-index this))
+ (.preventDefault e))))))
+
+(defn- make-list-view [name]
+ (let [config goog/ui.tree.TreeControl.defaultConfig
+ tree (MyTree. name config)]
+ tree))
+
;; view state
;;
;; {:selected 3
@@ -82,7 +104,7 @@
(let [dialog (make-input-dialog message default)]
(with-value dialog callback)))
-(defrecord WebTodoView [list state]
+(defrecord WebTodoView [list]
ViewOperations
(set-state [view new-state]
(clear-list-view (:list view))
@@ -92,14 +114,18 @@
(set-selected-index (:list view) (:selected new-state))))
(with-dialog-input [view message default callback]
- (with-web-dialog-input message default callback)))
+ (with-web-dialog-input message default
+ (fn [& results]
+ (apply callback results)
+ (.. (:list view) (getElement) (focus))))))
(defn make-web-view [el]
(let [list (make-list-view "[]")
create-button (goog/ui.Button. "Create")
clear-button (goog/ui.Button. "Clear")
view (WebTodoView. list)]
+ (set! (.-hiddenView list) view)
(.render list el)
(.render create-button el)
(.render clear-button el)
@@ -108,7 +134,7 @@
(ge/listen clear-button "action" #(events/fire [:clear view]))
(ge/listen (.getElement list) (.-DBLCLICK goog/events.EventType)
#(events/fire [:edit view] (get-selected-index list)))
- (ge/listen (.getElement list) (.-CLICK goog/events.EventType)
+ (ge/listen list (.-CHANGE goog/events.EventType)
#(events/fire [:select view] (get-selected-index list)))
view))
@@ -119,5 +145,3 @@
(with-dialog-input [view message default callback]
(callback test-input))))
-
-
View
@@ -1,4 +1,6 @@
-(ns move.macros)
+(ns move.macros
+ (:require [cljs.compiler :as compiler]
+ [cljs.core :as cljs]))
(defmacro report-result [success statement]
`(if ~success
@@ -103,4 +105,19 @@ asynchronous calls.
(doasync
[~@bindings
result# (do ~@body)
- junk# (nextfn# result#)]))))
+ junk# (nextfn# result#)]))))
+
+(defn- to-property [sym]
+ (symbol (str "-" sym)))
+
+(defmacro goog-extend [type base-type ctor & methods]
+ `(do
+ (defn ~type ~@ctor)
+
+ (goog/inherits ~type ~base-type)
+
+ ~@(map
+ (fn [method]
+ `(set! (.. ~type -prototype ~(to-property (first method)))
+ (fn ~@(rest method))))
+ methods)))

0 comments on commit b93e512

Please sign in to comment.