org.wings.js.yui.connection.connection_core.js Maven / Gradle / Ivy
The newest version!
/*
Copyright (c) 2011, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.com/yui/license.html
version: 2.9.0
*/
/**
* The Connection Manager provides a simplified interface to the XMLHttpRequest
* object. It handles cross-browser instantiantion of XMLHttpRequest, negotiates the
* interactive states and server response, returning the results to a pre-defined
* callback you create.
*
* @namespace YAHOO.util
* @module connection
* @requires yahoo
* @requires event
*/
/**
* The Connection Manager singleton provides methods for creating and managing
* asynchronous transactions.
*
* @class YAHOO.util.Connect
*/
YAHOO.util.Connect =
{
/**
* @description Array of MSFT ActiveX ids for XMLHttpRequest.
* @property _msxml_progid
* @private
* @static
* @type array
*/
_msxml_progid:[
'Microsoft.XMLHTTP',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP'
],
/**
* @description Object literal of HTTP header(s)
* @property _http_header
* @private
* @static
* @type object
*/
_http_headers:{},
/**
* @description Determines if HTTP headers are set.
* @property _has_http_headers
* @private
* @static
* @type boolean
*/
_has_http_headers:false,
/**
* @description Determines if a default header of
* Content-Type of 'application/x-www-form-urlencoded'
* will be added to any client HTTP headers sent for POST
* transactions.
* @property _use_default_post_header
* @private
* @static
* @type boolean
*/
_use_default_post_header:true,
/**
* @description The default header used for POST transactions.
* @property _default_post_header
* @private
* @static
* @type boolean
*/
_default_post_header:'application/x-www-form-urlencoded; charset=UTF-8',
/**
* @description The default header used for transactions involving the
* use of HTML forms.
* @property _default_form_header
* @private
* @static
* @type boolean
*/
_default_form_header:'application/x-www-form-urlencoded',
/**
* @description Determines if a default header of
* 'X-Requested-With: XMLHttpRequest'
* will be added to each transaction.
* @property _use_default_xhr_header
* @private
* @static
* @type boolean
*/
_use_default_xhr_header:true,
/**
* @description The default header value for the label
* "X-Requested-With". This is sent with each
* transaction, by default, to identify the
* request as being made by YUI Connection Manager.
* @property _default_xhr_header
* @private
* @static
* @type boolean
*/
_default_xhr_header:'XMLHttpRequest',
/**
* @description Determines if custom, default headers
* are set for each transaction.
* @property _has_default_header
* @private
* @static
* @type boolean
*/
_has_default_headers:true,
/**
* @description Property modified by setForm() to determine if the data
* should be submitted as an HTML form.
* @property _isFormSubmit
* @private
* @static
* @type boolean
*/
_isFormSubmit:false,
/**
* @description Determines if custom, default headers
* are set for each transaction.
* @property _has_default_header
* @private
* @static
* @type boolean
*/
_default_headers:{},
/**
* @description Collection of polling references to the polling mechanism in handleReadyState.
* @property _poll
* @private
* @static
* @type object
*/
_poll:{},
/**
* @description Queue of timeout values for each transaction callback with a defined timeout value.
* @property _timeOut
* @private
* @static
* @type object
*/
_timeOut:{},
/**
* @description The polling frequency, in milliseconds, for HandleReadyState.
* when attempting to determine a transaction's XHR readyState.
* The default is 50 milliseconds.
* @property _polling_interval
* @private
* @static
* @type int
*/
_polling_interval:50,
/**
* @description A transaction counter that increments the transaction id for each transaction.
* @property _transaction_id
* @private
* @static
* @type int
*/
_transaction_id:0,
/**
* @description Custom event that fires at the start of a transaction
* @property startEvent
* @private
* @static
* @type CustomEvent
*/
startEvent: new YAHOO.util.CustomEvent('start'),
/**
* @description Custom event that fires when a transaction response has completed.
* @property completeEvent
* @private
* @static
* @type CustomEvent
*/
completeEvent: new YAHOO.util.CustomEvent('complete'),
/**
* @description Custom event that fires when handleTransactionResponse() determines a
* response in the HTTP 2xx range.
* @property successEvent
* @private
* @static
* @type CustomEvent
*/
successEvent: new YAHOO.util.CustomEvent('success'),
/**
* @description Custom event that fires when handleTransactionResponse() determines a
* response in the HTTP 4xx/5xx range.
* @property failureEvent
* @private
* @static
* @type CustomEvent
*/
failureEvent: new YAHOO.util.CustomEvent('failure'),
/**
* @description Custom event that fires when a transaction is successfully aborted.
* @property abortEvent
* @private
* @static
* @type CustomEvent
*/
abortEvent: new YAHOO.util.CustomEvent('abort'),
/**
* @description A reference table that maps callback custom events members to its specific
* event name.
* @property _customEvents
* @private
* @static
* @type object
*/
_customEvents:
{
onStart:['startEvent', 'start'],
onComplete:['completeEvent', 'complete'],
onSuccess:['successEvent', 'success'],
onFailure:['failureEvent', 'failure'],
onUpload:['uploadEvent', 'upload'],
onAbort:['abortEvent', 'abort']
},
/**
* @description Member to add an ActiveX id to the existing xml_progid array.
* In the event(unlikely) a new ActiveX id is introduced, it can be added
* without internal code modifications.
* @method setProgId
* @public
* @static
* @param {string} id The ActiveX id to be added to initialize the XHR object.
* @return void
*/
setProgId:function(id)
{
this._msxml_progid.unshift(id);
},
/**
* @description Member to override the default POST header.
* @method setDefaultPostHeader
* @public
* @static
* @param {boolean} b Set and use default header - true or false .
* @return void
*/
setDefaultPostHeader:function(b)
{
if(typeof b == 'string'){
this._default_post_header = b;
this._use_default_post_header = true;
}
else if(typeof b == 'boolean'){
this._use_default_post_header = b;
}
},
/**
* @description Member to override the default transaction header..
* @method setDefaultXhrHeader
* @public
* @static
* @param {boolean} b Set and use default header - true or false .
* @return void
*/
setDefaultXhrHeader:function(b)
{
if(typeof b == 'string'){
this._default_xhr_header = b;
}
else{
this._use_default_xhr_header = b;
}
},
/**
* @description Member to modify the default polling interval.
* @method setPollingInterval
* @public
* @static
* @param {int} i The polling interval in milliseconds.
* @return void
*/
setPollingInterval:function(i)
{
if(typeof i == 'number' && isFinite(i)){
this._polling_interval = i;
}
},
/**
* @description Instantiates a XMLHttpRequest object and returns an object with two properties:
* the XMLHttpRequest instance and the transaction id.
* @method createXhrObject
* @private
* @static
* @param {int} transactionId Property containing the transaction id for this transaction.
* @return object
*/
createXhrObject:function(transactionId)
{
var obj,http,i;
try
{
// Instantiates XMLHttpRequest in non-IE browsers and assigns to http.
http = new XMLHttpRequest();
// Object literal with http and tId properties
obj = { conn:http, tId:transactionId, xhr: true };
}
catch(e)
{
for(i=0; i= 200 && httpStatus < 300) || httpStatus === 1223 || xdrS){
responseObject = o.xdr ? o.r : this.createResponseObject(o, args);
if(callback && callback.success){
if(!callback.scope){
callback.success(responseObject);
}
else{
// If a scope property is defined, the callback will be fired from
// the context of the object.
callback.success.apply(callback.scope, [responseObject]);
}
}
// Fire global custom event -- successEvent
this.successEvent.fire(responseObject);
if(o.successEvent){
// Fire transaction custom event -- successEvent
o.successEvent.fire(responseObject);
}
}
else{
switch(httpStatus){
// The following cases are wininet.dll error codes that may be encountered.
case 12002: // Server timeout
case 12029: // 12029 to 12031 correspond to dropped connections.
case 12030:
case 12031:
case 12152: // Connection closed by server.
case 13030: // See above comments for variable status.
// XDR transactions will not resolve to this case, since the
// response object is already built in the xdr response.
responseObject = this.createExceptionObject(o.tId, args, (isAbort?isAbort:false));
if(callback && callback.failure){
if(!callback.scope){
callback.failure(responseObject);
}
else{
callback.failure.apply(callback.scope, [responseObject]);
}
}
break;
default:
responseObject = (o.xdr) ? o.response : this.createResponseObject(o, args);
if(callback && callback.failure){
if(!callback.scope){
callback.failure(responseObject);
}
else{
callback.failure.apply(callback.scope, [responseObject]);
}
}
}
// Fire global custom event -- failureEvent
this.failureEvent.fire(responseObject);
if(o.failureEvent){
// Fire transaction custom event -- failureEvent
o.failureEvent.fire(responseObject);
}
}
this.releaseObject(o);
responseObject = null;
},
/**
* @description This method evaluates the server response, creates and returns the results via
* its properties. Success and failure cases will differ in the response
* object's property values.
* @method createResponseObject
* @private
* @static
* @param {object} o The connection object
* @param {callbackArg} callbackArg The user-defined argument or arguments to be passed to the callback
* @return {object}
*/
createResponseObject:function(o, callbackArg)
{
var obj = {}, headerObj = {},
i, headerStr, header, delimitPos;
try
{
headerStr = o.conn.getAllResponseHeaders();
header = headerStr.split('\n');
for(i=0; i