io-xdr.js revision 68b67a7ca77bd6e51a6d67602d38a4cceede1676
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * Extends the IO base class to provide an alternate, Flash transport, for making
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * cross-domain requests.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @submodule io-xdr
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @event io:xdrReady
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description This event is fired by YUI.io when the specified transport is
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * ready for use.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @type Event Custom
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith var E_XDR_READY = Y.publish('io:xdrReady', { fireOnce: true }),
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Map of stored configuration objects when using
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * Flash as the transport for cross-domain requests.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @property _cB
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @type object
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Map of transaction simulated readyState values
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * when XDomainRequest is the transport.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @property _rS
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @type object
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith // Document reference
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith // Window reference
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith // XDomainRequest cross-origin request detection
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Method that creates the Flash transport swf.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @method _swf
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {string} uri - location of io.swf.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {string} yid - YUI instance id.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @return void
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith var o = '<object id="io_swf" type="application/x-shockwave-flash" data="' +
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith '<param name="movie" value="' + uri + '">' +
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith '<param name="FlashVars" value="yid=' + yid + '">' +
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith '<param name="allowScriptAccess" value="always">' +
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Sets event handlers for XDomainRequest transactions.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @method _evt
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} o - Transaction object generated by _create() in io-base.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} c - configuration object for the transaction.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @return void
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith function _evt(o, c) {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith p = 'xdrResponse',
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith t = 'timeout';
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith o.c.onprogress = function() { _rS[i] = 3; };
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith o.c.onload = function() {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith o.c.onerror = function() {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith o.c.ontimeout = function() {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith o.c[t] = c[t];
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Creates a response object for XDR transactions, for success
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * and failure cases.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @method _data
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} o - Transaction object generated by _create() in io-base.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {boolean} u - Configuration xdr.use.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {boolean} d - Configuration xdr.dataType.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @return object
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith function _data(o, u, d) {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith if (u === 'flash') {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith o.c.responseText = decodeURI(o.c.responseText);
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith if (d === 'xml') {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith o.c.responseXML = Y.DataType.XML.parse(o.c.responseXML);
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Method for intiating an XDR transaction abort.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @method _abort
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} o - Transaction object generated by _create() in io-base.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} c - configuration object for the transaction.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith function _abort(o, c) {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Method for determining if an XDR transaction has completed
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * and all data are received.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @method _isInProgress.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} o - Transaction object generated by _create() in io-base.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith return xdr ? _rS[o.id] !== 4 : o.c.isInProgress(o.id);
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Map of io transports.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @property _transport
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @type object
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Method for accessing the transport's interface for making a
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * cross-domain transaction.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @method xdr
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {string} uri - qualified path to transaction resource.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} o - Transaction object generated by _create() in io-base.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} c - configuration object for the transaction.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith // The configuration object cannot be serialized safely
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith // across Flash's ExternalInterface.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith abort: function() {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith return o.c ? _abort(o, c) : false;
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Response controller for cross-domain requests when using the
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * Flash transport or IE8's XDomainRequest object.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @method xdrResponse
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {string} e - Event name
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} o - Transaction object generated by _create() in io-base.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} c - configuration object for the transaction.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @return object
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith xdrResponse: function(e, o, c) {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith case 'start':
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith //case 'complete':
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith //This case is not used by Flash or XDomainRequest.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith //io.complete(o, c);
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith case 'success':
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith case 'timeout':
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith case 'abort':
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith case 'transport error':
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith case 'failure':
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Fires event "io:xdrReady"
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @method xdrReady
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {number} id - transaction id
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @return void
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Method to initialize the desired transport.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @method transport
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @param {object} o - object of transport configurations.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @return void
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith transport: function(o) {
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith _swf(Y.UA.ie ? o.src + '?d=' + new Date().valueOf().toString() : o.src, Y.id);
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith this._transport[o.id] = (o.id === 'flash') ? d.getElementById('io_swf') : o.src;
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @description Delay value to calling the Flash transport, in the
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * event io.swf has not finished loading. Once the E_XDR_READY
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * event is fired, this value will be set to 0.
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @property delay
c1bef59b02d89a84c23d29663cc4e6d46148ebd2David Goldsmith * @type number