/*
* Copyright 2005 Joe Walker
*
* Licensed 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.
*/
/**
* The DWR object is also defined by dwr.util etc.
*/
if (typeof dwr == 'undefined') dwr = {};
(function() {
if (!dwr.engine) dwr.engine = {};
/**
* Set an alternative error handler from the default alert box.
* @param {Function} handler The function to call when an error happens
*/
dwr.engine.setErrorHandler = function(handler) {
dwr.engine._errorHandler = handler;
};
/**
* Set an alternative warning handler from the default alert box.
* @param {Function} handler The function to call when a warning happens
*/
dwr.engine.setWarningHandler = function(handler) {
dwr.engine._warningHandler = handler;
};
/**
* @deprecated - In favor of the errorHandler being called with an exception name:"dwr.engine.textHtmlReply"
* containing a responseText property if available.
*
* Setter for the text/html handler - what happens if a DWR request gets an HTML
* reply rather than the expected Javascript. Often due to login timeout
* @param {Function} handler The function to call on an unexpected text/html content type
*/
dwr.engine.setTextHtmlHandler = function(handler) {
dwr.engine._textHtmlHandler = handler;
};
dwr.engine.setPollStatusHandler = function(handler) {
dwr.engine._pollStatusHandler = handler;
};
/**
* Set a default timeout value for all calls. 0 (the default) turns timeouts off.
* @param {Function} handler The function to call when we get bored of waiting for a call
*/
dwr.engine.setTimeout = function(timeout) {
dwr.engine._timeout = timeout;
};
/**
* Set a timeout value for Reverse Ajax polls. 0 (the default) turns timeouts off.
*/
dwr.engine.setPollTimeout = function(timeout) {
dwr.engine._pollTimeout = timeout;
};
/**
* The Pre-Hook is called before any DWR remoting is done.
* @param {Function} handler The function to call before any remote calls
*/
dwr.engine.setPreHook = function(handler) {
dwr.engine._preHook = handler;
};
/**
* The Post-Hook is called after any DWR remoting is done.
* @param {Function} handler The function to call after any remote calls
*/
dwr.engine.setPostHook = function(handler) {
dwr.engine._postHook = handler;
};
/**
* Set a custom path to the DWR servlet (may be a full URL for cross-domain usage)
* @param {string} path path or URL
*/
dwr.engine.setOverridePath = function(path) {
dwr.engine._overridePath = path;
};
/**
* Set a custom contextPath (typically used when rewriting paths through a web server)
* @param {string} path path
*/
dwr.engine.setOverrideContextPath = function(path) {
dwr.engine._overrideContextPath = path;
};
/**
* Extra attributes to append to the DWRSESSIONID cookie (domain, secure, etc)
* @param {string} attributeString attribute string according to cookie syntax
*/
dwr.engine.setCookieAttributes = function(attributeString) {
dwr.engine._cookieAttributes = attributeString;
};
/**
* Custom headers for all DWR calls
* @param {Object} headers Object containing name/value pairs for extra headers
*/
dwr.engine.setHeaders = function(headers) {
dwr.engine._headers = headers;
};
/**
* Custom attributes transferred to server for all DWR calls
* @param {Object} attributes Object containing name/value pairs for attributes
*/
dwr.engine.setAttributes = function(attributes) {
dwr.engine._attributes = attributes;
};
/**
* Ensure that remote calls happen in the order in which they were sent? (Default: false)
* @param {boolean} ordered true to enable ordered processing
*/
dwr.engine.setOrdered = function(ordered) {
dwr.engine._ordered = ordered;
};
/**
* Do we ask the XHR object to be asynchronous? (Default: true)
* Warning: it is <strong>highly</strong> advised to use the default of async
* processing, especially when dealing with Internet based requests.
* @param {boolean} async false to enable sync processing for XHR queries
*/
dwr.engine.setAsync = function(async) {
dwr.engine._async = async;
};
/**
* Does the client actively check the server for updates? (Default: false)
* @param {boolean} async true to enable low latency reverse ajax
*/
dwr.engine.setActiveReverseAjax = function(activeReverseAjax) {
if (activeReverseAjax) {
// Bail if we are already started
if (dwr.engine._activeReverseAjax) return;
// We always want a retry policy when reverse AJAX is enabled.
if (!dwr.engine._retryIntervals || dwr.engine._retryIntervals.length === 0) { dwr.engine._retryIntervals = dwr.engine._defaultRetryIntervals; }
dwr.engine._activeReverseAjax = true;
if (dwr.engine._initializing) return; // We'll start after init is done
dwr.engine._poll();
}
else {
// Can we cancel an existing request?
if (dwr.engine._activeReverseAjax && dwr.engine._pollBatch) {
dwr.engine.transport.abort(dwr.engine._pollBatch);
}
dwr.engine._activeReverseAjax = false;
}
// TODO: in iframe mode, if we start, stop, start then the second start may
// well kick off a second iframe while the first is still about to return
// we should cope with this but we don't
};
/**
* Turn server notification of page unload on and off
* @param {boolean} notify true or false depending on if we want to turn unload notification on or off
*/
dwr.engine.setNotifyServerOnPageUnload = function(notify, asyncUnload) {
dwr.engine._asyncUnload = (asyncUnload !== undefined) ? asyncUnload : false;
dwr.engine._isNotifyServerOnPageUnload = notify;
};
/**
* Turn server notification of page load on and off
* @param {boolean} notify true or false depending on if we want to turn load notification on or off
*/
dwr.engine.setNotifyServerOnPageLoad = function(notify) {
dwr.engine._isNotifyServerOnPageLoad = notify;
if (notify && !dwr.engine._initializing && !dwr.engine._isNotifyServerOnPageLoadSent) {
eval("${initCode}");
dwr.engine._isNotifyServerOnPageLoadSent = true;
}
};
/*
* The maximum number of retries, when reached we will no longer attempt to check for server status.
* @param - maxRetries
*/
dwr.engine.setMaxRetries = function(maxRetries) {
dwr.engine._maxRetries = maxRetries;
};
/*
* The intervals between successive retries in seconds
* @param - array of integers representing the retry interval in seconds.
*/
dwr.engine.setRetryIntervals = function(intervalsArray) {
dwr.engine._retryIntervals = intervalsArray;
};
/**
* The default message handler.
* @param {String} message The text of the error message
* @param {Object} ex An error object containing at least a name and message
*/
dwr.engine.defaultErrorHandler = function(message, ex) {
dwr.engine._debug("Error: " + ex.name + ", " + ex.message, true);
if (!message) message = "A server error has occurred.";
// Ignore NS_ERROR_NOT_AVAILABLE if Mozilla is being narky
if (message.indexOf("0x80040111") != -1) return;
if ("${debug}" == "true") alert(message);
};
/**
* The default warning handler.
* @param {String} message The text of the error message
* @param {Object} ex An error object containing at least a name and message
*/
dwr.engine.defaultWarningHandler = fu