Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.ajax;
 
The base class for Wicket's default AJAX implementation.

Author(s):
Igor Vaynberg (ivaynberg)
Since:
1.2
 
 public abstract class AbstractDefaultAjaxBehavior extends AbstractAjaxBehavior
 {
 	private static final long serialVersionUID = 1L;

reference to the default indicator gif file.
 
 	public static final ResourceReference INDICATOR = new ResourceReference(
 		AbstractDefaultAjaxBehavior.class"indicator.gif");

reference to the default ajax debug support javascript file.
 
 		AbstractDefaultAjaxBehavior.class"wicket-ajax-debug.js");

Subclasses should call super.onBind()

 
 	protected void onBind()
 	{
 	}

 
 	public void renderHead(IHeaderResponse response)
 	{
 		super.renderHead(response);
 
 		final IDebugSettings debugSettings = Application.get().getDebugSettings();
 
 
 		if (debugSettings.isAjaxDebugModeEnabled())
 		{
 			response.renderJavascript("wicketAjaxDebugEnable=true;""wicket-ajax-debug-enable");
 		}
 
 		RequestContext context = RequestContext.get();
 		if (context.isPortletRequest())
 		{
 			response.renderJavascript("Wicket.portlet=true""wicket-ajax-portlet-flag");
 		}
 	}

Returns:
ajax call decorator used to decorate the call generated by this behavior or null for none
 
 	{
 		return null;
 	}

Returns:
javascript that will generate an ajax GET request to this behavior
	{
		return getCallbackScript(false);
	}

Parameters:
onlyTargetActivePage if true the callback to this behavior will be ignore if the page is not the last one the user accessed
Returns:
javascript that will generate an ajax GET request to this behavior *
	protected CharSequence getCallbackScript(boolean onlyTargetActivePage)
	{
		return generateCallbackScript("wicketAjaxGet('" + getCallbackUrl(onlyTargetActivePage) +
			"'");
	}

Returns:
an optional javascript expression that determines whether the request will actually execute (in form of return XXX;);
	{
		if (getComponent() instanceof Page)
		{
			return "return true;";
		}
		else
		{
			return "return Wicket.$('" + getComponent().getMarkupId() + "') != null;";
		}
	}

Returns:
javascript that will run when the ajax call finishes with an error status
	{
		return null;
	}

Returns:
javascript that will run when the ajax call finishes successfully
	{
		return null;
	}

Returns javascript that performs an ajax callback to this behavior. The script is decorated by the ajax callback decorator from getAjaxCallDecorator().

Parameters:
partialCall Javascript of a partial call to the function performing the actual ajax callback. Must be in format function(params, with signature function(params, onSuccessHandler, onFailureHandler. Example: wicketAjaxGet('callbackurl'
Returns:
script that performs ajax callback to this behavior
	protected CharSequence generateCallbackScript(final CharSequence partialCall)
	{
		final CharSequence onSuccessScript = getSuccessScript();
		final CharSequence onFailureScript = getFailureScript();
		final CharSequence precondition = getPreconditionScript();
		String indicatorId = findIndicatorId();
		CharSequence success = (onSuccessScript == null) ? "" : onSuccessScript;
		CharSequence failure = (onFailureScript == null) ? "" : onFailureScript;
		if (decorator != null)
		{
			success = decorator.decorateOnSuccessScript(success);
		}
		if (!Strings.isEmpty(indicatorId))
		{
			String hide = ";wicketHide('" + indicatorId + "');";
			success = success + hide;
			failure = failure + hide;
		}
		if (decorator != null)
		{
			failure = decorator.decorateOnFailureScript(failure);
		}
		buff.append(partialCall);
		if (success.length() == 0)
		{
			buff.append(",null");
		}
		else
		{
			buff.append(",function(){").append(success).append("}.bind(this)");
		}
		if (failure.length() == 0)
		{
			buff.append(",null");
		}
		else
		{
			buff.append(",function() { ").append(failure).append("}.bind(this)");
		}
		if (precondition != null)
		{
			buff.append(", function() {");
			buff.append(precondition);
			buff.append("}.bind(this)");
		}
		String channel = getChannelName();
		if (channel != null)
		{
			if (precondition == null)
			{
				buff.append(", null");
			}
			buff.append(", '");
			buff.append(channel);
			buff.append("'");
		}
		buff.append(");");
		CharSequence call = buff;
		if (!Strings.isEmpty(indicatorId))
		{
			call = new AppendingStringBuffer("wicketShow('").append(indicatorId)
				.append("');")
				.append(call);
		}
		if (decorator != null)
		{
			call = decorator.decorateScript(call);
		}
		return call;
	}
	protected String getChannelName()
	{
		return null;
	}

Finds the markup id of the indicator. The default search order is: component, behavior, component's parent hieararchy.

Returns:
markup id or null if no indicator found
	protected String findIndicatorId()
	{
		if (getComponent() instanceof IAjaxIndicatorAware)
		{
		}
		if (this instanceof IAjaxIndicatorAware)
		{
		}
		while (parent != null)
		{
			if (parent instanceof IAjaxIndicatorAware)
			{
			}
			parent = parent.getParent();
		}
		return null;
	}

	public final void onRequest()
	{
		RequestCycle.get().setRequestTarget(target);
		respond(target);
	}

Parameters:
target The AJAX target
	// TODO rename this to onEvent or something? respond is mostly the same as
	// onRender
	// this is not the case this is still the event handling period. respond is
	// called
	// in the RequestCycle on the AjaxRequestTarget..
	protected abstract void respond(AjaxRequestTarget target);

Wraps the provided javascript with a throttled block. Throttled behaviors only execute once within the given delay even though they are triggered multiple times.

For example, this is useful when attaching an event behavior to the onkeypress event. It is not desirable to have an ajax call made every time the user types so we throttle that call to a desirable delay, such as once per second. This gives us a near real time ability to provide feedback without overloading the server with ajax calls.

Parameters:
script javascript to be throttled
throttleId the id of the throttle to be used. Usually this should remain constant for the same javascript block.
throttleDelay time span within which the javascript block will only execute once
Returns:
wrapped javascript
	public static final CharSequence throttleScript(CharSequence scriptString throttleId,
		Duration throttleDelay)
	{
		if (Strings.isEmpty(script))
		{
			throw new IllegalArgumentException("script cannot be empty");
		}
		if (Strings.isEmpty(throttleId))
		{
			throw new IllegalArgumentException("throttleId cannot be empty");
		}
		if (throttleDelay == null)
		{
			throw new IllegalArgumentException("throttleDelay cannot be null");
		}
		return new AppendingStringBuffer("wicketThrottler.throttle( '").append(throttleId)
			.append("', ")
			.append(throttleDelay.getMilliseconds())
			.append(", function() { ")
			.append(script)
			.append("});");
	}
New to GrepCode? Check out our FAQ X