You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by kn...@apache.org on 2008/08/27 14:40:37 UTC

svn commit: r689461 - in /wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng: AjaxBehavior.java AjaxRequestAttributes.java AjaxRequestAttributesImpl.java js/wicket-ajax-ng.js markup/html/AjaxLink.java

Author: knopp
Date: Wed Aug 27 05:40:37 2008
New Revision: 689461

URL: http://svn.apache.org/viewvc?rev=689461&view=rev
Log:
remove AjaxRequestAttributesImpl, asynchronous preconditions

Removed:
    wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributesImpl.java
Modified:
    wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxBehavior.java
    wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributes.java
    wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/js/wicket-ajax-ng.js
    wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/markup/html/AjaxLink.java

Modified: wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxBehavior.java
URL: http://svn.apache.org/viewvc/wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxBehavior.java?rev=689461&r1=689460&r2=689461&view=diff
==============================================================================
--- wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxBehavior.java (original)
+++ wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxBehavior.java Wed Aug 27 05:40:37 2008
@@ -324,6 +324,6 @@
 	 */
 	public AjaxRequestAttributes getAttributes()
 	{
-		return new AjaxRequestAttributesImpl();
+		return new AjaxRequestAttributes();
 	}
 }

Modified: wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributes.java
URL: http://svn.apache.org/viewvc/wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributes.java?rev=689461&r1=689460&r2=689461&view=diff
==============================================================================
--- wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributes.java (original)
+++ wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/AjaxRequestAttributes.java Wed Aug 27 05:40:37 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.wicket.ajaxng;
 
+import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.wicket.markup.html.form.Form;
@@ -55,18 +56,70 @@
  * 
  * </dl>
  * 
+ * This class supports delegating the calls to another {@link AjaxRequestAttributes} instance if one
+ * is specified. To extend attributes from behavior or component the following pattern can be used:
+ * 
+ * <pre>
+ * // add a precondition to super attirbutes
+ * class MyBehavior extends AjaxBehavior
+ * {
+ * 	public AjaxRequestAttributes getAttributes()
+ *          {
+ *              return new AjaxRequestAttributesImpl(super.getAttributes) 
+ *              {
+ *                  public FunctionList getPreconditions()
+ *                  {
+ *                      return super.getPreconditions().add(&quot;function(requestQueueItem) { return true; }&quot;;);
+ *                  }
+ *              }
+ *          }
+ * }
+ * </pre>
+ * 
  * 
  * @author Matej Knopp
  */
-public interface AjaxRequestAttributes
+public class AjaxRequestAttributes
 {
+	private final AjaxRequestAttributes delegate;
+
+	/**
+	 * Construct.
+	 * 
+	 * @param delegate
+	 */
+	public AjaxRequestAttributes(AjaxRequestAttributes delegate)
+	{
+		this.delegate = delegate;
+	}
+
+	/**
+	 * 
+	 * Construct.
+	 */
+	public AjaxRequestAttributes()
+	{
+		this(null);
+	}
+
 	/**
 	 * Form instance if the AJAX request should submit a form or <code>null</code> if the request
 	 * doesn't involve form submission.
 	 * 
 	 * @return form instance or <code>null</code>
 	 */
-	Form<?> getForm();
+	public Form<?> getForm()
+	{
+		if (delegate != null)
+		{
+			return delegate.getForm();
+		}
+		else
+		{
+			return null;
+		}
+	}
+
 
 	/**
 	 * Returns whether the form submit is multipart.
@@ -76,7 +129,17 @@
 	 * 
 	 * @return <code>true</code> if the form submit should be multipart, false otherwise
 	 */
-	Boolean isMultipart();
+	public Boolean isMultipart()
+	{
+		if (delegate != null)
+		{
+			return delegate.isMultipart();
+		}
+		else
+		{
+			return null;
+		}
+	}
 
 	/**
 	 * Timeout in milliseconds for the AJAX request. This only involves the actual communication and
@@ -85,7 +148,17 @@
 	 * 
 	 * @return request timeout in milliseconds or <code>null<code> for default timeout
 	 */
-	Integer getRequesTimeout();
+	public Integer getRequesTimeout()
+	{
+		if (delegate != null)
+		{
+			return delegate.getRequesTimeout();
+		}
+		else
+		{
+			return null;
+		}
+	}
 
 	/**
 	 * Timeout for the response processing. In case the response processing takes more than the
@@ -94,7 +167,17 @@
 	 * 
 	 * @return processing timeout in milliseconds or <code>null</code> for default timeout
 	 */
-	Integer getProcessingTimeout();
+	public Integer getProcessingTimeout()
+	{
+		if (delegate != null)
+		{
+			return delegate.getProcessingTimeout();
+		}
+		else
+		{
+			return null;
+		}
+	}
 
 	/**
 	 * Optional string identifying related items in request queue. Used to identify previous items
@@ -107,7 +190,17 @@
 	 * 
 	 * @return token string or <code>null</code>
 	 */
-	String getToken();
+	public String getToken()
+	{
+		if (delegate != null)
+		{
+			return delegate.getToken();
+		}
+		else
+		{
+			return null;
+		}
+	}
 
 	/**
 	 * If there are previous items with same token in the queue they will be removed if
@@ -123,7 +216,17 @@
 	 * 
 	 * @return boolean value or <code>null</code>
 	 */
-	Boolean isRemovePrevious();
+	public Boolean isRemovePrevious()
+	{
+		if (delegate != null)
+		{
+			return delegate.isRemovePrevious();
+		}
+		else
+		{
+			return null;
+		}
+	}
 
 	/**
 	 * Limits adding items with same token to at most one item per n milliseconds where n is the
@@ -136,7 +239,17 @@
 	 * 
 	 * @return throttling timeout in milliseconds or <code>null</code>
 	 */
-	Integer getThrottle();
+	public Integer getThrottle()
+	{
+		if (delegate != null)
+		{
+			return delegate.getThrottle();
+		}
+		else
+		{
+			return null;
+		}
+	}
 
 	/**
 	 * Only applicable when throttling is enabled. Defaults to <code>false</code>. Causes the
@@ -149,7 +262,17 @@
 	 * 
 	 * @return boolean value or <code>null</code>
 	 */
-	Boolean isThrottlePostpone();
+	public Boolean isThrottlePostpone()
+	{
+		if (delegate != null)
+		{
+			return delegate.isThrottlePostpone();
+		}
+		else
+		{
+			return null;
+		}
+	}
 
 	/**
 	 * Array of javascript functions that are invoked before the request executes. The functions
@@ -175,7 +298,20 @@
 	 * 
 	 * @return FunctionList or <code>null</code>
 	 */
-	FunctionList getPreconditions();
+	public FunctionList getPreconditions()
+	{
+		FunctionList result = null;
+		if (delegate != null)
+		{
+			result = delegate.getPreconditions();
+		}
+		if (result == null)
+		{
+			result = new FunctionList();
+		}
+		return result;
+	}
+
 
 	/**
 	 * Array of javascript functions that are invoked before the actual AJAX request. This
@@ -195,7 +331,19 @@
 	 * 
 	 * @return FunctionList or <code>null</code>
 	 */
-	FunctionList getBeforeHandlers();
+	public FunctionList getBeforeHandlers()
+	{
+		FunctionList result = null;
+		if (delegate != null)
+		{
+			result = delegate.getBeforeHandlers();
+		}
+		if (result == null)
+		{
+			result = new FunctionList();
+		}
+		return result;
+	}
 
 	/**
 	 * Array of javascript functions that are invoked after the request is successfully processed.
@@ -212,7 +360,19 @@
 	 * 
 	 * @return FunctionList or <code>null</code>
 	 */
-	FunctionList getSuccessHandlers();
+	public FunctionList getSuccessHandlers()
+	{
+		FunctionList result = null;
+		if (delegate != null)
+		{
+			result = delegate.getSuccessHandlers();
+		}
+		if (result == null)
+		{
+			result = new FunctionList();
+		}
+		return result;
+	}
 
 	/**
 	 * Array of javascript functions that are invoked when an unexpected error happens during the
@@ -234,7 +394,19 @@
 	 * 
 	 * @return FunctionList or <code>null</code>
 	 */
-	FunctionList getErrorHandlers();
+	public FunctionList getErrorHandlers()
+	{
+		FunctionList result = null;
+		if (delegate != null)
+		{
+			result = delegate.getErrorHandlers();
+		}
+		if (result == null)
+		{
+			result = new FunctionList();
+		}
+		return result;
+	}
 
 	/**
 	 * Map that contains additional URL arguments. These will be appended to the request URL. This
@@ -242,7 +414,19 @@
 	 * 
 	 * @return Map with additional URL arguments or <code>null</code>
 	 */
-	Map<String, Object> getUrlArguments();
+	public Map<String, Object> getUrlArguments()
+	{
+		Map<String, Object> result = null;
+		if (delegate != null)
+		{
+			result = delegate.getUrlArguments();
+		}
+		if (result == null)
+		{
+			result = new HashMap<String, Object>();
+		}
+		return result;
+	}
 
 	/**
 	 * Array of javascript functions that produce additional URL arguments. Each of the functions
@@ -261,7 +445,19 @@
 	 * 
 	 * @return FunctionList or <code>null</code>
 	 */
-	FunctionList getUrlArgumentMethods();
+	public FunctionList getUrlArgumentMethods()
+	{
+		FunctionList result = null;
+		if (delegate != null)
+		{
+			result = delegate.getUrlArgumentMethods();
+		}
+		if (result == null)
+		{
+			result = new FunctionList();
+		}
+		return result;
+	}
 
 	/**
 	 * Array of javascript functions invoked when a <code>RequestQueueItem</code> instance is
@@ -269,7 +465,19 @@
 	 * 
 	 * @return FunctionList or <code>null</code>
 	 */
-	FunctionList getRequestQueueItemCreationListeners();
+	public FunctionList getRequestQueueItemCreationListeners()
+	{
+		FunctionList result = null;
+		if (delegate != null)
+		{
+			result = delegate.getRequestQueueItemCreationListeners();
+		}
+		if (result == null)
+		{
+			result = new FunctionList();
+		}
+		return result;
+	}
 
 	/**
 	 * Only applies for event behaviors. Returns whether the behavior should allow the default event
@@ -281,5 +489,8 @@
 	 * @return <code>true</code> if the default event handler should be invoked,
 	 *         <code>false</code> otherwise.
 	 */
-	boolean allowDefault();
+	public boolean allowDefault()
+	{
+		return false;
+	}
 }

Modified: wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/js/wicket-ajax-ng.js
URL: http://svn.apache.org/viewvc/wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/js/wicket-ajax-ng.js?rev=689461&r1=689460&r2=689461&view=diff
==============================================================================
--- wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/js/wicket-ajax-ng.js (original)
+++ wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/js/wicket-ajax-ng.js Wed Aug 27 05:40:37 2008
@@ -24,7 +24,7 @@
 (function() {
 
 YUI().use('*', function(Y) {
-	
+
 	/*
 	 * YUI Shortcuts 
 	 */
@@ -1540,34 +1540,86 @@
 				}
 			}
 		},
-		checkPreconditions: function() 
-		{			
-			var res = iterateArray(this.attributes.preconditions, bind(function(precondition) 
+		
+		checkPrecondition: function(precondition, continueTrue, continueFalse)
+		{
+			var f = bind(function()
 			{
-				try 
-				{					
-					if (precondition(this) == false)
-					{
-						log.debug("RequestQueue", "Precondition failed - skiping item; Item: ", this, " Precondition: ", precondition);
-						return false;
-					}
-				} 
-				catch (exception) 
+				log.debug("RequestQueue", "Precondition failed - skiping item; Item: ", this, " Precondition: ", precondition);
+				continueFalse();
+			}, this);
+			
+			var async = false;
+			var makeAsync = function()
+			{
+				async = true;
+			};
+			var asyncReturn = function(value)
+			{
+				if (value == false)
 				{
-					log.error("RequestQueue", "Error evaluating precondition ", precondition, "Exception: ", exception);
-					return false;
+					f();
 				}
-			}, this));
-			if (res == null)
+				else
+				{
+					continueTrue();
+				}
+			}
+			
+			var res;
+			try 
+			{					
+				res = precondition(this, makeAsync, asyncReturn)
+			} 
+			catch (exception) 
 			{
-				return true;
+				log.error("RequestQueue", "Error evaluating precondition ", precondition, "Exception: ", exception);
+				async = false;
+				res = false;
 			}
-			else
+			
+			if (async == false)
 			{
-				return false;
+				if (res == false)
+				{
+					f();
+				}
+				else
+				{
+					continueTrue();
+				}
 			}
 		},
 		
+		checkPreconditions: function(continueTrue, continueFalse) 
+		{	
+			var steps = new Array();
+			
+			var preconditions = copyArray(this.attributes.preconditions);
+			
+			// make sure that the element check is the last precondition
+			preconditions.push(defaultPrecondition);
+			
+			var res = iterateArray(this.attributes.preconditions, bind(function(precondition) 
+			{
+				steps.push(bind(function(notify) 
+				{
+					var cTrue = notify;
+					var cFalse = continueFalse;
+					
+					this.checkPrecondition(precondition, cTrue, cFalse);
+				}, this));
+			}, this));
+			
+			steps.push(function(notify)
+			{
+				notify();
+				continueTrue();
+			});
+			
+			new FunctionsExecutor(steps).start();
+		},
+		
 		invokeBeforeHandlers: function()
 		{
 			iterateArray(this.attributes.beforeHandlers, bind(function(handler) 
@@ -2057,7 +2109,7 @@
 			{
 				var i = this.queue.shift();
 				
-				if (i.checkPreconditions())
+				var continueTrue = bind(function()
 				{
 					this.currentItem = i;
 					var s = bind(function() { this.skip(i); }, this);
@@ -2068,12 +2120,15 @@
 					window.setTimeout(s, t);
 					
 					var next = bind(this.next, this);
-					i.execute(next);
-				}
-				else
+					i.execute(next);	
+				}, this);
+				
+				var continueFalse = bind(function()
 				{
 					this.next();
-				}
+				}, this);	
+				
+				i.checkPreconditions(continueTrue, continueFalse);
 			}
 		},		
 		
@@ -2155,7 +2210,7 @@
 		defaultToken: null,
 		defaultRemovePrevious: false,
 		beforeHandlers: [],
-		preconditions: [defaultPrecondition],
+		preconditions: [],
 		successHandlers: [],
 		errorHandlers: [],
 		urlPostProcessors: [],
@@ -2478,7 +2533,7 @@
 		f = null;
 	}
 	
-	window.W = W;		
+	window.W = W;			
 	
 });
 

Modified: wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/markup/html/AjaxLink.java
URL: http://svn.apache.org/viewvc/wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/markup/html/AjaxLink.java?rev=689461&r1=689460&r2=689461&view=diff
==============================================================================
--- wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/markup/html/AjaxLink.java (original)
+++ wicket/sandbox/knopp/experimental/wicket/src/main/java/org/apache/wicket/ajaxng/markup/html/AjaxLink.java Wed Aug 27 05:40:37 2008
@@ -19,7 +19,6 @@
 import org.apache.wicket.Component;
 import org.apache.wicket.ajaxng.AjaxEventBehavior;
 import org.apache.wicket.ajaxng.AjaxRequestAttributes;
-import org.apache.wicket.ajaxng.AjaxRequestAttributesImpl;
 import org.apache.wicket.ajaxng.request.AjaxRequestTarget;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.html.link.AbstractLink;
@@ -84,9 +83,14 @@
 		});
 	}
 
+	/**
+	 * Returns {@link AjaxRequestAttributes} object for this link.
+	 * 
+	 * @return {@link AjaxRequestAttributes} instance
+	 */
 	public AjaxRequestAttributes getAttributes()
 	{
-		return new AjaxRequestAttributesImpl();
+		return new AjaxRequestAttributes();
 	}
 	
 	@Override