3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('io-xdr', function(Y) {
10 Extends IO to provide an alternate, Flash transport, for making
11 cross-domain requests.
18 Fires when the XDR transport is ready for use.
21 var E_XDR_READY = Y.publish('io:xdrReady', { fireOnce: true }),
24 Map of stored configuration objects when using
25 Flash as the transport for cross-domain requests.
34 Map of transaction simulated readyState values
35 when XDomainRequest is the transport.
47 // XDomainRequest cross-origin request detection
48 xdr = w && w.XDomainRequest;
51 Method that creates the Flash transport swf.
55 @param {String} uri - location of io.swf.
56 @param {String} yid - YUI sandbox id.
57 @param {String} yid - IO instance id.
59 function _swf(uri, yid, uid) {
60 var o = '<object id="io_swf" type="application/x-shockwave-flash" data="' +
61 uri + '" width="0" height="0">' +
62 '<param name="movie" value="' + uri + '">' +
63 '<param name="FlashVars" value="yid=' + yid + '&uid=' + uid + '">' +
64 '<param name="allowScriptAccess" value="always">' +
66 c = d.createElement('div');
68 d.body.appendChild(c);
73 Creates a response object for XDR transactions, for success
78 @param {Object} o - Transaction object generated by _create() in io-base.
79 @param {Boolean} u - Configuration xdr.use.
80 @param {Boolean} d - Configuration xdr.dataType.
84 function _data(o, u, d) {
86 o.c.responseText = decodeURI(o.c.responseText);
89 o.c.responseXML = Y.DataType.XML.parse(o.c.responseText);
96 Method for intiating an XDR transaction abort.
100 @param {Object} o - Transaction object generated by _create() in io-base.
101 @param {Object} c - configuration object for the transaction.
103 function _abort(o, c) {
104 return o.c.abort(o.id, c);
108 Method for determining if an XDR transaction has completed
109 and all data are received.
111 @method _isInProgress
113 @param {Object} o - Transaction object generated by _create() in io-base.
115 function _isInProgress(o) {
116 return xdr ? _rS[o.id] !== 4 : o.c.isInProgress(o.id);
119 Y.mix(Y.IO.prototype, {
122 Map of io transports.
131 Sets event handlers for XDomainRequest transactions.
136 @param {Object} o - Transaction object generated by _create() in io-base.
137 @param {Object} c - configuration object for the transaction.
139 _ieEvt: function(o, c) {
144 o.c.onprogress = function() { _rS[i] = 3; };
145 o.c.onload = function() {
147 io.xdrResponse('success', o, c);
149 o.c.onerror = function() {
151 io.xdrResponse('failure', o, c);
154 o.c.ontimeout = function() {
156 io.xdrResponse(t, o, c);
163 Method for accessing the transport's interface for making a
164 cross-domain transaction.
167 @param {String} uri - qualified path to transaction resource.
168 @param {Object} o - Transaction object generated by _create() in io-base.
169 @param {Object} c - configuration object for the transaction.
171 xdr: function(uri, o, c) {
174 if (c.xdr.use === 'flash') {
175 // The configuration object cannot be serialized safely
176 // across Flash's ExternalInterface.
178 w.setTimeout(function() {
180 o.c.send(uri, { id: o.id,
184 headers: c.headers });
187 io.xdrResponse('transport error', o, c);
194 o.c.open(c.method || 'GET', uri);
204 return o.c ? _abort(o, c) : false;
206 isInProgress: function() {
207 return o.c ? _isInProgress(o.id) : false;
214 Response controller for cross-domain requests when using the
215 Flash transport or IE8's XDomainRequest object.
218 @param {String} e Event name
219 @param {Object} o Transaction object generated by _create() in io-base.
220 @param {Object} c Configuration object for the transaction.
223 xdrResponse: function(e, o, c) {
224 c = _cB[o.id] ? _cB[o.id] : c;
235 //This case is not used by Flash or XDomainRequest.
239 io.success(_data(o, u, d), c);
244 case 'transport error':
245 o.c = { status: 0, statusText: e };
247 io.failure(_data(o, u, d), c);
254 Fires event "io:xdrReady"
258 @param {Number} yid - YUI sandbox id.
259 @param {Number} uid - IO instance id.
261 _xdrReady: function(yid, uid) {
262 Y.fire(E_XDR_READY, yid, uid);
266 Initializes the desired transport.
269 @param {Object} o - object of transport configurations.
271 transport: function(c) {
272 if (c.id === 'flash') {
273 _swf(Y.UA.ie ? c.src + '?d=' + new Date().valueOf().toString() : c.src, Y.id, c.uid);
274 Y.IO.transports.flash = function() { return d.getElementById('io_swf'); };
280 Fires event "io:xdrReady"
285 @param {Number} yid - YUI sandbox id.
286 @param {Number} uid - IO instance id.
288 Y.io.xdrReady = function(yid, uid){
289 var io = Y.io._map[uid];
291 io._xdrReady.apply(io, [yid, uid]);
294 Y.io.xdrResponse = function(e, o, c){
295 var io = Y.io._map[o.uid];
296 io.xdrResponse.apply(io, [e, o, c]);
299 Y.io.transport = function(c){
300 var io = Y.io._map['io:0'] || new Y.IO();
302 io.transport.apply(io, [c]);
306 Delay value to calling the Flash transport, in the
307 event io.swf has not finished loading. Once the E_XDR_READY
308 event is fired, this value will be set to 0.
314 Y.io.xdr = { delay : 100 };
318 }, '3.5.1' ,{requires:['io-base','datatype-xml-parse']});