2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
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 //--------------------------------------
109 * @description Method for requesting a transaction. _io() is implemented as
110 * yui.io(). Each transaction may include a configuration object. Its
113 * method: HTTP method verb (e.g., GET or POST). If this property is not
114 * not defined, the default value will be GET.
116 * data: This is the name-value string that will be sent as the transaction
117 * data. If the request is HTTP GET, the data become part of
118 * querystring. If HTTP POST, the data are sent in the message body.
120 * xdr: Defines the transport to be used for cross-domain requests. By
121 * setting this property, the transaction will use the specified
122 * transport instead of XMLHttpRequest. Currently, the only alternate
123 * transport supported is Flash (e.g., { xdr: 'flash' }).
125 * form: This is a defined object used to process HTML form as data. The
128 * id: object, //HTML form object or id of HTML form
129 * useDisabled: boolean, //Allow disabled HTML form field values
130 * to be sent as part of the data.
133 * on: This is a defined object used to create and handle specific
134 * events during a transaction lifecycle. These events will fire in
135 * addition to the global io events. The events are:
136 * start - This event is fired when a request is sent to a resource.
137 * complete - This event fires when the transaction is complete.
138 * success - This event fires when the response status resolves to
140 * failure - This event fires when the response status resolves to
141 * HTTP 4xx, 5xx; and, for all transaction exceptions,
142 * including aborted transactions and transaction timeouts.
143 * end - This even is fired at the conclusion of the transaction
144 * lifecycle, after a success or failure resolution.
146 * The properties are:
148 * start: function(id, args){},
149 * complete: function(id, responseobject, args){},
150 * success: function(id, responseobject, args){},
151 * failure: function(id, responseobject, args){},
152 * end: function(id, args){}
154 * Each property can reference a function or be written as an
157 * context: Object reference for an event handler when it is implemented
158 * as a method of a base object. Defining "context" will preserve
159 * the proper reference of "this" used in the event handler.
160 * headers: This is a defined object of client headers, as many as.
161 * desired for the transaction. These headers are sentThe object
167 * timeout: This value, defined as milliseconds, is a time threshold for the
168 * transaction. When this threshold is reached, and the transaction's
169 * Complete event has not yet fired, the transaction will be aborted.
170 * arguments: Object, array, string, or number passed to all registered
171 * event handlers. This value is available as the second
172 * argument in the "start" and "abort" event handlers; and, it is
173 * the third argument in the "complete", "success", and "failure"
179 * @param {string} uri - qualified path to transaction resource.
180 * @param {object} c - configuration object for the transaction.
181 * @param {number} i - transaction id, if already set by queue.
184 function _io(uri, c, i) {
187 o = _create(c.xdr || c.form, i);
188 m = c.method ? c.method.toUpperCase() : 'GET';
192 return Y.io._upload(o, uri, c);
195 f = Y.io._serialize(c.form, c.data);
198 _setHeader('Content-Type', 'application/x-www-form-urlencoded');
200 else if (m === 'GET') {
201 uri = _concat(uri, f);
205 else if (c.data && m === 'GET') {
206 uri = _concat(uri, c.data);
210 if (c.xdr.use === 'native' && window.XDomainRequest || c.xdr.use === 'flash') {
211 return Y.io.xdr(uri, o, c);
213 if (c.xdr.credentials) {
214 o.c.withCredentials = true;
218 o.c.onreadystatechange = function() { _readyState(o, c); };
220 o.c.open(m, uri, true);
224 // This exception is usually thrown by browsers
225 // that do not support native XDR transactions.
226 return _resend(o, uri, c);
230 if (c.data && m === 'POST') {
231 _setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
234 _setHeaders(o.c, c.headers || {});
236 // Using "null" will result in a POST request with
237 // no Content-Length defined.
238 o.c.send(c.data || '');
242 // This exception is usually thrown by browsers
243 // that do not support native XDR transactions.
244 return _resend(o, uri, c);
249 // If config.timeout is defined, and the request is standard XHR,
250 // initialize timeout polling.
252 _startTimeout(o, c.timeout);
258 return o.c ? _ioCancel(o, 'abort') : false;
260 isInProgress: function() {
261 return o.c ? o.c.readyState !== 4 && o.c.readyState !== 0 : false;
267 * @description Method for creating and subscribing transaction events.
272 * @param {string} e - event to be published
273 * @param {object} c - configuration data subset for event subscription.
277 function _subscribe(e, c){
278 var evt = new Y.EventTarget().publish('transaction:' + e);
279 evt.subscribe(c.on[e], (c.context || Y), c.arguments);
285 * @description Fires event "io:start" and creates, fires a
286 * transaction-specific start event, if config.on.start is
292 * @param {number} id - transaction id
293 * @param {object} c - configuration object for the transaction.
297 function _ioStart(id, c) {
299 // Set default value of argument c, property "on" to Object if
300 // the property is null or undefined.
305 evt = _subscribe('start', c);
312 * @description Fires event "io:complete" and creates, fires a
313 * transaction-specific "complete" event, if config.on.complete is
316 * @method _ioComplete
319 * @param {object} o - transaction object.
320 * @param {object} c - configuration object for the transaction.
324 function _ioComplete(o, c) {
326 r = o.status ? { status: 0, statusText: o.status } : o.c;
327 // Set default value of argument c, property "on" to Object if
328 // the property is null or undefined.
331 Y.fire(E_COMPLETE, o.id, r);
333 evt = _subscribe('complete', c);
339 * @description Fires event "io:success" and creates, fires a
340 * transaction-specific "success" event, if config.on.success is
346 * @param {object} o - transaction object.
347 * @param {object} c - configuration object for the transaction.
351 function _ioSuccess(o, c) {
353 // Set default value of argument c, property "on" to Object if
354 // the property is null or undefined.
357 Y.fire(E_SUCCESS, o.id, o.c);
359 evt = _subscribe('success', c);
367 * @description Fires event "io:failure" and creates, fires a
368 * transaction-specific "failure" event, if config.on.failure is
374 * @param {object} o - transaction object.
375 * @param {object} c - configuration object for the transaction.
379 function _ioFailure(o, c) {
381 r = o.status ? { status: 0, statusText: o.status } : o.c;
382 // Set default value of argument c, property "on" to Object if
383 // the property is null or undefined.
386 Y.fire(E_FAILURE, o.id, r);
388 evt = _subscribe('failure', c);
396 * @description Fires event "io:end" and creates, fires a
397 * transaction-specific "end" event, if config.on.end is
403 * @param {object} o - transaction object.
404 * @param {object} c - configuration object for the transaction.
408 function _ioEnd(o, c) {
410 // Set default value of argument c, property "on" to Object if
411 // the property is null or undefined.
416 evt = _subscribe('end', c);
420 _destroy(o, c.xdr ? true : false );
424 * @description Terminates a transaction due to an explicit abort or
430 * @param {object} o - Transaction object generated by _create().
431 * @param {string} s - Identifies timed out or aborted transaction.
435 function _ioCancel(o, s) {
443 * @description Resends an XDR transaction, using the Flash tranport,
444 * if the native transport fails.
450 * @param {object} o - Transaction object generated by _create().
451 * @param {string} uri - qualified path to transaction resource.
452 * @param {object} c - configuration object for the transaction.
456 function _resend(o, uri, c) {
457 var id = parseInt(o.id);
462 return Y.io(uri, c, id);
466 * @description Method that increments _transactionId for each transaction.
474 var id = transactionId;
482 * @description Method that creates a unique transaction object for each
488 * @param {number} c - configuration object subset to determine if
489 * the transaction is an XDR or file upload,
490 * requiring an alternate transport.
491 * @param {number} i - transaction id
494 function _create(c, i) {
496 o.id = Y.Lang.isNumber(i) ? i : _id();
499 if (!c.use && !c.upload) {
503 if (c.use === 'flash') {
504 o.c = Y.io._transport[c.use];
506 else if (c.use === 'native' && window.XDomainRequest) {
507 o.c = new XDomainRequest();
521 * @description Method that creates the XMLHttpRequest transport
529 return w.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
533 * @description Method that concatenates string data for HTTP GET transactions.
538 * @param {string} s - URI or root data.
539 * @param {string} d - data to be concatenated onto URI.
542 function _concat(s, d) {
543 s += ((s.indexOf('?') == -1) ? '?' : '&') + d;
548 * @description Method that stores default client headers for all transactions.
549 * If a label is passed with no value argument, the header will be deleted.
554 * @param {string} l - HTTP header
555 * @param {string} v - HTTP header value
558 function _setHeader(l, v) {
568 * @description Method that sets all HTTP headers to be sent in a transaction.
570 * @method _setHeaders
573 * @param {object} o - XHR instance for the specific transaction.
574 * @param {object} h - HTTP headers for the specific transaction, as defined
575 * in the configuration object passed to YUI.io().
578 function _setHeaders(o, h) {
581 for (p in _headers) {
582 if (_headers.hasOwnProperty(p)) {
584 // Configuration headers will supersede IO preset headers,
595 if (h.hasOwnProperty(p)) {
596 o.setRequestHeader(p, h[p]);
602 * @description Starts timeout count if the configuration object
603 * has a defined timeout property.
605 * @method _startTimeout
608 * @param {object} o - Transaction object generated by _create().
609 * @param {object} c - Configuration object passed to YUI.io().
612 function _startTimeout(o, timeout) {
613 _timeout[o.id] = w.setTimeout(function() { _ioCancel(o, 'timeout'); }, timeout);
617 * @description Clears the timeout interval started by _startTimeout().
619 * @method _clearTimeout
622 * @param {number} id - Transaction id.
625 function _clearTimeout(id) {
626 w.clearTimeout(_timeout[id]);
631 * @description Event handler bound to onreadystatechange.
633 * @method _readyState
636 * @param {object} o - Transaction object generated by _create().
637 * @param {object} c - Configuration object passed to YUI.io().
640 function _readyState(o, c) {
641 if (o.c.readyState === 4) {
649 _handleResponse(o, c);
655 * @description Method that determines if a transaction response qualifies
656 * as success or failure, based on the response HTTP status code, and
657 * fires the appropriate success or failure events.
659 * @method _handleResponse
662 * @param {object} o - Transaction object generated by _create().
663 * @param {object} c - Configuration object passed to io().
666 function _handleResponse(o, c) {
669 if (o.c.status && o.c.status !== 0) {
680 // IE reports HTTP 204 as HTTP 1223.
681 if (status >= 200 && status < 300 || status === 1223) {
689 function _destroy(o, transport) {
690 // IE, when using XMLHttpRequest as an ActiveX Object, will throw
691 // a "Type Mismatch" error if the event handler is set to "null".
692 if(w.XMLHttpRequest && !transport) {
694 o.c.onreadystatechange = null;
702 _io.start = _ioStart;
703 _io.complete = _ioComplete;
704 _io.success = _ioSuccess;
705 _io.failure = _ioFailure;
708 _io._timeout = _timeout;
710 //--------------------------------------
711 // Begin public interface definition
712 //--------------------------------------
714 * @description Method that stores default client headers for all transactions.
715 * If a label is passed with no value argument, the header will be deleted.
716 * This is the interface for _setHeader().
721 * @param {string} l - HTTP header
722 * @param {string} v - HTTP header value
725 _io.header = _setHeader;
728 * @description Method for requesting a transaction. This
729 * is the interface for _io().
734 * @param {string} uri - qualified path to transaction resource.
735 * @param {object} c - configuration object for the transaction.
743 }, '3.0.0' ,{requires:['event-custom-base']});
745 YUI.add('io-form', function(Y) {
748 * Extends the IO base class to enable HTML form data serialization, when specified
749 * in the transaction's configuration object.
756 * @description Method to enumerate through an HTML form's elements collection
757 * and return a string comprised of key-value pairs.
762 * @param {object} c - YUI form node or HTML form id.
763 * @param {string} s - Transaction data defined in the configuration.
766 _serialize: function(c, s) {
767 var eUC = encodeURIComponent,
769 useDf = c.useDisabled || false,
771 id = (typeof c.id === 'string') ? c.id : c.id.getAttribute('id'),
772 e, f, n, v, d, i, il, j, jl, o;
776 c.id.setAttribute('id', id);
779 f = Y.config.doc.getElementById(id);
781 // Iterate over the form elements collection to construct the
782 // label-value pairs.
783 for (i = 0, il = f.elements.length; i < il; ++i) {
788 if ((useDf) ? n : (n && !d)) {
789 n = encodeURIComponent(n) + '=';
790 v = encodeURIComponent(e.value);
793 // Safari, Opera, FF all default options.value from .text if
794 // value attribute not specified in markup
796 if (e.selectedIndex > -1) {
797 o = e.options[e.selectedIndex];
798 data[item++] = n + eUC((o.attributes.value && o.attributes.value.specified) ? o.value : o.text);
801 case 'select-multiple':
802 if (e.selectedIndex > -1) {
803 for (j = e.selectedIndex, jl = e.options.length; j < jl; ++j) {
806 data[item++] = n + eUC((o.attributes.value && o.attributes.value.specified) ? o.value : o.text);
814 data[item++] = n + v;
818 // stub case as XMLHttpRequest will only send the file path as a string.
820 // stub case for fieldset element which returns undefined.
822 // stub case for input type reset button.
824 // stub case for input type button elements.
828 data[item++] = n + v;
832 return s ? data.join('&') + "&" + s : data.join('&');
838 }, '3.0.0' ,{requires:['io-base','node-base','node-style']});
840 YUI.add('io-xdr', function(Y) {
843 * Extends the IO base class to provide an alternate, Flash transport, for making
844 * cross-domain requests.
851 * @description This event is fired by YUI.io when the specified transport is
855 var E_XDR_READY = 'io:xdrReady',
859 * @description Object that stores callback handlers for cross-domain requests
860 * when using Flash as the transport.
870 * @description Map of transaction state used when XDomainRequest is the
881 * @description Method that creates the Flash transport swf.
886 * @param {string} uri - location of io.swf.
887 * @param {string} yid - YUI instance id.
890 function _swf(uri, yid) {
891 var o = '<object id="yuiIoSwf" type="application/x-shockwave-flash" data="' +
892 uri + '" width="0" height="0">' +
893 '<param name="movie" value="' + uri + '">' +
894 '<param name="FlashVars" value="yid=' + yid + '">' +
895 '<param name="allowScriptAccess" value="always">' +
897 c = document.createElement('div');
899 document.body.appendChild(c);
904 * @description Sets event handlers for XDomainRequest transactions.
909 * @param {object} o - Transaction object generated by _create() in io-base.
910 * @param {object} c - configuration object for the transaction.
913 function _xdr(o, c) {
914 o.c.onprogress = function() { _rS[o.id] = 3; }
915 o.c.onload = function() {
917 Y.io.xdrResponse(o, c, 'success');
919 o.c.onerror = function() {
921 Y.io.xdrResponse(o, c, 'failure');
924 o.c.ontimeout = function() {
926 Y.io.xdrResponse(o, c, 'timeout');
928 o.c.timeout = c.timeout;
933 * @description Creates a response object for XDR transactions, for success
939 * @param {object} o - Transaction object generated by _create() in io-base.
940 * @param {boolean} isFlash - True if Flash was used as the transport.
941 * @param {boolean} isXML - True if the response data are XML.
945 function _data(o, isFlash, isXML) {
949 text = isFlash ? decodeURI(o.c.responseText) : o.c.responseText;
950 xml = isXML ? Y.DataType.XML.parse(text) : null;
952 return { id: o.id, c: { responseText: text, responseXML: xml } };
955 return { id: o.id, status: o.status };
961 * @description Method for intiating an XDR transaction abort.
966 * @param {object} o - Transaction object generated by _create() in io-base.
967 * @param {object} c - configuration object for the transaction.
969 function _abort(o, c) {
970 return c.xdr.use === 'flash' ? o.c.abort(o.id, c) : o.c.abort();
974 * @description Method for determining if an XDR transaction has completed
975 * and all data are received.
977 * @method _isInProgress.
980 * @param {object} o - Transaction object generated by _create() in io-base.
981 * @param {object} c - configuration object for the transaction.
983 function _isInProgress(o, t) {
984 return (t === 'flash' && o.c) ? o.c.isInProgress(o.id) : _rS[o.id] !== 4;
990 * @description Map of io transports.
992 * @property _transport
1000 * @description Method for accessing the transport's interface for making a
1001 * cross-domain transaction.
1006 * @param {string} uri - qualified path to transaction resource.
1007 * @param {object} o - Transaction object generated by _create() in io-base.
1008 * @param {object} c - configuration object for the transaction.
1010 xdr: function(uri, o, c) {
1011 if (c.on && c.xdr.use === 'flash') {
1015 arguments: c.arguments
1017 // These nodes do not need to be serialized across Flash's
1018 // ExternalInterface. Doing so will result in exceptions.
1022 o.c.send(uri, c, o.id);
1024 else if (window.XDomainRequest) {
1026 o.c.open(c.method || 'GET', uri);
1033 return o.c ? _abort(o, c) : false;
1035 isInProgress: function() {
1036 return o.c ? _isInProgress(o, c.xdr.use) : false;
1042 * @description Response controller for cross-domain requests when using the
1043 * Flash transport or IE8's XDomainRequest object.
1045 * @method xdrResponse
1048 * @param {object} o - Transaction object generated by _create() in io-base.
1049 * @param {object} c - configuration object for the transaction.
1050 * @param {string} e - Event name
1053 xdrResponse: function(o, c, e) {
1055 isFlash = c.xdr.use === 'flash' ? true : false,
1056 isXML = c.xdr.dataType === 'xml' ? true : false;
1061 fn = m[o.id] ? m[o.id] : null;
1064 c.context = fn.context;
1065 c.arguments = fn.arguments;
1068 if (e === ('abort' || 'timeout')) {
1074 Y.io.start(o.id, c);
1077 Y.io.success(_data(o, isFlash, isXML), c);
1078 isFlash ? delete m[o.id] : delete _rS[o.id];
1083 Y.io.failure(_data(o, isFlash, isXML), c);
1084 isFlash ? delete m[o.id] : delete _rS[o.id];
1090 * @description Fires event "io:xdrReady"
1095 * @param {number} id - transaction id
1096 * @param {object} c - configuration object for the transaction.
1100 xdrReady: function(id) {
1101 Y.fire(E_XDR_READY, id);
1105 * @description Method to initialize the desired transport.
1110 * @param {object} o - object of transport configurations.
1113 transport: function(o) {
1114 var id = o.yid ? o.yid : Y.id;
1117 this._transport.flash = Y.config.doc.getElementById('yuiIoSwf');
1123 }, '3.0.0' ,{requires:['io-base','datatype-xml']});
1125 YUI.add('io-upload-iframe', function(Y) {
1128 * Extends the IO base class to enable file uploads, with HTML forms,
1129 * using an iframe as the transport medium.
1131 * @submodule io-upload-iframe
1134 var w = Y.config.win;
1136 * @description Parses the POST data object and creates hidden form elements
1137 * for each key-value, and appends them to the HTML form object.
1138 * @method appendData
1141 * @param {object} f HTML form object.
1142 * @param {string} s The key-value POST data.
1143 * @return {array} o Array of created fields.
1145 function _addData(f, s) {
1150 for (i = 0, l = m.length - 1; i < l; i++) {
1151 o[i] = document.createElement('input');
1152 o[i].type = 'hidden';
1153 o[i].name = m[i].substring(m[i].lastIndexOf('&') + 1);
1154 o[i].value = (i + 1 === l) ? m[i + 1] : m[i + 1].substring(0, (m[i + 1].lastIndexOf('&')));
1155 f.appendChild(o[i]);
1162 * @description Removes the custom fields created to pass additional POST
1163 * data, along with the HTML form fields.
1167 * @param {object} f HTML form object.
1168 * @param {object} o HTML form fields created from configuration.data.
1171 function _removeData(f, o) {
1174 for(i = 0, l = o.length; i < l; i++){
1175 f.removeChild(o[i]);
1180 * @description Sets the appropriate attributes and values to the HTML
1181 * form, in preparation of a file upload transaction.
1185 * @param {object} f HTML form object.
1186 * @param {object} id The Transaction ID.
1187 * @param {object} uri Qualified path to transaction resource.
1190 function _setAttrs(f, id, uri) {
1191 var ie8 = (document.documentMode && document.documentMode === 8) ? true : false;
1193 f.setAttribute('action', uri);
1194 f.setAttribute('method', 'POST');
1195 f.setAttribute('target', 'ioupload' + id );
1196 f.setAttribute(Y.UA.ie && !ie8 ? 'encoding' : 'enctype', 'multipart/form-data');
1200 * @description Sets the appropriate attributes and values to the HTML
1201 * form, in preparation of a file upload transaction.
1202 * @method _resetAttrs
1205 * @param {object} f HTML form object.
1206 * @param {object} a Object of original attributes.
1209 function _resetAttrs(f, a){
1213 if (a.hasOwnProperty(a, p)) {
1215 f.setAttribute(p, f[p]);
1218 f.removeAttribute(p);
1225 * @description Creates the iframe transported used in file upload
1226 * transactions, and binds the response event handler.
1231 * @param {object} o Transaction object generated by _create().
1232 * @param {object} c Configuration object passed to YUI.io().
1235 function _create(o, c) {
1236 var i = Y.Node.create('<iframe id="ioupload' + o.id + '" name="ioupload' + o.id + '" />');
1237 i._node.style.position = 'absolute';
1238 i._node.style.top = '-1000px';
1239 i._node.style.left = '-1000px';
1241 Y.one('body').appendChild(i);
1242 // Bind the onload handler to the iframe to detect the file upload response.
1243 Y.on("load", function() { _handle(o, c) }, '#ioupload' + o.id);
1247 * @description Bound to the iframe's Load event and processes
1248 * the response data.
1252 * @param {o} o The transaction object
1253 * @param {object} c Configuration object for the transaction.
1256 function _handle(o, c) {
1257 var d = Y.one('#ioupload' + o.id).get('contentWindow.document'),
1259 xml = (d._node.nodeType === 9),
1263 _clearTimeout(o.id);
1267 // When a response Content-Type of "text/plain" is used, Firefox and Safari
1268 // will wrap the response string with <pre></pre>.
1269 p = b.query('pre:first-child');
1270 o.c.responseText = p ? p.get('innerHTML') : b.get('innerHTML');
1273 o.c.responseXML = d._node;
1276 Y.io.complete(o, c);
1278 // The transaction is complete, so call _destroy to remove
1279 // the event listener bound to the iframe transport, and then
1280 // destroy the iframe.
1281 w.setTimeout( function() { _destroy(o.id); }, 0);
1285 * @description Starts timeout count if the configuration object
1286 * has a defined timeout property.
1288 * @method _startTimeout
1291 * @param {object} o Transaction object generated by _create().
1292 * @param {object} c Configuration object passed to YUI.io().
1295 function _startTimeout(o, c) {
1296 Y.io._timeout[o.id] = w.setTimeout(
1298 var r = { id: o.id, status: 'timeout' };
1300 Y.io.complete(r, c);
1306 * @description Clears the timeout interval started by _startTimeout().
1307 * @method _clearTimeout
1310 * @param {number} id - Transaction ID.
1313 function _clearTimeout(id) {
1314 w.clearTimeout(Y.io._timeout[id]);
1315 delete Y.io._timeout[id];
1323 * @param {o} o The transaction object
1324 * @param {object} uri Qualified path to transaction resource.
1325 * @param {object} c Configuration object for the transaction.
1328 function _destroy(id) {
1329 Y.Event.purgeElement('#ioupload' + id, false);
1330 Y.one('body').removeChild(Y.one('#ioupload' + id));
1335 * @description Uploads HTML form data, inclusive of files/attachments,
1336 * using the iframe created in _create to facilitate the transaction.
1340 * @param {o} o The transaction object
1341 * @param {object} uri Qualified path to transaction resource.
1342 * @param {object} c Configuration object for the transaction.
1345 _upload: function(o, uri, c) {
1346 var f = (typeof c.form.id === 'string') ? Y.config.doc.getElementById(c.form.id) : c.form.id,
1348 // Track original HTML form attribute values.
1350 action: f.getAttribute('action'),
1351 target: f.getAttribute('target')
1355 // Initialize the HTML form properties in case they are
1356 // not defined in the HTML form.
1357 _setAttrs(f, o.id, uri);
1359 fields = _addData(f, c.data);
1362 // Start polling if a callback is present and the timeout
1363 // property has been defined.
1365 _startTimeout(o, c);
1368 // Start file upload.
1370 Y.io.start(o.id, c);
1372 _removeData(f, fields);
1374 // Restore HTML form attributes to their original values.
1375 _resetAttrs(f, attr);
1380 var r = { id: o.id, status: 'abort' };
1382 if (Y.one('#ioupload' + o.id)) {
1384 Y.io.complete(r, c);
1391 isInProgress: function() {
1392 return Y.one('#ioupload' + o.id) ? true : false;
1400 }, '3.0.0' ,{requires:['io-base','node-base','event-base']});
1402 YUI.add('io-queue', function(Y) {
1405 * Extends the IO base class to implement Queue for synchronous
1406 * transaction processing.
1408 * @submodule io-queue
1412 * @description Array of transactions queued for processing
1419 var _q = new Y.Queue(),
1422 * @description Reference to "io:complete" event handler.
1433 * @description Property to determine whether the queue is set to
1434 * 1 (active) or 0 (inactive). When inactive, transactions
1435 * will be stored in the queue until the queue is set to active.
1445 * @description Method for requesting a transaction, and queueing the
1446 * request before it is sent to the resource.
1453 function _queue(uri, c) {
1454 var o = { uri: uri, id: Y.io._id(), cfg:c };
1457 if (_qState === 1) {
1465 * @description Method Process the first transaction from the
1466 * queue in FIFO order.
1478 Y.io(o.uri, o.cfg, o.id);
1482 * @description Method for promoting a transaction to the top of the queue.
1489 function _unshift(o) {
1493 function _next(id) {
1495 if (_activeId === id && _q.size() > 0) {
1501 * @description Method for removing a specific, pending transaction from
1509 function _remove(o) {
1516 if (_q.size() > 0) {
1522 * @description Method for setting queue processing to inactive.
1523 * Transaction requests to YUI.io.queue() will be stored in the queue, but
1524 * not processed until the queue is reset to "active".
1536 * @description Method to query the current size of the queue.
1547 _e = Y.on('io:complete', function(id) { _next(id); }, Y.io);
1550 * @description Method to query the current size of the queue, or to
1551 * set a maximum queue size. This is the interface for _size().
1556 * @param {number} i - Specified maximum size of queue.
1559 _queue.size = _size;
1562 * @description Method for setting the queue to active. If there are
1563 * transactions pending in the queue, they will be processed from the
1564 * queue in FIFO order. This is the interface for _start().
1571 _queue.start = _start;
1574 * @description Method for setting queue processing to inactive.
1575 * Transaction requests to YUI.io.queue() will be stored in the queue, but
1576 * not processed until the queue is restarted. This is the
1577 * interface for _stop().
1584 _queue.stop = _stop;
1587 * @description Method for promoting a transaction to the top of the queue.
1588 * This is the interface for _unshift().
1593 * @param {Object} o - Reference to queued transaction.
1596 _queue.promote = _unshift;
1599 * @description Method for removing a specific, pending transaction from
1600 * the queue. This is the interface for _remove().
1605 * @param {Object} o - Reference to queued transaction.
1608 _queue.remove = _remove;
1616 }, '3.0.0' ,{requires:['io-base','queue-promote']});
1620 YUI.add('io', function(Y){}, '3.0.0' ,{use:['io-base', 'io-form', 'io-xdr', 'io-upload-iframe', 'io-queue']});