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-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 = Y.publish('io:xdrReady', { fireOnce: true }),
26 * @description Object that stores callback handlers for cross-domain requests
27 * when using Flash as the transport.
37 * @description Map of transaction readyState values used when
38 * XDomainRequest is the XDR transport.
51 // IE8 cross-origin request detection
52 ie = w && w.XDomainRequest;
55 * @description Method that creates the Flash transport swf.
60 * @param {string} uri - location of io.swf.
61 * @param {string} yid - YUI instance id.
64 function _swf(uri, yid) {
65 var o = '<object id="yuiIoSwf" type="application/x-shockwave-flash" data="' +
66 uri + '" width="0" height="0">' +
67 '<param name="movie" value="' + uri + '">' +
68 '<param name="FlashVars" value="yid=' + yid + '">' +
69 '<param name="allowScriptAccess" value="always">' +
71 c = d.createElement('div');
73 d.body.appendChild(c);
78 * @description Sets event handlers for XDomainRequest transactions.
83 * @param {object} o - Transaction object generated by _create() in io-base.
84 * @param {object} c - configuration object for the transaction.
88 o.c.onprogress = function() { _rS[o.id] = 3; };
89 o.c.onload = function() {
91 Y.io.xdrResponse(o, c, 'success');
93 o.c.onerror = function() {
95 Y.io.xdrResponse(o, c, 'failure');
98 o.c.ontimeout = function() {
100 Y.io.xdrResponse(o, c, 'timeout');
102 o.c.timeout = c.timeout;
107 * @description Creates a response object for XDR transactions, for success
113 * @param {object} o - Transaction object generated by _create() in io-base.
114 * @param {boolean} f - True if Flash was used as the transport.
115 * @param {boolean} t - DataType value, as defined in the configuration.
119 function _data(o, f, t) {
123 s = f ? decodeURI(o.c.responseText) : o.c.responseText;
124 x = t === 'xml' ? Y.DataType.XML.parse(s) : null;
126 return { id: o.id, c: { responseText: s, responseXML: x } };
129 return { id: o.id, e: o.e };
135 * @description Method for intiating an XDR transaction abort.
140 * @param {object} o - Transaction object generated by _create() in io-base.
141 * @param {object} c - configuration object for the transaction.
143 function _abort(o, c) {
144 return o.c.abort(o.id, c);
148 * @description Method for determining if an XDR transaction has completed
149 * and all data are received.
151 * @method _isInProgress.
154 * @param {object} o - Transaction object generated by _create() in io-base.
156 function _isInProgress(o) {
157 return ie ? _rS[o.id] !== 4 : o.c.isInProgress(o.id);
163 * @description Map of io transports.
165 * @property _transport
173 * @description Method for accessing the transport's interface for making a
174 * cross-domain transaction.
179 * @param {string} uri - qualified path to transaction resource.
180 * @param {object} o - Transaction object generated by _create() in io-base.
181 * @param {object} c - configuration object for the transaction.
183 xdr: function(uri, o, c) {
184 if (c.xdr.use === 'flash') {
188 arguments: c.arguments
190 // These properties cannot be serialized across Flash's
191 // ExternalInterface. Doing so will result in exceptions.
195 w.setTimeout(function() {
196 if (o.c && o.c.send) {
197 o.c.send(uri, c, o.id);
200 Y.io.xdrResponse(o, c, 'transport error');
206 o.c.open(c.method || 'GET', uri);
216 return o.c ? _abort(o, c) : false;
218 isInProgress: function() {
219 return o.c ? _isInProgress(o.id) : false;
225 * @description Response controller for cross-domain requests when using the
226 * Flash transport or IE8's XDomainRequest object.
228 * @method xdrResponse
231 * @param {object} o - Transaction object generated by _create() in io-base.
232 * @param {object} c - configuration object for the transaction.
233 * @param {string} e - Event name
236 xdrResponse: function(o, c, e) {
239 f = c.xdr.use === 'flash' ? true : false,
244 cb = _cB[o.id] ? _cB[o.id] : null;
247 c.context = cb.context;
248 c.arguments = cb.arguments;
260 Y.io.success(t || f ? _data(o, f, t) : o, c);
265 case 'transport error':
268 Y.io.failure(t || f ? _data(o, f, t) : o, c);
275 * @description Fires event "io:xdrReady"
280 * @param {number} id - transaction id
281 * @param {object} c - configuration object for the transaction.
285 xdrReady: function(id) {
287 Y.fire(E_XDR_READY, id);
291 * @description Method to initialize the desired transport.
296 * @param {object} o - object of transport configurations.
299 transport: function(o) {
300 var yid = o.yid || Y.id,
301 oid = o.id || 'flash',
302 src = Y.UA.ie ? o.src + '?d=' + new Date().valueOf().toString() : o.src;
304 if (oid === 'native' || oid === 'flash') {
307 this._transport.flash = d.getElementById('yuiIoSwf');
310 this._transport[o.id] = o.src;
316 * @description Delay value to calling the Flash transport, in the
317 * event io.swf has not finished loading. Once the E_XDR_READY
318 * event is fired, this value will be set to 0.
329 }, '3.3.0' ,{requires:['io-base','datatype-xml']});