2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
7
YUI.add('io-base', function(Y) {
10
* HTTP communications module.
15
* The io class is a utility that brokers HTTP requests through a simplified
16
* interface. Specifically, it allows JavaScript to make HTTP requests to
17
* a resource without a page reload. The underlying transport for making
18
* same-domain requests is the XMLHttpRequest object. YUI.io can also use
19
* Flash, if specified as a transport, for cross-domain requests.
26
* @description This event is fired by YUI.io when a transaction is initiated..
29
var E_START = 'io:start',
33
* @description This event is fired by YUI.io when a transaction is complete and
34
* all response data are available.
37
E_COMPLETE = 'io:complete',
41
* @description This event is fired by YUI.io when a transaction is complete and
42
* the HTTP status resolves to HTTP2xx.
45
E_SUCCESS = 'io:success',
49
* @description This event is fired by YUI.io when a transaction is complete and
50
* the HTTP status resolves to HTTP4xx, 5xx and above.
53
E_FAILURE = 'io:failure',
57
* @description This event is fired by YUI.io when a transaction is aborted
58
* explicitly or by a defined config.timeout.
63
//--------------------------------------
65
//--------------------------------------
67
* @description A transaction counter that increments for each transaction.
69
* @property transactionId
77
* @description Object of default HTTP headers to be initialized and sent
78
* for all transactions.
86
'X-Requested-With' : 'XMLHttpRequest'
90
* @description Object that stores timeout values for any transaction with
91
* a defined "timeout" configuration property.
100
//--------------------------------------
102
//--------------------------------------
108
* @description Method for requesting a transaction. _io() is implemented as
109
* yui.io(). Each transaction may include a configuration object. Its
112
* method: HTTP method verb (e.g., GET or POST). If this property is not
113
* not defined, the default value will be GET.
115
* data: This is the name-value string that will be sent as the transaction
116
* data. If the request is HTTP GET, the data become part of
117
* querystring. If HTTP POST, the data are sent in the message body.
119
* xdr: Defines the transport to be used for cross-domain requests. By
120
* setting this property, the transaction will use the specified
121
* transport instead of XMLHttpRequest. Currently, the only alternate
122
* transport supported is Flash (e.g., { xdr: 'flash' }).
124
* form: This is a defined object used to process HTML form as data. The
127
* id: object, //HTML form object or id of HTML form
128
* useDisabled: boolean, //Allow disabled HTML form field values
129
* to be sent as part of the data.
132
* on: This is a defined object used to create and handle specific
133
* events during a transaction lifecycle. These events will fire in
134
* addition to the global io events. The events are:
135
* start - This event is fired when a request is sent to a resource.
136
* complete - This event fires when the transaction is complete.
137
* success - This event fires when the response status resolves to
139
* failure - This event fires when the response status resolves to
140
* HTTP 4xx, 5xx, and beyond.
141
* abort - This even is fired when a transaction abort is fire by
142
* timeout, or when it is manually aborted.
144
* The properties are:
146
* start: function(id, args){},
147
* complete: function(id, responseobject, args){},
148
* success: function(id, responseobject, args){},
149
* failure: function(id, responseobject, args){},
150
* abort: function(id, args){}
152
* Each property can reference a function or be written as an
155
* context: Object reference for an event handler when it is implemented
156
* as a method of a base object. Defining "context" will preserve
157
* the proper reference of "this" used in the event handler.
158
* headers: This is a defined object of client headers, as many as.
159
* desired for the transaction. These headers are sentThe object
165
* timeout: This value, defined as milliseconds, is a time threshold for the
166
* transaction. When this threshold is reached, and the transaction's
167
* Complete event has not yet fired, the transaction will be aborted.
168
* arguments: Object, array, string, or number passed to all registered
169
* event handlers. This value is available as the second
170
* argument in the "start" and "abort" event handlers; and, it is
171
* the third argument in the "complete", "success", and "failure"
177
* @param {string} uri - qualified path to transaction resource.
178
* @param {object} c - configuration object for the transaction.
181
function _io(uri, c) {
183
o = _create((arguments.length === 3) ? arguments[2] : null, c),
184
m = (c.method) ? c.method.toUpperCase() : 'GET',
185
d = (c.data) ? c.data : null,
188
/* Determine configuration properties */
189
// If config.form is defined, perform data operations.
193
u = Y.io._upload(o, uri, c);
197
// Serialize the HTML form into a string of name-value pairs.
198
f = Y.io._serialize(c.form);
199
// If config.data is defined, concatenate the data to the form string.
202
Y.log('Configuration object.data added to serialized HTML form data. The string is: ' + f, 'info', 'io');
207
_setHeader('Content-Type', 'application/x-www-form-urlencoded');
209
else if (m === 'GET') {
210
uri = _concat(uri, f);
211
Y.log('Configuration object.data added to serialized HTML form data. The querystring is: ' + uri, 'info', 'io');
214
else if (d && m === 'GET') {
215
uri = _concat(uri, c.data);
216
Y.log('Configuration object data added to URI. The querystring is: ' + uri, 'info', 'io');
218
else if (d && m === 'POST') {
219
_setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
223
Y.io._xdr(uri, o, c);
227
// If config.timeout is defined, and the request is standard XHR,
228
// initialize timeout polling.
232
/* End Configuration Properties */
234
o.c.onreadystatechange = function() { _readyState(o, c); };
240
_setHeaders(o.c, (c.headers || {}));
242
o.abort = function () {
246
o.isInProgress = function() {
247
return o.c.readyState !== 4 && o.c.readyState !== 0;
249
// Do not pass null, in the absence of data, as this
250
// will result in a POST request with no Content-Length
252
_async(o, (d || ''), c);
258
* @description Method for creating and subscribing transaction events.
263
* @param {string} e - event to be published
264
* @param {object} c - configuration object for the transaction.
268
function _tPubSub(e, c){
269
var event = new Y.Event.Target().publish('transaction:' + e);
270
event.subscribe(c.on[e], (c.context || this), c.arguments);
276
* @description Fires event "io:start" and creates, fires a
277
* transaction-specific start event, if config.on.start is
283
* @param {number} id - transaction id
284
* @param {object} c - configuration object for the transaction.
288
function _ioStart(id, c) {
289
// Set default value of argument c, property "on" to Object if
290
// the property is null or undefined.
292
var m = Y.io._fn || {},
293
fn = (m && m[id]) ? m[id] : null,
297
c.on.start = fn.start;
303
event = _tPubSub('start', c);
306
Y.log('Transaction ' + id + ' started.', 'info', 'io');
310
* @description Fires event "io:complete" and creates, fires a
311
* transaction-specific "complete" event, if config.on.complete is
314
* @method _ioComplete
317
* @param {object} o - transaction object.
318
* @param {object} c - configuration object for the transaction.
322
function _ioComplete(o, c) {
323
// Set default value of argument c, property "on" to Object if
324
// the property is null or undefined.
328
Y.fire(E_COMPLETE, o.id, o.c);
330
event = _tPubSub('complete', c);
331
event.fire(o.id, o.c);
333
Y.log('Transaction ' + o.id + ' completed.', 'info', 'io');
337
* @description Fires event "io:success" and creates, fires a
338
* transaction-specific "success" event, if config.on.success is
344
* @param {object} o - transaction object.
345
* @param {object} c - configuration object for the transaction.
349
function _ioSuccess(o, c) {
350
// Set default value of argument c, property "on" to Object if
351
// the property is null or undefined.
353
var m = Y.io._fn || {},
354
fn = (m && m[o.id]) ? m[o.id] : null,
358
c.on.success = fn.success;
360
//Decode the response from IO.swf
361
o.c.responseText = decodeURI(o.c.responseText);
364
Y.fire(E_SUCCESS, o.id, o.c);
366
event = _tPubSub('success', c);
367
event.fire(o.id, o.c);
370
_destroy(o, (c.xdr) ? true : false );
371
Y.log('HTTP Status evaluates to Success. The transaction is: ' + o.id, 'info', 'io');
375
* @description Fires event "io:failure" and creates, fires a
376
* transaction-specific "failure" event, if config.on.failure is
382
* @param {object} o - transaction object.
383
* @param {object} c - configuration object for the transaction.
387
function _ioFailure(o, c) {
388
// Set default value of argument c, property "on" to Object if
389
// the property is null or undefined.
391
var m = Y.io._fn || {},
392
fn = (m && m[o.id]) ? m[o.id] : null,
396
c.on.failure = fn.failure;
398
//Decode the response from IO.swf
399
o.c.responseText = decodeURI(o.c.responseText);
402
Y.fire(E_FAILURE, o.id, o.c);
404
event = _tPubSub('failure', c);
405
event.fire(o.id, o.c);
408
_destroy(o, (c.xdr) ? true : false );
409
Y.log('HTTP Status evaluates to Failure. The transaction is: ' + o.id, 'info', 'io');
413
* @description Fires event "io:abort" and creates, fires a
414
* transaction-specific "abort" event, if config.on.abort is
420
* @param {object} o - Transaction object generated by _create().
421
* @param {object} c - Configuration object passed to YUI.io().
425
function _ioAbort(o, c) {
426
// Set default value of argument c, property "on" to Object if
427
// the property is null or undefined.
429
var m = Y.io._fn || {},
430
fn = (m && m[o.id]) ? m[o.id] : null,
433
if(o && o.c && !c.xdr) {
434
// Terminate the transaction
437
// Clear the timeout poll for this specific transaction.
445
c.on.abort = fn.abort;
449
Y.fire(E_ABORT, o.id);
451
event = _tPubSub('abort', c);
455
_destroy(o, (c.xdr) ? true : false );
456
Y.log('Transaction timeout or explicit abort. The transaction is: ' + o.id, 'info', 'io');
460
* @description Method that increments _transactionId for each transaction.
468
var id = transactionId;
471
Y.log('Transaction id generated. The id is: ' + id, 'info', 'io');
476
* @description Method that creates a unique transaction object for each
482
* @param {number} s - URI or root data.
483
* @param {number} c - configuration object
486
function _create(i, c) {
488
o.id = Y.Lang.isNumber(i) ? i : _id();
491
o.c = Y.io._transportMap[c.xdr.use];
493
else if (c.form && c.form.upload) {
504
* @description Method that creates the XMLHttpRequest transport
512
return (w.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
516
* @description Method that concatenates string data for HTTP GET transactions.
521
* @param {string} s - URI or root data.
522
* @param {string} d - data to be concatenated onto URI.
525
function _concat(s, d) {
526
s += ((s.indexOf('?') == -1) ? '?' : '&') + d;
531
* @description Method that stores default client headers for all transactions.
532
* If a label is passed with no value argument, the header will be deleted.
537
* @param {string} l - HTTP header
538
* @param {string} v - HTTP header value
541
function _setHeader(l, v) {
551
* @description Method that sets all HTTP headers to be sent in a transaction.
553
* @method _setHeaders
556
* @param {object} o - XHR instance for the specific transaction.
557
* @param {object} h - HTTP headers for the specific transaction, as defined
558
* in the configuration object passed to YUI.io().
561
function _setHeaders(o, h) {
565
for (p in _headers) {
566
if (_headers.hasOwnProperty(p)) {
568
// Configuration headers will supersede IO preset headers,
570
Y.log('Matching configuration HTTP header: ' + p + ' found with value of ' + _headers[p], 'info', 'io');
575
Y.log('HTTP header ' + p + ' found with value of ' + _headers[p], 'info', 'io');
581
if (h.hasOwnProperty(p)) {
582
o.setRequestHeader(p, h[p]);
583
Y.log('HTTP Header ' + p + ' set with value of ' + h[p], 'info', 'io');
588
function _open(o, m, uri) {
589
o.open(m, uri, true);
593
* @description Method that sends the transaction request.
598
* @param {object} o - Transaction object generated by _create().
599
* @param {string} d - Transaction data.
600
* @param {object} c - Configuration object passed to YUI.io().
603
function _async(o, d, c) {
609
* @description Starts timeout count if the configuration object
610
* has a defined timeout property.
612
* @method _startTimeout
615
* @param {object} o - Transaction object generated by _create().
616
* @param {object} c - Configuration object passed to YUI.io().
619
function _startTimeout(o, c) {
620
_timeout[o.id] = w.setTimeout(function() { _ioAbort(o, c); }, c.timeout);
624
* @description Clears the timeout interval started by _startTimeout().
626
* @method _clearTimeout
629
* @param {number} id - Transaction id.
632
function _clearTimeout(id) {
633
w.clearTimeout(_timeout[id]);
638
* @description Event handler bound to onreadystatechange.
640
* @method _readyState
643
* @param {object} o - Transaction object generated by _create().
644
* @param {object} c - Configuration object passed to YUI.io().
647
function _readyState(o, c) {
648
if (o.c.readyState === 4) {
653
_handleResponse(o, c);
658
* @description Method that determines if a transaction response qualifies
659
* as success or failure, based on the response HTTP status code, and
660
* fires the appropriate success or failure events.
662
* @method _handleResponse
665
* @param {object} o - Transaction object generated by _create().
666
* @param {object} c - Configuration object passed to YUI.io().
669
function _handleResponse(o, c) {
672
if (o.c.status && o.c.status !== 0) {
681
Y.log('HTTP status unreadable. The transaction is: ' + o.id, 'warn', 'io');
685
* IE reports HTTP 204 as HTTP 1223.
686
* However, the response data are still available.
688
* setTimeout() is used to prevent transactions from becoming
689
* synchronous, in IE, when the response data are read from cache
692
if (status >= 200 && status < 300 || status === 1223) {
693
w.setTimeout( function() { _ioSuccess(o, c); }, 0);
696
w.setTimeout( function() {_ioFailure(o, c); }, 0);
700
function _destroy(o, isTransport) {
701
// IE6 will throw a "Type Mismatch" error if the event handler is set to "null".
702
if(w.XMLHttpRequest && !isTransport) {
704
o.c.onreadystatechange = null;
712
_io.start = _ioStart;
713
_io.complete = _ioComplete;
714
_io.success = _ioSuccess;
715
_io.failure = _ioFailure;
716
_io.abort = _ioAbort;
718
_io._timeout = _timeout;
720
//--------------------------------------
721
// Begin public interface definition
722
//--------------------------------------
724
* @description Method that stores default client headers for all transactions.
725
* If a label is passed with no value argument, the header will be deleted.
726
* This is the interface for _setHeader().
731
* @param {string} l - HTTP header
732
* @param {string} v - HTTP header value
735
_io.header = _setHeader;
738
* @description Method for requesting a transaction. This
739
* is the interface for _io().
744
* @param {string} uri - qualified path to transaction resource.
745
* @param {object} c - configuration object for the transaction.