Author: ivaynberg
Date: Mon Dec 7 17:15:06 2009
New Revision: 888017
URL: http://svn.apache.org/viewvc?rev=888017&view=rev
Log:
WICKET-2595 Ajax multipart fails for inner forms added via ajax
Issue: WICKET-2595
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js?rev=888017&r1=888016&r2=888017&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js Mon
Dec 7 17:15:06 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/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java?rev=888017&r1=888016&r2=888017&view=diff
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
(original)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
Mon Dec 7 17:15:06 2009
@@ -34,6 +34,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.form.upload.FileUploadField;
import org.apache.wicket.markup.html.form.validation.IFormValidator;
@@ -131,7 +133,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
@@ -1154,8 +1156,8 @@
{
RequestCycle rc = RequestCycle.get();
IRequestCycleProcessor processor = rc.getProcessor();
- final ObsoleteRequestParameters requestParameters =
processor.getRequestCodingStrategy().decode(
- new FormDispatchRequest(rc.getRequest(),
Url.parse(url)));
+ final ObsoleteRequestParameters requestParameters =
processor.getRequestCodingStrategy()
+ .decode(new FormDispatchRequest(rc.getRequest(),
Url.parse(url)));
IRequestTarget rt = processor.resolve(rc, requestParameters);
if (rt instanceof IListenerInterfaceRequestTarget)
{
@@ -2069,4 +2071,30 @@
{
return component.findParent(Form.class);
}
+
+ /** {...@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());
+ }
+
}