You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by re...@apache.org on 2020/02/26 11:46:54 UTC

[wicket] branch feature/reiern70/WICKET-6750-aborting-ajax-requests-take-II updated (c569f71 -> 46c656f)

This is an automated email from the ASF dual-hosted git repository.

reiern70 pushed a change to branch feature/reiern70/WICKET-6750-aborting-ajax-requests-take-II
in repository https://gitbox.apache.org/repos/asf/wicket.git.


 discard c569f71  [WICKET-6750] add a request monitor class that can be used operate in on AJAX request.
    omit 90556bd  [WICKET-6750] improve comment
    omit 46bc76f  [WICKET-6750] allow to abort currently executing request for a given Ajax channel + example
     new 46c656f  [WICKET-6750] allow to abort currently executing request for a given Ajax channel + example

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (c569f71)
            \
             N -- N -- N   refs/heads/feature/reiern70/WICKET-6750-aborting-ajax-requests-take-II (46c656f)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:


[wicket] 01/01: [WICKET-6750] allow to abort currently executing request for a given Ajax channel + example

Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

reiern70 pushed a commit to branch feature/reiern70/WICKET-6750-aborting-ajax-requests-take-II
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit 46c656f82e919af81ffd673595c936141d371314
Author: reiern70 <re...@gmail.com>
AuthorDate: Sun Feb 23 09:08:57 2020 +0200

    [WICKET-6750] allow to abort currently executing request for a given Ajax channel + example
---
 .../wicket/ajax/res/js/wicket-ajax-jquery.js       | 52 ++++++++++++++++++++++
 .../examples/ajax/builtin/FileUploadPage.html      |  2 +-
 .../examples/ajax/builtin/FileUploadPage.java      | 49 +++++++++++++++++---
 3 files changed, 97 insertions(+), 6 deletions(-)

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 3ee9f3b..91125f1 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
@@ -336,6 +336,58 @@
 	Wicket.ChannelManager.FunctionsExecuter = FunctionsExecuter;
 
 	/**
+	 * Channel manager maintains a map of channels.
+	 */
+	Wicket.AjaxRequestMonitor = Wicket.Class.create();
+
+	Wicket.AjaxRequestMonitor.prototype = {
+		initialize: function () {
+			this.requests = [];
+		},
+
+		init: function() {
+			var requests = this.requests;
+			Wicket.Event.subscribe('/ajax/call/beforeSend',
+				function(jqEvent, attributes, jqXHR, errorThrown, textStatus) {
+					requests [attributes.ch] = jqXHR;
+				}
+			);
+
+			Wicket.Event.subscribe('/ajax/call/complete',
+				function(jqEvent, attributes, jqXHR, errorThrown, textStatus) {
+					delete requests[attributes.ch];
+				}
+			);
+		},
+
+		getReuest: function (channel) {
+			var channelName = channel;
+			// (ajax channel)
+			if (typeof (channelName) !== 'string') {
+				channelName = '0|s';
+			}
+			return this.requests[channelName];
+
+		},
+
+		abortRequest: function (channel) {
+			var channelName = channel;
+			// (ajax channel)
+			if (typeof(channelName) !== 'string') {
+				channelName = '0|s';
+			}
+			var jqXHR = getReuest(channelName);
+			if (jqXHR) {
+				try {
+					jqXHR.abort();
+				} catch (exception) {
+					Wicket.Log.error("Couldn't abort current AJAX request:", exception);
+				}
+			}
+		}
+	};
+
+	/**
 	 * The Ajax.Request class encapsulates a XmlHttpRequest.
 	 */
 	Wicket.Ajax = {};
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 3d54b24..d95be48 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
@@ -12,7 +12,7 @@ Demonstrates Wicket's ability to transparently handle multipart forms via AJAX.<
 	Text field: <input wicket:id="text" type="text"/><br/>
 	File field: <input wicket:id="file" type="file"/> (<span wicket:id="max"></span> max)<br/><br/>
 	<div wicket:id="progress"></div>
-	<input type="submit" value="Regular Submit"/> <input wicket:id="ajaxSubmit" type="button" value="Ajax Submit"/>
+	<input type="submit" value="Regular Submit"/> <input wicket:id="ajaxSubmit" type="button" value="Ajax Submit"/> <a wicket:id="cancelUpload">Cancel AJAX upload</a>
 </form>
 
 <div wicket:id="drop" class="drop-zone">
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 598efee..448537f 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,9 +21,13 @@ 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.attributes.AjaxRequestAttributes;
 import org.apache.wicket.ajax.markup.html.form.AjaxButton;
 import org.apache.wicket.extensions.ajax.AjaxFileDropBehavior;
 import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar;
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
 import org.apache.wicket.markup.html.WebMarkupContainer;
 import org.apache.wicket.markup.html.basic.Label;
 import org.apache.wicket.markup.html.form.Form;
@@ -46,6 +50,7 @@ public class FileUploadPage extends BasePage
 
 	private final FileUploadField file;
 	private final TextField<String> text;
+	private WebMarkupContainer cancelUpload;
 
 	/**
 	 * Constructor
@@ -79,7 +84,8 @@ public class FileUploadPage extends BasePage
 				}
 			}
 		};
-		form.setMaxSize(Bytes.megabytes(1));
+		///form.setMaxSize(Bytes.megabytes(1));
+		form.setMaxSize(Bytes.megabytes(500));
 		add(form);
 
 		// create a textfield to demo non-file content
@@ -105,6 +111,7 @@ public class FileUploadPage extends BasePage
 
 				// ajax-update the feedback panel
 				target.add(feedback);
+				target.appendJavaScript(hideCancelUploadButtonScript());
 			}
 
 			@Override
@@ -112,10 +119,30 @@ public class FileUploadPage extends BasePage
 			{
 				// update feedback to display errors
 				target.add(feedback);
+				target.appendJavaScript(hideCancelUploadButtonScript());
 			}
 
+			@Override
+			public void renderHead(IHeaderResponse response)
+			{
+				String script = "$('#" + getMarkupId() +"').on('click', function() { "+ showCancelUploadButtonScript() +" });";
+				response.render(OnDomReadyHeaderItem.forScript(script));
+				response.render(OnDomReadyHeaderItem.forScript(hideCancelUploadButtonScript()));
+			}
 		});
-		
+
+		cancelUpload = new WebMarkupContainer("cancelUpload")
+		{
+			@Override
+			public void renderHead(IHeaderResponse response)
+			{
+				String script = "$('#" + cancelUpload.getMarkupId() +"').on('click', function() { window.requestMonitor.abortRequest(); });";
+				response.render(OnDomReadyHeaderItem.forScript(script));
+			}
+		};
+		cancelUpload.setOutputMarkupId(true);
+		form.add(cancelUpload);
+
 		WebMarkupContainer drop = new WebMarkupContainer("drop");
 		drop.add(new AjaxFileDropBehavior() {
 			protected void onFileUpload(AjaxRequestTarget target, List<FileUpload> files) {
@@ -133,17 +160,29 @@ public class FileUploadPage extends BasePage
 				    }
 				}
 				
-				target.add(feedback);
+				target.add(feedback, cancelUpload);
 			}
 			
 			@Override
 			protected void onError(AjaxRequestTarget target, FileUploadException fux)
 			{
 				info(fux.getMessage());
-				
-				target.add(feedback);				
+				target.add(feedback,cancelUpload);
 			}
 		});
 		add(drop);
 	}
+
+	@Override
+	public void renderHead(IHeaderResponse response) {
+		response.render(OnDomReadyHeaderItem.forScript("window.requestMonitor = new Wicket.AjaxRequestMonitor(); window.requestMonitor.init();"));
+	}
+
+	private String showCancelUploadButtonScript() {
+		return "$('#" + cancelUpload.getMarkupId() + "').show();";
+	}
+
+	private String hideCancelUploadButtonScript() {
+		return "$('#" + cancelUpload.getMarkupId() + "').hide();";
+	}
 }