Author: hlship
Date: Sat Mar  6 02:59:09 2010
New Revision: 919693

URL: http://svn.apache.org/viewvc?rev=919693&view=rev
Log:
TAP5-1046: Change Tapestry client-side JavaScript to make the tapx/Confirm 
component easier to implement

Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/LinkSubmit.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Submit.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/TriggerFragment.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/linksubmit.js
    
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/tapestry.js
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/CancelDemo.tml
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SubmitTest.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app2/pages/TestPageForSubmit.tml

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java?rev=919693&r1=919692&r2=919693&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java
 Sat Mar  6 02:59:09 2010
@@ -121,6 +121,14 @@
     public static final String FORM_DATA = "t:formdata";
 
     /**
+     * Used by {...@link Submit}, etc., to identify which particular 
client-side element (by element id)
+     * was responsible for the submission. An empty hidden field is created, 
as needed, to store this value.
+     * 
+     * @since 5.2.0
+     */
+    public static final String SUBMITTING_ELEMENT_ID = "t:submit";
+
+    /**
      * The context for the link (optional parameter). This list of values will
      * be converted into strings and included in
      * the URI. The strings will be coerced back to whatever their values are

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/LinkSubmit.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/LinkSubmit.java?rev=919693&r1=919692&r2=919693&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/LinkSubmit.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/LinkSubmit.java
 Sat Mar  6 02:59:09 2010
@@ -104,9 +104,7 @@
     {
         this.clientId = clientId;
 
-        String hiddenFieldName = this.clientId + "-hidden";
-
-        if (request.getParameter(hiddenFieldName) != null)
+        if (clientId.equals(request.getParameter(Form.SUBMITTING_ELEMENT_ID)))
         {
             Runnable notification = new Runnable()
             {

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Submit.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Submit.java?rev=919693&r1=919692&r2=919693&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Submit.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Submit.java
 Sat Mar  6 02:59:09 2010
@@ -98,27 +98,26 @@
 
     @Inject
     private JavascriptSupport javascriptSupport;
-    
+
     @SuppressWarnings("unchecked")
     @Environmental
     private TrackableComponentEventCallback eventCallback;
 
-    private Element element;
-
     private String clientId;
 
     private static class ProcessSubmission implements ComponentAction<Submit>
     {
-        private final String elementName;
+        private final String clientId, elementName;
 
-        public ProcessSubmission(String elementName)
+        public ProcessSubmission(String clientId, String elementName)
         {
+            this.clientId = clientId;
             this.elementName = elementName;
         }
 
         public void execute(Submit component)
         {
-            component.processSubmission(elementName);
+            component.processSubmission(clientId, elementName);
         }
     }
 
@@ -133,7 +132,7 @@
 
     void beginRender(MarkupWriter writer)
     {
-        clientId = null;
+        clientId = javascriptSupport.allocateClientId(resources);
 
         String name = formSupport.allocateControlName(resources.getId());
 
@@ -141,7 +140,13 @@
 
         String type = image == null ? "submit" : "image";
 
-        element = writer.element("input", "type", type, "name", name);
+        writer.element("input",
+
+        "type", type,
+
+        "name", name,
+
+        "id", clientId);
 
         if (disabled)
             writer.attributes("disabled", "disabled");
@@ -149,7 +154,7 @@
         if (image != null)
             writer.attributes("src", image.toClientURL());
 
-        formSupport.store(this, new ProcessSubmission(name));
+        formSupport.store(this, new ProcessSubmission(clientId, name));
 
         resources.renderInformalParameters(writer);
     }
@@ -162,14 +167,9 @@
             javascriptSupport.addInitializerCall("cancelButton", 
getClientId());
     }
 
-    void processSubmission(String elementName)
+    void processSubmission(String clientId, String elementName)
     {
-        if (disabled)
-            return;
-
-        String value = request.getParameter(image == null ? elementName : 
elementName + ".x");
-
-        if (value == null)
+        if (disabled || !selected(clientId, elementName))
             return;
 
         Runnable sendNotification = new Runnable()
@@ -191,21 +191,31 @@
             heartbeat.defer(sendNotification);
     }
 
+    private boolean selected(String clientId, String elementName)
+    {
+        // Case #1: via JavaScript, the client id is passed up.
+
+        if (clientId.equals(request.getParameter(Form.SUBMITTING_ELEMENT_ID)))
+            return true;
+
+        // Case #2: No JavaScript, look for normal semantic (non-null value 
for the element's name).
+        // If configured as an image submit, look for a value for the x 
position. Ah, the ugliness
+        // of HTML.
+
+        String name = image == null ? elementName : elementName + ".x";
+
+        String value = request.getParameter(name);
+
+        return value != null;
+    }
+
     /**
-     * Returns the component's client id. This must be called after the 
component has rendered. The id is allocated
-     * lazily (first time this method is invoked).
+     * Returns the component's client id. This must be called after the 
component has rendered.
      * 
      * @return client id for the component
      */
     public String getClientId()
     {
-        if (clientId == null)
-        {
-            clientId = javascriptSupport.allocateClientId(resources);
-
-            element.forceAttributes("id", clientId);
-        }
-
         return clientId;
     }
 }

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/TriggerFragment.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/TriggerFragment.java?rev=919693&r1=919692&r2=919693&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/TriggerFragment.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/TriggerFragment.java
 Sat Mar  6 02:59:09 2010
@@ -1,10 +1,10 @@
-// Copyright 2008 The Apache Software Foundation
+// Copyright 2008, 2010 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
 // You may obtain a copy of the License at
 //
-//     http://www.apache.org/licenses/LICENSE-2.0
+// http://www.apache.org/licenses/LICENSE-2.0
 //
 // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,17 +17,18 @@
 import org.apache.tapestry5.BindingConstants;
 import org.apache.tapestry5.ClientElement;
 import org.apache.tapestry5.Field;
-import org.apache.tapestry5.RenderSupport;
 import org.apache.tapestry5.annotations.Environmental;
 import org.apache.tapestry5.annotations.InjectContainer;
 import org.apache.tapestry5.annotations.Parameter;
-import org.apache.tapestry5.json.JSONArray;
+import org.apache.tapestry5.json.JSONObject;
 import org.apache.tapestry5.services.Heartbeat;
+import org.apache.tapestry5.services.javascript.JavascriptSupport;
 
 /**
- * A mixin that can be applied to a {...@link 
org.apache.tapestry5.corelib.components.Checkbox} or {...@link
- * org.apache.tapestry5.corelib.components.Radio} component that will link the 
input field and a {...@link
- * org.apache.tapestry5.corelib.components.FormFragment}, making the field 
control the client-side visibility of the
+ * A mixin that can be applied to a {...@link 
org.apache.tapestry5.corelib.components.Checkbox} or
+ * {...@link org.apache.tapestry5.corelib.components.Radio} component that 
will link the input field and a
+ * {...@link org.apache.tapestry5.corelib.components.FormFragment}, making the 
field control the client-side visibility of
+ * the
  * FormFragment.
  */
 public class TriggerFragment
@@ -42,7 +43,7 @@
     private ClientElement fragment;
 
     @Environmental
-    private RenderSupport renderSupport;
+    private JavascriptSupport javascriptSupport;
 
     @Environmental
     private Heartbeat heartbeat;
@@ -53,11 +54,10 @@
         {
             public void run()
             {
-                JSONArray spec = new JSONArray();
-                spec.put(container.getClientId());
-                spec.put(fragment.getClientId());
+                JSONObject spec = new JSONObject("triggerId", 
container.getClientId(), "fragmentId", fragment
+                        .getClientId());
 
-                renderSupport.addInit("linkTriggerToFormFragment", spec);
+                
javascriptSupport.addInitializerCall("linkTriggerToFormFragment", spec);
             }
         };
 

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/linksubmit.js
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/linksubmit.js?rev=919693&r1=919692&r2=919693&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/linksubmit.js
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/corelib/components/linksubmit.js
 Sat Mar  6 02:59:09 2010
@@ -12,52 +12,17 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-Tapestry.LinkSubmit = Class
-               .create( {
+Tapestry.Initializer.linkSubmit = function(spec) {
 
-                       initialize : function(spec) {
-                               this.form = $(spec.form);
-                               this.element = $(spec.clientId);
-                               this.validate = spec.validate;
-
-                               this.element.observe("click", this.onClick
-                                               .bindAsEventListener(this));
-                       },
-
-                       createHidden : function() {
-                               var hidden = new Element("input", {
-                                       "type" : "hidden",
-                                       "id" : this.element.id + "-hidden",
-                                       "name" : this.element.id + "-hidden",
-                                       "value" : this.element.id
-                               });
-
-                               if (this.form.select("input#" + this.element.id 
+ "-hidden").length == 0)
-                                       this.element.insert( {
-                                               after : hidden
-                                       });
-                       },
-
-                       onClick : function(event) {
-                               // Tapestry.debug("LinkSubmit #{id} clicked.", 
this.element);
-
-                       Event.stop(event);
-
-                       var onsubmit = this.form.onsubmit;
-
-                       this.createHidden();
-
-                       if (!this.validate)
-                               $T(this.form).skipValidation = true;
-
-                       if (onsubmit == undefined || 
onsubmit.call(window.document, event)) {
-                               this.form.submit();
-                       }
-
-                       return false;
-               }
-               });
+       $(spec.clientId).observeAction("click", function(event) {
 
-Tapestry.Initializer.linkSubmit = function(spec) {
-       new Tapestry.LinkSubmit(spec);
+               var form = $(spec.form);
+
+               if (!spec.validate)
+                       form.skipValidation();
+
+               form.setSubmittingElement(this);
+
+               form.performSubmit(event);
+       });
 }
\ No newline at end of file


Reply via email to