src/cm/media/js/lib/yui/yui_3.10.3/build/io-upload-iframe/io-upload-iframe.js
changeset 525 89ef5ed3c48b
equal deleted inserted replaced
524:322d0feea350 525:89ef5ed3c48b
       
     1 /*
       
     2 YUI 3.10.3 (build 2fb5187)
       
     3 Copyright 2013 Yahoo! Inc. All rights reserved.
       
     4 Licensed under the BSD License.
       
     5 http://yuilibrary.com/license/
       
     6 */
       
     7 
       
     8 YUI.add('io-upload-iframe', function (Y, NAME) {
       
     9 
       
    10 /**
       
    11 Extends the IO  to enable file uploads, with HTML forms
       
    12 using an iframe as the transport medium.
       
    13 @module io
       
    14 @submodule io-upload-iframe
       
    15 @for IO
       
    16 **/
       
    17 
       
    18 var w = Y.config.win,
       
    19     d = Y.config.doc,
       
    20     _std = (d.documentMode && d.documentMode >= 8),
       
    21     _d = decodeURIComponent,
       
    22     _end = Y.IO.prototype.end;
       
    23 
       
    24 /**
       
    25  * Creates the iframe transported used in file upload
       
    26  * transactions, and binds the response event handler.
       
    27  *
       
    28  * @method _cFrame
       
    29  * @private
       
    30  * @param {Object} o Transaction object generated by _create().
       
    31  * @param {Object} c Configuration object passed to YUI.io().
       
    32  * @param {Object} io
       
    33  */
       
    34 function _cFrame(o, c, io) {
       
    35     var i = Y.Node.create('<iframe src="#" id="io_iframe' + o.id + '" name="io_iframe' + o.id + '" />');
       
    36         i._node.style.position = 'absolute';
       
    37         i._node.style.top = '-1000px';
       
    38         i._node.style.left = '-1000px';
       
    39         Y.one('body').appendChild(i);
       
    40     // Bind the onload handler to the iframe to detect the file upload response.
       
    41     Y.on("load", function() { io._uploadComplete(o, c); }, '#io_iframe' + o.id);
       
    42 }
       
    43 
       
    44 /**
       
    45  * Removes the iframe transport used in the file upload
       
    46  * transaction.
       
    47  *
       
    48  * @method _dFrame
       
    49  * @private
       
    50  * @param {Number} id The transaction ID used in the iframe's creation.
       
    51  */
       
    52 function _dFrame(id) {
       
    53 	Y.Event.purgeElement('#io_iframe' + id, false);
       
    54 	Y.one('body').removeChild(Y.one('#io_iframe' + id));
       
    55 }
       
    56 
       
    57 Y.mix(Y.IO.prototype, {
       
    58    /**
       
    59     * Parses the POST data object and creates hidden form elements
       
    60     * for each key-value, and appends them to the HTML form object.
       
    61     * @method appendData
       
    62     * @private
       
    63     * @static
       
    64     * @param {Object} f HTML form object.
       
    65     * @param {String} s The key-value POST data.
       
    66     * @return {Array} o Array of created fields.
       
    67     */
       
    68     _addData: function(f, s) {
       
    69         // Serialize an object into a key-value string using
       
    70         // querystring-stringify-simple.
       
    71         if (Y.Lang.isObject(s)) {
       
    72             s = Y.QueryString.stringify(s);
       
    73         }
       
    74 
       
    75         var o = [],
       
    76             m = s.split('='),
       
    77             i, l;
       
    78 
       
    79         for (i = 0, l = m.length - 1; i < l; i++) {
       
    80             o[i] = d.createElement('input');
       
    81             o[i].type = 'hidden';
       
    82             o[i].name = _d(m[i].substring(m[i].lastIndexOf('&') + 1));
       
    83             o[i].value = (i + 1 === l) ? _d(m[i + 1]) : _d(m[i + 1].substring(0, (m[i + 1].lastIndexOf('&'))));
       
    84             f.appendChild(o[i]);
       
    85         }
       
    86 
       
    87         return o;
       
    88     },
       
    89 
       
    90    /**
       
    91     * Removes the custom fields created to pass additional POST
       
    92     * data, along with the HTML form fields.
       
    93     * @method _removeData
       
    94     * @private
       
    95     * @static
       
    96     * @param {Object} f HTML form object.
       
    97     * @param {Object} o HTML form fields created from configuration.data.
       
    98     */
       
    99     _removeData: function(f, o) {
       
   100         var i, l;
       
   101 
       
   102         for (i = 0, l = o.length; i < l; i++) {
       
   103             f.removeChild(o[i]);
       
   104         }
       
   105     },
       
   106 
       
   107    /**
       
   108     * Sets the appropriate attributes and values to the HTML
       
   109     * form, in preparation of a file upload transaction.
       
   110     * @method _setAttrs
       
   111     * @private
       
   112     * @static
       
   113     * @param {Object} f HTML form object.
       
   114     * @param {Object} id The Transaction ID.
       
   115     * @param {Object} uri Qualified path to transaction resource.
       
   116     */
       
   117     _setAttrs: function(f, id, uri) {
       
   118         f.setAttribute('action', uri);
       
   119         f.setAttribute('method', 'POST');
       
   120         f.setAttribute('target', 'io_iframe' + id );
       
   121         f.setAttribute(Y.UA.ie && !_std ? 'encoding' : 'enctype', 'multipart/form-data');
       
   122     },
       
   123 
       
   124    /**
       
   125     * Reset the HTML form attributes to their original values.
       
   126     * @method _resetAttrs
       
   127     * @private
       
   128     * @static
       
   129     * @param {Object} f HTML form object.
       
   130     * @param {Object} a Object of original attributes.
       
   131     */
       
   132     _resetAttrs: function(f, a) {
       
   133         Y.Object.each(a, function(v, p) {
       
   134             if (v) {
       
   135                 f.setAttribute(p, v);
       
   136             }
       
   137             else {
       
   138                 f.removeAttribute(p);
       
   139             }
       
   140         });
       
   141     },
       
   142 
       
   143    /**
       
   144     * Starts timeout count if the configuration object
       
   145     * has a defined timeout property.
       
   146     *
       
   147     * @method _startUploadTimeout
       
   148     * @private
       
   149     * @static
       
   150     * @param {Object} o Transaction object generated by _create().
       
   151     * @param {Object} c Configuration object passed to YUI.io().
       
   152     */
       
   153     _startUploadTimeout: function(o, c) {
       
   154         var io = this;
       
   155 
       
   156         io._timeout[o.id] = w.setTimeout(
       
   157             function() {
       
   158                 o.status = 0;
       
   159                 o.statusText = 'timeout';
       
   160                 io.complete(o, c);
       
   161                 io.end(o, c);
       
   162             }, c.timeout);
       
   163     },
       
   164 
       
   165    /**
       
   166     * Clears the timeout interval started by _startUploadTimeout().
       
   167     * @method _clearUploadTimeout
       
   168     * @private
       
   169     * @static
       
   170     * @param {Number} id - Transaction ID.
       
   171     */
       
   172     _clearUploadTimeout: function(id) {
       
   173         var io = this;
       
   174 
       
   175         w.clearTimeout(io._timeout[id]);
       
   176         delete io._timeout[id];
       
   177     },
       
   178 
       
   179    /**
       
   180     * Bound to the iframe's Load event and processes
       
   181     * the response data.
       
   182     * @method _uploadComplete
       
   183     * @private
       
   184     * @static
       
   185     * @param {Object} o The transaction object
       
   186     * @param {Object} c Configuration object for the transaction.
       
   187     */
       
   188     _uploadComplete: function(o, c) {
       
   189         var io = this,
       
   190             d = Y.one('#io_iframe' + o.id).get('contentWindow.document'),
       
   191             b = d.one('body'),
       
   192             p;
       
   193 
       
   194         if (c.timeout) {
       
   195             io._clearUploadTimeout(o.id);
       
   196         }
       
   197 
       
   198 		try {
       
   199 			if (b) {
       
   200 				// When a response Content-Type of "text/plain" is used, Firefox and Safari
       
   201 				// will wrap the response string with <pre></pre>.
       
   202 				p = b.one('pre:first-child');
       
   203 				o.c.responseText = p ? p.get('text') : b.get('text');
       
   204 			}
       
   205 			else {
       
   206 				o.c.responseXML = d._node;
       
   207 			}
       
   208 		}
       
   209 		catch (e) {
       
   210 			o.e = "upload failure";
       
   211 		}
       
   212 
       
   213         io.complete(o, c);
       
   214         io.end(o, c);
       
   215         // The transaction is complete, so call _dFrame to remove
       
   216         // the event listener bound to the iframe transport, and then
       
   217         // destroy the iframe.
       
   218         w.setTimeout( function() { _dFrame(o.id); }, 0);
       
   219     },
       
   220 
       
   221    /**
       
   222     * Uploads HTML form data, inclusive of files/attachments,
       
   223     * using the iframe created in _create to facilitate the transaction.
       
   224     * @method _upload
       
   225     * @private
       
   226     * @static
       
   227     * @param {Object} o The transaction object
       
   228     * @param {Object} uri Qualified path to transaction resource.
       
   229     * @param {Object} c Configuration object for the transaction.
       
   230     */
       
   231     _upload: function(o, uri, c) {
       
   232         var io = this,
       
   233             f = (typeof c.form.id === 'string') ? d.getElementById(c.form.id) : c.form.id,
       
   234             // Track original HTML form attribute values.
       
   235             attr = {
       
   236                 action: f.getAttribute('action'),
       
   237                 target: f.getAttribute('target')
       
   238             },
       
   239             fields;
       
   240 
       
   241         // Initialize the HTML form properties in case they are
       
   242         // not defined in the HTML form.
       
   243         io._setAttrs(f, o.id, uri);
       
   244         if (c.data) {
       
   245             fields = io._addData(f, c.data);
       
   246         }
       
   247 
       
   248         // Start polling if a callback is present and the timeout
       
   249         // property has been defined.
       
   250         if (c.timeout) {
       
   251             io._startUploadTimeout(o, c);
       
   252         }
       
   253 
       
   254         // Start file upload.
       
   255         f.submit();
       
   256         io.start(o, c);
       
   257         if (c.data) {
       
   258             io._removeData(f, fields);
       
   259         }
       
   260 
       
   261         return {
       
   262             id: o.id,
       
   263             abort: function() {
       
   264                 o.status = 0;
       
   265                 o.statusText = 'abort';
       
   266                 if (Y.one('#io_iframe' + o.id)) {
       
   267                     _dFrame(o.id);
       
   268                     io.complete(o, c);
       
   269                     io.end(o, c, attr);
       
   270                 }
       
   271                 else {
       
   272                     return false;
       
   273                 }
       
   274             },
       
   275             isInProgress: function() {
       
   276                 return Y.one('#io_iframe' + o.id) ? true : false;
       
   277             },
       
   278             io: io
       
   279         };
       
   280     },
       
   281 
       
   282     upload: function(o, uri, c) {
       
   283         _cFrame(o, c, this);
       
   284         return this._upload(o, uri, c);
       
   285     },
       
   286 
       
   287     end: function(transaction, config, attr) {
       
   288         if (config && config.form && config.form.upload) {
       
   289             var io = this;
       
   290             // Restore HTML form attributes to their original values.
       
   291             io._resetAttrs(f, attr);
       
   292         }
       
   293 
       
   294         return _end.call(this, transaction, config);
       
   295     }
       
   296 });
       
   297 
       
   298 
       
   299 }, '3.10.3', {"requires": ["io-base", "node-base"]});