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 2021/02/20 12:03:50 UTC

[wicket] branch WICKET-6868-ajax-submit-onsubmit created (now f043d06)

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

svenmeier pushed a change to branch WICKET-6868-ajax-submit-onsubmit
in repository https://gitbox.apache.org/repos/asf/wicket.git.


      at f043d06  WICKET-6868 ajax submit allow calling onsubmit on form

This branch includes the following new commits:

     new f043d06  WICKET-6868 ajax submit allow calling onsubmit on form

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.



[wicket] 01/01: WICKET-6868 ajax submit allow calling onsubmit on form

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

svenmeier pushed a commit to branch WICKET-6868-ajax-submit-onsubmit
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit f043d0692728902e2f5f346cb37606e85c90d93c
Author: Sven Meier <sv...@apache.org>
AuthorDate: Sat Feb 20 00:47:34 2021 +0100

    WICKET-6868 ajax submit allow calling onsubmit on form
---
 .../wicket/ajax/form/AjaxFormSubmitBehavior.java   | 22 ++++++++++++++++++++++
 .../wicket/ajax/markup/html/form/AjaxButton.java   | 16 ++++++++++++++++
 .../ajax/markup/html/form/AjaxFallbackButton.java  | 16 ++++++++++++++++
 .../examples/ajax/builtin/FileUploadPage.java      |  9 +++++++++
 4 files changed, 63 insertions(+)

diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java
index 0caca83..7446768 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java
@@ -19,6 +19,7 @@ package org.apache.wicket.ajax.form;
 import org.apache.wicket.Component;
 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.markup.html.form.Button;
@@ -139,6 +140,17 @@ public abstract class AjaxFormSubmitBehavior extends AjaxEventBehavior
 		}
 	}
 
+	/**
+	 * Controls whether or not JS <code>onsubmit()</code> should be called on the submitting form.
+	 * False by default.
+	 * 
+	 * @return true if form's <code>onsubmit()</code> should be called, false otherwise
+	 */
+	protected boolean shouldCallJavaScriptOnsubmit()
+	{
+		return false;
+	}
+
 	@Override
 	protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
 	{
@@ -165,6 +177,16 @@ public abstract class AjaxFormSubmitBehavior extends AjaxEventBehavior
 			String submittingComponentName = submittingComponent.getInputName();
 			attributes.setSubmittingComponentName(submittingComponentName);
 		}
+		
+		if (shouldCallJavaScriptOnsubmit()) {
+			attributes.getAjaxCallListeners().add(new AjaxCallListener() {
+				@Override
+				public CharSequence getPrecondition(Component component)
+				{
+					return String.format("return (Wicket.$('%s').onsubmit || jQuery.noop)();", form.getMarkupId());
+				}
+			});
+		}
 	}
 
 	@Override
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxButton.java b/wicket-core/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxButton.java
index b0b5139..fd59920 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxButton.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxButton.java
@@ -135,6 +135,12 @@ public abstract class AjaxButton extends Button
 
 				AjaxButton.this.updateAjaxAttributes(attributes);
 			}
+			
+			@Override
+			protected boolean shouldCallJavaScriptOnsubmit()
+			{
+				return AjaxButton.this.shouldCallJavaScriptOnsubmit();
+			}
 
 			@Override
 			public boolean getDefaultProcessing()
@@ -150,6 +156,16 @@ public abstract class AjaxButton extends Button
 		};
 	}
 
+	/**
+	 * Controls whether or not JS <code>onsubmit()</code> should be called on the submitting form.
+	 * False by default.
+	 * 
+	 * @return true if form's <code>onsubmit()</code> should be called, false otherwise
+	 */
+	protected boolean shouldCallJavaScriptOnsubmit() {
+		return false;
+	}
+
 	protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
 	{
 	}
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxFallbackButton.java b/wicket-core/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxFallbackButton.java
index 03ca2a2..eaed28f 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxFallbackButton.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxFallbackButton.java
@@ -116,9 +116,25 @@ public abstract class AjaxFallbackButton extends Button
 			{
 				return AjaxFallbackButton.this.getStatelessHint();
 			}
+			
+			@Override
+			protected boolean shouldCallJavaScriptOnsubmit()
+			{
+				return AjaxFallbackButton.this.shouldCallJavaScriptOnsubmit();
+			}
 		};
 	}
 
+	/**
+	 * Controls whether or not JS <code>onsubmit()</code> should be called on the submitting form.
+	 * False by default.
+	 * 
+	 * @return true if form's <code>onsubmit()</code> should be called, false otherwise
+	 */
+	protected boolean shouldCallJavaScriptOnsubmit() {
+		return false;
+	}
+
 	protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
 	{
 	}
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..d33c9bd 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
@@ -98,6 +98,15 @@ public class FileUploadPage extends BasePage
 		{
 			private static final long serialVersionUID = 1L;
 
+			/**
+			 * Need to call onsubmit to initiate progressbar. 
+			 */
+			@Override
+			protected boolean shouldCallJavaScriptOnsubmit()
+			{
+				return true;
+			}
+			
 			@Override
 			protected void onSubmit(AjaxRequestTarget target)
 			{