/* * 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; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Attributes of an Ajax Request. * *
* Note that some of these attributes represent javascript functions. Those functions get a
* RequestQueueItem
instance as first argument. The instance provides access to
* following properties that the javascript functions can use:
*
attributes
Object contains
* following properties:
* null
if the behavior is attached to pagenull
if not specifiednull
if not specifiedevent
* property contains the actual event instance.
*
*
*
* @author Matej Knopp
*/
public final class AjaxRequestAttributes
{
/**
*
* Construct.
*/
public AjaxRequestAttributes()
{
}
private boolean multipart = false;
private boolean forcePost = false;
private Integer requestTimeout;
private Integer processingTimeout;
private String token;
private boolean removePrevious = false;
private Integer throttle;
private boolean throttlePostpone = false;
private boolean allowDefault = false;
private List
* Note that for multipart AJAX requests a hidden IFRAME will be used and that can have negative
* impact on error detection.
*
* @return true
if the form submit should be multipart, false
* otherwise
*/
public boolean isMultipart()
{
return multipart;
}
/**
* Determines whether the form submit is multipart.
*
*
* Note that for multipart AJAX requests a hidden IFRAME will be used and that can have negative
* impact on error detection.
*
* @param multipart
*/
public void setMultipart(boolean multipart)
{
this.multipart = multipart;
}
/**
* Returns whether the Ajax request should be a POST
regardless of whether a form
* is being submitted.
*
* For a POST
request all URL arguments are submitted as body. This can be useful
* if the URL parameters are longer than maximal URL length.
*
* @return true
if the request should be post, false
otherwise.
*/
public boolean isForcePost()
{
return forcePost;
}
/**
* Determines whether the Ajax request should be a POST
regardless of whether a
* form is being submitted.
*
* For a
* An example of this could be periodically updated component. There is no point of having
* multiple refreshing requests stored in the queue for such component because only the last
* request is relevant. Alternative to this is throttling.
*
* @see #getToken()
* @see #getThrottle()
*
* @return boolean value or
* An example of this could be periodically updated component. There is no point of having
* multiple refreshing requests stored in the queue for such component because only the last
* request is relevant. Alternative to this is throttling.
*
* @param removePrevious
*/
public void setRemovePrevious(boolean removePrevious)
{
this.removePrevious = removePrevious;
}
/**
* Limits adding items with same token to at most one item per n milliseconds where n is the
* return value.
*
* Useful to limit the number of AJAX requests that are triggered by a user action such as
* typing into a text field. Throttle attribute only applies when token is specified.
*
* @see #getToken()
*
* @return throttling timeout in milliseconds or
* Useful to limit the number of AJAX requests that are triggered by a user action such as
* typing into a text field. Throttle attribute only applies when token is specified.
*
* @see #getToken()
*
* @param throttle
*/
public void setThrottle(Integer throttle)
{
this.throttle = throttle;
}
/**
* Only applicable when throttling is enabled. Defaults to
* Example: Event is fired by user typing in a TextField. Throttle value is 2000 (ms), throttle
* postpone is
* Example of single function:
*
*
* Example of single function:
*
*
* Example of single function:
*
*
* Example of single function:
*
*
* Example of single function:
*
*
* This usually decorates the javascript expression that is responsible for creating
*
* Note that not all Ajax behaviors are required to support decorating the expression.
*
* @return list of {@link ExpressionDecorator} .
*/
public ListPOST
request all URL arguments are submitted as body. This can be useful
* if the URL parameters are longer than maximal URL length.
*
* @param forcePost
*/
public void setForcePost(boolean forcePost)
{
this.forcePost = forcePost;
}
/**
* Returns the timeout in milliseconds for the AJAX request. This only involves the actual
* communication and not the processing afterwards. Can be null
in which case the
* default request timeout will be used.
*
* @return request timeout in milliseconds or null
for default timeout
*/
public Integer getRequestTimeout()
{
return requestTimeout;
}
/**
* Sets the timeout in milliseconds for the AJAX request. This only involves the actual
* communication and not the processing afterwards. Can be
null
in which case the
* default request timeout will be used.
*
* @param requestTimeout
*/
public void setRequestTimeout(Integer requestTimeout)
{
this.requestTimeout = requestTimeout;
}
/**
* Returns the timeout for the response processing. In case the response processing takes more
* than the timeout it won't block the request queue. Can be null
in which case
* the default processing timeout will be used.
*
* @return processing timeout in milliseconds or null
for default timeout
*/
public Integer getProcessingTimeout()
{
return processingTimeout;
}
/**
* Sets the timeout for the response processing. In case the response processing takes more than
* the timeout it won't block the request queue. Can be null
in which case the
* default processing timeout will be used.
*
* @param processingTimeout
*/
public void setProcessingTimeout(Integer processingTimeout)
{
this.processingTimeout = processingTimeout;
}
/**
* Returns optional string identifying related items in request queue. Used to identify previous
* items (items with same token) that will be removed when this item is added and
* {@link #isRemovePrevious()} returns true
. Also required when throttling is
* enabled.
*
* @see #getThrottle()
* @see #isRemovePrevious()
*
* @return token string or null
*/
public String getToken()
{
return token;
}
/**
* Sets optional string identifying related items in request queue. Used to identify previous
* items (items with same token) that will be removed when this item is added and
* {@link #isRemovePrevious()} returns true
. Also required when throttling is
* enabled.
*
* @see #getThrottle()
* @see #isRemovePrevious()
*
* @param token
*/
public void setToken(String token)
{
this.token = token;
}
/**
* If there are previous items with same token in the queue they will be removed if
* {@link #isRemovePrevious()} returns true
. This can be useful when the items
* are added in queue faster than they are processed and only the latest request matters.
* null
*/
public Boolean isRemovePrevious()
{
return removePrevious;
}
/**
* If there are previous items with same token in the queue they will be removed if
* {@link #setRemovePrevious(boolean)} is set to true
. This can be useful when
* the items are added in queue faster than they are processed and only the latest request
* matters.
* null
*/
public Integer getThrottle()
{
return throttle;
}
/**
* Limits adding items with same token to at most one item per n milliseconds where n is the
* return value.
* false
. Causes the
* throttle timer reset each time item with same token is being added to queue.
* true
. The event will be fired 2000ms after user typed the last
* character. If throttle postpone is false
, The event is fired immediately
* after user starts typing and then every two seconds as long as user keeps typing.
*
* @return boolean value or null
*/
public boolean isThrottlePostpone()
{
return throttlePostpone;
}
/**
* When set to true causes the throttle timer reset each time item with same token is being
* added to queue.
*
* @see #isThrottlePostpone()
*
* @param throttlePostpone
*/
public void setThrottlePostpone(boolean throttlePostpone)
{
this.throttlePostpone = throttlePostpone;
}
/**
* Array of javascript functions that are invoked before the request executes. The functions
* will get a RequestQueueItem
instance passed as fist argument and have to
* return a boolean value. If any of these functions returns false
the request is
* canceled.
*
* function(requestQueueItem)
* {
* if (someCondition())
* {
* return true;
* }
* else
* {
* return false;
* }
* }
*
*
* Preconditions can also be asynchronous (with the rest of the queue waiting until precondition
* finishes). An example of asynchronous precondition:
*
*
* function(requestQueueItem, makeAsync, asyncReturn)
* {
* makeAsync(); // let the queue know that this precondition is asynchronous
* var f = function()
* {
* if (someCondition())
* {
* asyncReturn(true); // return the precondition value
* }
* else
* {
* asyncReturn(false); // return the precondition value
* }
* };
* window.setTimeout(f, 1000); // postpone the actual check 1000 millisecond. The queue will wait.
* }
*
*
* @return Listnull
*/
public ListRequestQueueItem
instance passed as first argument.
*
* function(requestQueueItem)
* {
* doSomethingWhenAjaxRequestBegins();
* }
*
*
* @see #getPreconditions()
*
* @return Listnull
*/
public ListRequestQueueItem
instance passed as fist argument.
*
* function(requestQueueItem)
* {
* doSomethingWhenRequestIsSuccessfullyProcessed();
* }
*
*
* @return Listnull
*/
public ListRequestQueueItem
passed as fist argument. If possible
* error message will be second argument passed to the handlers.
*
* function(requestQueueItem, error)
* {
* if (typeof(error) == "string")
* {
* alert('Error processing request ' + error);
* }
* }
*
*
* @return Listnull
*/
public Listnull
*/
public MapRequestQueueItem
passed as first argument and must return a
* Map
(Object).
*
* function(requestQueueItem)
* {
* return { param1: "value1", param2: 15 }
* }
*
*
* @return Listnull
*/
public ListRequestQueueItem
instance is
* created. The RequestQueueItem
instance will be passed as first argument.
*
* @return Listnull
*/
public ListRequestQueueItem
and adding it to the queue. The decorator may be useful to
* intercept the RequestQueueItem
creation on in the earliest possible time.
* false
(which is default value), the link's
* URL will not be followed. If {@link #isAllowDefault()} returns true
, the link
* URL will be loaded (and the onclick handler fired if there is any).
*
* @return true
if the default event handler should be invoked,
* false
otherwise.
*/
public boolean isAllowDefault()
{
return allowDefault;
}
/**
* Only applies for event behaviors. Determines whether the behavior should allow the default
* event handler to be invoked.
*
* @see #isAllowDefault()
*
* @param allowDefault
*/
public void setAllowDefault(boolean allowDefault)
{
this.allowDefault = allowDefault;
}
}