You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2009/12/07 09:45:30 UTC

svn commit: r887862 - in /wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket: ajax/wicket-ajax.js markup/html/form/Form.java

Author: ivaynberg
Date: Mon Dec  7 08:45:29 2009
New Revision: 887862

URL: http://svn.apache.org/viewvc?rev=887862&view=rev
Log:
WICKET-2595 Ajax multipart fails for inner forms added via ajax
Issue: WICKET-2595

Modified:
    wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
    wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java

Modified: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js?rev=887862&r1=887861&r2=887862&view=diff
==============================================================================
--- wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js (original)
+++ wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js Mon Dec  7 08:45:29 2009
@@ -1069,30 +1069,47 @@
 	    return this.request.post(body);
 	},
 
+
 	// If the form contains multipart content this function will post 
 	// the form using an iframe instead of the regular ajax call
 	// and bridge the output - transparently making this work  as if it was an ajax call
 	handleMultipart: function (form, submitButton) {
 		
+		var multipart=false;
+		
 		// find root form
 		if (form.tagName.toLowerCase() != "form") {
 			do {
+				// check if any inner forms are multipart
+				if (multipart==false&&Wicket!=undefined&&Wicket.Forms!=undefined) {
+					var meta=Wicket.Forms[form.id];
+					if (meta!=undefined) {
+						if (meta["multipart"]!=undefined) {
+							multipart=multipart||meta["multipart"];
+						}
+					}
+				}
 				form = form.parentNode;
 			} while(form.tagName.toLowerCase() != "form" && form.tagName.toLowerCase() != "body")
 		}	
 
+		
 		if (form.tagName.toLowerCase() != "form") {
 			// no form in the hierarchy, cant handle multipart
 			return false;
 		}
 		
-	 	if (form.enctype!="multipart/form-data") {
-	 		// not handled, return false
-	 		return false;
-	 	}
-			
+		multipart=multipart||form.enctype=="multipart/form-data";
+		
+		if (multipart==false) {
+			// nothing to handle
+			return false;
+		}
+		
 		var originalFormAction=form.action;
 		var originalFormTarget=form.target;
+		var originalFormMethod=form.method;
+		var originalFormEnctype=form.enctype;
 		
 		var iframeName="wicket-submit-"+(""+Math.random()).substr(2);
 		
@@ -1100,13 +1117,11 @@
     		var iframe = document.createElement("<iframe name='"+iframeName+"' id='"+iframeName+"' src='about:blank'/>");
 		} catch (ex) {
 		    var iframe = document.createElement("iframe");
-		    iframe.name=iframeName;
+			iframe.name=iframeName;
 			iframe.id=iframe.name;
 			iframe.src="about:blank";
 		}
 		
-		//iframe.style.width="600px";
-		//iframe.style.height="300px";
 		iframe.style.display="none";
 		iframe.style.visibility="hidden";
 				
@@ -1117,6 +1132,8 @@
 		// reconfigure the form
 		form.target=iframe.name;
 		form.action=this.request.url + "&wicket:ajax=true";
+		form.method="post";
+		form.enctype="multipart/form-data";
 		
 		// create submitting button element
 		if (submitButton!=null) {
@@ -1138,6 +1155,8 @@
 		// handled, restore state and return true
 		form.action=originalFormAction;
 		form.target=originalFormTarget;
+		form.method=originalFormMethod;
+		form.enctype=originalFormEnctype;
 		
  		return true;
  	},
@@ -1145,7 +1164,11 @@
  	// Completes the multipart ajax handling started via handleMultipart()
 	handleMultipartComplete: function (event) {
 		if (event==null) { event=window.event; }
-		if (event.target!=null) { var iframe=event.target; } else { var iframe=event.srcElement};
+		if (event.target!=null) {
+			var iframe=event.target;
+		} else {
+			var iframe=event.srcElement
+		};
 
 		var envelope=iframe.contentWindow.document;
 		if (envelope.XMLDocument!=null) { envelope=envelope.XMLDocument; }
@@ -1163,7 +1186,13 @@
 			iframe.removeEventListener("load", this.handleMultipartComplete, false);
 		
 		// remove the iframe and button elements
-		setTimeout(function() { var e=document.getElementById(iframe.id+"-btn"); if (e!=null) { e.parentNode.removeChild(e); } iframe.parentNode.removeChild(iframe); }, 250);
+		setTimeout(function() {
+			var e=document.getElementById(iframe.id+"-btn");
+			if (e!=null) {
+				e.parentNode.removeChild(e);
+			}
+			iframe.parentNode.removeChild(iframe);
+		}, 250);
 	},
 	
 	// Processes the response

Modified: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java?rev=887862&r1=887861&r2=887862&view=diff
==============================================================================
--- wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java (original)
+++ wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java Mon Dec  7 08:45:29 2009
@@ -36,6 +36,8 @@
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.MarkupStream;
+import org.apache.wicket.markup.html.IHeaderContributor;
+import org.apache.wicket.markup.html.IHeaderResponse;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.border.Border;
 import org.apache.wicket.markup.html.form.persistence.CookieValuePersister;
@@ -138,7 +140,7 @@
  * @param <T>
  *            The model object type
  */
-public class Form<T> extends WebMarkupContainer implements IFormSubmitListener
+public class Form<T> extends WebMarkupContainer implements IFormSubmitListener, IHeaderContributor
 {
 	/**
 	 * Visitor used for validation
@@ -1847,9 +1849,11 @@
 			tag.remove("method");
 			tag.remove("action");
 			tag.remove("enctype");
+			// see renderhead for some non-root javascript markers
 		}
 	}
 
+
 	@Override
 	protected void renderPlaceholderTag(ComponentTag tag, Response response)
 	{
@@ -2332,4 +2336,29 @@
 		return form;
 
 	}
+
+	/** {@inheritDoc} */
+	public void renderHead(IHeaderResponse response)
+	{
+		if (!isRootForm() && isMultiPart())
+		{
+			// register some metadata so we can later properly handle multipart ajax posts for
+			// embedded forms
+			registerJavascriptNamespaces(response);
+			response.renderJavascript("Wicket.Forms[\"" + getMarkupId() + "\"]={multipart:true};",
+				Form.class.getName() + "." + getMarkupId() + ".metadata");
+		}
+	}
+
+	/**
+	 * Produces javascript that registereds Wicket.Forms namespaces
+	 * 
+	 * @param response
+	 */
+	protected void registerJavascriptNamespaces(IHeaderResponse response)
+	{
+		response.renderJavascript(
+			"if (Wicket==undefined) { Wicket={}; } if (Wicket.Forms==undefined) { Wicket.Forms={}; }",
+			Form.class.getName());
+	}
 }