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/23 01:21:20 UTC

[2/6] git commit: Fix problems with prior commits Add core/form:gatherParameters to assemble a object of parameter keys and values Add a document event handler to handle Ajax form submissions

Fix problems with prior commits
Add core/form:gatherParameters to assemble a object of parameter keys and values
Add a document event handler to handle Ajax form submissions


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

Branch: refs/heads/5.4-js-rewrite
Commit: 18fb8a082730a8d3d9c7e111ab456d937249b62b
Parents: 0fce406
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Mon Oct 22 16:17:10 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Mon Oct 22 16:17:10 2012 -0700

----------------------------------------------------------------------
 .../META-INF/modules/core/forms.coffee             |   74 ++++++++++++---
 .../coffeescript/META-INF/modules/core/zone.coffee |   19 +++-
 .../apache/tapestry5/corelib/components/Form.java  |   11 +-
 .../resources/org/apache/tapestry5/tapestry.js     |    3 +-
 4 files changed, 84 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/18fb8a08/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 020e18b..57a8fbd 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
@@ -16,19 +16,21 @@
 #
 # Defines handlers for HTML forms and HTML field elements, specifically to control input validation.
 
-define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
-  (events, spi, builder) ->
+define ["core/events", "core/spi", "core/builder", "_"],
+  (events, spi, builder, _) ->
 
-    SKIP_VALIDATION = "data-skip-validation"
+    # Meta-data name that indicates the next submission should skip validation (typically, because
+    # the form was submitted by a "cancel" button).
+    SKIP_VALIDATION = "t5:skip-validation"
 
     isPreventSubmission = (element) ->
-      (element.hasClass Tapestry.PREVENT_SUBMISSION) or
-      (element.geattribute "data-prevent-submission")
+      element.attribute "data-prevent-submission"
 
     clearSubmittingHidden = (form) ->
       hidden = form.findFirst "[name='t:submit']"
 
-      hidden.setValue null if hidden
+      # Clear if found
+      hidden and hidden.value null
 
       return
 
@@ -43,14 +45,55 @@ define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
       # TODO: Research why we need id and name and get rid of one if possible.
       value = Object.toJSON [ wrapper.element.id, wrapper.element.name ]
 
-      hidden.setValue value
+      hidden.value value
+
+      return
+
+    # Passed the element wrapper for a form element, returns a map of all the values
+    # for all non-disabled fields (including hidden fields, select, textarea). This is primarily
+    # used when assembling an Ajax request for a form submission.
+    gatherParameters = (form) ->
+      result = {}
+
+      fields = form.find "input, select, textarea"
+
+      _.each fields, (field) ->
+          return if field.attribute "disabled"
+
+          type = field.element.type
+
+          # Ignore types file and submit; file doesn't make sense for Ajax, and submit
+          # is handled by keeping a hidden field active with the data Tapestry needs
+          # on the server.
+          return if type is "file" || type is "submit"
+
+          value = field.value()
+
+          return if value is null
+
+          name = field.element.name
+
+          existing = result[name]
+
+          if _.isArray existing
+            existing.push value
+            return
+
+          if existing
+            result[name] = [existing, value]
+            return
+
+          result[name] = value
+
+      return result
+
 
     defaultValidateAndSubmit = ->
 
       if ((this.attribute "data-validate") is "submit") and
-         (not this.attribute SKIP_VALIDATION)
+         (not this.meta SKIP_VALIDATION)
 
-        this.attribute SKIP_VALIDATION
+        this.meta SKIP_VALIDATION, null
 
         memo = error: false
 
@@ -68,12 +111,15 @@ define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
 
       # Allow certain types of elements to do last-moment set up. Basically, this is for
       # FormFragment, or similar, to make their hidden field enabled or disabled to match
-      # their UI's visible/hidden status. This is assumed to work.
+      # their UI's visible/hidden status. This is assumed to work or throw an exception; there
+      # is no memo.
       this.trigger events.form.prepareForSubmit, this
 
       # Sometimes we want to submit the form normally, for a full-page render.
       # Othertimes we want to stop here and let the `events.form.processSubmit`
       # handler take it from here.
+      # TODO: Prevent Submission may not be necessary, as we can simply handle the submit
+      # at a higher level. This may be Tapestry 5.3 thinking.
       if isPreventSubmission this
         this.trigger events.form.processSubmit, this
         return false
@@ -92,7 +138,11 @@ define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
       setSubmittingHidden (spi this.element.form), this
 
     exports =
+      gatherParameters: gatherParameters
+
       setSubmittingElement: setSubmittingHidden
 
-      skipValidation: (formWrapper) ->
-        formWrapper.attribute SKIP_VALIDATION, true
\ No newline at end of file
+      # Sets a flag on the form to indicate that client-side validation should be bypassed.
+      # This is typically associated with submit buttons that "cancel" the form.
+      skipValidation: (form) ->
+        form.meta SKIP_VALIDATION, true
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/18fb8a08/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 d9527d1..0bbc68f 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
@@ -19,9 +19,9 @@
 # Typically, a client-side zone element is rendered by, and corresponds to, a server-side
 # core/Zone component; however, certain other components (such as core/ProgressiveDisplay) may
 # also be treated as zones.
-define ["core/spi", "core/events", "core/ajax", "core/console", "_"],
+define ["core/spi", "core/events", "core/ajax", "core/console", "core/forms",  "_"],
 
-  (spi, events, ajax, console, _) ->
+  (spi, events, ajax, console, forms, _) ->
 
     findZone = (element) ->
       zoneId = element.attribute "data-update-zone"
@@ -50,6 +50,19 @@ define ["core/spi", "core/events", "core/ajax", "core/console", "_"],
 
       return false
 
+    spi.onDocument "submit", "form[data-update-zone]", ->
+
+      zone = findZone this
+
+      if zone
+        formParameters = forms.gatherParameters this
+
+        zone.trigger events.zone.refresh,
+          url: (this.attribute "action")
+          parameters: formParameters
+
+      return false
+
     spi.onDocument events.zone.update, (event) ->
 
       this.trigger events.zone.willUpdate
@@ -95,5 +108,5 @@ define ["core/spi", "core/events", "core/ajax", "core/console", "_"],
 
         zone.trigger events.zone.refresh, { url }
 
-
+    # Most of this module is document event handlers, but there's also one export.
     return { deferredZoneUpdate }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/18fb8a08/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java
index 4be3dcf..1810f91 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java
@@ -324,11 +324,6 @@ public class Form implements ClientElement, FormValidationControl
 
         formSupport = createRenderTimeFormSupport(clientId, actionSink, allocator);
 
-        if (zone != null)
-        {
-            linkFormToZone(link);
-        }
-
         environment.push(FormSupport.class, formSupport);
         environment.push(ValidationTracker.class, tracker);
 
@@ -353,7 +348,11 @@ public class Form implements ClientElement, FormValidationControl
         // Save the form element for later, in case we want to write an encoding
         // type attribute.
 
-        form = writer.element("form", "id", clientId, "method", "post", "action", actionURL);
+        form = writer.element("form",
+                "id", clientId,
+                "method", "post",
+                "action", actionURL,
+                "data-update-zone", zone);
 
         if (clientValidation != ClientValidation.NONE)
         {

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/18fb8a08/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js b/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
index 8aa80f2..ac8beb8 100644
--- a/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
+++ b/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
@@ -105,8 +105,7 @@ define("core/compat/tapestry", [
         /**
          * CSS Class added to a &lt;form&gt; element that directs Tapestry to
          * prevent normal (HTTP POST) form submission, in favor of Ajax
-         * (XmlHttpRequest) submission.   This is still supported in Tapestry 5.4, but
-         * replaced with the data-prevent-submission attribute. It will be removed in Tapestry 5.5.
+         * (XmlHttpRequest) submission.    It will be removed in Tapestry 5.5.
          *
          * @deprecated Use data-prevent-submission="true" instead
          */