2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('io-base', function(Y) {
11 * Base IO functionality. Provides basic XHR transport support.
17 * The io class is a utility that brokers HTTP requests through a simplified
18 * interface. Specifically, it allows JavaScript to make HTTP requests to
19 * a resource without a page reload. The underlying transport for making
20 * same-domain requests is the XMLHttpRequest object. YUI.io can also use
21 * Flash, if specified as a transport, for cross-domain requests.
28 * @description This event is fired by YUI.io when a transaction is initiated.
31 var E_START = 'io:start',
35 * @description This event is fired by YUI.io when a transaction is complete.
36 * Response status and data are accessible, if available.
39 E_COMPLETE = 'io:complete',
43 * @description This event is fired by YUI.io when a transaction is complete, and
44 * the HTTP status resolves to HTTP2xx.
47 E_SUCCESS = 'io:success',
51 * @description This event is fired by YUI.io when a transaction is complete, and
52 * the HTTP status resolves to HTTP4xx, 5xx and above.
55 E_FAILURE = 'io:failure',
59 * @description This event signifies the end of the transaction lifecycle. The
60 * transaction transport is destroyed.
65 //--------------------------------------
67 //--------------------------------------
69 * @description A transaction counter that increments for each transaction.
71 * @property transactionId
79 * @description Object of default HTTP headers to be initialized and sent
80 * for all transactions.
88 'X-Requested-With' : 'XMLHttpRequest'
92 * @description Object that stores timeout values for any transaction with
93 * a defined "timeout" configuration property.
105 //--------------------------------------
107 //--------------------------------------
110 * @description Method that creates the XMLHttpRequest transport
118 return w.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
123 * @description Method that increments _transactionId for each transaction.
131 var id = transactionId;
139 * @description Method that creates a unique transaction object for each
145 * @param {number} c - configuration object subset to determine if
146 * the transaction is an XDR or file upload,
147 * requiring an alternate transport.
148 * @param {number} i - transaction id
151 function _create(c, i) {
153 o.id = Y.Lang.isNumber(i) ? i : _id();
156 if (!c.use && !c.upload) {
160 if (c.use === 'native') {
161 if (w.XDomainRequest) {
162 o.c = new XDomainRequest();
170 o.c = Y.io._transport[c.use];
183 function _destroy(o) {
185 if (o.c && w.XMLHttpRequest) {
186 o.c.onreadystatechange = null;
188 else if (Y.UA.ie === 6 && !o.t) {
189 // IE, when using XMLHttpRequest as an ActiveX Object, will throw
190 // a "Type Mismatch" error if the event handler is set to "null".
200 * @description Method for creating and subscribing transaction events.
205 * @param {string} e - event to be published
206 * @param {object} c - configuration data subset for event subscription.
211 var eT = new Y.EventTarget().publish('transaction:' + e),
216 eT.on(c.on[e], cT, a);
226 * @description Fires event "io:start" and creates, fires a
227 * transaction-specific start event, if config.on.start is
233 * @param {number} id - transaction id
234 * @param {object} c - configuration object for the transaction.
238 function _ioStart(id, c) {
242 Y.fire(E_START, id, a);
248 if (c.on && c.on.start) {
249 _tE('start', c).fire(id);
255 * @description Fires event "io:complete" and creates, fires a
256 * transaction-specific "complete" event, if config.on.complete is
259 * @method _ioComplete
262 * @param {object} o - transaction object.
263 * @param {object} c - configuration object for the transaction.
267 function _ioComplete(o, c) {
268 var r = o.e ? { status: 0, statusText: o.e } : o.c,
272 Y.fire(E_COMPLETE, o.id, r, a);
275 Y.fire(E_COMPLETE, o.id, r);
278 if (c.on && c.on.complete) {
279 _tE('complete', c).fire(o.id, r);
284 * @description Fires event "io:end" and creates, fires a
285 * transaction-specific "end" event, if config.on.end is
291 * @param {object} o - transaction object.
292 * @param {object} c - configuration object for the transaction.
296 function _ioEnd(o, c) {
300 Y.fire(E_END, o.id, a);
306 if (c.on && c.on.end) {
307 _tE('end', c).fire(o.id);
314 * @description Fires event "io:success" and creates, fires a
315 * transaction-specific "success" event, if config.on.success is
321 * @param {object} o - transaction object.
322 * @param {object} c - configuration object for the transaction.
326 function _ioSuccess(o, c) {
330 Y.fire(E_SUCCESS, o.id, o.c, a);
333 Y.fire(E_SUCCESS, o.id, o.c);
336 if (c.on && c.on.success) {
337 _tE('success', c).fire(o.id, o.c);
344 * @description Fires event "io:failure" and creates, fires a
345 * transaction-specific "failure" event, if config.on.failure is
351 * @param {object} o - transaction object.
352 * @param {object} c - configuration object for the transaction.
356 function _ioFailure(o, c) {
357 var r = o.e ? { status: 0, statusText: o.e } : o.c,
361 Y.fire(E_FAILURE, o.id, r, a);
364 Y.fire(E_FAILURE, o.id, r);
367 if (c.on && c.on.failure) {
368 _tE('failure', c).fire(o.id, r);
375 * @description Resends an XDR transaction, using the Flash tranport,
376 * if the native transport fails.
382 * @param {object} o - Transaction object generated by _create().
383 * @param {string} uri - qualified path to transaction resource.
384 * @param {object} c - configuration object for the transaction.
388 function _resend(o, uri, c, d) {
391 // If the original request included serialized form data and
392 // additional data are defined in configuration.data, it must
393 // be reset to prevent data duplication.
394 c.data = c.form && d ? d : null;
396 return Y.io(uri, c, o.id);
400 * @description Method that concatenates string data for HTTP GET transactions.
405 * @param {string} s - URI or root data.
406 * @param {string} d - data to be concatenated onto URI.
409 function _concat(s, d) {
410 s += ((s.indexOf('?') == -1) ? '?' : '&') + d;
415 * @description Method that stores default client headers for all transactions.
416 * If a label is passed with no value argument, the header will be deleted.
421 * @param {string} l - HTTP header
422 * @param {string} v - HTTP header value
425 function _setHeader(l, v) {
435 * @description Method that sets all HTTP headers to be sent in a transaction.
437 * @method _setHeaders
440 * @param {object} o - XHR instance for the specific transaction.
441 * @param {object} h - HTTP headers for the specific transaction, as defined
442 * in the configuration object passed to YUI.io().
445 function _setHeaders(o, h) {
449 for (p in _headers) {
450 if (_headers.hasOwnProperty(p)) {
453 // Configuration headers will supersede preset io headers,
468 if (h.hasOwnProperty(p)) {
469 if (h[p] !== 'disable') {
470 o.setRequestHeader(p, h[p]);
477 * @description Terminates a transaction due to an explicit abort or
483 * @param {object} o - Transaction object generated by _create().
484 * @param {string} s - Identifies timed out or aborted transaction.
488 function _ioCancel(o, s) {
496 * @description Starts timeout count if the configuration object
497 * has a defined timeout property.
499 * @method _startTimeout
502 * @param {object} o - Transaction object generated by _create().
503 * @param {object} t - Timeout in milliseconds.
506 function _startTimeout(o, t) {
507 _timeout[o.id] = w.setTimeout(function() { _ioCancel(o, 'timeout'); }, t);
511 * @description Clears the timeout interval started by _startTimeout().
513 * @method _clearTimeout
516 * @param {number} id - Transaction id.
519 function _clearTimeout(id) {
520 w.clearTimeout(_timeout[id]);
525 * @description Method that determines if a transaction response qualifies
526 * as success or failure, based on the response HTTP status code, and
527 * fires the appropriate success or failure events.
529 * @method _handleResponse
532 * @param {object} o - Transaction object generated by _create().
533 * @param {object} c - Configuration object passed to io().
536 function _handleResponse(o, c) {
540 status = (o.c.status && o.c.status !== 0) ? o.c.status : 0;
546 // IE reports HTTP 204 as HTTP 1223.
547 if (status >= 200 && status < 300 || status === 1223) {
556 * @description Event handler bound to onreadystatechange.
558 * @method _readyState
561 * @param {object} o - Transaction object generated by _create().
562 * @param {object} c - Configuration object passed to YUI.io().
565 function _readyState(o, c) {
566 if (o.c.readyState === 4) {
574 _handleResponse(o, c);
580 * @description Method for requesting a transaction. _io() is implemented as
581 * yui.io(). Each transaction may include a configuration object. Its
584 * method: HTTP method verb (e.g., GET or POST). If this property is not
585 * not defined, the default value will be GET.
587 * data: This is the name-value string that will be sent as the transaction
588 * data. If the request is HTTP GET, the data become part of
589 * querystring. If HTTP POST, the data are sent in the message body.
591 * xdr: Defines the transport to be used for cross-domain requests. By
592 * setting this property, the transaction will use the specified
593 * transport instead of XMLHttpRequest.
594 * The properties are:
596 * use: Specify the transport to be used: 'flash' and 'native'
597 * dataType: Set the value to 'XML' if that is the expected
598 * response content type.
602 * form: This is a defined object used to process HTML form as data. The
605 * id: Node object or id of HTML form.
606 * useDisabled: Boolean value to allow disabled HTML form field
607 * values to be sent as part of the data.
610 * on: This is a defined object used to create and handle specific
611 * events during a transaction lifecycle. These events will fire in
612 * addition to the global io events. The events are:
613 * start - This event is fired when a request is sent to a resource.
614 * complete - This event fires when the transaction is complete.
615 * success - This event fires when the response status resolves to
617 * failure - This event fires when the response status resolves to
618 * HTTP 4xx, 5xx; and, for all transaction exceptions,
619 * including aborted transactions and transaction timeouts.
620 * end - This even is fired at the conclusion of the transaction
621 * lifecycle, after a success or failure resolution.
623 * The properties are:
625 * start: function(id, arguments){},
626 * complete: function(id, responseobject, arguments){},
627 * success: function(id, responseobject, arguments){},
628 * failure: function(id, responseobject, arguments){},
629 * end: function(id, arguments){}
631 * Each property can reference a function or be written as an
634 * sync: To enable synchronous transactions, set the configuration property
635 * "sync" to true; the default behavior is false. Synchronous
636 * transactions are limited to same-domain requests only.
638 * context: Object reference for all defined transaction event handlers
639 * when it is implemented as a method of a base object. Defining
640 * "context" will set the reference of "this," used in the
641 * event handlers, to the context value. In the case where
642 * different event handlers all have different contexts,
643 * use Y.bind() to set the execution context, bypassing this
646 * headers: This is a defined object of client headers, as many as.
647 * desired for the transaction. The object pattern is:
648 * { 'header': 'value' }.
650 * timeout: This value, defined as milliseconds, is a time threshold for the
651 * transaction. When this threshold is reached, and the transaction's
652 * Complete event has not yet fired, the transaction will be aborted.
654 * arguments: Object, array, string, or number passed to all registered
655 * event handlers. This value is available as the second
656 * argument in the "start" and "abort" event handlers; and, it is
657 * the third argument in the "complete", "success", and "failure"
663 * @param {string} uri - qualified path to transaction resource.
664 * @param {object} c - configuration object for the transaction.
665 * @param {number} i - transaction id, if already set.
668 function _io(uri, c, i) {
669 var f, o, d, m, r, s, oD, a, j,
672 o = _create(c.xdr || c.form, i);
673 m = c.method ? c.method = c.method.toUpperCase() : c.method = 'GET';
677 //To serialize an object into a key-value string, add the
678 //QueryString module to the YUI instance's 'use' method.
679 if (Y.Lang.isObject(c.data) && Y.QueryString) {
680 c.data = Y.QueryString.stringify(c.data);
685 // This is a file upload transaction, calling
686 // upload() in io-upload-iframe.
687 return Y.io.upload(o, uri, c);
690 // Serialize HTML form data.
691 f = Y.io._serialize(c.form, c.data);
692 if (m === 'POST' || m === 'PUT') {
695 else if (m === 'GET') {
696 uri = _concat(uri, f);
701 if (c.data && m === 'GET') {
702 uri = _concat(uri, c.data);
705 if (c.data && m === 'POST') {
706 c.headers = Y.merge({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, c.headers);
710 return Y.io.xdr(uri, o, c);
714 o.c.onreadystatechange = function() { _readyState(o, c); };
718 o.c.open(m, uri, s ? false : true);
719 // Will work only in browsers that implement the
720 // Cross-Origin Resource Sharing draft.
721 if (c.xdr && c.xdr.credentials) {
722 o.c.withCredentials = true;
727 // This exception is usually thrown by browsers
728 // that do not support native XDR transactions.
729 return _resend(o, u, c, oD);
733 _setHeaders(o.c, c.headers);
736 // Using "null" with HTTP POST will result in a request
737 // with no Content-Length header defined.
738 o.c.send(c.data || '');
741 a = ['status', 'statusText', 'responseText', 'responseXML'];
742 r = c.arguments ? { id: o.id, arguments: c.arguments } : { id: o.id };
744 for (j = 0; j < 4; j++) {
748 r.getAllResponseHeaders = function() { return d.getAllResponseHeaders(); };
749 r.getResponseHeader = function(h) { return d.getResponseHeader(h); };
751 _handleResponse(o, c);
758 // This exception is usually thrown by browsers
759 // that do not support native XDR transactions.
760 return _resend(o, u, c, oD);
764 // If config.timeout is defined, and the request is standard XHR,
765 // initialize timeout polling.
767 _startTimeout(o, c.timeout);
773 return o.c ? _ioCancel(o, 'abort') : false;
775 isInProgress: function() {
776 return o.c ? o.c.readyState !== 4 && o.c.readyState !== 0 : false;
781 _io.start = _ioStart;
782 _io.complete = _ioComplete;
783 _io.success = _ioSuccess;
784 _io.failure = _ioFailure;
787 _io._timeout = _timeout;
789 //--------------------------------------
790 // Begin public interface definition
791 //--------------------------------------
793 * @description Method that stores default client headers for all transactions.
794 * If a label is passed with no value argument, the header will be deleted.
795 * This is the interface for _setHeader().
800 * @param {string} l - HTTP header
801 * @param {string} v - HTTP header value
804 _io.header = _setHeader;
807 * @description Method for requesting a transaction. This
808 * is the interface for _io().
813 * @param {string} uri - qualified path to transaction resource.
814 * @param {object} c - configuration object for the transaction.
822 }, '3.3.0' ,{requires:['event-custom-base', 'querystring-stringify-simple']});
824 YUI.add('io-form', function(Y) {
827 * Extends the IO base class to enable HTML form data serialization, when specified
828 * in the transaction's configuration object.
833 var eUC = encodeURIComponent;
837 * @description Method to enumerate through an HTML form's elements collection
838 * and return a string comprised of key-value pairs.
843 * @param {object} c - YUI form node or HTML form id.
844 * @param {string} s - Transaction data defined in the configuration.
847 _serialize: function(c, s) {
849 useDf = c.useDisabled || false,
851 id = (typeof c.id === 'string') ? c.id : c.id.getAttribute('id'),
852 e, f, n, v, d, i, il, j, jl, o;
856 c.id.setAttribute('id', id);
859 f = Y.config.doc.getElementById(id);
861 // Iterate over the form elements collection to construct the
862 // label-value pairs.
863 for (i = 0, il = f.elements.length; i < il; ++i) {
868 if (useDf ? n : n && !d) {
873 // Safari, Opera, FF all default options.value from .text if
874 // value attribute not specified in markup
876 if (e.selectedIndex > -1) {
877 o = e.options[e.selectedIndex];
878 data[item++] = n + eUC(o.attributes.value && o.attributes.value.specified ? o.value : o.text);
881 case 'select-multiple':
882 if (e.selectedIndex > -1) {
883 for (j = e.selectedIndex, jl = e.options.length; j < jl; ++j) {
886 data[item++] = n + eUC(o.attributes.value && o.attributes.value.specified ? o.value : o.text);
894 data[item++] = n + v;
898 // stub case as XMLHttpRequest will only send the file path as a string.
900 // stub case for fieldset element which returns undefined.
902 // stub case for input type reset button.
904 // stub case for input type button elements.
908 data[item++] = n + v;
912 return s ? data.join('&') + "&" + s : data.join('&');
918 }, '3.3.0' ,{requires:['io-base','node-base']});
920 YUI.add('io-xdr', function(Y) {
923 * Extends the IO base class to provide an alternate, Flash transport, for making
924 * cross-domain requests.
931 * @description This event is fired by YUI.io when the specified transport is
935 var E_XDR_READY = Y.publish('io:xdrReady', { fireOnce: true }),
938 * @description Object that stores callback handlers for cross-domain requests
939 * when using Flash as the transport.
949 * @description Map of transaction readyState values used when
950 * XDomainRequest is the XDR transport.
959 // Document reference
963 // IE8 cross-origin request detection
964 ie = w && w.XDomainRequest;
967 * @description Method that creates the Flash transport swf.
972 * @param {string} uri - location of io.swf.
973 * @param {string} yid - YUI instance id.
976 function _swf(uri, yid) {
977 var o = '<object id="yuiIoSwf" type="application/x-shockwave-flash" data="' +
978 uri + '" width="0" height="0">' +
979 '<param name="movie" value="' + uri + '">' +
980 '<param name="FlashVars" value="yid=' + yid + '">' +
981 '<param name="allowScriptAccess" value="always">' +
983 c = d.createElement('div');
985 d.body.appendChild(c);
990 * @description Sets event handlers for XDomainRequest transactions.
995 * @param {object} o - Transaction object generated by _create() in io-base.
996 * @param {object} c - configuration object for the transaction.
999 function _evt(o, c) {
1000 o.c.onprogress = function() { _rS[o.id] = 3; };
1001 o.c.onload = function() {
1003 Y.io.xdrResponse(o, c, 'success');
1005 o.c.onerror = function() {
1007 Y.io.xdrResponse(o, c, 'failure');
1010 o.c.ontimeout = function() {
1012 Y.io.xdrResponse(o, c, 'timeout');
1014 o.c.timeout = c.timeout;
1019 * @description Creates a response object for XDR transactions, for success
1020 * and failure cases.
1025 * @param {object} o - Transaction object generated by _create() in io-base.
1026 * @param {boolean} f - True if Flash was used as the transport.
1027 * @param {boolean} t - DataType value, as defined in the configuration.
1031 function _data(o, f, t) {
1035 s = f ? decodeURI(o.c.responseText) : o.c.responseText;
1036 x = t === 'xml' ? Y.DataType.XML.parse(s) : null;
1038 return { id: o.id, c: { responseText: s, responseXML: x } };
1041 return { id: o.id, e: o.e };
1047 * @description Method for intiating an XDR transaction abort.
1052 * @param {object} o - Transaction object generated by _create() in io-base.
1053 * @param {object} c - configuration object for the transaction.
1055 function _abort(o, c) {
1056 return o.c.abort(o.id, c);
1060 * @description Method for determining if an XDR transaction has completed
1061 * and all data are received.
1063 * @method _isInProgress.
1066 * @param {object} o - Transaction object generated by _create() in io-base.
1068 function _isInProgress(o) {
1069 return ie ? _rS[o.id] !== 4 : o.c.isInProgress(o.id);
1075 * @description Map of io transports.
1077 * @property _transport
1085 * @description Method for accessing the transport's interface for making a
1086 * cross-domain transaction.
1091 * @param {string} uri - qualified path to transaction resource.
1092 * @param {object} o - Transaction object generated by _create() in io-base.
1093 * @param {object} c - configuration object for the transaction.
1095 xdr: function(uri, o, c) {
1096 if (c.xdr.use === 'flash') {
1100 arguments: c.arguments
1102 // These properties cannot be serialized across Flash's
1103 // ExternalInterface. Doing so will result in exceptions.
1107 w.setTimeout(function() {
1108 if (o.c && o.c.send) {
1109 o.c.send(uri, c, o.id);
1112 Y.io.xdrResponse(o, c, 'transport error');
1118 o.c.open(c.method || 'GET', uri);
1122 o.c.send(uri, o, c);
1128 return o.c ? _abort(o, c) : false;
1130 isInProgress: function() {
1131 return o.c ? _isInProgress(o.id) : false;
1137 * @description Response controller for cross-domain requests when using the
1138 * Flash transport or IE8's XDomainRequest object.
1140 * @method xdrResponse
1143 * @param {object} o - Transaction object generated by _create() in io-base.
1144 * @param {object} c - configuration object for the transaction.
1145 * @param {string} e - Event name
1148 xdrResponse: function(o, c, e) {
1151 f = c.xdr.use === 'flash' ? true : false,
1156 cb = _cB[o.id] ? _cB[o.id] : null;
1159 c.context = cb.context;
1160 c.arguments = cb.arguments;
1166 Y.io.start(o.id, c);
1169 Y.io.complete(o, c);
1172 Y.io.success(t || f ? _data(o, f, t) : o, c);
1177 case 'transport error':
1180 Y.io.failure(t || f ? _data(o, f, t) : o, c);
1187 * @description Fires event "io:xdrReady"
1192 * @param {number} id - transaction id
1193 * @param {object} c - configuration object for the transaction.
1197 xdrReady: function(id) {
1199 Y.fire(E_XDR_READY, id);
1203 * @description Method to initialize the desired transport.
1208 * @param {object} o - object of transport configurations.
1211 transport: function(o) {
1212 var yid = o.yid || Y.id,
1213 oid = o.id || 'flash',
1214 src = Y.UA.ie ? o.src + '?d=' + new Date().valueOf().toString() : o.src;
1216 if (oid === 'native' || oid === 'flash') {
1219 this._transport.flash = d.getElementById('yuiIoSwf');
1222 this._transport[o.id] = o.src;
1228 * @description Delay value to calling the Flash transport, in the
1229 * event io.swf has not finished loading. Once the E_XDR_READY
1230 * event is fired, this value will be set to 0.
1237 Y.io.xdr.delay = 50;
1241 }, '3.3.0' ,{requires:['io-base','datatype-xml']});
1243 YUI.add('io-upload-iframe', function(Y) {
1246 * Extends the IO base class to enable file uploads, with HTML forms,
1247 * using an iframe as the transport medium.
1249 * @submodule io-upload-iframe
1252 var w = Y.config.win,
1254 _std = (d.documentMode && d.documentMode >= 8),
1255 _d = decodeURIComponent;
1257 * @description Parses the POST data object and creates hidden form elements
1258 * for each key-value, and appends them to the HTML form object.
1259 * @method appendData
1262 * @param {object} f HTML form object.
1263 * @param {string} s The key-value POST data.
1264 * @return {array} e Array of created fields.
1266 function _addData(f, s) {
1271 for (i = 0, l = m.length - 1; i < l; i++) {
1272 o[i] = d.createElement('input');
1273 o[i].type = 'hidden';
1274 o[i].name = _d(m[i].substring(m[i].lastIndexOf('&') + 1));
1275 o[i].value = (i + 1 === l) ? _d(m[i + 1]) : _d(m[i + 1].substring(0, (m[i + 1].lastIndexOf('&'))));
1276 f.appendChild(o[i]);
1283 * @description Removes the custom fields created to pass additional POST
1284 * data, along with the HTML form fields.
1288 * @param {object} f HTML form object.
1289 * @param {object} o HTML form fields created from configuration.data.
1292 function _removeData(f, o) {
1295 for (i = 0, l = o.length; i < l; i++) {
1296 f.removeChild(o[i]);
1301 * @description Sets the appropriate attributes and values to the HTML
1302 * form, in preparation of a file upload transaction.
1306 * @param {object} f HTML form object.
1307 * @param {object} id The Transaction ID.
1308 * @param {object} uri Qualified path to transaction resource.
1311 function _setAttrs(f, id, uri) {
1312 f.setAttribute('action', uri);
1313 f.setAttribute('method', 'POST');
1314 f.setAttribute('target', 'ioupload' + id );
1315 f.setAttribute(Y.UA.ie && !_std ? 'encoding' : 'enctype', 'multipart/form-data');
1319 * @description Reset the HTML form attributes to their original values.
1320 * @method _resetAttrs
1323 * @param {object} f HTML form object.
1324 * @param {object} a Object of original attributes.
1327 function _resetAttrs(f, a){
1331 if (a.hasOwnProperty(p)) {
1333 f.setAttribute(p, f[p]);
1336 f.removeAttribute(p);
1343 * @description Starts timeout count if the configuration object
1344 * has a defined timeout property.
1346 * @method _startTimeout
1349 * @param {object} o Transaction object generated by _create().
1350 * @param {object} c Configuration object passed to YUI.io().
1353 function _startTimeout(o, c) {
1354 Y.io._timeout[o.id] = w.setTimeout(
1356 var r = { id: o.id, status: 'timeout' };
1358 Y.io.complete(r, c);
1364 * @description Clears the timeout interval started by _startTimeout().
1365 * @method _clearTimeout
1368 * @param {number} id - Transaction ID.
1371 function _clearTimeout(id) {
1372 w.clearTimeout(Y.io._timeout[id]);
1373 delete Y.io._timeout[id];
1381 * @param {o} o The transaction object
1382 * @param {object} uri Qualified path to transaction resource.
1383 * @param {object} c Configuration object for the transaction.
1386 function _destroy(id) {
1387 Y.Event.purgeElement('#ioupload' + id, false);
1388 Y.one('body').removeChild(Y.one('#ioupload' + id));
1392 * @description Bound to the iframe's Load event and processes
1393 * the response data.
1397 * @param {o} o The transaction object
1398 * @param {object} c Configuration object for the transaction.
1401 function _handle(o, c) {
1402 var d = Y.one('#ioupload' + o.id).get('contentWindow.document'),
1407 _clearTimeout(o.id);
1411 // When a response Content-Type of "text/plain" is used, Firefox and Safari
1412 // will wrap the response string with <pre></pre>.
1413 p = b.one('pre:first-child');
1414 o.c.responseText = p ? p.get('text') : b.get('text');
1417 o.c.responseXML = d._node;
1420 Y.io.complete(o, c);
1422 // The transaction is complete, so call _destroy to remove
1423 // the event listener bound to the iframe transport, and then
1424 // destroy the iframe.
1425 w.setTimeout( function() { _destroy(o.id); }, 0);
1429 * @description Creates the iframe transported used in file upload
1430 * transactions, and binds the response event handler.
1435 * @param {object} o Transaction object generated by _create().
1436 * @param {object} c Configuration object passed to YUI.io().
1439 function _create(o, c) {
1440 var i = Y.Node.create('<iframe id="ioupload' + o.id + '" name="ioupload' + o.id + '" />');
1441 i._node.style.position = 'absolute';
1442 i._node.style.top = '-1000px';
1443 i._node.style.left = '-1000px';
1445 Y.one('body').appendChild(i);
1446 // Bind the onload handler to the iframe to detect the file upload response.
1447 Y.on("load", function() { _handle(o, c); }, '#ioupload' + o.id);
1451 * @description Uploads HTML form data, inclusive of files/attachments,
1452 * using the iframe created in _create to facilitate the transaction.
1456 * @param {o} o The transaction object
1457 * @param {object} uri Qualified path to transaction resource.
1458 * @param {object} c Configuration object for the transaction.
1461 function _send(o, uri, c) {
1462 var f = (typeof c.form.id === 'string') ? d.getElementById(c.form.id) : c.form.id,
1464 // Track original HTML form attribute values.
1466 action: f.getAttribute('action'),
1467 target: f.getAttribute('target')
1470 // Initialize the HTML form properties in case they are
1471 // not defined in the HTML form.
1472 _setAttrs(f, o.id, uri);
1474 fields = _addData(f, c.data);
1477 // Start polling if a callback is present and the timeout
1478 // property has been defined.
1480 _startTimeout(o, c);
1483 // Start file upload.
1485 Y.io.start(o.id, c);
1487 _removeData(f, fields);
1489 // Restore HTML form attributes to their original values.
1490 _resetAttrs(f, attr);
1495 var r = { id: o.id, status: 'abort' };
1497 if (Y.one('#ioupload' + o.id)) {
1499 Y.io.complete(r, c);
1506 isInProgress: function() {
1507 return Y.one('#ioupload' + o.id) ? true : false;
1513 upload: function(o, uri, c) {
1515 return _send(o, uri, c);
1521 }, '3.3.0' ,{requires:['io-base','node-base']});
1523 YUI.add('io-queue', function(Y) {
1526 * Extends the IO base class to implement Queue for synchronous
1527 * transaction processing.
1529 * @submodule io-queue
1533 * @description Array of transactions queued for processing
1540 var _q = new Y.Queue(),
1544 * @description Property to determine whether the queue is set to
1545 * 1 (active) or 0 (inactive). When inactive, transactions
1546 * will be stored in the queue until the queue is set to active.
1556 * @description Method Process the first transaction from the
1557 * queue in FIFO order.
1569 Y.io(o.uri, o.cfg, o.id);
1573 * @description Method for promoting a transaction to the top of the queue.
1580 function _unshift(o) {
1585 * @description Method for requesting a transaction, and queueing the
1586 * request before it is sent to the resource.
1593 function _queue(uri, c) {
1594 var o = { uri: uri, id: Y.io._id(), cfg:c };
1597 if (_qState === 1) {
1604 function _next(id) {
1606 if (_activeId === id && _q.size() > 0) {
1612 * @description Method for removing a specific, pending transaction from
1620 function _remove(o) {
1627 if (_q.size() > 0) {
1633 * @description Method for setting queue processing to inactive.
1634 * Transaction requests to YUI.io.queue() will be stored in the queue, but
1635 * not processed until the queue is reset to "active".
1647 * @description Method to query the current size of the queue.
1659 * @description Method to query the current size of the queue, or to
1660 * set a maximum queue size. This is the interface for _size().
1665 * @param {number} i - Specified maximum size of queue.
1668 _queue.size = _size;
1671 * @description Method for setting the queue to active. If there are
1672 * transactions pending in the queue, they will be processed from the
1673 * queue in FIFO order. This is the interface for _start().
1680 _queue.start = _start;
1683 * @description Method for setting queue processing to inactive.
1684 * Transaction requests to YUI.io.queue() will be stored in the queue, but
1685 * not processed until the queue is restarted. This is the
1686 * interface for _stop().
1693 _queue.stop = _stop;
1696 * @description Method for promoting a transaction to the top of the queue.
1697 * This is the interface for _unshift().
1702 * @param {Object} o - Reference to queued transaction.
1705 _queue.promote = _unshift;
1708 * @description Method for removing a specific, pending transaction from
1709 * the queue. This is the interface for _remove().
1714 * @param {Object} o - Reference to queued transaction.
1717 _queue.remove = _remove;
1719 Y.on('io:complete', function(id) { _next(id); }, Y.io);
1727 }, '3.3.0' ,{requires:['io-base','queue-promote']});
1731 YUI.add('io', function(Y){}, '3.3.0' ,{use:['io-base', 'io-form', 'io-xdr', 'io-upload-iframe', 'io-queue']});