io-upload-iframe.js revision 904fe53fbfc910d8f047ab4b4191622da50dad36
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * Extends the IO base class to enable file uploads, with HTML forms,
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * using an iframe as the transport medium.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @module io
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @submodule io-upload-iframe
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var w = Y.config.win,
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai d = Y.config.doc,
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _std = (d.documentMode && d.documentMode >= 8),
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _d = decodeURIComponent;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Creates the iframe transported used in file upload
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * transactions, and binds the response event handler.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai *
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _create
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @private
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @static
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} o Transaction object generated by _create().
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} c Configuration object passed to YUI.io().
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai function _iframe(o, c, io) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var i = Y.Node.create('<iframe id="io_iframe' + o.id + '" name="io_iframe' + o.id + '" />');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai i._node.style.position = 'absolute';
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai i._node.style.top = '-1000px';
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai i._node.style.left = '-1000px';
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.one('body').appendChild(i);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Bind the onload handler to the iframe to detect the file upload response.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.on("load", function() { io._uploadComplete(o, c); }, '#io_iframe' + o.id);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.mix(Y.IO.prototype, {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Parses the POST data object and creates hidden form elements
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * for each key-value, and appends them to the HTML form object.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method appendData
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @private
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @static
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} f HTML form object.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {string} s The key-value POST data.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {array} e Array of created fields.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _addData: function(f, s) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var o = [],
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai m = s.split('='),
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai i, l;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai for (i = 0, l = m.length - 1; i < l; i++) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai o[i] = d.createElement('input');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai o[i].type = 'hidden';
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai o[i].name = _d(m[i].substring(m[i].lastIndexOf('&') + 1));
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai o[i].value = (i + 1 === l) ? _d(m[i + 1]) : _d(m[i + 1].substring(0, (m[i + 1].lastIndexOf('&'))));
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai f.appendChild(o[i]);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('key: ' + o[i].name + ' and value: ' + o[i].value + ' added as form data.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return o;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Removes the custom fields created to pass additional POST
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * data, along with the HTML form fields.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method f
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @private
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @static
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} f HTML form object.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} o HTML form fields created from configuration.data.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _removeData: function(f, o) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var i, l;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai for (i = 0, l = o.length; i < l; i++) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai f.removeChild(o[i]);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Sets the appropriate attributes and values to the HTML
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * form, in preparation of a file upload transaction.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _setAttrs
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @private
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @static
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} f HTML form object.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} id The Transaction ID.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} uri Qualified path to transaction resource.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _setAttrs: function(f, id, uri) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai f.setAttribute('action', uri);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai f.setAttribute('method', 'POST');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai f.setAttribute('target', 'io_iframe' + id );
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai f.setAttribute(Y.UA.ie && !_std ? 'encoding' : 'enctype', 'multipart/form-data');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Reset the HTML form attributes to their original values.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _resetAttrs
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @private
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @static
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} f HTML form object.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} a Object of original attributes.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _resetAttrs: function(f, a) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.Object.each(a, function(v, p) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (v) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai f.setAttribute(p, v);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai else {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai f.removeAttribute(p);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai });
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Starts timeout count if the configuration object
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * has a defined timeout property.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai *
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _startTimeout
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @private
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @static
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} o Transaction object generated by _create().
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} c Configuration object passed to YUI.io().
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _startTimeout: function(o, c) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var io = this;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io._timeout[o.id] = w.setTimeout(
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai function() {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai o.status = 0;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai o.statusText = 'timeout';
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io.complete(o, c);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io.end(o, c);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('Transaction ' + o.id + ' timeout.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }, c.timeout);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Clears the timeout interval started by _startTimeout().
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _clearTimeout
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @private
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @static
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {number} id - Transaction ID.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _clearTimeout: function(id) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var io = this;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai w.clearTimeout(io._timeout[id]);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai delete io._timeout[id];
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _destroy
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @private
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @static
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {o} o The transaction object
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} uri Qualified path to transaction resource.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} c Configuration object for the transaction.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _destroy: function(id) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.Event.purgeElement('#io_iframe' + id, false);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.one('body').removeChild(Y.one('#io_iframe' + id));
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('The iframe transport for transaction ' + id + ' has been destroyed.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Bound to the iframe's Load event and processes
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * the response data.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _uploadComplete
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @private
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @static
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {o} o The transaction object
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} c Configuration object for the transaction.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _uploadComplete: function(o, c) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var io = this,
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai d = Y.one('#io_iframe' + o.id).get('contentWindow.document'),
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai b = d.one('body'),
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai p;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (c.timeout) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io._clearTimeout(o.id);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (b) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // When a response Content-Type of "text/plain" is used, Firefox and Safari
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // will wrap the response string with <pre></pre>.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai p = b.one('pre:first-child');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai o.c.responseText = p ? p.get('text') : b.get('text');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('The responseText value for transaction ' + o.id + ' is: ' + o.c.responseText + '.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai else {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai o.c.responseXML = d._node;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('The response for transaction ' + o.id + ' is an XML document.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io.complete(o, c);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io.end(o, c);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // The transaction is complete, so call _destroy to remove
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // the event listener bound to the iframe transport, and then
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // destroy the iframe.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai w.setTimeout( function() { io._destroy(o.id); }, 0);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai /**
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Uploads HTML form data, inclusive of files/attachments,
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * using the iframe created in _create to facilitate the transaction.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _upload
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @private
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @static
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {o} o The transaction object
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} uri Qualified path to transaction resource.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} c Configuration object for the transaction.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai */
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _upload: function(o, uri, c) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai var io = this,
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai f = (typeof c.form.id === 'string') ? d.getElementById(c.form.id) : c.form.id,
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Track original HTML form attribute values.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai attr = {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai action: f.getAttribute('action'),
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai target: f.getAttribute('target')
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai fields;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Initialize the HTML form properties in case they are
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // not defined in the HTML form.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io._setAttrs(f, o.id, uri);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (c.data) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai fields = io._addData(f, c.data);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Start polling if a callback is present and the timeout
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // property has been defined.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (c.timeout) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io._startTimeout(o, c);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('Transaction timeout started for transaction ' + o.id + '.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Start file upload.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai f.submit();
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io.start(o, c);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (c.data) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io._removeData(f, fields);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Restore HTML form attributes to their original values.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io._resetAttrs(f, attr);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai id: o.id,
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai abort: function() {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai o.status = 0;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai o.statusText = 'abort';
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai if (Y.one('#io_iframe' + o.id)) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io._destroy(o.id);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io.complete(o, c);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io.end(o, c);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('Transaction ' + o.id + ' aborted.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai else {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('Attempted to abort transaction ' + o.id + ' but transaction has completed.', 'warn', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return false;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai isInProgress: function() {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return Y.one('#io_iframe' + o.id) ? true : false;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai io: io
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai };
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai },
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai upload: function(o, uri, c) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _iframe(o, c, this);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return this._upload(o, uri, c);
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai }
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai });