You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by sv...@apache.org on 2018/01/26 22:52:53 UTC

[1/6] wicket git commit: WICKET-6517 added some CSS styling

Repository: wicket
Updated Branches:
  refs/heads/master 1d20044b5 -> 29d2775c0


WICKET-6517 added some CSS styling


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

Branch: refs/heads/master
Commit: f1dd096c772feb9f878f783c5764a37403e82e5b
Parents: 3f5c293
Author: Sven Meier <sv...@apache.org>
Authored: Thu Jan 18 22:43:21 2018 +0100
Committer: Sven Meier <sv...@apache.org>
Committed: Fri Jan 26 23:09:37 2018 +0100

----------------------------------------------------------------------
 .../examples/ajax/builtin/FileUploadPage.html   | 17 +++++++++++-
 .../wicket/extensions/Initializer.properties    |  2 ++
 .../ajax/markup/html/AjaxFileDropBehavior.java  | 27 ++++++++++++++------
 3 files changed, 37 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/f1dd096c/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
index 4642ae3..2ed083d 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
@@ -1,5 +1,20 @@
 <?xml version="1.0" encoding="UTF-8" ?>
 <html>
+<head>
+	<wicket:head>
+		<style>
+			.drop-zone {
+				border: 1px dotted #FF9925;
+				width:200px;
+				height:200px;
+			}
+			.drop-zone.drag-over {
+				background: #FF9925;
+				color: #ffffff;
+			}
+		</style>
+	</wicket:head>
+</head>
 <body>
 <wicket:extend xmlns:wicket="http://wicket.apache.org">
 
@@ -15,7 +30,7 @@ Demonstrates Wicket's ability to transparently handle multipart forms via AJAX.<
 	<input type="submit" value="Regular Submit"/> <input wicket:id="ajaxSubmit" type="button" value="Ajax Submit"/>
 </form>
 
-<div wicket:id="drop" style="border: 1px dotted black; width:200px; height:200px;">
+<div wicket:id="drop" class="drop-zone">
 	Drop file here.
 </div>
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/f1dd096c/wicket-extensions/src/main/java/org/apache/wicket/extensions/Initializer.properties
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/Initializer.properties b/wicket-extensions/src/main/java/org/apache/wicket/extensions/Initializer.properties
index 98f7925..6b43d43 100644
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/Initializer.properties
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/Initializer.properties
@@ -45,3 +45,5 @@ AbstractDateTimeField.CSS.time=datetime-time
 TimeField.hoursSeparator=\u00a0:\u00a0
 TimeField.CSS.hours=time-hours
 TimeField.CSS.minutes=time-minutes
+
+AjaxFileDropBehavior.CSS.dragover=drag-over
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/f1dd096c/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
index b8806d9..817c2f4 100644
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
@@ -25,11 +25,12 @@ import org.apache.wicket.Component;
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.ajax.AjaxEventBehavior;
 import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.attributes.AjaxCallListener;
 import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
 import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method;
+import org.apache.wicket.core.util.string.CssUtils;
 import org.apache.wicket.markup.head.IHeaderResponse;
 import org.apache.wicket.markup.head.JavaScriptHeaderItem;
-import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
 import org.apache.wicket.markup.html.form.upload.FileUpload;
 import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest;
 import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
@@ -47,6 +48,8 @@ import org.apache.wicket.util.lang.Bytes;
 public class AjaxFileDropBehavior extends AjaxEventBehavior
 {
 
+	public static final String DRAG_OVER_CLASS_KEY = CssUtils.key(AjaxFileDropBehavior.class, "dragover");
+
 	private static final ResourceReference JS = new PackageResourceReference(
 		AjaxFileDropBehavior.class, "datatransfer.js");
 
@@ -62,9 +65,13 @@ public class AjaxFileDropBehavior extends AjaxEventBehavior
 
 	private String parameterName = "f";
 
+	/**
+	 * Listen for 'dragover' and 'drop' events and prevent them, only 'drop' will initiate
+	 * an Ajax request.
+	 */
 	public AjaxFileDropBehavior()
 	{
-		super("drop");
+		super("dragenter dragover dragleave drop");
 	}
 
 	@Override
@@ -73,12 +80,6 @@ public class AjaxFileDropBehavior extends AjaxEventBehavior
 		super.renderHead(component, response);
 
 		response.render(JavaScriptHeaderItem.forReference(JS));
-
-		// default must be prevented for dragover event, otherwise browser will consume the
-		// dataTransfer
-		response.render(OnDomReadyHeaderItem.forScript(
-			String.format("jQuery('#%s').on('dragover', function(e) { e.preventDefault(); });",
-				component.getMarkupId())));
 	}
 
 	@Override
@@ -91,6 +92,16 @@ public class AjaxFileDropBehavior extends AjaxEventBehavior
 		// default must be prevented, otherwise browser will consume the dataTransfer
 		attributes.setPreventDefault(true);
 
+		attributes.getAjaxCallListeners().add(new AjaxCallListener() {
+			@Override
+			public CharSequence getPrecondition(Component component)
+			{
+				String css = getComponent().getString(DRAG_OVER_CLASS_KEY);
+				
+				return String.format("jQuery('#' + attrs.c).toggleClass('%s', attrs.event.type === 'dragover'); return (attrs.event.type === 'drop');", css);
+			}
+		});
+		
 		attributes.getDynamicExtraParameters()
 			.add(String.format(
 				"return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');",


[4/6] wicket git commit: WICKET-6517 dep could be multipart too

Posted by sv...@apache.org.
WICKET-6517 dep could be multipart too


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

Branch: refs/heads/master
Commit: 44fb374876210fc0545a5c02a83232ebc1500a38
Parents: f642f4a
Author: Sven Meier <sv...@apache.org>
Authored: Wed Jan 17 00:32:29 2018 +0100
Committer: Sven Meier <sv...@apache.org>
Committed: Fri Jan 26 23:09:37 2018 +0100

----------------------------------------------------------------------
 .../wicket/ajax/res/js/wicket-ajax-jquery.js    |  49 ++----
 .../examples/ajax/builtin/FileUploadPage.html   |  10 +-
 .../examples/ajax/builtin/FileUploadPage.java   |  26 ++++
 .../markup/html/AjaxFileUploadBehavior.java     | 156 +++++++++++++++++++
 .../extensions/ajax/markup/html/datatransfer.js |  54 +++++++
 5 files changed, 261 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/44fb3748/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
index 801a47d..b2ad3a0 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
@@ -49,8 +49,7 @@
 		};
 	}
 
-	var createIFrame,
-		getAjaxBaseUrl,
+	var getAjaxBaseUrl,
 		isUndef,
 		replaceAll,
 		htmlToDomDocument,
@@ -66,19 +65,6 @@
 	};
 
 	/**
-	 * Creates an iframe that can be used to load data asynchronously or as a
-	 * target for Ajax form submit.
-	 *
-	 * @param iframeName {String} the value of the iframe's name attribute
-	 */
-	createIFrame = function (iframeName) {
-		// WICKET-6340 properly close tag for XHTML markup
-		var $iframe = jQuery('<iframe name="'+iframeName+'" id="'+iframeName+
-			'" src="about:blank" style="position: absolute; top: -9999px; left: -9999px;"></iframe>');
-		return $iframe[0];
-	};
-
-	/**
 	 * A safe getter for Wicket's Ajax base URL.
 	 * If the value is not defined or is empty string then
 	 * return '.' (current folder) as base URL.
@@ -553,7 +539,7 @@
 				extraParam = this._asParamArray(extraParam);
 				params = params.concat(extraParam);
 			}
-			return jQuery.param(params);
+			return params;
 		},
 
 		/**
@@ -583,6 +569,8 @@
 					'Wicket-Ajax': 'true',
 					'Wicket-Ajax-BaseURL': getAjaxBaseUrl()
 				},
+				
+				url = attrs.u,
 
 				// the request (extra) parameters
 				data = this._asParamArray(attrs.ep),
@@ -658,6 +646,17 @@
 				var el = Wicket.$(attrs.c);
 				data = data.concat(Wicket.Form.serializeElement(el, attrs.sr));
 			}
+			
+			// collect the dynamic extra parameters
+			if (jQuery.isArray(attrs.dep)) {
+				var dynamicData = this._calculateDynamicParameters(attrs);
+				if (attrs.m.toLowerCase() === 'post') {
+					data = data.concat(dynamicData);
+				} else {
+					var separator = url.indexOf('?') > -1 ? '&' : '?';
+					url = url + separator + jQuery.param(dynamicData);
+				}
+			}
 
 			var wwwFormUrlEncoded = undefined; // default
 			if (attrs.mp) {
@@ -676,29 +675,13 @@
 
 			// execute the request
 			var jqXHR = jQuery.ajax({
-				url: attrs.u,
+				url: url,
 				type: attrs.m,
 				context: self,
 				processData: wwwFormUrlEncoded,
 				contentType: wwwFormUrlEncoded,
 				
 				beforeSend: function (jqXHR, settings) {
-					// collect the dynamic extra parameters
-					if (jQuery.isArray(attrs.dep)) {
-						var queryString,
-							separator;
-
-						queryString = this._calculateDynamicParameters(attrs);
-						if (settings.type.toLowerCase() === 'post') {
-							separator = settings.data.length > 0 ? '&' : '';
-							settings.data = settings.data + separator + queryString;
-							jqXHR.setRequestHeader("Content-Type", settings.contentType);
-						} else {
-							separator = settings.url.indexOf('?') > -1 ? '&' : '?';
-							settings.url = settings.url + separator + queryString;
-						}
-					}
-
 					self._executeHandlers(attrs.bsh, attrs, jqXHR, settings);
 					we.publish(topic.AJAX_CALL_BEFORE_SEND, attrs, jqXHR, settings);
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/44fb3748/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
index 9351d4c..4642ae3 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
@@ -1,4 +1,6 @@
 <?xml version="1.0" encoding="UTF-8" ?>
+<html>
+<body>
 <wicket:extend xmlns:wicket="http://wicket.apache.org">
 
 Demonstrates Wicket's ability to transparently handle multipart forms via AJAX.<br/><br/>The only difference between this example and other non-AJAX upload examples is the option to trigger the form submit via an AjaxButton, everything else is handled transparently by Wicket.<br/><br/>
@@ -13,4 +15,10 @@ Demonstrates Wicket's ability to transparently handle multipart forms via AJAX.<
 	<input type="submit" value="Regular Submit"/> <input wicket:id="ajaxSubmit" type="button" value="Ajax Submit"/>
 </form>
 
-</wicket:extend>
\ No newline at end of file
+<div wicket:id="drop" style="border: 1px dotted black; width:200px; height:200px;">
+	Drop file here.
+</div>
+
+</wicket:extend>
+</body>
+</html>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/44fb3748/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
index e2eda2e..ca6dc70 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
@@ -16,10 +16,14 @@
  */
 package org.apache.wicket.examples.ajax.builtin;
 
+import java.util.List;
+
 import org.apache.wicket.Component;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.markup.html.form.AjaxButton;
+import org.apache.wicket.extensions.ajax.markup.html.AjaxFileUploadBehavior;
 import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar;
+import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.basic.Label;
 import org.apache.wicket.markup.html.form.Form;
 import org.apache.wicket.markup.html.form.TextField;
@@ -110,5 +114,27 @@ public class FileUploadPage extends BasePage
 			}
 
 		});
+		
+		WebMarkupContainer drop = new WebMarkupContainer("drop");
+		drop.add(new AjaxFileUploadBehavior() {
+			protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files) {
+			    
+				// display uploaded info
+				if (files == null || files.isEmpty())
+				{
+					info("No file uploaded");
+				}
+				else
+				{
+				    for (FileUpload file : files) {
+				    	info("File-Name: " + file.getClientFileName() + " File-Size: " +
+				    		Bytes.bytes(file.getSize()).toString());
+				    }
+				}
+				
+				target.add(feedback);
+			}
+		});
+		add(drop);
 	}
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/44fb3748/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileUploadBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileUploadBehavior.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileUploadBehavior.java
new file mode 100644
index 0000000..3801c62
--- /dev/null
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileUploadBehavior.java
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.extensions.ajax.markup.html;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadException;
+import org.apache.wicket.Component;
+import org.apache.wicket.ajax.AjaxEventBehavior;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
+import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method;
+import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.head.JavaScriptHeaderItem;
+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
+import org.apache.wicket.markup.html.form.upload.FileUpload;
+import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest;
+import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
+import org.apache.wicket.request.resource.PackageResourceReference;
+import org.apache.wicket.request.resource.ResourceReference;
+import org.apache.wicket.util.lang.Args;
+import org.apache.wicket.util.lang.Bytes;
+
+/**
+ * Uploads files from a drop event.
+ * 
+ * @author svenmeier
+ */
+public class AjaxFileUploadBehavior extends AjaxEventBehavior
+{
+
+	private static final ResourceReference JS = new PackageResourceReference(AjaxFileUploadBehavior.class, "datatransfer.js");
+
+	/**
+	 *  Maximum size of all uploaded files in bytes in a request.
+	 */
+	private Bytes maxSize;
+
+	/**
+	 *  Maximum size of file of upload in bytes (if there are more than one) in a request.
+	 */
+	private Bytes fileMaxSize;
+
+	private String parameterName = "f";
+	
+	public AjaxFileUploadBehavior()
+	{
+		super("drop");
+	}
+	
+	@Override
+	public void renderHead(Component component, IHeaderResponse response)
+	{
+		super.renderHead(component, response);
+		
+		response.render(JavaScriptHeaderItem.forReference(JS));
+		
+		// default must be prevented for dragover event, otherwise browser will consume the dataTransfer
+		response.render(OnDomReadyHeaderItem.forScript(String.format("jQuery('#%s').on('dragover', function(e) { e.preventDefault(); });", component.getMarkupId())));
+	}
+
+	@Override
+	protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
+	{
+		super.updateAjaxAttributes(attributes);
+
+		attributes.setMultipart(true);
+		attributes.setMethod(Method.POST);
+		// default must be prevented, otherwise browser will consume the dataTransfer
+		attributes.setPreventDefault(true);
+
+		attributes.getDynamicExtraParameters().add(
+			String.format("return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');", parameterName)
+		);
+	}
+
+	@Override
+	protected void onEvent(AjaxRequestTarget target)
+	{
+		try
+		{
+			ServletWebRequest request = (ServletWebRequest)getComponent().getRequest();
+			final MultipartServletWebRequest multipartWebRequest = request.newMultipartWebRequest(
+				getMaxSize(), getComponent().getPage().getId());
+			multipartWebRequest.setFileMaxSize(getFileMaxSize());
+			multipartWebRequest.parseFileParts();
+
+			// TODO: Can't this be detected from header?
+			getComponent().getRequestCycle().setRequest(multipartWebRequest);
+			
+			ArrayList<FileUpload> fileUploads = new ArrayList<>();
+
+			// Get the item for the path
+			final List<FileItem> fileItems = multipartWebRequest.getFile(parameterName);
+
+			if (fileItems != null)
+			{
+				for (FileItem item : fileItems)
+				{
+					fileUploads.add(new FileUpload(item));
+				}
+			}
+			
+			onFileUpload(target, fileUploads);
+		}
+		catch (final FileUploadException fux)
+		{
+			onError(fux);
+		}
+	}
+
+	public Bytes getMaxSize()
+	{
+		return maxSize;
+	}
+
+	public void setMaxSize(Bytes maxSize)
+	{
+		Args.notNull(maxSize, "maxSize");
+		this.maxSize = maxSize;
+	}
+
+	public Bytes getFileMaxSize()
+	{
+		return fileMaxSize;
+	}
+
+	public void setFileMaxSize(Bytes fileMaxSize)
+	{
+		this.fileMaxSize = fileMaxSize;
+	}
+	
+	protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files)
+	{
+	}
+
+	protected void onError(FileUploadException fux)
+	{
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/44fb3748/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js
new file mode 100644
index 0000000..05ff68a
--- /dev/null
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+;(function (undefined) {
+	'use strict';
+
+	if (typeof(Wicket) === "undefined") {
+		window.Wicket = {};
+	}
+
+	if (Wicket.DataTransfer) {
+		return;
+	}
+
+	Wicket.DataTransfer = {
+		getFilesAsParamArray : function(ev, name) {
+			var files = [];
+				
+			function pushFile(file) {
+				files.push({'name' : name, 'value' : file}); 
+			};
+
+			var dataTransfer = ev.dataTransfer; 
+			var i;
+			if (dataTransfer.items) { 
+			  for (i = 0; i < dataTransfer.items.length; i++) { 
+			    if (dataTransfer.items[i].kind == 'file') { 
+			      pushFile(dataTransfer.items[i].getAsFile()); 
+			    } 
+			  } 
+			} else { 
+			  for (i = 0; i < dataTransfer.files.length; i++) { 
+			    pushFile(dataTransfer.files[i]); 
+			  } 
+			}
+			
+			return files;
+		}
+	};
+})();


[2/6] wicket git commit: WICKET-6517 pass ajax target to onError

Posted by sv...@apache.org.
WICKET-6517 pass ajax target to onError


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

Branch: refs/heads/master
Commit: 3f5c2932c47a6798b3dd361c98bb5756b623eaaf
Parents: 237147f
Author: Sven Meier <sv...@apache.org>
Authored: Thu Jan 18 07:46:51 2018 +0100
Committer: Sven Meier <sv...@apache.org>
Committed: Fri Jan 26 23:09:37 2018 +0100

----------------------------------------------------------------------
 .../examples/ajax/builtin/FileUploadPage.java   |  9 +++
 .../ajax/markup/html/AjaxFileDropBehavior.java  | 82 +++++++++++++++-----
 2 files changed, 70 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/3f5c2932/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
index 7399301..f7a473c 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
@@ -18,6 +18,7 @@ package org.apache.wicket.examples.ajax.builtin;
 
 import java.util.List;
 
+import org.apache.commons.fileupload.FileUploadException;
 import org.apache.wicket.Component;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.markup.html.form.AjaxButton;
@@ -134,6 +135,14 @@ public class FileUploadPage extends BasePage
 				
 				target.add(feedback);
 			}
+			
+			@Override
+			protected void onError(AjaxRequestTarget target, FileUploadException fux)
+			{
+				info(fux.getMessage());
+				
+				target.add(feedback);				
+			}
 		});
 		add(drop);
 	}

http://git-wip-us.apache.org/repos/asf/wicket/blob/3f5c2932/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
index 9073cd3..b8806d9 100644
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
@@ -22,6 +22,7 @@ import java.util.List;
 import org.apache.commons.fileupload.FileItem;
 import org.apache.commons.fileupload.FileUploadException;
 import org.apache.wicket.Component;
+import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.ajax.AjaxEventBehavior;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
@@ -46,34 +47,38 @@ import org.apache.wicket.util.lang.Bytes;
 public class AjaxFileDropBehavior extends AjaxEventBehavior
 {
 
-	private static final ResourceReference JS = new PackageResourceReference(AjaxFileDropBehavior.class, "datatransfer.js");
+	private static final ResourceReference JS = new PackageResourceReference(
+		AjaxFileDropBehavior.class, "datatransfer.js");
 
 	/**
-	 *  Maximum size of all uploaded files in bytes in a request.
+	 * Maximum size of all uploaded files in bytes in a request.
 	 */
 	private Bytes maxSize;
 
 	/**
-	 *  Maximum size of file of upload in bytes (if there are more than one) in a request.
+	 * Maximum size of file of upload in bytes (if there are more than one) in a request.
 	 */
 	private Bytes fileMaxSize;
 
 	private String parameterName = "f";
-	
+
 	public AjaxFileDropBehavior()
 	{
 		super("drop");
 	}
-	
+
 	@Override
 	public void renderHead(Component component, IHeaderResponse response)
 	{
 		super.renderHead(component, response);
-		
+
 		response.render(JavaScriptHeaderItem.forReference(JS));
-		
-		// default must be prevented for dragover event, otherwise browser will consume the dataTransfer
-		response.render(OnDomReadyHeaderItem.forScript(String.format("jQuery('#%s').on('dragover', function(e) { e.preventDefault(); });", component.getMarkupId())));
+
+		// default must be prevented for dragover event, otherwise browser will consume the
+		// dataTransfer
+		response.render(OnDomReadyHeaderItem.forScript(
+			String.format("jQuery('#%s').on('dragover', function(e) { e.preventDefault(); });",
+				component.getMarkupId())));
 	}
 
 	@Override
@@ -86,9 +91,10 @@ public class AjaxFileDropBehavior extends AjaxEventBehavior
 		// default must be prevented, otherwise browser will consume the dataTransfer
 		attributes.setPreventDefault(true);
 
-		attributes.getDynamicExtraParameters().add(
-			String.format("return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');", parameterName)
-		);
+		attributes.getDynamicExtraParameters()
+			.add(String.format(
+				"return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');",
+				parameterName));
 	}
 
 	@Override
@@ -97,14 +103,14 @@ public class AjaxFileDropBehavior extends AjaxEventBehavior
 		try
 		{
 			ServletWebRequest request = (ServletWebRequest)getComponent().getRequest();
-			final MultipartServletWebRequest multipartWebRequest = request.newMultipartWebRequest(
-				getMaxSize(), getComponent().getPage().getId());
+			final MultipartServletWebRequest multipartWebRequest = request
+				.newMultipartWebRequest(getMaxSize(), getComponent().getPage().getId());
 			multipartWebRequest.setFileMaxSize(getFileMaxSize());
 			multipartWebRequest.parseFileParts();
 
 			// TODO: Can't this be detected from header?
 			getComponent().getRequestCycle().setRequest(multipartWebRequest);
-			
+
 			ArrayList<FileUpload> fileUploads = new ArrayList<>();
 
 			// Get the item for the path
@@ -117,23 +123,30 @@ public class AjaxFileDropBehavior extends AjaxEventBehavior
 					fileUploads.add(new FileUpload(item));
 				}
 			}
-			
+
 			onFileUpload(target, fileUploads);
 		}
 		catch (final FileUploadException fux)
 		{
-			onError(fux);
+			onError(target, fux);
 		}
 	}
 
 	public Bytes getMaxSize()
 	{
-		if (maxSize == null) {
-			maxSize = getComponent().getApplication().getApplicationSettings().getDefaultMaximumUploadSize();
+		if (maxSize == null)
+		{
+			maxSize = getComponent().getApplication().getApplicationSettings()
+				.getDefaultMaximumUploadSize();
 		}
 		return maxSize;
 	}
 
+	/**
+	 * Set the maximum upload size.
+	 * 
+	 * @param maxSize maximum size, must not be null
+	 */
 	public void setMaxSize(Bytes maxSize)
 	{
 		Args.notNull(maxSize, "maxSize");
@@ -145,16 +158,43 @@ public class AjaxFileDropBehavior extends AjaxEventBehavior
 		return fileMaxSize;
 	}
 
+	/**
+	 * Set an optional maximum size per file.
+	 * 
+	 * @param fileMaxSize maximum size for each uploaded file
+	 */
 	public void setFileMaxSize(Bytes fileMaxSize)
 	{
 		this.fileMaxSize = fileMaxSize;
 	}
-	
+
+	/**
+	 * Hook method called after a file was uploaded.
+	 * <p>
+	 * Note: {@link #onError(AjaxRequestTarget, FileUploadException)} is called instead when
+	 * uploading failed
+	 * 
+	 * @param target
+	 *            the current request handler
+	 * @param files
+	 *            uploaded files
+	 */
 	protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files)
 	{
 	}
 
-	protected void onError(FileUploadException fux)
+	/**
+	 * Hook method called to handle any error during uploading of the file.
+	 * <p>
+	 * Default implementation re-throws the exception. 
+	 *
+	 * @param target
+	 *            the current request handler
+	 * @param e
+	 *            the error that occurred
+	 */
+	protected void onError(AjaxRequestTarget target, FileUploadException fux)
 	{
+		throw new WicketRuntimeException(fux);
 	}
 }
\ No newline at end of file


[3/6] wicket git commit: WICKET-6517 use FormData, removed submitMultipartForm

Posted by sv...@apache.org.
WICKET-6517 use FormData, removed submitMultipartForm


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

Branch: refs/heads/master
Commit: f642f4ad4266319731b200e517eadf53fef5a56b
Parents: 1d20044
Author: Sven Meier <sv...@apache.org>
Authored: Fri Jan 12 12:45:55 2018 +0100
Committer: Sven Meier <sv...@apache.org>
Committed: Fri Jan 26 23:09:37 2018 +0100

----------------------------------------------------------------------
 .../wicket/ajax/res/js/wicket-ajax-jquery.js    | 234 +++----------------
 wicket-core/src/test/js/ajax.js                 |  28 +--
 2 files changed, 28 insertions(+), 234 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/f642f4ad/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
index d4f3100..801a47d 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
@@ -643,11 +643,6 @@
 
 			we.publish(topic.AJAX_CALL_PRECONDITION, attrs);
 
-			if (attrs.mp) { // multipart form. jQuery.ajax() doesn't help here ...
-				var ret = self.submitMultipartForm(context);
-				return ret;
-			}
-
 			if (attrs.f) {
 				// serialize the form with id == attrs.f
 				var form = Wicket.$(attrs.f);
@@ -658,23 +653,36 @@
 					var scName = attrs.sc;
 					data = data.concat({name: scName, value: 1});
 				}
-
 			} else if (attrs.c && !jQuery.isWindow(attrs.c)) {
 				// serialize just the form component with id == attrs.c
 				var el = Wicket.$(attrs.c);
 				data = data.concat(Wicket.Form.serializeElement(el, attrs.sr));
 			}
 
-			// convert to URL encoded string
-			data = jQuery.param(data);
+			var wwwFormUrlEncoded = undefined; // default
+			if (attrs.mp) {
+				try {
+					var formData = new FormData();
+					for (var i = 0; i < data.length; i++) {
+						formData.append(data[i].name, data[i].value);
+					}
+					
+					data = formData;
+					wwwFormUrlEncoded = false;
+				} catch (exception) {
+					Wicket.Log.error("Ajax multipat not supported:" + exception);
+				}
+			}
 
 			// execute the request
 			var jqXHR = jQuery.ajax({
 				url: attrs.u,
 				type: attrs.m,
 				context: self,
+				processData: wwwFormUrlEncoded,
+				contentType: wwwFormUrlEncoded,
+				
 				beforeSend: function (jqXHR, settings) {
-
 					// collect the dynamic extra parameters
 					if (jQuery.isArray(attrs.dep)) {
 						var queryString,
@@ -831,203 +839,6 @@
 			}
 		},
 
-		/**
-		 * This method serializes a form and sends it as 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.
-		 *
-		 * @param {Object} context - the context for the ajax call (request attributes + steps)
-		 */
-		submitMultipartForm: function (context) {
-
-			var attrs = context.attrs;
-
-			var form = Wicket.$(attrs.f);
-			if (!form) {
-				Wicket.Log.error("Wicket.Ajax.Call.submitForm: Trying to submit form with id '" + attrs.f + "' that is not in document.");
-				return;
-			}
-
-			// find root form
-			if (form.tagName.toLowerCase() !== "form") {
-				do {
-					form = form.parentNode;
-				} while(form.tagName.toLowerCase() !== "form" && form !== document.body);
-			}
-
-			if (form.tagName.toLowerCase() !== "form") {
-				Wicket.Log.error("Cannot submit form with id " + attrs.f + " because there is no form element in the hierarchy.");
-				return false;
-			}
-
-			var submittingAttribute = 'data-wicket-submitting';
-
-			if (form.onsubmit && !form.getAttribute(submittingAttribute)) {
-				form.setAttribute(submittingAttribute, submittingAttribute);
-				var retValue = true;
-				try {
-					retValue = form.onsubmit();
-				} finally {
-					form.removeAttribute(submittingAttribute);
-				}
-				if (!retValue) {
-					return;
-				}
-			}
-
-			var originalFormAction = form.action;
-			var originalFormTarget = form.target;
-			var originalFormMethod = form.method;
-			var originalFormEnctype = form.enctype;
-			var originalFormEncoding = form.encoding;
-
-			var iframeName = "wicket-submit-" + ("" + Math.random()).substr(2);
-
-			var iframe = createIFrame(iframeName);
-
-			document.body.appendChild(iframe);
-
-			// reconfigure the form
-			form.target = iframe.name;
-			var separator = (attrs.u.indexOf("?")>-1 ? "&" : "?");
-			form.action = attrs.u + separator + "wicket-ajax=true&wicket-ajax-baseurl=" + Wicket.Form.encode(getAjaxBaseUrl());
-
-			// add the static extra parameters
-			if (attrs.ep) {
-				var extraParametersArray = this._asParamArray(attrs.ep);
-				if (extraParametersArray.length > 0) {
-					var extraParametersQueryString = jQuery.param(extraParametersArray);
-					form.action = form.action + '&' + extraParametersQueryString;
-				}
-			}
-
-			// add the dynamic extra parameters
-			if (jQuery.isArray(attrs.dep)) {
-				var dynamicExtraParameters = this._calculateDynamicParameters(attrs);
-				if (dynamicExtraParameters) {
-					form.action = form.action + '&' + dynamicExtraParameters;
-				}
-			}
-			form.method = 'post';
-			form.enctype = "multipart/form-data";
-			form.encoding = "multipart/form-data";
-
-			// create submitting button element
-			if (attrs.sc) {
-				var $btn = jQuery("<input type='hidden' name='" + attrs.sc + "' id='" + iframe.id + "-btn' value='1'/>");
-				form.appendChild($btn[0]);
-			}
-
-			var we = Wicket.Event;
-			var topic = we.Topic;
-
-			this._executeHandlers(attrs.bsh, attrs, null, null);
-			we.publish(topic.AJAX_CALL_BEFORE_SEND, attrs, null, null);
-
-			if (attrs.i) {
-				// show the indicator
-				Wicket.DOM.showIncrementally(attrs.i);
-			}
-
-			//submit the form into the iframe, response will be handled by the onload callback
-			form.submit();
-
-			this._executeHandlers(attrs.ah, attrs);
-			we.publish(topic.AJAX_CALL_AFTER, attrs);
-
-			// a step to execute in both successful and erroneous completion
-			context.endStep = jQuery.proxy(function(notify) {
-				// remove the iframe and button elements
-				setTimeout(function() {
-					jQuery('#'+iframe.id + '-btn').remove();
-					jQuery(iframe).remove();
-				}, 0);
-
-				var attrs = context.attrs;
-				if (attrs.i && context.isRedirecting !== true) {
-					// hide the indicator
-					Wicket.DOM.hideIncrementally(attrs.i);
-				}
-
-				this._executeHandlers(attrs.coh, attrs, null, null);
-				Wicket.Event.publish(Wicket.Event.Topic.AJAX_CALL_COMPLETE, attrs, null, null);
-
-				this.done(attrs);
-				return FunctionsExecuter.DONE;
-			}, this);
-
-			// an error handler that is used when the connection to the server fails for any reason
-			if (attrs.rt) {
-				context.errorHandle = setTimeout(jQuery.proxy(function () {
-					this.failure(context, null, "No XML response in the IFrame document", "Failure");
-
-					context.steps.push(context.endStep);
-					var executer = new FunctionsExecuter(context.steps);
-					executer.start();
-				}, this), attrs.rt);
-			} else {
-				Wicket.Log.info("Submitting a multipart form without a timeout. " +
-					"Use AjaxRequestAttributes.setRequestTimeout(duration) if need to handle connection timeouts.");
-			}
-
-			// install handler to deal with the ajax response
-			// ... we add the onload event after form submit because chrome fires it prematurely
-			we.add(iframe, "load.handleMultipartComplete", jQuery.proxy(this.handleMultipartComplete, this), context);
-
-
-			// handled, restore state and return true
-			form.action = originalFormAction;
-			form.target = originalFormTarget;
-			form.method = originalFormMethod;
-			form.enctype = originalFormEnctype;
-			form.encoding = originalFormEncoding;
-
-			return true;
-		},
-
-		/**
-		 * Completes the multipart ajax handling started via handleMultipart()
-		 * @param {jQuery.Event} event
-		 */
-		handleMultipartComplete: function (event) {
-
-			var context = event.data,
-				iframe = event.target,
-				envelope;
-
-			if (!isUndef(context.errorHandle)) {
-				clearTimeout(context.errorHandle);
-			}
-
-			// stop the event
-			event.stopPropagation();
-
-			// remove the event
-			jQuery(iframe).off("load.handleMultipartComplete");
-
-			try {
-				envelope = iframe.contentWindow.document;
-			} catch (e) {
-				Wicket.Log.error("Cannot read Ajax response for multipart form submit: " + e);
-			}
-
-			if (isUndef(envelope)) {
-				this.failure(context, null, "No XML response in the IFrame document", "Failure");
-			}
-			else {
-				if (envelope.XMLDocument) {
-					envelope = envelope.XMLDocument;
-				}
-
-				// process the response
-				this.loadedCallback(envelope, context);
-			}
-
-			context.steps.push(context.endStep);
-			var executer = new FunctionsExecuter(context.steps);
-			executer.start();
-		},
-
 		// Processes the response
 		loadedCallback: function (envelope, context) {
 			// To process the response, we go through the xml document and add a function for every action (step).
@@ -1488,9 +1299,16 @@
 			 */
 			serializeInput: function (input) {
 				var result = [];
-				if (input && input.type && !(input.type === 'image' || input.type === 'submit')) {
+				if (input && input.type) {
 					var $input = jQuery(input);
-					result = $input.serializeArray();
+					
+					if (input.type === 'file') {
+						for (var f = 0; f < input.files.length; f++) {
+							result.push({"name" : input.name, "value" : input.files[f]});
+						}
+					} else if (!(input.type === 'image' || input.type === 'submit')) {
+						result = $input.serializeArray();
+					}
 				}
 				return result;
 			},

http://git-wip-us.apache.org/repos/asf/wicket/blob/f642f4ad/wicket-core/src/test/js/ajax.js
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/js/ajax.js b/wicket-core/src/test/js/ajax.js
index 71f9fce..982aa47 100644
--- a/wicket-core/src/test/js/ajax.js
+++ b/wicket-core/src/test/js/ajax.js
@@ -1042,7 +1042,7 @@ jQuery(document).ready(function() {
 		 */
 		asyncTest('Submit nested form - success scenario.', function () {
 
-			expect(13);
+			expect(9);
 
 			var attrs = {
 				f:  "innerForm", // the id of the form to submit
@@ -1056,18 +1056,6 @@ jQuery(document).ready(function() {
 				pre: [ function(attrs) {ok(true, "Precondition executed"); return true; } ],
 				bsh: [ function(attrs) {
 					ok(true, "BeforeSend handler executed");
-
-					var form = Wicket.$(attrs.f);
-					if (form.tagName.toLowerCase() !== "form") {
-						do {
-							form = form.parentNode;
-						} while(form.tagName.toLowerCase() !== "form" && form !== document.body);
-					}
-					var formUrl = form.action;
-					ok(formUrl.indexOf('dynamicEPName') > -1, "Dynamic extra parameter name is in the request query string");
-					ok(formUrl.indexOf('dynamicEPValue') > -1, "Dynamic extra parameter value is in the request query string");
-					ok(formUrl.indexOf('extraParamName') > -1, "Static extra parameter name is in the request query string");
-					ok(formUrl.indexOf('extraParamValue') > -1, "Static extra parameter value is in the request query string");
 				} ],
 				ah: [ function(attrs) { ok(true, "After handler executed"); } ],
 				sh: [ function(attrs) { ok(true, "Success handler executed"); } ],
@@ -1101,7 +1089,7 @@ jQuery(document).ready(function() {
 		 */
 		asyncTest('Submit nested form - failure scenario.', function () {
 
-			expect(12);
+			expect(8);
 
 			var attrs = {
 				f:  "innerForm", // the id of the form to submit
@@ -1115,18 +1103,6 @@ jQuery(document).ready(function() {
 				pre: [ function(attrs) {ok(true, "Precondition executed"); return true; } ],
 				bsh: [ function(attrs) {
 					ok(true, "BeforeSend handler executed");
-
-					var form = Wicket.$(attrs.f);
-					if (form.tagName.toLowerCase() !== "form") {
-						do {
-							form = form.parentNode;
-						} while(form.tagName.toLowerCase() !== "form" && form !== document.body);
-					}
-					var formUrl = form.action;
-					ok(formUrl.indexOf('dynamicEPName') > -1, "Dynamic extra parameter name is in the request query string");
-					ok(formUrl.indexOf('dynamicEPValue') > -1, "Dynamic extra parameter value is in the request query string");
-					ok(formUrl.indexOf('extraParamName') > -1, "Static extra parameter name is in the request query string");
-					ok(formUrl.indexOf('extraParamValue') > -1, "Static extra parameter value is in the request query string");
 				} ],
 				ah: [ function(attrs) { ok(true, "After handler executed"); } ],
 				sh: [ function(attrs) { ok(false, "Success handler should not be executed"); } ],


[6/6] wicket git commit: WICKET-6517 moved behavior out of markup package

Posted by sv...@apache.org.
WICKET-6517 moved behavior out of markup package

This closes #256


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

Branch: refs/heads/master
Commit: 29d2775c05e8084ff3a01c2f2da27efcaa6d4b03
Parents: f1dd096
Author: Sven Meier <sv...@apache.org>
Authored: Fri Jan 19 09:22:35 2018 +0100
Committer: Sven Meier <sv...@apache.org>
Committed: Fri Jan 26 23:51:35 2018 +0100

----------------------------------------------------------------------
 .../examples/ajax/builtin/FileUploadPage.html   |   2 +-
 .../examples/ajax/builtin/FileUploadPage.java   |   2 +-
 .../extensions/ajax/AjaxFileDropBehavior.java   | 211 +++++++++++++++++++
 .../ajax/markup/html/AjaxFileDropBehavior.java  | 211 -------------------
 .../extensions/ajax/markup/html/datatransfer.js |  54 -----
 .../wicket/extensions/ajax/wicket-ajaxupload.js |  54 +++++
 6 files changed, 267 insertions(+), 267 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/29d2775c/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
index 2ed083d..18d2bfb 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.html
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<html>
+<html xmlns:wicket="http://wicket.apache.org">
 <head>
 	<wicket:head>
 		<style>

http://git-wip-us.apache.org/repos/asf/wicket/blob/29d2775c/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
index f7a473c..598efee 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
@@ -22,7 +22,7 @@ import org.apache.commons.fileupload.FileUploadException;
 import org.apache.wicket.Component;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.markup.html.form.AjaxButton;
-import org.apache.wicket.extensions.ajax.markup.html.AjaxFileDropBehavior;
+import org.apache.wicket.extensions.ajax.AjaxFileDropBehavior;
 import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.basic.Label;

http://git-wip-us.apache.org/repos/asf/wicket/blob/29d2775c/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxFileDropBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxFileDropBehavior.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxFileDropBehavior.java
new file mode 100644
index 0000000..0501610
--- /dev/null
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/AjaxFileDropBehavior.java
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.extensions.ajax;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadException;
+import org.apache.wicket.Component;
+import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.ajax.AjaxEventBehavior;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.attributes.AjaxCallListener;
+import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
+import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method;
+import org.apache.wicket.core.util.string.CssUtils;
+import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.head.JavaScriptHeaderItem;
+import org.apache.wicket.markup.html.form.upload.FileUpload;
+import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest;
+import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
+import org.apache.wicket.request.resource.PackageResourceReference;
+import org.apache.wicket.request.resource.ResourceReference;
+import org.apache.wicket.util.lang.Args;
+import org.apache.wicket.util.lang.Bytes;
+
+/**
+ * Uploads files from a drop event.
+ *
+ * @author Andrew Kondratev
+ * @author svenmeier
+ */
+public class AjaxFileDropBehavior extends AjaxEventBehavior
+{
+
+	public static final String DRAG_OVER_CLASS_KEY = CssUtils.key(AjaxFileDropBehavior.class, "dragover");
+
+	private static final ResourceReference JS = new PackageResourceReference(
+		AjaxFileDropBehavior.class, "wicket-ajaxupload.js");
+
+	/**
+	 * Maximum size of all uploaded files in bytes in a request.
+	 */
+	private Bytes maxSize;
+
+	/**
+	 * Maximum size of file of upload in bytes (if there are more than one) in a request.
+	 */
+	private Bytes fileMaxSize;
+
+	private String parameterName = "f";
+
+	/**
+	 * Listen for 'dragover' and 'drop' events and prevent them, only 'drop' will initiate
+	 * an Ajax request.
+	 */
+	public AjaxFileDropBehavior()
+	{
+		super("dragenter dragover dragleave drop");
+	}
+
+	@Override
+	public void renderHead(Component component, IHeaderResponse response)
+	{
+		super.renderHead(component, response);
+
+		response.render(JavaScriptHeaderItem.forReference(JS));
+	}
+
+	@Override
+	protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
+	{
+		super.updateAjaxAttributes(attributes);
+
+		attributes.setMultipart(true);
+		attributes.setMethod(Method.POST);
+		// default must be prevented, otherwise browser will consume the dataTransfer
+		attributes.setPreventDefault(true);
+
+		attributes.getAjaxCallListeners().add(new AjaxCallListener() {
+			@Override
+			public CharSequence getPrecondition(Component component)
+			{
+				String css = getComponent().getString(DRAG_OVER_CLASS_KEY);
+				
+				return String.format("jQuery('#' + attrs.c).toggleClass('%s', attrs.event.type === 'dragover'); return (attrs.event.type === 'drop');", css);
+			}
+		});
+		
+		attributes.getDynamicExtraParameters()
+			.add(String.format(
+				"return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');",
+				parameterName));
+	}
+
+	@Override
+	protected void onEvent(AjaxRequestTarget target)
+	{
+		try
+		{
+			ServletWebRequest request = (ServletWebRequest)getComponent().getRequest();
+			final MultipartServletWebRequest multipartWebRequest = request
+				.newMultipartWebRequest(getMaxSize(), getComponent().getPage().getId());
+			multipartWebRequest.setFileMaxSize(getFileMaxSize());
+			multipartWebRequest.parseFileParts();
+
+			// TODO: Can't this be detected from header?
+			getComponent().getRequestCycle().setRequest(multipartWebRequest);
+
+			ArrayList<FileUpload> fileUploads = new ArrayList<>();
+
+			// Get the item for the path
+			final List<FileItem> fileItems = multipartWebRequest.getFile(parameterName);
+
+			if (fileItems != null)
+			{
+				for (FileItem item : fileItems)
+				{
+					fileUploads.add(new FileUpload(item));
+				}
+			}
+
+			onFileUpload(target, fileUploads);
+		}
+		catch (final FileUploadException fux)
+		{
+			onError(target, fux);
+		}
+	}
+
+	public Bytes getMaxSize()
+	{
+		if (maxSize == null)
+		{
+			maxSize = getComponent().getApplication().getApplicationSettings()
+				.getDefaultMaximumUploadSize();
+		}
+		return maxSize;
+	}
+
+	/**
+	 * Set the maximum upload size.
+	 * 
+	 * @param maxSize maximum size, must not be null
+	 */
+	public void setMaxSize(Bytes maxSize)
+	{
+		Args.notNull(maxSize, "maxSize");
+		this.maxSize = maxSize;
+	}
+
+	public Bytes getFileMaxSize()
+	{
+		return fileMaxSize;
+	}
+
+	/**
+	 * Set an optional maximum size per file.
+	 * 
+	 * @param fileMaxSize maximum size for each uploaded file
+	 */
+	public void setFileMaxSize(Bytes fileMaxSize)
+	{
+		this.fileMaxSize = fileMaxSize;
+	}
+
+	/**
+	 * Hook method called after a file was uploaded.
+	 * <p>
+	 * Note: {@link #onError(AjaxRequestTarget, FileUploadException)} is called instead when
+	 * uploading failed
+	 * 
+	 * @param target
+	 *            the current request handler
+	 * @param files
+	 *            uploaded files
+	 */
+	protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files)
+	{
+	}
+
+	/**
+	 * Hook method called to handle any error during uploading of the file.
+	 * <p>
+	 * Default implementation re-throws the exception. 
+	 *
+	 * @param target
+	 *            the current request handler
+	 * @param e
+	 *            the error that occurred
+	 */
+	protected void onError(AjaxRequestTarget target, FileUploadException fux)
+	{
+		throw new WicketRuntimeException(fux);
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/29d2775c/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
deleted file mode 100644
index 817c2f4..0000000
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
+++ /dev/null
@@ -1,211 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.wicket.extensions.ajax.markup.html;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileUploadException;
-import org.apache.wicket.Component;
-import org.apache.wicket.WicketRuntimeException;
-import org.apache.wicket.ajax.AjaxEventBehavior;
-import org.apache.wicket.ajax.AjaxRequestTarget;
-import org.apache.wicket.ajax.attributes.AjaxCallListener;
-import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
-import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method;
-import org.apache.wicket.core.util.string.CssUtils;
-import org.apache.wicket.markup.head.IHeaderResponse;
-import org.apache.wicket.markup.head.JavaScriptHeaderItem;
-import org.apache.wicket.markup.html.form.upload.FileUpload;
-import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest;
-import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
-import org.apache.wicket.request.resource.PackageResourceReference;
-import org.apache.wicket.request.resource.ResourceReference;
-import org.apache.wicket.util.lang.Args;
-import org.apache.wicket.util.lang.Bytes;
-
-/**
- * Uploads files from a drop event.
- *
- * @author Andrew Kondratev
- * @author svenmeier
- */
-public class AjaxFileDropBehavior extends AjaxEventBehavior
-{
-
-	public static final String DRAG_OVER_CLASS_KEY = CssUtils.key(AjaxFileDropBehavior.class, "dragover");
-
-	private static final ResourceReference JS = new PackageResourceReference(
-		AjaxFileDropBehavior.class, "datatransfer.js");
-
-	/**
-	 * Maximum size of all uploaded files in bytes in a request.
-	 */
-	private Bytes maxSize;
-
-	/**
-	 * Maximum size of file of upload in bytes (if there are more than one) in a request.
-	 */
-	private Bytes fileMaxSize;
-
-	private String parameterName = "f";
-
-	/**
-	 * Listen for 'dragover' and 'drop' events and prevent them, only 'drop' will initiate
-	 * an Ajax request.
-	 */
-	public AjaxFileDropBehavior()
-	{
-		super("dragenter dragover dragleave drop");
-	}
-
-	@Override
-	public void renderHead(Component component, IHeaderResponse response)
-	{
-		super.renderHead(component, response);
-
-		response.render(JavaScriptHeaderItem.forReference(JS));
-	}
-
-	@Override
-	protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
-	{
-		super.updateAjaxAttributes(attributes);
-
-		attributes.setMultipart(true);
-		attributes.setMethod(Method.POST);
-		// default must be prevented, otherwise browser will consume the dataTransfer
-		attributes.setPreventDefault(true);
-
-		attributes.getAjaxCallListeners().add(new AjaxCallListener() {
-			@Override
-			public CharSequence getPrecondition(Component component)
-			{
-				String css = getComponent().getString(DRAG_OVER_CLASS_KEY);
-				
-				return String.format("jQuery('#' + attrs.c).toggleClass('%s', attrs.event.type === 'dragover'); return (attrs.event.type === 'drop');", css);
-			}
-		});
-		
-		attributes.getDynamicExtraParameters()
-			.add(String.format(
-				"return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');",
-				parameterName));
-	}
-
-	@Override
-	protected void onEvent(AjaxRequestTarget target)
-	{
-		try
-		{
-			ServletWebRequest request = (ServletWebRequest)getComponent().getRequest();
-			final MultipartServletWebRequest multipartWebRequest = request
-				.newMultipartWebRequest(getMaxSize(), getComponent().getPage().getId());
-			multipartWebRequest.setFileMaxSize(getFileMaxSize());
-			multipartWebRequest.parseFileParts();
-
-			// TODO: Can't this be detected from header?
-			getComponent().getRequestCycle().setRequest(multipartWebRequest);
-
-			ArrayList<FileUpload> fileUploads = new ArrayList<>();
-
-			// Get the item for the path
-			final List<FileItem> fileItems = multipartWebRequest.getFile(parameterName);
-
-			if (fileItems != null)
-			{
-				for (FileItem item : fileItems)
-				{
-					fileUploads.add(new FileUpload(item));
-				}
-			}
-
-			onFileUpload(target, fileUploads);
-		}
-		catch (final FileUploadException fux)
-		{
-			onError(target, fux);
-		}
-	}
-
-	public Bytes getMaxSize()
-	{
-		if (maxSize == null)
-		{
-			maxSize = getComponent().getApplication().getApplicationSettings()
-				.getDefaultMaximumUploadSize();
-		}
-		return maxSize;
-	}
-
-	/**
-	 * Set the maximum upload size.
-	 * 
-	 * @param maxSize maximum size, must not be null
-	 */
-	public void setMaxSize(Bytes maxSize)
-	{
-		Args.notNull(maxSize, "maxSize");
-		this.maxSize = maxSize;
-	}
-
-	public Bytes getFileMaxSize()
-	{
-		return fileMaxSize;
-	}
-
-	/**
-	 * Set an optional maximum size per file.
-	 * 
-	 * @param fileMaxSize maximum size for each uploaded file
-	 */
-	public void setFileMaxSize(Bytes fileMaxSize)
-	{
-		this.fileMaxSize = fileMaxSize;
-	}
-
-	/**
-	 * Hook method called after a file was uploaded.
-	 * <p>
-	 * Note: {@link #onError(AjaxRequestTarget, FileUploadException)} is called instead when
-	 * uploading failed
-	 * 
-	 * @param target
-	 *            the current request handler
-	 * @param files
-	 *            uploaded files
-	 */
-	protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files)
-	{
-	}
-
-	/**
-	 * Hook method called to handle any error during uploading of the file.
-	 * <p>
-	 * Default implementation re-throws the exception. 
-	 *
-	 * @param target
-	 *            the current request handler
-	 * @param e
-	 *            the error that occurred
-	 */
-	protected void onError(AjaxRequestTarget target, FileUploadException fux)
-	{
-		throw new WicketRuntimeException(fux);
-	}
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/29d2775c/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js
deleted file mode 100644
index 05ff68a..0000000
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/datatransfer.js
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-;(function (undefined) {
-	'use strict';
-
-	if (typeof(Wicket) === "undefined") {
-		window.Wicket = {};
-	}
-
-	if (Wicket.DataTransfer) {
-		return;
-	}
-
-	Wicket.DataTransfer = {
-		getFilesAsParamArray : function(ev, name) {
-			var files = [];
-				
-			function pushFile(file) {
-				files.push({'name' : name, 'value' : file}); 
-			};
-
-			var dataTransfer = ev.dataTransfer; 
-			var i;
-			if (dataTransfer.items) { 
-			  for (i = 0; i < dataTransfer.items.length; i++) { 
-			    if (dataTransfer.items[i].kind == 'file') { 
-			      pushFile(dataTransfer.items[i].getAsFile()); 
-			    } 
-			  } 
-			} else { 
-			  for (i = 0; i < dataTransfer.files.length; i++) { 
-			    pushFile(dataTransfer.files[i]); 
-			  } 
-			}
-			
-			return files;
-		}
-	};
-})();

http://git-wip-us.apache.org/repos/asf/wicket/blob/29d2775c/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxupload.js
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxupload.js b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxupload.js
new file mode 100644
index 0000000..05ff68a
--- /dev/null
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/wicket-ajaxupload.js
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+;(function (undefined) {
+	'use strict';
+
+	if (typeof(Wicket) === "undefined") {
+		window.Wicket = {};
+	}
+
+	if (Wicket.DataTransfer) {
+		return;
+	}
+
+	Wicket.DataTransfer = {
+		getFilesAsParamArray : function(ev, name) {
+			var files = [];
+				
+			function pushFile(file) {
+				files.push({'name' : name, 'value' : file}); 
+			};
+
+			var dataTransfer = ev.dataTransfer; 
+			var i;
+			if (dataTransfer.items) { 
+			  for (i = 0; i < dataTransfer.items.length; i++) { 
+			    if (dataTransfer.items[i].kind == 'file') { 
+			      pushFile(dataTransfer.items[i].getAsFile()); 
+			    } 
+			  } 
+			} else { 
+			  for (i = 0; i < dataTransfer.files.length; i++) { 
+			    pushFile(dataTransfer.files[i]); 
+			  } 
+			}
+			
+			return files;
+		}
+	};
+})();


[5/6] wicket git commit: WICKET-6517 new AjaxFileDropBehavior

Posted by sv...@apache.org.
WICKET-6517 new AjaxFileDropBehavior


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

Branch: refs/heads/master
Commit: 237147fee94e7fb2c0e91216aee0e4a51b448c30
Parents: 44fb374
Author: Sven Meier <sv...@apache.org>
Authored: Wed Jan 17 09:23:27 2018 +0100
Committer: Sven Meier <sv...@apache.org>
Committed: Fri Jan 26 23:09:37 2018 +0100

----------------------------------------------------------------------
 .../examples/ajax/builtin/FileUploadPage.java   |   4 +-
 .../ajax/markup/html/AjaxFileDropBehavior.java  | 160 +++++++++++++++++++
 .../markup/html/AjaxFileUploadBehavior.java     | 156 ------------------
 3 files changed, 162 insertions(+), 158 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/237147fe/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
index ca6dc70..7399301 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FileUploadPage.java
@@ -21,7 +21,7 @@ import java.util.List;
 import org.apache.wicket.Component;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.markup.html.form.AjaxButton;
-import org.apache.wicket.extensions.ajax.markup.html.AjaxFileUploadBehavior;
+import org.apache.wicket.extensions.ajax.markup.html.AjaxFileDropBehavior;
 import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.basic.Label;
@@ -116,7 +116,7 @@ public class FileUploadPage extends BasePage
 		});
 		
 		WebMarkupContainer drop = new WebMarkupContainer("drop");
-		drop.add(new AjaxFileUploadBehavior() {
+		drop.add(new AjaxFileDropBehavior() {
 			protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files) {
 			    
 				// display uploaded info

http://git-wip-us.apache.org/repos/asf/wicket/blob/237147fe/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
new file mode 100644
index 0000000..9073cd3
--- /dev/null
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileDropBehavior.java
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.extensions.ajax.markup.html;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadException;
+import org.apache.wicket.Component;
+import org.apache.wicket.ajax.AjaxEventBehavior;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
+import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method;
+import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.head.JavaScriptHeaderItem;
+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
+import org.apache.wicket.markup.html.form.upload.FileUpload;
+import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest;
+import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
+import org.apache.wicket.request.resource.PackageResourceReference;
+import org.apache.wicket.request.resource.ResourceReference;
+import org.apache.wicket.util.lang.Args;
+import org.apache.wicket.util.lang.Bytes;
+
+/**
+ * Uploads files from a drop event.
+ *
+ * @author Andrew Kondratev
+ * @author svenmeier
+ */
+public class AjaxFileDropBehavior extends AjaxEventBehavior
+{
+
+	private static final ResourceReference JS = new PackageResourceReference(AjaxFileDropBehavior.class, "datatransfer.js");
+
+	/**
+	 *  Maximum size of all uploaded files in bytes in a request.
+	 */
+	private Bytes maxSize;
+
+	/**
+	 *  Maximum size of file of upload in bytes (if there are more than one) in a request.
+	 */
+	private Bytes fileMaxSize;
+
+	private String parameterName = "f";
+	
+	public AjaxFileDropBehavior()
+	{
+		super("drop");
+	}
+	
+	@Override
+	public void renderHead(Component component, IHeaderResponse response)
+	{
+		super.renderHead(component, response);
+		
+		response.render(JavaScriptHeaderItem.forReference(JS));
+		
+		// default must be prevented for dragover event, otherwise browser will consume the dataTransfer
+		response.render(OnDomReadyHeaderItem.forScript(String.format("jQuery('#%s').on('dragover', function(e) { e.preventDefault(); });", component.getMarkupId())));
+	}
+
+	@Override
+	protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
+	{
+		super.updateAjaxAttributes(attributes);
+
+		attributes.setMultipart(true);
+		attributes.setMethod(Method.POST);
+		// default must be prevented, otherwise browser will consume the dataTransfer
+		attributes.setPreventDefault(true);
+
+		attributes.getDynamicExtraParameters().add(
+			String.format("return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');", parameterName)
+		);
+	}
+
+	@Override
+	protected void onEvent(AjaxRequestTarget target)
+	{
+		try
+		{
+			ServletWebRequest request = (ServletWebRequest)getComponent().getRequest();
+			final MultipartServletWebRequest multipartWebRequest = request.newMultipartWebRequest(
+				getMaxSize(), getComponent().getPage().getId());
+			multipartWebRequest.setFileMaxSize(getFileMaxSize());
+			multipartWebRequest.parseFileParts();
+
+			// TODO: Can't this be detected from header?
+			getComponent().getRequestCycle().setRequest(multipartWebRequest);
+			
+			ArrayList<FileUpload> fileUploads = new ArrayList<>();
+
+			// Get the item for the path
+			final List<FileItem> fileItems = multipartWebRequest.getFile(parameterName);
+
+			if (fileItems != null)
+			{
+				for (FileItem item : fileItems)
+				{
+					fileUploads.add(new FileUpload(item));
+				}
+			}
+			
+			onFileUpload(target, fileUploads);
+		}
+		catch (final FileUploadException fux)
+		{
+			onError(fux);
+		}
+	}
+
+	public Bytes getMaxSize()
+	{
+		if (maxSize == null) {
+			maxSize = getComponent().getApplication().getApplicationSettings().getDefaultMaximumUploadSize();
+		}
+		return maxSize;
+	}
+
+	public void setMaxSize(Bytes maxSize)
+	{
+		Args.notNull(maxSize, "maxSize");
+		this.maxSize = maxSize;
+	}
+
+	public Bytes getFileMaxSize()
+	{
+		return fileMaxSize;
+	}
+
+	public void setFileMaxSize(Bytes fileMaxSize)
+	{
+		this.fileMaxSize = fileMaxSize;
+	}
+	
+	protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files)
+	{
+	}
+
+	protected void onError(FileUploadException fux)
+	{
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/237147fe/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileUploadBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileUploadBehavior.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileUploadBehavior.java
deleted file mode 100644
index 3801c62..0000000
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/AjaxFileUploadBehavior.java
+++ /dev/null
@@ -1,156 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.wicket.extensions.ajax.markup.html;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileUploadException;
-import org.apache.wicket.Component;
-import org.apache.wicket.ajax.AjaxEventBehavior;
-import org.apache.wicket.ajax.AjaxRequestTarget;
-import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
-import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method;
-import org.apache.wicket.markup.head.IHeaderResponse;
-import org.apache.wicket.markup.head.JavaScriptHeaderItem;
-import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
-import org.apache.wicket.markup.html.form.upload.FileUpload;
-import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest;
-import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
-import org.apache.wicket.request.resource.PackageResourceReference;
-import org.apache.wicket.request.resource.ResourceReference;
-import org.apache.wicket.util.lang.Args;
-import org.apache.wicket.util.lang.Bytes;
-
-/**
- * Uploads files from a drop event.
- * 
- * @author svenmeier
- */
-public class AjaxFileUploadBehavior extends AjaxEventBehavior
-{
-
-	private static final ResourceReference JS = new PackageResourceReference(AjaxFileUploadBehavior.class, "datatransfer.js");
-
-	/**
-	 *  Maximum size of all uploaded files in bytes in a request.
-	 */
-	private Bytes maxSize;
-
-	/**
-	 *  Maximum size of file of upload in bytes (if there are more than one) in a request.
-	 */
-	private Bytes fileMaxSize;
-
-	private String parameterName = "f";
-	
-	public AjaxFileUploadBehavior()
-	{
-		super("drop");
-	}
-	
-	@Override
-	public void renderHead(Component component, IHeaderResponse response)
-	{
-		super.renderHead(component, response);
-		
-		response.render(JavaScriptHeaderItem.forReference(JS));
-		
-		// default must be prevented for dragover event, otherwise browser will consume the dataTransfer
-		response.render(OnDomReadyHeaderItem.forScript(String.format("jQuery('#%s').on('dragover', function(e) { e.preventDefault(); });", component.getMarkupId())));
-	}
-
-	@Override
-	protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
-	{
-		super.updateAjaxAttributes(attributes);
-
-		attributes.setMultipart(true);
-		attributes.setMethod(Method.POST);
-		// default must be prevented, otherwise browser will consume the dataTransfer
-		attributes.setPreventDefault(true);
-
-		attributes.getDynamicExtraParameters().add(
-			String.format("return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');", parameterName)
-		);
-	}
-
-	@Override
-	protected void onEvent(AjaxRequestTarget target)
-	{
-		try
-		{
-			ServletWebRequest request = (ServletWebRequest)getComponent().getRequest();
-			final MultipartServletWebRequest multipartWebRequest = request.newMultipartWebRequest(
-				getMaxSize(), getComponent().getPage().getId());
-			multipartWebRequest.setFileMaxSize(getFileMaxSize());
-			multipartWebRequest.parseFileParts();
-
-			// TODO: Can't this be detected from header?
-			getComponent().getRequestCycle().setRequest(multipartWebRequest);
-			
-			ArrayList<FileUpload> fileUploads = new ArrayList<>();
-
-			// Get the item for the path
-			final List<FileItem> fileItems = multipartWebRequest.getFile(parameterName);
-
-			if (fileItems != null)
-			{
-				for (FileItem item : fileItems)
-				{
-					fileUploads.add(new FileUpload(item));
-				}
-			}
-			
-			onFileUpload(target, fileUploads);
-		}
-		catch (final FileUploadException fux)
-		{
-			onError(fux);
-		}
-	}
-
-	public Bytes getMaxSize()
-	{
-		return maxSize;
-	}
-
-	public void setMaxSize(Bytes maxSize)
-	{
-		Args.notNull(maxSize, "maxSize");
-		this.maxSize = maxSize;
-	}
-
-	public Bytes getFileMaxSize()
-	{
-		return fileMaxSize;
-	}
-
-	public void setFileMaxSize(Bytes fileMaxSize)
-	{
-		this.fileMaxSize = fileMaxSize;
-	}
-	
-	protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files)
-	{
-	}
-
-	protected void onError(FileUploadException fux)
-	{
-	}
-}
\ No newline at end of file