Author: ivaynberg
Date: Thu Aug  6 02:24:14 2009
New Revision: 801499

URL: http://svn.apache.org/viewvc?rev=801499&view=rev
Log:
WICKET-2420: first pass on multipart ajax support, so far only firefox seems to 
work ok
Issue: WICKET-2420

Modified:
    
wicket/sandbox/ivaynberg/wicket-ajax-multipart/src/main/java/org/apache/wicket/ajax/wicket-ajax.js

Modified: 
wicket/sandbox/ivaynberg/wicket-ajax-multipart/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
URL: 
http://svn.apache.org/viewvc/wicket/sandbox/ivaynberg/wicket-ajax-multipart/src/main/java/org/apache/wicket/ajax/wicket-ajax.js?rev=801499&r1=801498&r2=801499&view=diff
==============================================================================
--- 
wicket/sandbox/ivaynberg/wicket-ajax-multipart/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
 (original)
+++ 
wicket/sandbox/ivaynberg/wicket-ajax-multipart/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
 Thu Aug  6 02:24:14 2009
@@ -1046,9 +1046,20 @@
                return this.request.post(body);
        },
 
+       // Submits a form using ajax
+       submitFormById: function(formId, submitButton) {
+               var form = Wicket.$(formId);
+               if (form == null || typeof (form) == "undefined")
+                       Wicket.Log.error("Wicket.Ajax.Call.submitFormById: 
Trying to submit form with id '"+formId+"' that is not in document.");
+               return this.submitForm(form, submitButton);
+       },
+       
        // Submits a form using ajax.
        // This method serializes a form and sends it as POST body.
        submitForm: function(form, submitButton) {
+           if (this.handleMultipart(form)) {
+               return true;
+           }
            var body = function() {
                var s = Wicket.Form.serialize(form);
                if (submitButton != null) {
@@ -1058,13 +1069,59 @@
            }
            return this.request.post(body);
        },
-       
-       // Submits a form using ajax
-       submitFormById: function(formId, submitButton) {
-               var form = Wicket.$(formId);
-               if (form == null || typeof (form) == "undefined")
-                       Wicket.Log.error("Wicket.Ajax.Call.submitFormById: 
Trying to submit form with id '"+formId+"' that is not in document.");
-               return this.submitForm(form, submitButton);
+
+       // 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) {
+               
+               if (form.enctype!="multipart/form-data") {
+                       // not handled, return false
+                       return false;
+               }
+                       
+               var originalFormAction=form.action;
+               var originalFormTarget=form.target;
+               
+               var iframe=document.createElement("iframe");
+               
+               //iframe.style.width="600px";
+               //iframe.style.height="300px";
+               iframe.style.display="none";
+               iframe.style.visibility="hidden";
+               
+               iframe.name="wicket-submit-"+Math.random(); // TODO make sure 
this is unique
+               iframe.id=iframe.name;
+               iframe.onload=this.handleMultipartComplete.bind(this);
+               
+               document.body.appendChild(iframe);
+               
+               // reconfigure the form
+               form.target=iframe.name;
+               form.action=this.request.url;
+
+               //submit the form into the iframe, response will be handled by 
the onload callback
+               form.submit();
+
+               // handled, restore state and return true
+               form.action=originalFormAction;
+               form.target=originalFormTarget;
+               
+               return true;
+       },
+ 
+       // Completes the multipart ajax handling started via handleMultipart()
+       handleMultipartComplete: function (event) {
+               var iframe=event.target;
+
+               // process the response
+               this.loadedCallback(iframe.contentWindow.document);
+
+               // stop the event
+               if (event.stopPropagation) { event.stopPropagation(); } else 
{event.cancelBubble=true;}
+
+               // remove the iframe
+               document.body.removeChild(iframe);
        },
        
        // Processes the response


Reply via email to