MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / io-xdr / io-xdr-debug.js
bloba83eb56e41df7614f5f0c08a9863f7d063a7fc41
1 /*
2 YUI 3.5.1 (build 22)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('io-xdr', function(Y) {
9 /**
10 Extends IO to provide an alternate, Flash transport, for making
11 cross-domain requests.
12 @module io
13 @submodule io-xdr
14 @for IO
15 **/
17 /**
18 Fires when the XDR transport is ready for use.
19 @event io:xdrReady
20 **/
21 var E_XDR_READY = Y.publish('io:xdrReady', { fireOnce: true }),
23 /**
24 Map of stored configuration objects when using
25 Flash as the transport for cross-domain requests.
27 @property _cB
28 @private
29 @type {Object}
30 **/
31 _cB = {},
33 /**
34 Map of transaction simulated readyState values
35 when XDomainRequest is the transport.
37 @property _rS
38 @private
39 @type {Object}
40 **/
41 _rS = {},
43 // Document reference
44 d = Y.config.doc,
45 // Window reference
46 w = Y.config.win,
47 // XDomainRequest cross-origin request detection
48 xdr = w && w.XDomainRequest;
50 /**
51 Method that creates the Flash transport swf.
53 @method _swf
54 @private
55 @param {String} uri - location of io.swf.
56 @param {String} yid - YUI sandbox id.
57 @param {String} yid - IO instance id.
58 **/
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">' +
65             '</object>',
66         c = d.createElement('div');
68     d.body.appendChild(c);
69     c.innerHTML = o;
72 /**
73 Creates a response object for XDR transactions, for success
74 and failure cases.
76 @method _data
77 @private
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.
82 @return {Object}
83 **/
84 function _data(o, u, d) {
85     if (u === 'flash') {
86         o.c.responseText = decodeURI(o.c.responseText);
87     }
88     if (d === 'xml') {
89         o.c.responseXML = Y.DataType.XML.parse(o.c.responseText);
90     }
92     return o;
95 /**
96 Method for intiating an XDR transaction abort.
98 @method _abort
99 @private
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
112 @private
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, {
121     /**
122     Map of io transports.
124     @property _transport
125     @private
126     @type {Object}
127     **/
128     _transport: {},
130     /**
131     Sets event handlers for XDomainRequest transactions.
133     @method _ieEvt
134     @private
135     @static
136     @param {Object} o - Transaction object generated by _create() in io-base.
137     @param {Object} c - configuration object for the transaction.
138     **/
139     _ieEvt: function(o, c) {
140         var io = this,
141             i = o.id,
142             t = 'timeout';
144         o.c.onprogress = function() { _rS[i] = 3; };
145         o.c.onload = function() {
146             _rS[i] = 4;
147             io.xdrResponse('success', o, c);
148         };
149         o.c.onerror = function() {
150             _rS[i] = 4;
151             io.xdrResponse('failure', o, c);
152         };
153         if (c[t]) {
154             o.c.ontimeout = function() {
155                 _rS[i] = 4;
156                 io.xdrResponse(t, o, c);
157             };
158             o.c[t] = c[t];
159         }
160     },
162     /**
163     Method for accessing the transport's interface for making a
164     cross-domain transaction.
166     @method xdr
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.
170     **/
171     xdr: function(uri, o, c) {
172         var io = this;
174         if (c.xdr.use === 'flash') {
175             // The configuration object cannot be serialized safely
176             // across Flash's ExternalInterface.
177             _cB[o.id] = c;
178             w.setTimeout(function() {
179                 try {
180                     o.c.send(uri, { id: o.id,
181                                     uid: o.uid,
182                                     method: c.method,
183                                     data: c.data,
184                                     headers: c.headers });
185                 }
186                 catch(e) {
187                     io.xdrResponse('transport error', o, c);
188                     delete _cB[o.id];
189                 }
190             }, Y.io.xdr.delay);
191         }
192         else if (xdr) {
193             io._ieEvt(o, c);
194             o.c.open(c.method || 'GET', uri);
195             o.c.send(c.data);
196         }
197         else {
198             o.c.send(uri, o, c);
199         }
201         return {
202             id: o.id,
203             abort: function() {
204                 return o.c ? _abort(o, c) : false;
205             },
206             isInProgress: function() {
207                 return o.c ? _isInProgress(o.id) : false;
208             },
209             io: io
210         };
211     },
213     /**
214     Response controller for cross-domain requests when using the
215     Flash transport or IE8's XDomainRequest object.
217     @method xdrResponse
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.
221     @return {Object}
222     **/
223     xdrResponse: function(e, o, c) {
224         c = _cB[o.id] ? _cB[o.id] : c;
225         var io = this,
226             m = xdr ? _rS : _cB,
227             u = c.xdr.use,
228             d = c.xdr.dataType;
230         switch (e) {
231             case 'start':
232                 io.start(o, c);
233                 break;
234            //case 'complete':
235                 //This case is not used by Flash or XDomainRequest.
236                 //io.complete(o, c);
237                 //break;
238             case 'success':
239                 io.success(_data(o, u, d), c);
240                 delete m[o.id];
241                 break;
242             case 'timeout':
243             case 'abort':
244             case 'transport error':
245                 o.c = { status: 0, statusText: e };
246             case 'failure':
247                 io.failure(_data(o, u, d), c);
248                 delete m[o.id];
249                 break;
250         }
251     },
253     /**
254     Fires event "io:xdrReady"
256     @method _xdrReady
257     @private
258     @param {Number} yid - YUI sandbox id.
259     @param {Number} uid - IO instance id.
260     **/
261     _xdrReady: function(yid, uid) {
262         Y.fire(E_XDR_READY, yid, uid);
263     },
265     /**
266     Initializes the desired transport.
268     @method transport
269     @param {Object} o - object of transport configurations.
270     **/
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'); };
275         }
276     }
280 Fires event "io:xdrReady"
282 @method xdrReady
283 @protected
284 @static
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];
290     Y.io.xdr.delay = 0;
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();
301     c.uid = io._uid;
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.
310 @property delay
311 @static
312 @type {Number}
314 Y.io.xdr = { delay : 100 };
318 }, '3.5.1' ,{requires:['io-base','datatype-xml-parse']});