]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/io/io-xdr.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / io / io-xdr.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('io-xdr', function(Y) {
9
10    /**
11     * Extends the IO base class to provide an alternate, Flash transport, for making
12     * cross-domain requests.
13     * @module io
14     * @submodule io-xdr
15     */
16
17    /**
18     * @event io:xdrReady
19     * @description This event is fired by YUI.io when the specified transport is
20     * ready for use.
21     * @type Event Custom
22     */
23     var E_XDR_READY = Y.publish('io:xdrReady', { fireOnce: true }),
24
25    /**
26     * @description Object that stores callback handlers for cross-domain requests
27     * when using Flash as the transport.
28     *
29     * @property _cB
30     * @private
31     * @static
32     * @type object
33     */
34     _cB = {},
35
36    /**
37     * @description Map of transaction readyState values used when
38     * XDomainRequest is the XDR transport.
39     *
40     * @property _rS
41     * @private
42     * @static
43     * @type object
44     */
45     _rS = {},
46
47     // Document reference
48     d = Y.config.doc,
49     // Window reference
50     w = Y.config.win,
51         // IE8 cross-origin request detection
52     ie = w && w.XDomainRequest;
53
54    /**
55     * @description Method that creates the Flash transport swf.
56     *
57     * @method _swf
58     * @private
59     * @static
60     * @param {string} uri - location of io.swf.
61     * @param {string} yid - YUI instance id.
62     * @return void
63     */
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">' +
70                 '</object>',
71             c = d.createElement('div');
72
73         d.body.appendChild(c);
74         c.innerHTML = o;
75     }
76
77    /**
78     * @description Sets event handlers for XDomainRequest transactions.
79     *
80     * @method _evt
81     * @private
82     * @static
83     * @param {object} o - Transaction object generated by _create() in io-base.
84     * @param {object} c - configuration object for the transaction.
85     * @return void
86     */
87     function _evt(o, c) {
88         o.c.onprogress = function() { _rS[o.id] = 3; };
89         o.c.onload = function() {
90             _rS[o.id] = 4;
91             Y.io.xdrResponse(o, c, 'success');
92         };
93         o.c.onerror = function() {
94             _rS[o.id] = 4;
95             Y.io.xdrResponse(o, c, 'failure');
96         };
97         if (c.timeout) {
98             o.c.ontimeout = function() {
99                 _rS[o.id] = 4;
100                 Y.io.xdrResponse(o, c, 'timeout');
101             };
102             o.c.timeout = c.timeout;
103         }
104     }
105
106    /**
107     * @description Creates a response object for XDR transactions, for success
108     * and failure cases.
109     *
110     * @method _data
111     * @private
112     * @static
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.
116     *
117     * @return object
118     */
119     function _data(o, f, t) {
120         var s, x;
121
122         if (!o.e) {
123             s = f ? decodeURI(o.c.responseText) : o.c.responseText;
124             x = t === 'xml' ?  Y.DataType.XML.parse(s) : null;
125
126             return { id: o.id, c: { responseText: s, responseXML: x } };
127         }
128         else {
129             return { id: o.id, e: o.e };
130         }
131
132     }
133
134    /**
135     * @description Method for intiating an XDR transaction abort.
136     *
137     * @method _abort
138     * @private
139     * @static
140     * @param {object} o - Transaction object generated by _create() in io-base.
141     * @param {object} c - configuration object for the transaction.
142     */
143     function _abort(o, c) {
144         return o.c.abort(o.id, c);
145     }
146
147    /**
148     * @description Method for determining if an XDR transaction has completed
149     * and all data are received.
150     *
151     * @method _isInProgress.
152     * @private
153     * @static
154     * @param {object} o - Transaction object generated by _create() in io-base.
155     */
156     function _isInProgress(o) {
157         return ie ? _rS[o.id] !== 4 : o.c.isInProgress(o.id);
158     }
159
160     Y.mix(Y.io, {
161
162        /**
163         * @description Map of io transports.
164         *
165         * @property _transport
166         * @private
167         * @static
168         * @type object
169         */
170         _transport: {},
171
172        /**
173         * @description Method for accessing the transport's interface for making a
174         * cross-domain transaction.
175         *
176         * @method xdr
177         * @private
178         * @static
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.
182         */
183         xdr: function(uri, o, c) {
184                         if (c.xdr.use === 'flash') {
185                                 _cB[o.id] = {
186                                         on: c.on,
187                                         context: c.context,
188                                         arguments: c.arguments
189                                 };
190                                 // These properties cannot be serialized across Flash's
191                                 // ExternalInterface.  Doing so will result in exceptions.
192                                 c.context = null;
193                                 c.form = null;
194
195                                 w.setTimeout(function() {
196                                         if (o.c && o.c.send) {
197                                                 o.c.send(uri, c, o.id);
198                                         }
199                                         else {
200                                                 Y.io.xdrResponse(o, c, 'transport error');
201                                         }
202                                 }, Y.io.xdr.delay);
203                         }
204                         else if (ie) {
205                                 _evt(o, c);
206                                 o.c.open(c.method || 'GET', uri);
207                                 o.c.send(c.data);
208                         }
209                         else {
210                                 o.c.send(uri, o, c);
211                         }
212
213                         return {
214                                 id: o.id,
215                                 abort: function() {
216                                         return o.c ? _abort(o, c) : false;
217                                 },
218                                 isInProgress: function() {
219                                         return o.c ? _isInProgress(o.id) : false;
220                                 }
221                         };
222         },
223
224        /**
225         * @description Response controller for cross-domain requests when using the
226         * Flash transport or IE8's XDomainRequest object.
227         *
228         * @method xdrResponse
229         * @private
230         * @static
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
234         * @return object
235         */
236         xdrResponse: function(o, c, e) {
237             var cb,
238                 m = ie ? _rS : _cB,
239                 f = c.xdr.use === 'flash' ? true : false,
240                 t = c.xdr.dataType;
241                 c.on = c.on || {};
242
243             if (f) {
244                 cb = _cB[o.id] ? _cB[o.id] : null;
245                 if (cb) {
246                     c.on = cb.on;
247                     c.context = cb.context;
248                     c.arguments = cb.arguments;
249                 }
250             }
251
252             switch (e) {
253                 case 'start':
254                     Y.io.start(o.id, c);
255                     break;
256                 case 'complete':
257                     Y.io.complete(o, c);
258                     break;
259                 case 'success':
260                     Y.io.success(t || f ? _data(o, f, t) : o, c);
261                     delete m[o.id];
262                     break;
263                 case 'timeout':
264                 case 'abort':
265                                 case 'transport error':
266                                         o.e = e;
267                 case 'failure':
268                     Y.io.failure(t || f ? _data(o, f, t) : o, c);
269                     delete m[o.id];
270                     break;
271             }
272         },
273
274        /**
275         * @description Fires event "io:xdrReady"
276         *
277         * @method xdrReady
278         * @private
279         * @static
280         * @param {number} id - transaction id
281         * @param {object} c - configuration object for the transaction.
282         *
283         * @return void
284         */
285         xdrReady: function(id) {
286                         Y.io.xdr.delay = 0;
287             Y.fire(E_XDR_READY, id);
288         },
289
290        /**
291         * @description Method to initialize the desired transport.
292         *
293         * @method transport
294         * @public
295         * @static
296         * @param {object} o - object of transport configurations.
297         * @return void
298         */
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;
303
304             if (oid === 'native' || oid === 'flash') {
305
306                                 _swf(src, yid);
307                 this._transport.flash = d.getElementById('yuiIoSwf');
308             }
309             else if (oid) {
310                 this._transport[o.id] = o.src;
311             }
312         }
313     });
314
315    /**
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.
319         *
320         * @property delay
321         * @public
322         * @static
323         * @type number
324         */
325         Y.io.xdr.delay = 50;
326
327
328
329 }, '3.3.0' ,{requires:['io-base','datatype-xml']});