You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2012/10/24 18:51:19 UTC

[1/15] git commit: Change SPI to not return an EventHandler from ElementWrapper.on() - may add back if needed in the future - currently, no way to turn off event notifications

Updated Branches:
  refs/heads/5.4-js-rewrite eec74fc1c -> dd4ea5b52


Change SPI to not return an EventHandler from ElementWrapper.on()
- may add back if needed in the future
- currently, no way to turn off event notifications


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/dd4ea5b5
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/dd4ea5b5
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/dd4ea5b5

Branch: refs/heads/5.4-js-rewrite
Commit: dd4ea5b525615d9e9839ed3a160f23daf0db81a0
Parents: 02ed252
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Wed Oct 24 09:51:02 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Wed Oct 24 09:51:02 2012 -0700

----------------------------------------------------------------------
 .../coffeescript/META-INF/modules/core/spi.coffee  |   48 +++++---------
 tapestry-core/src/test/app1/JavaScriptTests.tml    |   15 ++++-
 .../integration/app1/pages/test-spi.coffee         |   51 ++-------------
 3 files changed, 37 insertions(+), 77 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/dd4ea5b5/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
index 47fac14..bc16766 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
@@ -114,10 +114,7 @@ define ["_", "prototype"], (_) ->
     stop: ->
       @nativeEvent.stop()
 
-  # Value returned from `on()` or `onDocument()`; an EventHandler is used to stop listening to
-  # events, or even temporarily pause listening.
-  #
-  # Registers the handler as an event listener for matching elements and event names.
+  # _internal_: Interface between the SPI's event model, and Prototype's.
   #
   # * elements - array of DOM elements (or the document object)
   # * eventNames - array of event names
@@ -128,12 +125,13 @@ define ["_", "prototype"], (_) ->
   # Event handlers may return false to stop event propogation; this prevents an event from bubbling up, and
   # prevents any browser default behavior from triggering.  This is often easier than accepting the `EventWrapper`
   # object as the first parameter and invoking `stop()`.
-  class EventHandler
-    constructor: (elements, eventNames, match, handler) ->
+
+  onevent = (elements, eventNames, match, handler) ->
       throw new Error "No event handler was provided." unless handler?
 
       wrapped = (prototypeEvent) ->
-        # Set `this` to be the matched ElementWrapper, rather than the element on which the event is observed.
+        # Set `this` to be the matched ElementWrapper, rather than the element on which the event is observed
+        # (which is often further up the hierarchy).
         elementWrapper = new ElementWrapper prototypeEvent.findElement()
         eventWrapper = new EventWrapper prototypeEvent
 
@@ -145,24 +143,11 @@ define ["_", "prototype"], (_) ->
 
         return
 
-      # Prototype Event.Handler instances
-      @protoHandlers = []
-
-      _.each elements, (element) =>
-        _.each eventNames, (eventName) =>
-          @protoHandlers.push Event.on element, eventName, match, wrapped
-
-    # Invoked after `stop()` to restart event listening.
-    start: ->
-      _.each @protoHandlers, (h) -> h.start()
-
-      return this
-
-    # Invoked to stop event listening. Listening can be re-instanted with `start()`.
-    stop: ->
-      _.each @protoHandlers, (h) -> h.stop()
+      for element in elements
+        for eventName in eventNames
+          Event.on element, eventName, match, wrapped
 
-      return this
+      return
 
   # Wraps a DOM element, providing some common behaviors.
   # Exposes the original element as property `element`.
@@ -408,10 +393,9 @@ define ["_", "prototype"], (_) ->
     #   will invoke the handler.
     # * handler - function invoked; the function is passed an `EventWrapper` object, and the
     #   context (`this`) is the `ElementWrapper` for the matched element.
-    #
-    # Returns an EventHandler object, making it possible to turn event observation on or off.
     on: (events, match, handler) ->
       exports.on @element, events, match, handler
+      return this
 
   # _internal_: converts a selector to an array of DOM elements
   parseSelectorToElements = (selector) ->
@@ -505,6 +489,8 @@ define ["_", "prototype"], (_) ->
     else
       throw new Error "Attempt to wrap a null DOM element" unless element
 
+    # Assume the object is a DOM element, document or window; something that is compatible with the
+    # Prototype API (especially with respect to events).
     new ElementWrapper element
 
   _.extend exports,
@@ -530,18 +516,17 @@ define ["_", "prototype"], (_) ->
 
       return exports
 
-    # on() is used to add an event handler
+    # Used to add an event handler to an element (possibly from elements below it in the hierarch).
     #
     # * selector - CSS selector used to select elements to attach handler to; alternately,
-    #   a single DOM element, or an array of DOM elements
+    #   a single DOM element, or an array of DOM elements. The document is considered an element
+    #   for these purposes.
     # * events - one or more event names, separated by spaces
     # * match - optional: CSS expression used as a filter; only events that bubble
     # * up to a selected element from an originating element that matches the CSS expression
     #   will invoke the handler.
     # * handler - function invoked; the function is passed an `EventWrapper` object, and the context (`this`)
     #   is the `ElementWrapper` for the matched element
-    #
-    # Returns an EventHandler object, making it possible to turn event notifications on or off.
     on: (selector, events, match, handler) ->
       unless handler?
         handler = match
@@ -549,7 +534,8 @@ define ["_", "prototype"], (_) ->
 
       elements = parseSelectorToElements selector
 
-      new EventHandler(elements, (split events), match, handler)
+      onevent elements, (split events), match, handler
+      return
 
     # onDocument() is used to add an event handler to the document object; this is used
     # for global (or default) handlers.

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/dd4ea5b5/tapestry-core/src/test/app1/JavaScriptTests.tml
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/app1/JavaScriptTests.tml b/tapestry-core/src/test/app1/JavaScriptTests.tml
index 8b25838..602413a 100644
--- a/tapestry-core/src/test/app1/JavaScriptTests.tml
+++ b/tapestry-core/src/test/app1/JavaScriptTests.tml
@@ -11,17 +11,28 @@
 
 <!-- The rest are test elements for the core/spi tests, etc. -->
 
-<hr/>
+<div class="container">
 
-<div id="spi-eventelement">
+<div id="spi-eventelement-native">
     <a href="#" class="btn btn-primary">primary</a>
     <a href="#" class="btn" data-use="secondary">secondary</a>
 </div>
 
+<div id="spi-eventelement-selector">
+    <a href="#" class="btn btn-primary">primary</a>
+    <a href="#" class="btn" data-use="secondary">secondary</a>
+</div>
+
+
+<div id="spi-eventelement-matched">
+    <a href="#" class="btn btn-primary">primary</a>
+</div>
+
 <div id="spi-visibility">
     <span>initially visible</span>
 </div>
 
+</div>
 
 </body>
 </html>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/dd4ea5b5/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee b/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee
index 6b2f2c3..8daa217 100644
--- a/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee
+++ b/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee
@@ -2,7 +2,7 @@ require ["core/spi"], (spi) ->
   module "core/spi"
 
   test "get wrapped element by id", ->
-    e = spi "spi-eventelement"
+    e = spi "spi-eventelement-native"
 
     ok e isnt null, "element found and wrapped"
 
@@ -11,44 +11,13 @@ require ["core/spi"], (spi) ->
 
     ok e is null, "element not found and null"
 
-  test "pause and resume events", ->
-
-    clicks = 0
-    container = spi "spi-eventelement"
-    button = container.findFirst "a"
-
-    # Remember that Prototype will never trigger a native event, just a
-    # custom event, so we create a custom event here.
-    # NOTE: support for native events was added later.
-    eh = container.on "x:click", "a", ->
-      clicks++
-      return false
-
-    button.trigger "x:click"
-
-    equal clicks, 1, "first click registered"
-
-    eh.stop()
-
-    button.trigger "x:click"
-
-    equal clicks, 1, "no notification when EventHandler stopped"
-
-    eh.start()
-
-    button.trigger "x:click"
-
-    equal clicks, 2, "notifications resume after EventHandler started"
-
-    eh.stop()
-
   test "trigger native events", ->
 
     clicks = 0
-    container = spi "spi-eventelement"
+    container = spi "spi-eventelement-native"
     button = container.findFirst "a"
 
-    eh = container.on "click", "a", ->
+    container.on "click", "a", ->
       clicks++
       return false
 
@@ -56,16 +25,14 @@ require ["core/spi"], (spi) ->
 
     equal clicks, 1, "native event was triggered"
 
-    eh.stop()
-
   test "selector used with events filters", ->
 
     clicks = 0
-    container = spi "spi-eventelement"
+    container = spi "spi-eventelement-selector"
     primary = container.findFirst "a.btn-primary"
     secondary = container.findFirst "a[data-use=secondary]"
 
-    eh = container.on "x:click", "a.btn-primary", ->
+    container.on "x:click", "a.btn-primary", ->
       clicks++
       return false
 
@@ -77,14 +44,12 @@ require ["core/spi"], (spi) ->
 
     equal clicks, 1, "click on non-selected element does not invoke handler"
 
-    eh.stop()
-
   test "this is matched element in handler", ->
 
-    container = spi "spi-eventelement"
+    container = spi "spi-eventelement-matched"
     primary = container.findFirst "a.btn-primary"
 
-    eh = container.on "x:click", "a.btn-primary", ->
+    container.on "x:click", "a.btn-primary", ->
 
       strictEqual this.element, primary.element, "this should be the wrapper for element that was matched"
 
@@ -92,8 +57,6 @@ require ["core/spi"], (spi) ->
 
     primary.trigger "x:click"
 
-    eh.stop()
-
   test "visibility, hide(), and show()", ->
 
     e = (spi "spi-visibility").findFirst "span"