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