[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/io-xdr/ -> io-xdr.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 Yahoo! Inc. All rights reserved.
   4  Licensed under the BSD License.
   5  http://yuilibrary.com/license/
   6  */
   7  
   8  YUI.add('io-xdr', function (Y, NAME) {
   9  
  10  /**
  11  Extends IO to provide an alternate, Flash transport, for making
  12  cross-domain requests.
  13  @module io
  14  @submodule io-xdr
  15  @for IO
  16  @deprecated
  17  **/
  18  
  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
  22  
  23  /**
  24  Fires when the XDR transport is ready for use.
  25  @event io:xdrReady
  26  **/
  27  var E_XDR_READY = Y.publish('io:xdrReady', { fireOnce: true }),
  28  
  29  /**
  30  Map of stored configuration objects when using
  31  Flash as the transport for cross-domain requests.
  32  
  33  @property _cB
  34  @private
  35  @type {Object}
  36  **/
  37  _cB = {},
  38  
  39  /**
  40  Map of transaction simulated readyState values
  41  when XDomainRequest is the transport.
  42  
  43  @property _rS
  44  @private
  45  @type {Object}
  46  **/
  47  _rS = {},
  48  
  49  // Document reference
  50  d = Y.config.doc,
  51  // Window reference
  52  w = Y.config.win,
  53  // XDomainRequest cross-origin request detection
  54  xdr = w && w.XDomainRequest;
  55  
  56  /**
  57  Method that creates the Flash transport swf.
  58  
  59  @method _swf
  60  @private
  61  @param {String} uri - location of io.swf.
  62  @param {String} yid - YUI sandbox id.
  63  @param {String} uid - IO instance id.
  64  **/
  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">' +
  71              '</object>',
  72          c = d.createElement('div');
  73  
  74      d.body.appendChild(c);
  75      c.innerHTML = o;
  76  }
  77  
  78  /**
  79  Creates a response object for XDR transactions, for success
  80  and failure cases.
  81  
  82  @method _data
  83  @private
  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.
  87  
  88  @return {Object}
  89  **/
  90  function _data(o, u, d) {
  91      if (u === 'flash') {
  92          o.c.responseText = decodeURI(o.c.responseText);
  93      }
  94      if (d === 'xml') {
  95          o.c.responseXML = Y.DataType.XML.parse(o.c.responseText);
  96      }
  97  
  98      return o;
  99  }
 100  
 101  /**
 102  Method for intiating an XDR transaction abort.
 103  
 104  @method _abort
 105  @private
 106  @param {Object} o - Transaction object generated by _create() in io-base.
 107  @param {Object} c - configuration object for the transaction.
 108  **/
 109  function _abort(o, c) {
 110      return o.c.abort(o.id, c);
 111  }
 112  
 113  /**
 114  Method for determining if an XDR transaction has completed
 115  and all data are received.
 116  
 117  @method _isInProgress
 118  @private
 119  @param {Object} o - Transaction object generated by _create() in io-base.
 120  **/
 121  function _isInProgress(o) {
 122      return xdr ? _rS[o.id] !== 4 : o.c.isInProgress(o.id);
 123  }
 124  
 125  Y.mix(Y.IO.prototype, {
 126  
 127      /**
 128      Map of io transports.
 129  
 130      @property _transport
 131      @private
 132      @type {Object}
 133      **/
 134      _transport: {},
 135  
 136      /**
 137      Sets event handlers for XDomainRequest transactions.
 138  
 139      @method _ieEvt
 140      @private
 141      @static
 142      @param {Object} o - Transaction object generated by _create() in io-base.
 143      @param {Object} c - configuration object for the transaction.
 144      **/
 145      _ieEvt: function(o, c) {
 146          var io = this,
 147              i = o.id,
 148              t = 'timeout';
 149  
 150          o.c.onprogress = function() { _rS[i] = 3; };
 151          o.c.onload = function() {
 152              _rS[i] = 4;
 153              io.xdrResponse('success', o, c);
 154          };
 155          o.c.onerror = function() {
 156              _rS[i] = 4;
 157              io.xdrResponse('failure', o, c);
 158          };
 159          o.c.ontimeout = function() {
 160              _rS[i] = 4;
 161              io.xdrResponse(t, o, c);
 162          };
 163          o.c[t] = c[t] || 0;
 164      },
 165  
 166      /**
 167      Method for accessing the transport's interface for making a
 168      cross-domain transaction.
 169  
 170      @method xdr
 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.
 174      **/
 175      xdr: function(uri, o, c) {
 176          var io = this;
 177  
 178          if (c.xdr.use === 'flash') {
 179              // The configuration object cannot be serialized safely
 180              // across Flash's ExternalInterface.
 181              _cB[o.id] = c;
 182              w.setTimeout(function() {
 183                  try {
 184                      o.c.send(uri, { id: o.id,
 185                                      uid: o.uid,
 186                                      method: c.method,
 187                                      data: c.data,
 188                                      headers: c.headers });
 189                  }
 190                  catch(e) {
 191                      io.xdrResponse('transport error', o, c);
 192                      delete _cB[o.id];
 193                  }
 194              }, Y.io.xdr.delay);
 195          }
 196          else if (xdr) {
 197              io._ieEvt(o, c);
 198              o.c.open(c.method || 'GET', uri);
 199  
 200              // Make async to protect against IE 8 oddities.
 201              setTimeout(function() {
 202                  o.c.send(c.data);
 203              }, 0);
 204          }
 205          else {
 206              o.c.send(uri, o, c);
 207          }
 208  
 209          return {
 210              id: o.id,
 211              abort: function() {
 212                  return o.c ? _abort(o, c) : false;
 213              },
 214              isInProgress: function() {
 215                  return o.c ? _isInProgress(o.id) : false;
 216              },
 217              io: io
 218          };
 219      },
 220  
 221      /**
 222      Response controller for cross-domain requests when using the
 223      Flash transport or IE8's XDomainRequest object.
 224  
 225      @method xdrResponse
 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.
 229      @return {Object}
 230      **/
 231      xdrResponse: function(e, o, c) {
 232          c = _cB[o.id] ? _cB[o.id] : c;
 233          var io = this,
 234              m = xdr ? _rS : _cB,
 235              u = c.xdr.use,
 236              d = c.xdr.dataType;
 237  
 238          switch (e) {
 239              case 'start':
 240                  io.start(o, c);
 241                  break;
 242             //case 'complete':
 243                  //This case is not used by Flash or XDomainRequest.
 244                  //io.complete(o, c);
 245                  //break;
 246              case 'success':
 247                  io.success(_data(o, u, d), c);
 248                  delete m[o.id];
 249                  break;
 250              case 'timeout':
 251              case 'abort':
 252              case 'transport error':
 253                  o.c = { status: 0, statusText: e };
 254              case 'failure':
 255                  io.failure(_data(o, u, d), c);
 256                  delete m[o.id];
 257                  break;
 258          }
 259      },
 260  
 261      /**
 262      Fires event "io:xdrReady"
 263  
 264      @method _xdrReady
 265      @private
 266      @param {Number} yid - YUI sandbox id.
 267      @param {Number} uid - IO instance id.
 268      **/
 269      _xdrReady: function(yid, uid) {
 270          Y.fire(E_XDR_READY, yid, uid);
 271      },
 272  
 273      /**
 274      Initializes the desired transport.
 275  
 276      @method transport
 277      @param {Object} o - object of transport configurations.
 278      **/
 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'); };
 283          }
 284      }
 285  });
 286  
 287  /**
 288  Fires event "io:xdrReady"
 289  
 290  @method xdrReady
 291  @protected
 292  @static
 293  @param {Number} yid - YUI sandbox id.
 294  @param {Number} uid - IO instance id.
 295  **/
 296  Y.io.xdrReady = function(yid, uid){
 297      var io = Y.io._map[uid];
 298      Y.io.xdr.delay = 0;
 299      io._xdrReady.apply(io, [yid, uid]);
 300  };
 301  
 302  Y.io.xdrResponse = function(e, o, c){
 303      var io = Y.io._map[o.uid];
 304      io.xdrResponse.apply(io, [e, o, c]);
 305  };
 306  
 307  Y.io.transport = function(c){
 308      var io = Y.io._map['io:0'] || new Y.IO();
 309      c.uid = io._uid;
 310      io.transport.apply(io, [c]);
 311  };
 312  
 313  /**
 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.
 317  
 318  @property delay
 319  @static
 320  @type {Number}
 321  **/
 322  Y.io.xdr = { delay : 100 };
 323  
 324  
 325  }, '3.17.2', {"requires": ["io-base", "datatype-xml-parse"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1