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

[6/9] git commit: Support adding event handlers to the document Change some default event handlers added to the body (after page load) to add to the document (immediately)

Support adding event handlers to the document
Change some default event handlers added to the body (after page load) to add to the document (immediately)


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

Branch: refs/heads/5.4-js-rewrite
Commit: bfde43ddc25e76d62013133d3c0394d153fc9b1c
Parents: c1c7aee
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Wed Oct 17 11:04:23 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Wed Oct 17 11:04:23 2012 -0700

----------------------------------------------------------------------
 .../META-INF/modules/core/form-fragment.coffee     |   44 +++++++--------
 .../META-INF/modules/core/forms.coffee             |   19 +++---
 .../coffeescript/META-INF/modules/core/spi.coffee  |    9 ++-
 .../coffeescript/META-INF/modules/core/zone.coffee |   23 ++++----
 4 files changed, 47 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bfde43dd/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
index 699f3af..493291f 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
@@ -20,37 +20,33 @@ define ["_", "core/spi", "core/events", "core/compat/tapestry"],
 
     SELECTOR = '[data-component-type="core/FormFragment"]'
 
-    # Setup up top-level event handlers for FormFragment-related DOM events.
-    spi.domReady ->
-      body = spi.body()
+    # This is mostly for compatibility with 5.3, which supported
+    # a DOM event to ask a fragment to remove itself.  This makes less sense since
+    # default animations were eliminated in 5.4.
+    spi.onDocument events.formfragment.remove, SELECTOR, (event) ->
+      this.remove()
 
-      # This is mostly for compatibility with 5.3, which supported
-      # a DOM event to ask a fragment to remove itself.  This makes less sense since
-      # default animations were eliminated in 5.4.
-      body.on events.formfragment.remove, SELECTOR, (event) ->
-        this.remove()
+    # When any form fires the prepareForSubmit event, check to see if
+    # any form fragments are contained within, and give them a chance
+    # to enabled/disable their hidden field.
+    spi.onDocument events.form.prepareForSubmit, "form", (event) ->
 
-      # When any form fires the prepareForSubmit event, check to see if
-      # any form fragments are contained within, and give them a chance
-      # to enabled/disable their hidden field.
-      body.on events.form.prepareForSubmit, "form", (event) ->
+      fragments = this.findAll SELECTOR
 
-        fragments = this.findAll SELECTOR
+      _.each fragments, (frag) ->
 
-        _.each fragments, (frag) ->
+        fragmentId = frag.getAttribute "id"
 
-          fragmentId = frag.getAttribute "id"
+        hidden = frag.find "input[type=hidden][data-for-fragment=#{fragmentId}]"
 
-          hidden = frag.find "input[type=hidden][data-for-fragment=#{fragmentId}]"
+        # If found (e.g., not alwaysSubmit), then enable/disable the field.
+        hidden && hidden.setAttribute "disabled", not frag.deepVisible()
 
-          # If found (e.g., not alwaysSubmit), then enable/disable the field.
-          hidden && hidden.setAttribute "disabled", not frag.deepVisible()
-
-      # Again, a DOM event to make the FormFragment visible or invisible; this is useful
-      # because of the didShow/didHide events ... but we're really just seeing the evolution
-      # from the old style (the FormFragment class as controller) to the new style (DOM events and
-      # top-level event handlers).
-      body.on events.formfragment.changeVisibility, SELECTOR, (event) ->
+    # Again, a DOM event to make the FormFragment visible or invisible; this is useful
+    # because of the didShow/didHide events ... but we're really just seeing the evolution
+    # from the old style (the FormFragment class as controller) to the new style (DOM events and
+    # top-level event handlers).
+    spi.onDocument events.formfragment.changeVisibility, SELECTOR, (event) ->
         event.stop()
 
         makeVisible = event.memo.visible

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bfde43dd/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
index d726209..4141b0e 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
@@ -81,16 +81,15 @@ define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
       # Otherwise, the event is good, there are no validation problems, let the normal processing commence.
       return
 
-    spi.domReady ->
-      # TODO: May want to define a data attribute to control whether Tapestry gets
-      # involved at all?
-      spi.body().on "submit", "form", defaultValidateAndSubmit
-
-      # On any click on a submit or image, update the containing form to indicate that the element
-      # was responsible for the eventual submit; this is very important to Ajax updates, otherwise the
-      # information about which control triggered the submit gets lost.
-      spi.body().on "click", "input[type=submit], input[type=image]", ->
-        setSubmittingHidden (spi this.element.form), this
+    # TODO: May want to define a data attribute to control whether Tapestry gets
+    # involved at all?
+    spi.onDocument "submit", "form", defaultValidateAndSubmit
+
+    # On any click on a submit or image, update the containing form to indicate that the element
+    # was responsible for the eventual submit; this is very important to Ajax updates, otherwise the
+    # information about which control triggered the submit gets lost.
+    spi.onDocument "click", "input[type=submit], input[type=image]", ->
+      setSubmittingHidden (spi this.element.form), this
 
     exports =
       setSubmittingElement: (form, element) ->

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bfde43dd/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 b189c3d..c943fcc 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,12 +114,12 @@ define ["_", "prototype"], (_) ->
     stop: ->
       @nativeEvent.stop()
 
-  # Value returned from `on()`; an EventHandler is used to stop listening to
+  # 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.
   #
-  # * elements - array of DOM elements
+  # * elements - array of DOM elements (or the document object)
   # * eventNames - array of event names
   # * match - selector to match bubbled elements, or null
   # * handler - event handler function to invoke; it will be passed an `EventWrapper` instance as the first parameter,
@@ -503,6 +503,11 @@ define ["_", "prototype"], (_) ->
 
       new EventHandler(elements, (split events), match, handler)
 
+    # onDocument() is used to add an event handler to the document object; this is used
+    # for global (or default) handlers.
+    onDocument: (events, match, handler) ->
+      exports.on document, events, match, handler
+
     # Returns a wrapped version of the document.body element. Care must be take to not invoke this function before the
     # body element exists; typically only after the DOM has loaded, such as a `domReady()` callback.
     body: ->

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/bfde43dd/tapestry-core/src/main/coffeescript/META-INF/modules/core/zone.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/zone.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/zone.coffee
index 43f0d22..5be2983 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/zone.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/zone.coffee
@@ -18,24 +18,23 @@
 # document body.
 define ["core/spi", "core/events", "_"],
   (spi, events, _) ->
-    spi.domReady ->
-      spi.body().on events.zone.update, (event) ->
+    spi.onDocument events.zone.update, (event) ->
 
-        this.trigger events.zone.willUpdate
+      this.trigger events.zone.willUpdate
 
-        # TODO: purge existing children?
+      # TODO: purge existing children?
 
-        content = event.memo.content
+      content = event.memo.content
 
-        # The server may have passed down the empty string for the content; that removes the existing content.
-        # On the other hand, the server may have not provided a content key; in that case, content is undefined
-        # which means to leave the existing content alone.
-        unless content is undefined
-          this.update content
+      # The server may have passed down the empty string for the content; that removes the existing content.
+      # On the other hand, the server may have not provided a content key; in that case, content is undefined
+      # which means to leave the existing content alone.
+      unless content is undefined
+        this.update content
 
-        this.show() unless this.visible()
+      this.show() unless this.visible()
 
-        this.trigger events.zone.didUpdate
+      this.trigger events.zone.didUpdate
 
     # No meaningful value is returned.
     return
\ No newline at end of file