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-xdr', function(Y) {
11 * Extends the IO base class to provide an alternate, Flash transport, for making
12 * cross-domain requests.
19 * @description This event is fired by YUI.io when the specified transport is
23 var E_XDR_READY = 'io:xdrReady',
27 * @description Object that stores callback handlers for cross-domain requests
28 * when using Flash as the transport.
38 * @description Map of transaction state used when XDomainRequest is the
49 * @description Method that creates the Flash transport swf.
54 * @param {string} uri - location of io.swf.
55 * @param {string} yid - YUI instance id.
58 function _swf(uri, yid) {
59 var o = '<object id="yuiIoSwf" type="application/x-shockwave-flash" data="' +
60 uri + '" width="0" height="0">' +
61 '<param name="movie" value="' + uri + '">' +
62 '<param name="FlashVars" value="yid=' + yid + '">' +
63 '<param name="allowScriptAccess" value="always">' +
65 c = document.createElement('div');
67 document.body.appendChild(c);
72 * @description Sets event handlers for XDomainRequest transactions.
77 * @param {object} o - Transaction object generated by _create() in io-base.
78 * @param {object} c - configuration object for the transaction.
82 o.c.onprogress = function() { _rS[o.id] = 3; }
83 o.c.onload = function() {
85 Y.io.xdrResponse(o, c, 'success');
87 o.c.onerror = function() {
89 Y.io.xdrResponse(o, c, 'failure');
92 o.c.ontimeout = function() {
94 Y.io.xdrResponse(o, c, 'timeout');
96 o.c.timeout = c.timeout;
101 * @description Creates a response object for XDR transactions, for success
107 * @param {object} o - Transaction object generated by _create() in io-base.
108 * @param {boolean} isFlash - True if Flash was used as the transport.
109 * @param {boolean} isXML - True if the response data are XML.
113 function _data(o, isFlash, isXML) {
117 text = isFlash ? decodeURI(o.c.responseText) : o.c.responseText;
118 xml = isXML ? Y.DataType.XML.parse(text) : null;
120 return { id: o.id, c: { responseText: text, responseXML: xml } };
123 return { id: o.id, status: o.status };
129 * @description Method for intiating an XDR transaction abort.
134 * @param {object} o - Transaction object generated by _create() in io-base.
135 * @param {object} c - configuration object for the transaction.
137 function _abort(o, c) {
138 return c.xdr.use === 'flash' ? o.c.abort(o.id, c) : o.c.abort();
142 * @description Method for determining if an XDR transaction has completed
143 * and all data are received.
145 * @method _isInProgress.
148 * @param {object} o - Transaction object generated by _create() in io-base.
149 * @param {object} c - configuration object for the transaction.
151 function _isInProgress(o, t) {
152 return (t === 'flash' && o.c) ? o.c.isInProgress(o.id) : _rS[o.id] !== 4;
158 * @description Map of io transports.
160 * @property _transport
168 * @description Method for accessing the transport's interface for making a
169 * cross-domain transaction.
174 * @param {string} uri - qualified path to transaction resource.
175 * @param {object} o - Transaction object generated by _create() in io-base.
176 * @param {object} c - configuration object for the transaction.
178 xdr: function(uri, o, c) {
179 if (c.on && c.xdr.use === 'flash') {
183 arguments: c.arguments
185 // These nodes do not need to be serialized across Flash's
186 // ExternalInterface. Doing so will result in exceptions.
190 o.c.send(uri, c, o.id);
192 else if (window.XDomainRequest) {
194 o.c.open(c.method || 'GET', uri);
201 return o.c ? _abort(o, c) : false;
203 isInProgress: function() {
204 return o.c ? _isInProgress(o, c.xdr.use) : false;
210 * @description Response controller for cross-domain requests when using the
211 * Flash transport or IE8's XDomainRequest object.
213 * @method xdrResponse
216 * @param {object} o - Transaction object generated by _create() in io-base.
217 * @param {object} c - configuration object for the transaction.
218 * @param {string} e - Event name
221 xdrResponse: function(o, c, e) {
223 isFlash = c.xdr.use === 'flash' ? true : false,
224 isXML = c.xdr.dataType === 'xml' ? true : false;
229 fn = m[o.id] ? m[o.id] : null;
232 c.context = fn.context;
233 c.arguments = fn.arguments;
236 if (e === ('abort' || 'timeout')) {
245 Y.io.success(_data(o, isFlash, isXML), c);
246 isFlash ? delete m[o.id] : delete _rS[o.id];
251 Y.io.failure(_data(o, isFlash, isXML), c);
252 isFlash ? delete m[o.id] : delete _rS[o.id];
258 * @description Fires event "io:xdrReady"
263 * @param {number} id - transaction id
264 * @param {object} c - configuration object for the transaction.
268 xdrReady: function(id) {
269 Y.fire(E_XDR_READY, id);
273 * @description Method to initialize the desired transport.
278 * @param {object} o - object of transport configurations.
281 transport: function(o) {
282 var id = o.yid ? o.yid : Y.id;
285 this._transport.flash = Y.config.doc.getElementById('yuiIoSwf');
291 }, '3.0.0' ,{requires:['io-base','datatype-xml']});