1 |
efrain |
1 |
YUI.add('yui2-connectioncore', function(Y) {
|
|
|
2 |
var YAHOO = Y.YUI2;
|
|
|
3 |
/*
|
|
|
4 |
Copyright (c) 2011, Yahoo! Inc. All rights reserved.
|
|
|
5 |
Code licensed under the BSD License:
|
|
|
6 |
http://developer.yahoo.com/yui/license.html
|
|
|
7 |
version: 2.9.0
|
|
|
8 |
*/
|
|
|
9 |
/**
|
|
|
10 |
* The Connection Manager provides a simplified interface to the XMLHttpRequest
|
|
|
11 |
* object. It handles cross-browser instantiantion of XMLHttpRequest, negotiates the
|
|
|
12 |
* interactive states and server response, returning the results to a pre-defined
|
|
|
13 |
* callback you create.
|
|
|
14 |
*
|
|
|
15 |
* @namespace YAHOO.util
|
|
|
16 |
* @module connection
|
|
|
17 |
* @requires yahoo
|
|
|
18 |
* @requires event
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* The Connection Manager singleton provides methods for creating and managing
|
|
|
23 |
* asynchronous transactions.
|
|
|
24 |
*
|
|
|
25 |
* @class YAHOO.util.Connect
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
YAHOO.util.Connect =
|
|
|
29 |
{
|
|
|
30 |
/**
|
|
|
31 |
* @description Array of MSFT ActiveX ids for XMLHttpRequest.
|
|
|
32 |
* @property _msxml_progid
|
|
|
33 |
* @private
|
|
|
34 |
* @static
|
|
|
35 |
* @type array
|
|
|
36 |
*/
|
|
|
37 |
_msxml_progid:[
|
|
|
38 |
'Microsoft.XMLHTTP',
|
|
|
39 |
'MSXML2.XMLHTTP.3.0',
|
|
|
40 |
'MSXML2.XMLHTTP'
|
|
|
41 |
],
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* @description Object literal of HTTP header(s)
|
|
|
45 |
* @property _http_header
|
|
|
46 |
* @private
|
|
|
47 |
* @static
|
|
|
48 |
* @type object
|
|
|
49 |
*/
|
|
|
50 |
_http_headers:{},
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* @description Determines if HTTP headers are set.
|
|
|
54 |
* @property _has_http_headers
|
|
|
55 |
* @private
|
|
|
56 |
* @static
|
|
|
57 |
* @type boolean
|
|
|
58 |
*/
|
|
|
59 |
_has_http_headers:false,
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* @description Determines if a default header of
|
|
|
63 |
* Content-Type of 'application/x-www-form-urlencoded'
|
|
|
64 |
* will be added to any client HTTP headers sent for POST
|
|
|
65 |
* transactions.
|
|
|
66 |
* @property _use_default_post_header
|
|
|
67 |
* @private
|
|
|
68 |
* @static
|
|
|
69 |
* @type boolean
|
|
|
70 |
*/
|
|
|
71 |
_use_default_post_header:true,
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* @description The default header used for POST transactions.
|
|
|
75 |
* @property _default_post_header
|
|
|
76 |
* @private
|
|
|
77 |
* @static
|
|
|
78 |
* @type boolean
|
|
|
79 |
*/
|
|
|
80 |
_default_post_header:'application/x-www-form-urlencoded; charset=UTF-8',
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @description The default header used for transactions involving the
|
|
|
84 |
* use of HTML forms.
|
|
|
85 |
* @property _default_form_header
|
|
|
86 |
* @private
|
|
|
87 |
* @static
|
|
|
88 |
* @type boolean
|
|
|
89 |
*/
|
|
|
90 |
_default_form_header:'application/x-www-form-urlencoded',
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* @description Determines if a default header of
|
|
|
94 |
* 'X-Requested-With: XMLHttpRequest'
|
|
|
95 |
* will be added to each transaction.
|
|
|
96 |
* @property _use_default_xhr_header
|
|
|
97 |
* @private
|
|
|
98 |
* @static
|
|
|
99 |
* @type boolean
|
|
|
100 |
*/
|
|
|
101 |
_use_default_xhr_header:true,
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* @description The default header value for the label
|
|
|
105 |
* "X-Requested-With". This is sent with each
|
|
|
106 |
* transaction, by default, to identify the
|
|
|
107 |
* request as being made by YUI Connection Manager.
|
|
|
108 |
* @property _default_xhr_header
|
|
|
109 |
* @private
|
|
|
110 |
* @static
|
|
|
111 |
* @type boolean
|
|
|
112 |
*/
|
|
|
113 |
_default_xhr_header:'XMLHttpRequest',
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* @description Determines if custom, default headers
|
|
|
117 |
* are set for each transaction.
|
|
|
118 |
* @property _has_default_header
|
|
|
119 |
* @private
|
|
|
120 |
* @static
|
|
|
121 |
* @type boolean
|
|
|
122 |
*/
|
|
|
123 |
_has_default_headers:true,
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* @description Property modified by setForm() to determine if the data
|
|
|
127 |
* should be submitted as an HTML form.
|
|
|
128 |
* @property _isFormSubmit
|
|
|
129 |
* @private
|
|
|
130 |
* @static
|
|
|
131 |
* @type boolean
|
|
|
132 |
*/
|
|
|
133 |
_isFormSubmit:false,
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* @description Determines if custom, default headers
|
|
|
137 |
* are set for each transaction.
|
|
|
138 |
* @property _has_default_header
|
|
|
139 |
* @private
|
|
|
140 |
* @static
|
|
|
141 |
* @type boolean
|
|
|
142 |
*/
|
|
|
143 |
_default_headers:{},
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* @description Collection of polling references to the polling mechanism in handleReadyState.
|
|
|
147 |
* @property _poll
|
|
|
148 |
* @private
|
|
|
149 |
* @static
|
|
|
150 |
* @type object
|
|
|
151 |
*/
|
|
|
152 |
_poll:{},
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* @description Queue of timeout values for each transaction callback with a defined timeout value.
|
|
|
156 |
* @property _timeOut
|
|
|
157 |
* @private
|
|
|
158 |
* @static
|
|
|
159 |
* @type object
|
|
|
160 |
*/
|
|
|
161 |
_timeOut:{},
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* @description The polling frequency, in milliseconds, for HandleReadyState.
|
|
|
165 |
* when attempting to determine a transaction's XHR readyState.
|
|
|
166 |
* The default is 50 milliseconds.
|
|
|
167 |
* @property _polling_interval
|
|
|
168 |
* @private
|
|
|
169 |
* @static
|
|
|
170 |
* @type int
|
|
|
171 |
*/
|
|
|
172 |
_polling_interval:50,
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* @description A transaction counter that increments the transaction id for each transaction.
|
|
|
176 |
* @property _transaction_id
|
|
|
177 |
* @private
|
|
|
178 |
* @static
|
|
|
179 |
* @type int
|
|
|
180 |
*/
|
|
|
181 |
_transaction_id:0,
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* @description Custom event that fires at the start of a transaction
|
|
|
185 |
* @property startEvent
|
|
|
186 |
* @private
|
|
|
187 |
* @static
|
|
|
188 |
* @type CustomEvent
|
|
|
189 |
*/
|
|
|
190 |
startEvent: new YAHOO.util.CustomEvent('start'),
|
|
|
191 |
|
|
|
192 |
/**
|
|
|
193 |
* @description Custom event that fires when a transaction response has completed.
|
|
|
194 |
* @property completeEvent
|
|
|
195 |
* @private
|
|
|
196 |
* @static
|
|
|
197 |
* @type CustomEvent
|
|
|
198 |
*/
|
|
|
199 |
completeEvent: new YAHOO.util.CustomEvent('complete'),
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* @description Custom event that fires when handleTransactionResponse() determines a
|
|
|
203 |
* response in the HTTP 2xx range.
|
|
|
204 |
* @property successEvent
|
|
|
205 |
* @private
|
|
|
206 |
* @static
|
|
|
207 |
* @type CustomEvent
|
|
|
208 |
*/
|
|
|
209 |
successEvent: new YAHOO.util.CustomEvent('success'),
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* @description Custom event that fires when handleTransactionResponse() determines a
|
|
|
213 |
* response in the HTTP 4xx/5xx range.
|
|
|
214 |
* @property failureEvent
|
|
|
215 |
* @private
|
|
|
216 |
* @static
|
|
|
217 |
* @type CustomEvent
|
|
|
218 |
*/
|
|
|
219 |
failureEvent: new YAHOO.util.CustomEvent('failure'),
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* @description Custom event that fires when a transaction is successfully aborted.
|
|
|
223 |
* @property abortEvent
|
|
|
224 |
* @private
|
|
|
225 |
* @static
|
|
|
226 |
* @type CustomEvent
|
|
|
227 |
*/
|
|
|
228 |
abortEvent: new YAHOO.util.CustomEvent('abort'),
|
|
|
229 |
|
|
|
230 |
/**
|
|
|
231 |
* @description A reference table that maps callback custom events members to its specific
|
|
|
232 |
* event name.
|
|
|
233 |
* @property _customEvents
|
|
|
234 |
* @private
|
|
|
235 |
* @static
|
|
|
236 |
* @type object
|
|
|
237 |
*/
|
|
|
238 |
_customEvents:
|
|
|
239 |
{
|
|
|
240 |
onStart:['startEvent', 'start'],
|
|
|
241 |
onComplete:['completeEvent', 'complete'],
|
|
|
242 |
onSuccess:['successEvent', 'success'],
|
|
|
243 |
onFailure:['failureEvent', 'failure'],
|
|
|
244 |
onUpload:['uploadEvent', 'upload'],
|
|
|
245 |
onAbort:['abortEvent', 'abort']
|
|
|
246 |
},
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* @description Member to add an ActiveX id to the existing xml_progid array.
|
|
|
250 |
* In the event(unlikely) a new ActiveX id is introduced, it can be added
|
|
|
251 |
* without internal code modifications.
|
|
|
252 |
* @method setProgId
|
|
|
253 |
* @public
|
|
|
254 |
* @static
|
|
|
255 |
* @param {string} id The ActiveX id to be added to initialize the XHR object.
|
|
|
256 |
* @return void
|
|
|
257 |
*/
|
|
|
258 |
setProgId:function(id)
|
|
|
259 |
{
|
|
|
260 |
this._msxml_progid.unshift(id);
|
|
|
261 |
YAHOO.log('ActiveX Program Id ' + id + ' added to _msxml_progid.', 'info', 'Connection');
|
|
|
262 |
},
|
|
|
263 |
|
|
|
264 |
/**
|
|
|
265 |
* @description Member to override the default POST header.
|
|
|
266 |
* @method setDefaultPostHeader
|
|
|
267 |
* @public
|
|
|
268 |
* @static
|
|
|
269 |
* @param {boolean} b Set and use default header - true or false .
|
|
|
270 |
* @return void
|
|
|
271 |
*/
|
|
|
272 |
setDefaultPostHeader:function(b)
|
|
|
273 |
{
|
|
|
274 |
if(typeof b == 'string'){
|
|
|
275 |
this._default_post_header = b;
|
|
|
276 |
this._use_default_post_header = true;
|
|
|
277 |
|
|
|
278 |
YAHOO.log('Default POST header set to ' + b, 'info', 'Connection');
|
|
|
279 |
}
|
|
|
280 |
else if(typeof b == 'boolean'){
|
|
|
281 |
this._use_default_post_header = b;
|
|
|
282 |
}
|
|
|
283 |
},
|
|
|
284 |
|
|
|
285 |
/**
|
|
|
286 |
* @description Member to override the default transaction header..
|
|
|
287 |
* @method setDefaultXhrHeader
|
|
|
288 |
* @public
|
|
|
289 |
* @static
|
|
|
290 |
* @param {boolean} b Set and use default header - true or false .
|
|
|
291 |
* @return void
|
|
|
292 |
*/
|
|
|
293 |
setDefaultXhrHeader:function(b)
|
|
|
294 |
{
|
|
|
295 |
if(typeof b == 'string'){
|
|
|
296 |
this._default_xhr_header = b;
|
|
|
297 |
YAHOO.log('Default XHR header set to ' + b, 'info', 'Connection');
|
|
|
298 |
}
|
|
|
299 |
else{
|
|
|
300 |
this._use_default_xhr_header = b;
|
|
|
301 |
}
|
|
|
302 |
},
|
|
|
303 |
|
|
|
304 |
/**
|
|
|
305 |
* @description Member to modify the default polling interval.
|
|
|
306 |
* @method setPollingInterval
|
|
|
307 |
* @public
|
|
|
308 |
* @static
|
|
|
309 |
* @param {int} i The polling interval in milliseconds.
|
|
|
310 |
* @return void
|
|
|
311 |
*/
|
|
|
312 |
setPollingInterval:function(i)
|
|
|
313 |
{
|
|
|
314 |
if(typeof i == 'number' && isFinite(i)){
|
|
|
315 |
this._polling_interval = i;
|
|
|
316 |
YAHOO.log('Default polling interval set to ' + i +'ms', 'info', 'Connection');
|
|
|
317 |
}
|
|
|
318 |
},
|
|
|
319 |
|
|
|
320 |
/**
|
|
|
321 |
* @description Instantiates a XMLHttpRequest object and returns an object with two properties:
|
|
|
322 |
* the XMLHttpRequest instance and the transaction id.
|
|
|
323 |
* @method createXhrObject
|
|
|
324 |
* @private
|
|
|
325 |
* @static
|
|
|
326 |
* @param {int} transactionId Property containing the transaction id for this transaction.
|
|
|
327 |
* @return object
|
|
|
328 |
*/
|
|
|
329 |
createXhrObject:function(transactionId)
|
|
|
330 |
{
|
|
|
331 |
var obj,http,i;
|
|
|
332 |
try
|
|
|
333 |
{
|
|
|
334 |
// Instantiates XMLHttpRequest in non-IE browsers and assigns to http.
|
|
|
335 |
http = new XMLHttpRequest();
|
|
|
336 |
// Object literal with http and tId properties
|
|
|
337 |
obj = { conn:http, tId:transactionId, xhr: true };
|
|
|
338 |
YAHOO.log('XHR object created for transaction ' + transactionId, 'info', 'Connection');
|
|
|
339 |
}
|
|
|
340 |
catch(e)
|
|
|
341 |
{
|
|
|
342 |
for(i=0; i<this._msxml_progid.length; ++i){
|
|
|
343 |
try
|
|
|
344 |
{
|
|
|
345 |
// Instantiates XMLHttpRequest for IE and assign to http
|
|
|
346 |
http = new ActiveXObject(this._msxml_progid[i]);
|
|
|
347 |
// Object literal with conn and tId properties
|
|
|
348 |
obj = { conn:http, tId:transactionId, xhr: true };
|
|
|
349 |
YAHOO.log('ActiveX XHR object created for transaction ' + transactionId, 'info', 'Connection');
|
|
|
350 |
break;
|
|
|
351 |
}
|
|
|
352 |
catch(e1){}
|
|
|
353 |
}
|
|
|
354 |
}
|
|
|
355 |
finally
|
|
|
356 |
{
|
|
|
357 |
return obj;
|
|
|
358 |
}
|
|
|
359 |
},
|
|
|
360 |
|
|
|
361 |
/**
|
|
|
362 |
* @description This method is called by asyncRequest to create a
|
|
|
363 |
* valid connection object for the transaction. It also passes a
|
|
|
364 |
* transaction id and increments the transaction id counter.
|
|
|
365 |
* @method getConnectionObject
|
|
|
366 |
* @private
|
|
|
367 |
* @static
|
|
|
368 |
* @return {object}
|
|
|
369 |
*/
|
|
|
370 |
getConnectionObject:function(t)
|
|
|
371 |
{
|
|
|
372 |
var o, tId = this._transaction_id;
|
|
|
373 |
|
|
|
374 |
try
|
|
|
375 |
{
|
|
|
376 |
if(!t){
|
|
|
377 |
o = this.createXhrObject(tId);
|
|
|
378 |
}
|
|
|
379 |
else{
|
|
|
380 |
o = {tId:tId};
|
|
|
381 |
if(t==='xdr'){
|
|
|
382 |
o.conn = this._transport;
|
|
|
383 |
o.xdr = true;
|
|
|
384 |
}
|
|
|
385 |
else if(t==='upload'){
|
|
|
386 |
o.upload = true;
|
|
|
387 |
}
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
if(o){
|
|
|
391 |
this._transaction_id++;
|
|
|
392 |
}
|
|
|
393 |
}
|
|
|
394 |
catch(e){}
|
|
|
395 |
return o;
|
|
|
396 |
},
|
|
|
397 |
|
|
|
398 |
/**
|
|
|
399 |
* @description Method for initiating an asynchronous request via the XHR object.
|
|
|
400 |
* @method asyncRequest
|
|
|
401 |
* @public
|
|
|
402 |
* @static
|
|
|
403 |
* @param {string} method HTTP transaction method
|
|
|
404 |
* @param {string} uri Fully qualified path of resource
|
|
|
405 |
* @param {callback} callback User-defined callback function or object
|
|
|
406 |
* @param {string} postData POST body
|
|
|
407 |
* @return {object} Returns the connection object
|
|
|
408 |
*/
|
|
|
409 |
asyncRequest:function(method, uri, callback, postData)
|
|
|
410 |
{
|
|
|
411 |
var args = callback&&callback.argument?callback.argument:null,
|
|
|
412 |
YCM = this,
|
|
|
413 |
o, t;
|
|
|
414 |
|
|
|
415 |
if(this._isFileUpload){
|
|
|
416 |
t = 'upload';
|
|
|
417 |
}
|
|
|
418 |
else if(callback && callback.xdr){
|
|
|
419 |
t = 'xdr';
|
|
|
420 |
}
|
|
|
421 |
|
|
|
422 |
o = this.getConnectionObject(t);
|
|
|
423 |
if(!o){
|
|
|
424 |
YAHOO.log('Unable to create connection object.', 'error', 'Connection');
|
|
|
425 |
return null;
|
|
|
426 |
}
|
|
|
427 |
else{
|
|
|
428 |
|
|
|
429 |
// Intialize any transaction-specific custom events, if provided.
|
|
|
430 |
if(callback && callback.customevents){
|
|
|
431 |
this.initCustomEvents(o, callback);
|
|
|
432 |
}
|
|
|
433 |
|
|
|
434 |
if(this._isFormSubmit){
|
|
|
435 |
if(this._isFileUpload){
|
|
|
436 |
window.setTimeout(function(){YCM.uploadFile(o, callback, uri, postData);}, 10);
|
|
|
437 |
return o;
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
// If the specified HTTP method is GET, setForm() will return an
|
|
|
441 |
// encoded string that is concatenated to the uri to
|
|
|
442 |
// create a querystring.
|
|
|
443 |
if(method.toUpperCase() == 'GET'){
|
|
|
444 |
if(this._sFormData.length !== 0){
|
|
|
445 |
// If the URI already contains a querystring, append an ampersand
|
|
|
446 |
// and then concatenate _sFormData to the URI.
|
|
|
447 |
uri += ((uri.indexOf('?') == -1)?'?':'&') + this._sFormData;
|
|
|
448 |
}
|
|
|
449 |
}
|
|
|
450 |
else if(method.toUpperCase() == 'POST'){
|
|
|
451 |
// If POST data exist in addition to the HTML form data,
|
|
|
452 |
// it will be concatenated to the form data.
|
|
|
453 |
postData = postData?this._sFormData + "&" + postData:this._sFormData;
|
|
|
454 |
}
|
|
|
455 |
}
|
|
|
456 |
|
|
|
457 |
if(method.toUpperCase() == 'GET' && (callback && callback.cache === false)){
|
|
|
458 |
// If callback.cache is defined and set to false, a
|
|
|
459 |
// timestamp value will be added to the querystring.
|
|
|
460 |
uri += ((uri.indexOf('?') == -1)?'?':'&') + "rnd=" + new Date().valueOf().toString();
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
// Each transaction will automatically include a custom header of
|
|
|
464 |
// "X-Requested-With: XMLHttpRequest" to identify the request as
|
|
|
465 |
// having originated from Connection Manager.
|
|
|
466 |
if(this._use_default_xhr_header){
|
|
|
467 |
if(!this._default_headers['X-Requested-With']){
|
|
|
468 |
this.initHeader('X-Requested-With', this._default_xhr_header, true);
|
|
|
469 |
YAHOO.log('Initialize transaction header X-Request-Header to XMLHttpRequest.', 'info', 'Connection');
|
|
|
470 |
}
|
|
|
471 |
}
|
|
|
472 |
|
|
|
473 |
//If the transaction method is POST and the POST header value is set to true
|
|
|
474 |
//or a custom value, initalize the Content-Type header to this value.
|
|
|
475 |
if((method.toUpperCase() === 'POST' && this._use_default_post_header) && this._isFormSubmit === false){
|
|
|
476 |
this.initHeader('Content-Type', this._default_post_header);
|
|
|
477 |
YAHOO.log('Initialize header Content-Type to application/x-www-form-urlencoded; UTF-8 for POST transaction.', 'info', 'Connection');
|
|
|
478 |
}
|
|
|
479 |
|
|
|
480 |
if(o.xdr){
|
|
|
481 |
this.xdr(o, method, uri, callback, postData);
|
|
|
482 |
return o;
|
|
|
483 |
}
|
|
|
484 |
|
|
|
485 |
o.conn.open(method, uri, true);
|
|
|
486 |
//Initialize all default and custom HTTP headers,
|
|
|
487 |
if(this._has_default_headers || this._has_http_headers){
|
|
|
488 |
this.setHeader(o);
|
|
|
489 |
}
|
|
|
490 |
|
|
|
491 |
this.handleReadyState(o, callback);
|
|
|
492 |
o.conn.send(postData || '');
|
|
|
493 |
YAHOO.log('Transaction ' + o.tId + ' sent.', 'info', 'Connection');
|
|
|
494 |
|
|
|
495 |
// Reset the HTML form data and state properties as
|
|
|
496 |
// soon as the data are submitted.
|
|
|
497 |
if(this._isFormSubmit === true){
|
|
|
498 |
this.resetFormState();
|
|
|
499 |
}
|
|
|
500 |
|
|
|
501 |
// Fire global custom event -- startEvent
|
|
|
502 |
this.startEvent.fire(o, args);
|
|
|
503 |
|
|
|
504 |
if(o.startEvent){
|
|
|
505 |
// Fire transaction custom event -- startEvent
|
|
|
506 |
o.startEvent.fire(o, args);
|
|
|
507 |
}
|
|
|
508 |
|
|
|
509 |
return o;
|
|
|
510 |
}
|
|
|
511 |
},
|
|
|
512 |
|
|
|
513 |
/**
|
|
|
514 |
* @description This method creates and subscribes custom events,
|
|
|
515 |
* specific to each transaction
|
|
|
516 |
* @method initCustomEvents
|
|
|
517 |
* @private
|
|
|
518 |
* @static
|
|
|
519 |
* @param {object} o The connection object
|
|
|
520 |
* @param {callback} callback The user-defined callback object
|
|
|
521 |
* @return {void}
|
|
|
522 |
*/
|
|
|
523 |
initCustomEvents:function(o, callback)
|
|
|
524 |
{
|
|
|
525 |
var prop;
|
|
|
526 |
// Enumerate through callback.customevents members and bind/subscribe
|
|
|
527 |
// events that match in the _customEvents table.
|
|
|
528 |
for(prop in callback.customevents){
|
|
|
529 |
if(this._customEvents[prop][0]){
|
|
|
530 |
// Create the custom event
|
|
|
531 |
o[this._customEvents[prop][0]] = new YAHOO.util.CustomEvent(this._customEvents[prop][1], (callback.scope)?callback.scope:null);
|
|
|
532 |
YAHOO.log('Transaction-specific Custom Event ' + o[this._customEvents[prop][1]] + ' created.', 'info', 'Connection');
|
|
|
533 |
|
|
|
534 |
// Subscribe the custom event
|
|
|
535 |
o[this._customEvents[prop][0]].subscribe(callback.customevents[prop]);
|
|
|
536 |
YAHOO.log('Transaction-specific Custom Event ' + o[this._customEvents[prop][1]] + ' subscribed.', 'info', 'Connection');
|
|
|
537 |
}
|
|
|
538 |
}
|
|
|
539 |
},
|
|
|
540 |
|
|
|
541 |
/**
|
|
|
542 |
* @description This method serves as a timer that polls the XHR object's readyState
|
|
|
543 |
* property during a transaction, instead of binding a callback to the
|
|
|
544 |
* onreadystatechange event. Upon readyState 4, handleTransactionResponse
|
|
|
545 |
* will process the response, and the timer will be cleared.
|
|
|
546 |
* @method handleReadyState
|
|
|
547 |
* @private
|
|
|
548 |
* @static
|
|
|
549 |
* @param {object} o The connection object
|
|
|
550 |
* @param {callback} callback The user-defined callback object
|
|
|
551 |
* @return {void}
|
|
|
552 |
*/
|
|
|
553 |
|
|
|
554 |
handleReadyState:function(o, callback)
|
|
|
555 |
|
|
|
556 |
{
|
|
|
557 |
var oConn = this,
|
|
|
558 |
args = (callback && callback.argument)?callback.argument:null;
|
|
|
559 |
|
|
|
560 |
if(callback && callback.timeout){
|
|
|
561 |
this._timeOut[o.tId] = window.setTimeout(function(){ oConn.abort(o, callback, true); }, callback.timeout);
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
this._poll[o.tId] = window.setInterval(
|
|
|
565 |
function(){
|
|
|
566 |
if(o.conn && o.conn.readyState === 4){
|
|
|
567 |
|
|
|
568 |
// Clear the polling interval for the transaction
|
|
|
569 |
// and remove the reference from _poll.
|
|
|
570 |
window.clearInterval(oConn._poll[o.tId]);
|
|
|
571 |
delete oConn._poll[o.tId];
|
|
|
572 |
|
|
|
573 |
if(callback && callback.timeout){
|
|
|
574 |
window.clearTimeout(oConn._timeOut[o.tId]);
|
|
|
575 |
delete oConn._timeOut[o.tId];
|
|
|
576 |
}
|
|
|
577 |
|
|
|
578 |
// Fire global custom event -- completeEvent
|
|
|
579 |
oConn.completeEvent.fire(o, args);
|
|
|
580 |
|
|
|
581 |
if(o.completeEvent){
|
|
|
582 |
// Fire transaction custom event -- completeEvent
|
|
|
583 |
o.completeEvent.fire(o, args);
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
oConn.handleTransactionResponse(o, callback);
|
|
|
587 |
}
|
|
|
588 |
}
|
|
|
589 |
,this._polling_interval);
|
|
|
590 |
},
|
|
|
591 |
|
|
|
592 |
/**
|
|
|
593 |
* @description This method attempts to interpret the server response and
|
|
|
594 |
* determine whether the transaction was successful, or if an error or
|
|
|
595 |
* exception was encountered.
|
|
|
596 |
* @method handleTransactionResponse
|
|
|
597 |
* @private
|
|
|
598 |
* @static
|
|
|
599 |
* @param {object} o The connection object
|
|
|
600 |
* @param {object} callback The user-defined callback object
|
|
|
601 |
* @param {boolean} isAbort Determines if the transaction was terminated via abort().
|
|
|
602 |
* @return {void}
|
|
|
603 |
*/
|
|
|
604 |
handleTransactionResponse:function(o, callback, isAbort)
|
|
|
605 |
{
|
|
|
606 |
var httpStatus, responseObject,
|
|
|
607 |
args = (callback && callback.argument)?callback.argument:null,
|
|
|
608 |
xdrS = (o.r && o.r.statusText === 'xdr:success')?true:false,
|
|
|
609 |
xdrF = (o.r && o.r.statusText === 'xdr:failure')?true:false,
|
|
|
610 |
xdrA = isAbort;
|
|
|
611 |
|
|
|
612 |
try
|
|
|
613 |
{
|
|
|
614 |
if((o.conn.status !== undefined && o.conn.status !== 0) || xdrS){
|
|
|
615 |
// XDR requests will not have HTTP status defined. The
|
|
|
616 |
// statusText property will define the response status
|
|
|
617 |
// set by the Flash transport.
|
|
|
618 |
httpStatus = o.conn.status;
|
|
|
619 |
}
|
|
|
620 |
else if(xdrF && !xdrA){
|
|
|
621 |
// Set XDR transaction failure to a status of 0, which
|
|
|
622 |
// resolves as an HTTP failure, instead of an exception.
|
|
|
623 |
httpStatus = 0;
|
|
|
624 |
}
|
|
|
625 |
else{
|
|
|
626 |
httpStatus = 13030;
|
|
|
627 |
}
|
|
|
628 |
}
|
|
|
629 |
catch(e){
|
|
|
630 |
|
|
|
631 |
// 13030 is a custom code to indicate the condition -- in Mozilla/FF --
|
|
|
632 |
// when the XHR object's status and statusText properties are
|
|
|
633 |
// unavailable, and a query attempt throws an exception.
|
|
|
634 |
httpStatus = 13030;
|
|
|
635 |
}
|
|
|
636 |
|
|
|
637 |
if((httpStatus >= 200 && httpStatus < 300) || httpStatus === 1223 || xdrS){
|
|
|
638 |
responseObject = o.xdr ? o.r : this.createResponseObject(o, args);
|
|
|
639 |
if(callback && callback.success){
|
|
|
640 |
if(!callback.scope){
|
|
|
641 |
callback.success(responseObject);
|
|
|
642 |
YAHOO.log('Success callback. HTTP code is ' + httpStatus, 'info', 'Connection');
|
|
|
643 |
}
|
|
|
644 |
else{
|
|
|
645 |
// If a scope property is defined, the callback will be fired from
|
|
|
646 |
// the context of the object.
|
|
|
647 |
callback.success.apply(callback.scope, [responseObject]);
|
|
|
648 |
YAHOO.log('Success callback with scope. HTTP code is ' + httpStatus, 'info', 'Connection');
|
|
|
649 |
}
|
|
|
650 |
}
|
|
|
651 |
|
|
|
652 |
// Fire global custom event -- successEvent
|
|
|
653 |
this.successEvent.fire(responseObject);
|
|
|
654 |
|
|
|
655 |
if(o.successEvent){
|
|
|
656 |
// Fire transaction custom event -- successEvent
|
|
|
657 |
o.successEvent.fire(responseObject);
|
|
|
658 |
}
|
|
|
659 |
}
|
|
|
660 |
else{
|
|
|
661 |
switch(httpStatus){
|
|
|
662 |
// The following cases are wininet.dll error codes that may be encountered.
|
|
|
663 |
case 12002: // Server timeout
|
|
|
664 |
case 12029: // 12029 to 12031 correspond to dropped connections.
|
|
|
665 |
case 12030:
|
|
|
666 |
case 12031:
|
|
|
667 |
case 12152: // Connection closed by server.
|
|
|
668 |
case 13030: // See above comments for variable status.
|
|
|
669 |
// XDR transactions will not resolve to this case, since the
|
|
|
670 |
// response object is already built in the xdr response.
|
|
|
671 |
responseObject = this.createExceptionObject(o.tId, args, (isAbort?isAbort:false));
|
|
|
672 |
if(callback && callback.failure){
|
|
|
673 |
if(!callback.scope){
|
|
|
674 |
callback.failure(responseObject);
|
|
|
675 |
YAHOO.log('Failure callback. Exception detected. Status code is ' + httpStatus, 'warn', 'Connection');
|
|
|
676 |
}
|
|
|
677 |
else{
|
|
|
678 |
callback.failure.apply(callback.scope, [responseObject]);
|
|
|
679 |
YAHOO.log('Failure callback with scope. Exception detected. Status code is ' + httpStatus, 'warn', 'Connection');
|
|
|
680 |
}
|
|
|
681 |
}
|
|
|
682 |
|
|
|
683 |
break;
|
|
|
684 |
default:
|
|
|
685 |
responseObject = (o.xdr) ? o.response : this.createResponseObject(o, args);
|
|
|
686 |
if(callback && callback.failure){
|
|
|
687 |
if(!callback.scope){
|
|
|
688 |
callback.failure(responseObject);
|
|
|
689 |
YAHOO.log('Failure callback. HTTP status code is ' + httpStatus, 'warn', 'Connection');
|
|
|
690 |
}
|
|
|
691 |
else{
|
|
|
692 |
callback.failure.apply(callback.scope, [responseObject]);
|
|
|
693 |
YAHOO.log('Failure callback with scope. HTTP status code is ' + httpStatus, 'warn', 'Connection');
|
|
|
694 |
}
|
|
|
695 |
}
|
|
|
696 |
}
|
|
|
697 |
|
|
|
698 |
// Fire global custom event -- failureEvent
|
|
|
699 |
this.failureEvent.fire(responseObject);
|
|
|
700 |
|
|
|
701 |
if(o.failureEvent){
|
|
|
702 |
// Fire transaction custom event -- failureEvent
|
|
|
703 |
o.failureEvent.fire(responseObject);
|
|
|
704 |
}
|
|
|
705 |
|
|
|
706 |
}
|
|
|
707 |
|
|
|
708 |
this.releaseObject(o);
|
|
|
709 |
responseObject = null;
|
|
|
710 |
},
|
|
|
711 |
|
|
|
712 |
/**
|
|
|
713 |
* @description This method evaluates the server response, creates and returns the results via
|
|
|
714 |
* its properties. Success and failure cases will differ in the response
|
|
|
715 |
* object's property values.
|
|
|
716 |
* @method createResponseObject
|
|
|
717 |
* @private
|
|
|
718 |
* @static
|
|
|
719 |
* @param {object} o The connection object
|
|
|
720 |
* @param {callbackArg} callbackArg The user-defined argument or arguments to be passed to the callback
|
|
|
721 |
* @return {object}
|
|
|
722 |
*/
|
|
|
723 |
createResponseObject:function(o, callbackArg)
|
|
|
724 |
{
|
|
|
725 |
var obj = {}, headerObj = {},
|
|
|
726 |
i, headerStr, header, delimitPos;
|
|
|
727 |
|
|
|
728 |
try
|
|
|
729 |
{
|
|
|
730 |
headerStr = o.conn.getAllResponseHeaders();
|
|
|
731 |
header = headerStr.split('\n');
|
|
|
732 |
for(i=0; i<header.length; i++){
|
|
|
733 |
delimitPos = header[i].indexOf(':');
|
|
|
734 |
if(delimitPos != -1){
|
|
|
735 |
headerObj[header[i].substring(0,delimitPos)] = YAHOO.lang.trim(header[i].substring(delimitPos+2));
|
|
|
736 |
}
|
|
|
737 |
}
|
|
|
738 |
}
|
|
|
739 |
catch(e){}
|
|
|
740 |
|
|
|
741 |
obj.tId = o.tId;
|
|
|
742 |
// Normalize IE's response to HTTP 204 when Win error 1223.
|
|
|
743 |
obj.status = (o.conn.status == 1223)?204:o.conn.status;
|
|
|
744 |
// Normalize IE's statusText to "No Content" instead of "Unknown".
|
|
|
745 |
obj.statusText = (o.conn.status == 1223)?"No Content":o.conn.statusText;
|
|
|
746 |
obj.getResponseHeader = headerObj;
|
|
|
747 |
obj.getAllResponseHeaders = headerStr;
|
|
|
748 |
obj.responseText = o.conn.responseText;
|
|
|
749 |
obj.responseXML = o.conn.responseXML;
|
|
|
750 |
|
|
|
751 |
if(callbackArg){
|
|
|
752 |
obj.argument = callbackArg;
|
|
|
753 |
}
|
|
|
754 |
|
|
|
755 |
return obj;
|
|
|
756 |
},
|
|
|
757 |
|
|
|
758 |
/**
|
|
|
759 |
* @description If a transaction cannot be completed due to dropped or closed connections,
|
|
|
760 |
* there may be not be enough information to build a full response object.
|
|
|
761 |
* The failure callback will be fired and this specific condition can be identified
|
|
|
762 |
* by a status property value of 0.
|
|
|
763 |
*
|
|
|
764 |
* If an abort was successful, the status property will report a value of -1.
|
|
|
765 |
*
|
|
|
766 |
* @method createExceptionObject
|
|
|
767 |
* @private
|
|
|
768 |
* @static
|
|
|
769 |
* @param {int} tId The Transaction Id
|
|
|
770 |
* @param {callbackArg} callbackArg The user-defined argument or arguments to be passed to the callback
|
|
|
771 |
* @param {boolean} isAbort Determines if the exception case is caused by a transaction abort
|
|
|
772 |
* @return {object}
|
|
|
773 |
*/
|
|
|
774 |
createExceptionObject:function(tId, callbackArg, isAbort)
|
|
|
775 |
{
|
|
|
776 |
var COMM_CODE = 0,
|
|
|
777 |
COMM_ERROR = 'communication failure',
|
|
|
778 |
ABORT_CODE = -1,
|
|
|
779 |
ABORT_ERROR = 'transaction aborted',
|
|
|
780 |
obj = {};
|
|
|
781 |
|
|
|
782 |
obj.tId = tId;
|
|
|
783 |
if(isAbort){
|
|
|
784 |
obj.status = ABORT_CODE;
|
|
|
785 |
obj.statusText = ABORT_ERROR;
|
|
|
786 |
}
|
|
|
787 |
else{
|
|
|
788 |
obj.status = COMM_CODE;
|
|
|
789 |
obj.statusText = COMM_ERROR;
|
|
|
790 |
}
|
|
|
791 |
|
|
|
792 |
if(callbackArg){
|
|
|
793 |
obj.argument = callbackArg;
|
|
|
794 |
}
|
|
|
795 |
|
|
|
796 |
return obj;
|
|
|
797 |
},
|
|
|
798 |
|
|
|
799 |
/**
|
|
|
800 |
* @description Method that initializes the custom HTTP headers for the each transaction.
|
|
|
801 |
* @method initHeader
|
|
|
802 |
* @public
|
|
|
803 |
* @static
|
|
|
804 |
* @param {string} label The HTTP header label
|
|
|
805 |
* @param {string} value The HTTP header value
|
|
|
806 |
* @param {string} isDefault Determines if the specific header is a default header
|
|
|
807 |
* automatically sent with each transaction.
|
|
|
808 |
* @return {void}
|
|
|
809 |
*/
|
|
|
810 |
initHeader:function(label, value, isDefault)
|
|
|
811 |
{
|
|
|
812 |
var headerObj = (isDefault)?this._default_headers:this._http_headers;
|
|
|
813 |
|
|
|
814 |
headerObj[label] = value;
|
|
|
815 |
if(isDefault){
|
|
|
816 |
this._has_default_headers = true;
|
|
|
817 |
}
|
|
|
818 |
else{
|
|
|
819 |
this._has_http_headers = true;
|
|
|
820 |
}
|
|
|
821 |
},
|
|
|
822 |
|
|
|
823 |
|
|
|
824 |
/**
|
|
|
825 |
* @description Accessor that sets the HTTP headers for each transaction.
|
|
|
826 |
* @method setHeader
|
|
|
827 |
* @private
|
|
|
828 |
* @static
|
|
|
829 |
* @param {object} o The connection object for the transaction.
|
|
|
830 |
* @return {void}
|
|
|
831 |
*/
|
|
|
832 |
setHeader:function(o)
|
|
|
833 |
{
|
|
|
834 |
var prop;
|
|
|
835 |
if(this._has_default_headers){
|
|
|
836 |
for(prop in this._default_headers){
|
|
|
837 |
if(YAHOO.lang.hasOwnProperty(this._default_headers, prop)){
|
|
|
838 |
o.conn.setRequestHeader(prop, this._default_headers[prop]);
|
|
|
839 |
YAHOO.log('Default HTTP header ' + prop + ' set with value of ' + this._default_headers[prop], 'info', 'Connection');
|
|
|
840 |
}
|
|
|
841 |
}
|
|
|
842 |
}
|
|
|
843 |
|
|
|
844 |
if(this._has_http_headers){
|
|
|
845 |
for(prop in this._http_headers){
|
|
|
846 |
if(YAHOO.lang.hasOwnProperty(this._http_headers, prop)){
|
|
|
847 |
o.conn.setRequestHeader(prop, this._http_headers[prop]);
|
|
|
848 |
YAHOO.log('HTTP header ' + prop + ' set with value of ' + this._http_headers[prop], 'info', 'Connection');
|
|
|
849 |
}
|
|
|
850 |
}
|
|
|
851 |
|
|
|
852 |
this._http_headers = {};
|
|
|
853 |
this._has_http_headers = false;
|
|
|
854 |
}
|
|
|
855 |
},
|
|
|
856 |
|
|
|
857 |
/**
|
|
|
858 |
* @description Resets the default HTTP headers object
|
|
|
859 |
* @method resetDefaultHeaders
|
|
|
860 |
* @public
|
|
|
861 |
* @static
|
|
|
862 |
* @return {void}
|
|
|
863 |
*/
|
|
|
864 |
resetDefaultHeaders:function(){
|
|
|
865 |
this._default_headers = {};
|
|
|
866 |
this._has_default_headers = false;
|
|
|
867 |
},
|
|
|
868 |
|
|
|
869 |
/**
|
|
|
870 |
* @description Method to terminate a transaction, if it has not reached readyState 4.
|
|
|
871 |
* @method abort
|
|
|
872 |
* @public
|
|
|
873 |
* @static
|
|
|
874 |
* @param {object} o The connection object returned by asyncRequest.
|
|
|
875 |
* @param {object} callback User-defined callback object.
|
|
|
876 |
* @param {string} isTimeout boolean to indicate if abort resulted from a callback timeout.
|
|
|
877 |
* @return {boolean}
|
|
|
878 |
*/
|
|
|
879 |
abort:function(o, callback, isTimeout)
|
|
|
880 |
{
|
|
|
881 |
var abortStatus,
|
|
|
882 |
args = (callback && callback.argument)?callback.argument:null;
|
|
|
883 |
o = o || {};
|
|
|
884 |
|
|
|
885 |
if(o.conn){
|
|
|
886 |
if(o.xhr){
|
|
|
887 |
if(this.isCallInProgress(o)){
|
|
|
888 |
// Issue abort request
|
|
|
889 |
o.conn.abort();
|
|
|
890 |
|
|
|
891 |
window.clearInterval(this._poll[o.tId]);
|
|
|
892 |
delete this._poll[o.tId];
|
|
|
893 |
|
|
|
894 |
if(isTimeout){
|
|
|
895 |
window.clearTimeout(this._timeOut[o.tId]);
|
|
|
896 |
delete this._timeOut[o.tId];
|
|
|
897 |
}
|
|
|
898 |
|
|
|
899 |
abortStatus = true;
|
|
|
900 |
}
|
|
|
901 |
}
|
|
|
902 |
else if(o.xdr){
|
|
|
903 |
o.conn.abort(o.tId);
|
|
|
904 |
abortStatus = true;
|
|
|
905 |
}
|
|
|
906 |
}
|
|
|
907 |
else if(o.upload){
|
|
|
908 |
var frameId = 'yuiIO' + o.tId;
|
|
|
909 |
var io = document.getElementById(frameId);
|
|
|
910 |
|
|
|
911 |
if(io){
|
|
|
912 |
// Remove all listeners on the iframe prior to
|
|
|
913 |
// its destruction.
|
|
|
914 |
YAHOO.util.Event.removeListener(io, "load");
|
|
|
915 |
// Destroy the iframe facilitating the transaction.
|
|
|
916 |
document.body.removeChild(io);
|
|
|
917 |
YAHOO.log('File upload iframe destroyed. Id is:' + frameId, 'info', 'Connection');
|
|
|
918 |
|
|
|
919 |
if(isTimeout){
|
|
|
920 |
window.clearTimeout(this._timeOut[o.tId]);
|
|
|
921 |
delete this._timeOut[o.tId];
|
|
|
922 |
}
|
|
|
923 |
|
|
|
924 |
abortStatus = true;
|
|
|
925 |
}
|
|
|
926 |
}
|
|
|
927 |
else{
|
|
|
928 |
abortStatus = false;
|
|
|
929 |
}
|
|
|
930 |
|
|
|
931 |
if(abortStatus === true){
|
|
|
932 |
// Fire global custom event -- abortEvent
|
|
|
933 |
this.abortEvent.fire(o, args);
|
|
|
934 |
|
|
|
935 |
if(o.abortEvent){
|
|
|
936 |
// Fire transaction custom event -- abortEvent
|
|
|
937 |
o.abortEvent.fire(o, args);
|
|
|
938 |
}
|
|
|
939 |
|
|
|
940 |
this.handleTransactionResponse(o, callback, true);
|
|
|
941 |
YAHOO.log('Transaction ' + o.tId + ' aborted.', 'info', 'Connection');
|
|
|
942 |
}
|
|
|
943 |
|
|
|
944 |
return abortStatus;
|
|
|
945 |
},
|
|
|
946 |
|
|
|
947 |
/**
|
|
|
948 |
* @description Determines if the transaction is still being processed.
|
|
|
949 |
* @method isCallInProgress
|
|
|
950 |
* @public
|
|
|
951 |
* @static
|
|
|
952 |
* @param {object} o The connection object returned by asyncRequest
|
|
|
953 |
* @return {boolean}
|
|
|
954 |
*/
|
|
|
955 |
isCallInProgress:function(o)
|
|
|
956 |
{
|
|
|
957 |
o = o || {};
|
|
|
958 |
// if the XHR object assigned to the transaction has not been dereferenced,
|
|
|
959 |
// then check its readyState status. Otherwise, return false.
|
|
|
960 |
if(o.xhr && o.conn){
|
|
|
961 |
return o.conn.readyState !== 4 && o.conn.readyState !== 0;
|
|
|
962 |
}
|
|
|
963 |
else if(o.xdr && o.conn){
|
|
|
964 |
return o.conn.isCallInProgress(o.tId);
|
|
|
965 |
}
|
|
|
966 |
else if(o.upload === true){
|
|
|
967 |
return document.getElementById('yuiIO' + o.tId)?true:false;
|
|
|
968 |
}
|
|
|
969 |
else{
|
|
|
970 |
return false;
|
|
|
971 |
}
|
|
|
972 |
},
|
|
|
973 |
|
|
|
974 |
/**
|
|
|
975 |
* @description Dereference the XHR instance and the connection object after the transaction is completed.
|
|
|
976 |
* @method releaseObject
|
|
|
977 |
* @private
|
|
|
978 |
* @static
|
|
|
979 |
* @param {object} o The connection object
|
|
|
980 |
* @return {void}
|
|
|
981 |
*/
|
|
|
982 |
releaseObject:function(o)
|
|
|
983 |
{
|
|
|
984 |
if(o && o.conn){
|
|
|
985 |
//dereference the XHR instance.
|
|
|
986 |
o.conn = null;
|
|
|
987 |
|
|
|
988 |
YAHOO.log('Connection object for transaction ' + o.tId + ' destroyed.', 'info', 'Connection');
|
|
|
989 |
|
|
|
990 |
//dereference the connection object.
|
|
|
991 |
o = null;
|
|
|
992 |
}
|
|
|
993 |
}
|
|
|
994 |
};
|
|
|
995 |
|
|
|
996 |
YAHOO.register("connection_core", YAHOO.util.Connect, {version: "2.9.0", build: "2800"});
|
|
|
997 |
|
|
|
998 |
}, '2.9.0' ,{"requires": ["yui2-yahoo", "yui2-event"]});
|