Guest User

Custom YUI3 IO Utlity io upload iframe that returns HTML

a guest
Dec 21st, 2012
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. YUI.add('customyuiiouploadiframe', function(Y) {
  2.    
  3.     var w = Y.config.win,
  4.         d = Y.config.doc,
  5.         _std = (d.documentMode && d.documentMode >= 8),
  6.         _d = decodeURIComponent,
  7.         _end = Y.IO.prototype.end;
  8.    
  9.    
  10.     /**
  11.      * Creates the iframe transported used in file upload
  12.      * transactions, and binds the response event handler.
  13.      *
  14.      * @method _cFrame
  15.      * @private
  16.      * @param {Object} o Transaction object generated by _create().
  17.      * @param {Object} c Configuration object passed to YUI.io().
  18.      * @param {Object} io
  19.      */
  20.     function _cFrame(o, c, io) {
  21.         var i = Y.Node.create('<iframe src="#" id="io_iframe' + o.id + '" name="io_iframe' + o.id + '" />');
  22.             i._node.style.position = 'absolute';
  23.             i._node.style.top = '-1000px';
  24.             i._node.style.left = '-1000px';
  25.             Y.one('body').appendChild(i);
  26.         // Bind the onload handler to the iframe to detect the file upload response.
  27.         Y.on("load", function() { io._uploadComplete(o, c); }, '#io_iframe' + o.id);
  28.     }
  29.  
  30.     /**
  31.      * Removes the iframe transport used in the file upload
  32.      * transaction.
  33.      *
  34.      * @method _dFrame
  35.      * @private
  36.      * @param {Number} id The transaction ID used in the iframe's creation.
  37.      */
  38.     function _dFrame(id) {
  39.         Y.Event.purgeElement('#io_iframe' + id, false);
  40.         Y.one('body').removeChild(Y.one('#io_iframe' + id));
  41.     }  
  42.    
  43.     Y.IO.prototype._uploadComplete = function(o, c) {
  44.         var io = this,
  45.             d = Y.one('#io_iframe' + o.id).get('contentWindow.document'),
  46.             b = d.one('body'),
  47.             p;
  48.  
  49.         if (c.timeout) {
  50.             io._clearUploadTimeout(o.id);
  51.         }
  52.  
  53.         try {
  54.             if (b) {
  55.                 // When a response Content-Type of "text/plain" is used, Firefox and Safari
  56.                 // will wrap the response string with <pre></pre>.
  57.                 p = b.one('pre:first-child');
  58.                 o.c.responseText = p ? p.innerHTML.get('text') : b.get('text');
  59.                 o.c.responseHTML = p ? p.innerHTML.get('innerHTML') : b.get('innerHTML');
  60.             }
  61.             else {
  62.                 o.c.responseXML = d._node;
  63.             }
  64.         }
  65.         catch (e) {
  66.             o.e = "upload failure";
  67.         }
  68.  
  69.         io.complete(o, c);
  70.         io.end(o, c);
  71.         // The transaction is complete, so call _dFrame to remove
  72.         // the event listener bound to the iframe transport, and then
  73.         // destroy the iframe.
  74.         w.setTimeout( function() { _dFrame(o.id); }, 0);
  75.     }
  76.    
  77. }, '1.0.0', { requires: [ 'io-upload-iframe' ] });
Advertisement
Add Comment
Please, Sign In to add comment