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 2013/01/10 13:25:29 UTC

git commit: WICKET-4962 let AjaxFormChoiceComponentUpdatingBehavior extend AjaxFormComponentUpdatingBehavior

Updated Branches:
  refs/heads/master c6c6ffbdc -> 84e71742a


WICKET-4962 let AjaxFormChoiceComponentUpdatingBehavior extend
AjaxFormComponentUpdatingBehavior

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

Branch: refs/heads/master
Commit: 84e71742a050a85edb8672f129242fce4595a222
Parents: c6c6ffb
Author: svenmeier <sv...@apache.org>
Authored: Thu Jan 10 13:25:01 2013 +0100
Committer: svenmeier <sv...@apache.org>
Committed: Thu Jan 10 13:25:01 2013 +0100

----------------------------------------------------------------------
 .../AjaxFormChoiceComponentUpdatingBehavior.java   |  109 ++++-----------
 .../AjaxFormChoiceComponentUpdatingBehavior.js     |   20 +---
 .../form/AjaxFormComponentUpdatingBehavior.java    |   31 +++-
 3 files changed, 56 insertions(+), 104 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/84e71742/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.java
index 14eca56..1017c46 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.java
@@ -18,12 +18,10 @@ package org.apache.wicket.ajax.form;
 
 import org.apache.wicket.Component;
 import org.apache.wicket.WicketRuntimeException;
-import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
-import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.attributes.AjaxCallListener;
 import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
 import org.apache.wicket.markup.head.IHeaderResponse;
 import org.apache.wicket.markup.head.JavaScriptHeaderItem;
-import org.apache.wicket.markup.head.OnLoadHeaderItem;
 import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
 import org.apache.wicket.markup.html.form.CheckGroup;
 import org.apache.wicket.markup.html.form.FormComponent;
@@ -45,61 +43,50 @@ import org.apache.wicket.request.resource.ResourceReference;
  * @see RadioGroup
  * @see CheckGroup
  */
-public abstract class AjaxFormChoiceComponentUpdatingBehavior extends AbstractDefaultAjaxBehavior
+public abstract class AjaxFormChoiceComponentUpdatingBehavior extends
+	AjaxFormComponentUpdatingBehavior
 {
 	private static final ResourceReference CHOICE_JS = new JavaScriptResourceReference(
 		AjaxFormChoiceComponentUpdatingBehavior.class, "AjaxFormChoiceComponentUpdatingBehavior.js");
 
 	private static final long serialVersionUID = 1L;
 
+	/**
+	 * Construct.
+	 */
+	public AjaxFormChoiceComponentUpdatingBehavior()
+	{
+		super("click");
+	}
+
 	@Override
 	public void renderHead(Component component, IHeaderResponse response)
 	{
 		super.renderHead(component, response);
 
 		response.render(JavaScriptHeaderItem.forReference(CHOICE_JS));
-
-		String onLoadScript = String.format("Wicket.Choice.attach('%s', '%s', %s)",
-			component.getMarkupId(), getFormComponent().getInputName(),
-			renderAjaxAttributes(component));
-		response.render(OnLoadHeaderItem.forScript(onLoadScript));
 	}
 
 	@Override
 	protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
 	{
 		super.updateAjaxAttributes(attributes);
-		attributes.setEventNames("click");
-		attributes.setAllowDefault(true);
-	}
-
-	/**
-	 * Listener invoked on the ajax request. This listener is invoked after the component's model
-	 * has been updated.
-	 * 
-	 * @param target
-	 *            the current request handler
-	 */
-	protected abstract void onUpdate(AjaxRequestTarget target);
 
-	/**
-	 * Called to handle any error resulting from updating form component. Errors thrown from
-	 * {@link #onUpdate(org.apache.wicket.ajax.AjaxRequestTarget)} will not be caught here.
-	 * 
-	 * The RuntimeException will be null if it was just a validation or conversion error of the
-	 * FormComponent
-	 * 
-	 * @param target
-	 *            the current request handler
-	 * @param e
-	 *            the error that occurred while updating the component
-	 */
-	protected void onError(AjaxRequestTarget target, RuntimeException e)
-	{
-		if (e != null)
+		attributes.getAjaxCallListeners().add(new AjaxCallListener()
 		{
-			throw e;
-		}
+			private static final long serialVersionUID = 1L;
+
+			@Override
+			public CharSequence getPrecondition(Component component)
+			{
+				return String.format("return Wicket.Choice.acceptInput('%s', attrs)",
+					getFormComponent().getInputName());
+			}
+		});
+
+		attributes.getDynamicExtraParameters().add(
+			String.format("return Wicket.Choice.getInputValues('%s', attrs)",
+				getFormComponent().getInputName()));
 	}
 
 	/**
@@ -111,57 +98,19 @@ public abstract class AjaxFormChoiceComponentUpdatingBehavior extends AbstractDe
 	{
 		super.onBind();
 
-		if (!AjaxFormChoiceComponentUpdatingBehavior.appliesTo(getComponent()))
-		{
-			throw new WicketRuntimeException("Behavior " + getClass().getName() +
-				" can only be added to an instance of a RadioChoice/CheckboxChoice/RadioGroup/CheckGroup");
-		}
-
 		if (getComponent() instanceof RadioGroup || getComponent() instanceof CheckGroup)
 		{
 			getComponent().setRenderBodyOnly(false);
 		}
 	}
 
-	/**
-	 * 
-	 * @return FormComponent
-	 */
-	protected final FormComponent<?> getFormComponent()
-	{
-		return (FormComponent<?>)getComponent();
-	}
-
-	/**
-	 * 
-	 * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(org.apache.wicket.ajax.AjaxRequestTarget)
-	 */
 	@Override
-	protected final void respond(final AjaxRequestTarget target)
+	protected void checkComponent(FormComponent<?> component)
 	{
-		final FormComponent<?> formComponent = getFormComponent();
-
-		try
-		{
-			formComponent.inputChanged();
-			formComponent.validate();
-			if (formComponent.hasErrorMessage())
-			{
-				formComponent.invalid();
-
-				onError(target, null);
-			}
-			else
-			{
-				formComponent.valid();
-				formComponent.updateModel();
-				onUpdate(target);
-			}
-		}
-		catch (RuntimeException e)
+		if (!AjaxFormChoiceComponentUpdatingBehavior.appliesTo(getComponent()))
 		{
-			onError(target, e);
-
+			throw new WicketRuntimeException("Behavior " + getClass().getName() +
+				" can only be added to an instance of a RadioChoice/CheckboxChoice/RadioGroup/CheckGroup");
 		}
 	}
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/84e71742/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.js
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.js b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.js
index cc00654..f2da088 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.js
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.js
@@ -38,20 +38,19 @@
 
 			var srcElement = attrs.event.target;
 
-			return srcElement.name === name;
+			return (srcElement.name === name);
 		};
 
 		/**
 		 * Get all checked input values.
 		 *
-		 * @param markupId markup id of choice
 		 * @param name input name of choice
 		 * @param attrs ajax attributes
 		 */
-		Wicket.Choice.getInputValues = function(markupId, name, attrs) {
+		Wicket.Choice.getInputValues = function(name, attrs) {
 			var result = [], srcElement = attrs.event.target;
 
-			var inputNodes = Wicket.$(markupId).getElementsByTagName("input");
+			var inputNodes = Wicket.$(attrs.c).getElementsByTagName("input");
 			for (var i = 0 ; i < inputNodes.length ; i ++) {
 				var inputNode = inputNodes[i];
 
@@ -68,18 +67,5 @@
 
 			return result;
 		};
-
-		/**
-		 * Attach to a choice.
-		 *
-		 * @param markupId markup id of choice
-		 * @param input name of choice
-		 * @attrs ajax attributes
-		 */
-		Wicket.Choice.attach = function(markupId, name, attrs) {
-			attrs.pre = (attrs.pre || []).concat([ function(attributes) { var pre = Wicket.Choice.acceptInput(name, attributes); return pre; } ]);
-			attrs.dep = (attrs.dep || []).concat([ function(attributes) { var deps = Wicket.Choice.getInputValues(markupId, name, attributes); return deps; } ]);
-			Wicket.Ajax.post(attrs);
-		};
 	}
 })();

http://git-wip-us.apache.org/repos/asf/wicket/blob/84e71742/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormComponentUpdatingBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormComponentUpdatingBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormComponentUpdatingBehavior.java
index 23c7edf..e78c67d 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormComponentUpdatingBehavior.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormComponentUpdatingBehavior.java
@@ -17,6 +17,7 @@
 package org.apache.wicket.ajax.form;
 
 import org.apache.wicket.Application;
+import org.apache.wicket.Component;
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.ajax.AjaxEventBehavior;
 import org.apache.wicket.ajax.AjaxRequestTarget;
@@ -72,18 +73,34 @@ public abstract class AjaxFormComponentUpdatingBehavior extends AjaxEventBehavio
 	{
 		super.onBind();
 
-		if (!(getComponent() instanceof FormComponent))
+		Component component = getComponent();
+		if (!(component instanceof FormComponent))
 		{
 			throw new WicketRuntimeException("Behavior " + getClass().getName() +
 				" can only be added to an instance of a FormComponent");
 		}
-		else if (Application.get().usesDevelopmentConfig() &&
-			AjaxFormChoiceComponentUpdatingBehavior.appliesTo(getComponent()))
+
+		checkComponent((FormComponent<?>)component);
+	}
+
+	/**
+	 * Check the component this behavior is bound to.
+	 * <p>
+	 * Logs a warning in development mode when an {@link AjaxFormChoiceComponentUpdatingBehavior}
+	 * should be used.
+	 * 
+	 * @param component
+	 *            bound component
+	 */
+	protected void checkComponent(FormComponent<?> component)
+	{
+		if (Application.get().usesDevelopmentConfig() &&
+			AjaxFormChoiceComponentUpdatingBehavior.appliesTo(component))
 		{
 			log.warn(String.format(
 				"AjaxFormComponentUpdatingBehavior is not supposed to be added in the form component at path: \"%s\". "
 					+ "Use the AjaxFormChoiceComponentUpdatingBehavior instead, that is meant for choices/groups that are not one component in the html but many",
-				getComponent().getPageRelativePath()));
+				component.getPageRelativePath()));
 		}
 	}
 
@@ -171,7 +188,7 @@ public abstract class AjaxFormComponentUpdatingBehavior extends AjaxEventBehavio
 	 * has been updated.
 	 * 
 	 * @param target
-	 *      the current request handler
+	 *            the current request handler
 	 */
 	protected abstract void onUpdate(AjaxRequestTarget target);
 
@@ -183,9 +200,9 @@ public abstract class AjaxFormComponentUpdatingBehavior extends AjaxEventBehavio
 	 * FormComponent
 	 * 
 	 * @param target
-	 *       the current request handler
+	 *            the current request handler
 	 * @param e
-	 *      the error that occurred during the update of the component
+	 *            the error that occurred during the update of the component
 	 */
 	protected void onError(AjaxRequestTarget target, RuntimeException e)
 	{