Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- YUI.add('customyuiiouploadiframe', function(Y) {
- var w = Y.config.win,
- d = Y.config.doc,
- _std = (d.documentMode && d.documentMode >= 8),
- _d = decodeURIComponent,
- _end = Y.IO.prototype.end;
- /**
- * Creates the iframe transported used in file upload
- * transactions, and binds the response event handler.
- *
- * @method _cFrame
- * @private
- * @param {Object} o Transaction object generated by _create().
- * @param {Object} c Configuration object passed to YUI.io().
- * @param {Object} io
- */
- function _cFrame(o, c, io) {
- var i = Y.Node.create('<iframe src="#" id="io_iframe' + o.id + '" name="io_iframe' + o.id + '" />');
- i._node.style.position = 'absolute';
- i._node.style.top = '-1000px';
- i._node.style.left = '-1000px';
- Y.one('body').appendChild(i);
- // Bind the onload handler to the iframe to detect the file upload response.
- Y.on("load", function() { io._uploadComplete(o, c); }, '#io_iframe' + o.id);
- }
- /**
- * Removes the iframe transport used in the file upload
- * transaction.
- *
- * @method _dFrame
- * @private
- * @param {Number} id The transaction ID used in the iframe's creation.
- */
- function _dFrame(id) {
- Y.Event.purgeElement('#io_iframe' + id, false);
- Y.one('body').removeChild(Y.one('#io_iframe' + id));
- }
- Y.IO.prototype._uploadComplete = function(o, c) {
- var io = this,
- d = Y.one('#io_iframe' + o.id).get('contentWindow.document'),
- b = d.one('body'),
- p;
- if (c.timeout) {
- io._clearUploadTimeout(o.id);
- }
- try {
- if (b) {
- // When a response Content-Type of "text/plain" is used, Firefox and Safari
- // will wrap the response string with <pre></pre>.
- p = b.one('pre:first-child');
- o.c.responseText = p ? p.innerHTML.get('text') : b.get('text');
- o.c.responseHTML = p ? p.innerHTML.get('innerHTML') : b.get('innerHTML');
- }
- else {
- o.c.responseXML = d._node;
- }
- }
- catch (e) {
- o.e = "upload failure";
- }
- io.complete(o, c);
- io.end(o, c);
- // The transaction is complete, so call _dFrame to remove
- // the event listener bound to the iframe transport, and then
- // destroy the iframe.
- w.setTimeout( function() { _dFrame(o.id); }, 0);
- }
- }, '1.0.0', { requires: [ 'io-upload-iframe' ] });
Advertisement
Add Comment
Please, Sign In to add comment