2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('io-xdr', function (Y, NAME) {
11 Extends IO to provide an alternate, Flash transport, for making
12 cross-domain requests.
19 // Helpful resources when working with the mess that is XDomainRequest:
20 // http://www.cypressnorth.com/blog/web-programming-and-development/internet-explorer-aborting-ajax-requests-fixed/
21 // http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
24 Fires when the XDR transport is ready for use.
27 var E_XDR_READY = Y.publish('io:xdrReady', { fireOnce: true }),
30 Map of stored configuration objects when using
31 Flash as the transport for cross-domain requests.
40 Map of transaction simulated readyState values
41 when XDomainRequest is the transport.
53 // XDomainRequest cross-origin request detection
54 xdr = w && w.XDomainRequest;
57 Method that creates the Flash transport swf.
61 @param {String} uri - location of io.swf.
62 @param {String} yid - YUI sandbox id.
63 @param {String} yid - IO instance id.
65 function _swf(uri, yid, uid) {
66 var o = '<object id="io_swf" type="application/x-shockwave-flash" data="' +
67 uri + '" width="0" height="0">' +
68 '<param name="movie" value="' + uri + '">' +
69 '<param name="FlashVars" value="yid=' + yid + '&uid=' + uid + '">' +
70 '<param name="allowScriptAccess" value="always">' +
72 c = d.createElement('div');
74 d.body.appendChild(c);
79 Creates a response object for XDR transactions, for success
84 @param {Object} o - Transaction object generated by _create() in io-base.
85 @param {Boolean} u - Configuration xdr.use.
86 @param {Boolean} d - Configuration xdr.dataType.
90 function _data(o, u, d) {
92 o.c.responseText = decodeURI(o.c.responseText);
95 o.c.responseXML = Y.DataType.XML.parse(o.c.responseText);
102 Method for intiating an XDR transaction abort.
106 @param {Object} o - Transaction object generated by _create() in io-base.
107 @param {Object} c - configuration object for the transaction.
109 function _abort(o, c) {
110 return o.c.abort(o.id, c);
114 Method for determining if an XDR transaction has completed
115 and all data are received.
117 @method _isInProgress
119 @param {Object} o - Transaction object generated by _create() in io-base.
121 function _isInProgress(o) {
122 return xdr ? _rS[o.id] !== 4 : o.c.isInProgress(o.id);
125 Y.mix(Y.IO.prototype, {
128 Map of io transports.
137 Sets event handlers for XDomainRequest transactions.
142 @param {Object} o - Transaction object generated by _create() in io-base.
143 @param {Object} c - configuration object for the transaction.
145 _ieEvt: function(o, c) {
150 o.c.onprogress = function() { _rS[i] = 3; };
151 o.c.onload = function() {
153 io.xdrResponse('success', o, c);
155 o.c.onerror = function() {
157 io.xdrResponse('failure', o, c);
159 o.c.ontimeout = function() {
161 io.xdrResponse(t, o, c);
167 Method for accessing the transport's interface for making a
168 cross-domain transaction.
171 @param {String} uri - qualified path to transaction resource.
172 @param {Object} o - Transaction object generated by _create() in io-base.
173 @param {Object} c - configuration object for the transaction.
175 xdr: function(uri, o, c) {
178 if (c.xdr.use === 'flash') {
179 // The configuration object cannot be serialized safely
180 // across Flash's ExternalInterface.
182 w.setTimeout(function() {
184 o.c.send(uri, { id: o.id,
188 headers: c.headers });
191 io.xdrResponse('transport error', o, c);
198 o.c.open(c.method || 'GET', uri);
200 // Make async to protect against IE 8 oddities.
201 setTimeout(function() {
212 return o.c ? _abort(o, c) : false;
214 isInProgress: function() {
215 return o.c ? _isInProgress(o.id) : false;
222 Response controller for cross-domain requests when using the
223 Flash transport or IE8's XDomainRequest object.
226 @param {String} e Event name
227 @param {Object} o Transaction object generated by _create() in io-base.
228 @param {Object} c Configuration object for the transaction.
231 xdrResponse: function(e, o, c) {
232 c = _cB[o.id] ? _cB[o.id] : c;
243 //This case is not used by Flash or XDomainRequest.
247 io.success(_data(o, u, d), c);
252 case 'transport error':
253 o.c = { status: 0, statusText: e };
255 io.failure(_data(o, u, d), c);
262 Fires event "io:xdrReady"
266 @param {Number} yid - YUI sandbox id.
267 @param {Number} uid - IO instance id.
269 _xdrReady: function(yid, uid) {
270 Y.fire(E_XDR_READY, yid, uid);
274 Initializes the desired transport.
277 @param {Object} o - object of transport configurations.
279 transport: function(c) {
280 if (c.id === 'flash') {
281 _swf(Y.UA.ie ? c.src + '?d=' + new Date().valueOf().toString() : c.src, Y.id, c.uid);
282 Y.IO.transports.flash = function() { return d.getElementById('io_swf'); };
288 Fires event "io:xdrReady"
293 @param {Number} yid - YUI sandbox id.
294 @param {Number} uid - IO instance id.
296 Y.io.xdrReady = function(yid, uid){
297 var io = Y.io._map[uid];
299 io._xdrReady.apply(io, [yid, uid]);
302 Y.io.xdrResponse = function(e, o, c){
303 var io = Y.io._map[o.uid];
304 io.xdrResponse.apply(io, [e, o, c]);
307 Y.io.transport = function(c){
308 var io = Y.io._map['io:0'] || new Y.IO();
310 io.transport.apply(io, [c]);
314 Delay value to calling the Flash transport, in the
315 event io.swf has not finished loading. Once the E_XDR_READY
316 event is fired, this value will be set to 0.
322 Y.io.xdr = { delay : 100 };
325 }, '3.13.0', {"requires": ["io-base", "datatype-xml-parse"]});