You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2017/01/31 21:53:09 UTC

[2/2] wicket git commit: WICKET-6286 Add setting to control how to download the resource - via iframe (default) or via document.location

WICKET-6286 Add setting to control how to download the resource - via iframe (default) or via document.location


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/4db836dc
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/4db836dc
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/4db836dc

Branch: refs/heads/WICKET-6286-ajax-download
Commit: 4db836dcea70d641a6d9c62980f8ed90ce04cd3b
Parents: 39dc2b2
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Tue Jan 31 22:52:09 2017 +0100
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Tue Jan 31 22:52:09 2017 +0100

----------------------------------------------------------------------
 .../wicket/extensions/ajax/AjaxDownload.java    | 29 ++++++++++++++
 .../extensions/ajax/wicket-ajaxdownload.js      | 40 +++++++++++++-------
 2 files changed, 55 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/4db836dc/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxDownload.java
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxDownload.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxDownload.java
index 11c97d4..0b941de 100644
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxDownload.java
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxDownload.java
@@ -40,6 +40,8 @@ import org.apache.wicket.request.resource.ResourceReference;
 import org.apache.wicket.resource.JQueryPluginResourceReference;
 import org.apache.wicket.util.lang.Args;
 
+import java.util.Locale;
+
 /**
  * Download resources via Ajax.
  * <p>
@@ -65,6 +67,21 @@ public class AjaxDownload extends AbstractDefaultAjaxBehavior
 {
 	private static final long serialVersionUID = 1L;
 
+	public enum Location {
+		/**
+		 * The resource will be downloaded via a temporary created iframe.
+		 * This is recommended when there are resources in the DOM which will be
+		 * closed automatically on JavaScript <em>unload</em> event, like WebSockets.
+		 */
+		IFrame,
+
+		/**
+		 * The resource will be downloaded by changing the location of the current DOM document.
+		 * Note: This will trigger JavaScript <em>unload</em> event on the page!
+		 */
+		Self
+	}
+
 	/**
 	 * Name of parameter used to transfer the download identifier to the resource.
 	 *
@@ -81,6 +98,8 @@ public class AjaxDownload extends AbstractDefaultAjaxBehavior
 
 	private PageParameters resourceParameters;
 
+	private Location location = Location.IFrame;
+
 	/**
 	 * Download of a {@link Resource}.
 	 * 
@@ -194,6 +213,7 @@ public class AjaxDownload extends AbstractDefaultAjaxBehavior
 		settings.put("attributes", new JsonFunction(renderAjaxAttributes(getComponent())));
 		settings.put("name", getName());
 		settings.put("downloadUrl", url);
+		settings.put("method", location.name().toLowerCase(Locale.ENGLISH));
 
 		target.appendJavaScript(String.format("Wicket.AjaxDownload.initiate(%s);", settings));
 
@@ -231,6 +251,15 @@ public class AjaxDownload extends AbstractDefaultAjaxBehavior
 		}
 	}
 
+	public final Location getLocation() {
+		return location;
+	}
+
+	public AjaxDownload setLocation(final Location location) {
+		this.location = Args.notNull(location, "location");
+		return this;
+	}
+
 	/**
 	 * Identifying name of this behavior.
 	 */

http://git-wip-us.apache.org/repos/asf/wicket/blob/4db836dc/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxdownload.js
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxdownload.js b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxdownload.js
index a383fa8..b821883 100644
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxdownload.js
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxdownload.js
@@ -27,15 +27,13 @@
 
 	Wicket.AjaxDownload = {
 		initiate : function(settings) {
-			
-			var frame = jQuery("<iframe>").hide().prop("src", settings.downloadUrl).appendTo("body");
-			
-			var checkComplete = function() {
+
+			var checkComplete = function (frame) {
 				var result;
-				
+
 				if (document.cookie.indexOf(settings.name + '=') > -1) {
 					result = "success";
-				} else {
+				} else if (frame) {
 					var html = frame.contents().find('body').html();
 					if (html && html.length) {
 						result = "failed";
@@ -43,18 +41,32 @@
 				}
 
 				if (result) {
-					setTimeout(function() { frame.remove(); }, 0);
-					
+					if (frame) {
+						setTimeout(function () {
+							frame.remove();
+						}, 0);
+					}
+
 					settings.attributes.ep = settings.attributes.ep || {};
-					settings.attributes.ep['result'] = result;
+					settings.attributes.ep.result = result;
 					Wicket.Ajax.ajax(settings.attributes);
 				} else {
-					setTimeout(checkComplete, 100);
+					setTimeout(function() {
+						checkComplete(frame);
+					}, 100);
 				}
-			}; 
-			
-			checkComplete();
+			};
+
+			if (settings.method === 'self') {
+				setTimeout(function() {
+					window.location.href=settings.downloadUrl;
+					checkComplete();
+				}, 100);
+			} else {
+				var frame = jQuery("<iframe>").hide().prop("src", settings.downloadUrl).appendTo("body");
+				checkComplete(frame);
+			}
 		}
 	}; 
 	
-})();
\ No newline at end of file
+})();