io-upload-iframe.js revision 904fe53fbfc910d8f047ab4b4191622da50dad36
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 _std = (d.documentMode && d.documentMode >= 8),
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Creates the iframe transported used in file upload
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * transactions, and binds the response event handler.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _create
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 var i = Y.Node.create('<iframe id="io_iframe' + o.id + '" name="io_iframe' + o.id + '" />');
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 * @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 * @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 _addData: function(f, s) {
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 Y.log('key: ' + o[i].name + ' and value: ' + o[i].value + ' added as form data.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Removes the custom fields created to pass additional POST
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * data, along with the HTML form fields.
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 _removeData: function(f, o) {
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 * @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 f.setAttribute(Y.UA.ie && !_std ? 'encoding' : 'enctype', 'multipart/form-data');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Reset the HTML form attributes to their original values.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _resetAttrs
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} f HTML form object.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} a Object of original attributes.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _resetAttrs: function(f, a) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.Object.each(a, function(v, p) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Starts timeout count if the configuration object
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * has a defined timeout property.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _startTimeout
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 _startTimeout: function(o, c) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai function() {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('Transaction ' + o.id + ' timeout.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description Clears the timeout interval started by _startTimeout().
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _clearTimeout
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {number} id - Transaction ID.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @description
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _destroy
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 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 * @description Bound to the iframe's Load event and processes
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * the response data.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @method _uploadComplete
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {o} o The transaction object
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @param {object} c Configuration object for the transaction.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai * @return {void}
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai _uploadComplete: function(o, c) {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai d = Y.one('#io_iframe' + o.id).get('contentWindow.document'),
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 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 Y.log('The response for transaction ' + o.id + ' is an XML document.', 'info', 'io');
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 * @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 * @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 f = (typeof c.form.id === 'string') ? d.getElementById(c.form.id) : c.form.id,
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Track original HTML form attribute values.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Initialize the HTML form properties in case they are
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // not defined in the HTML form.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Start polling if a callback is present and the timeout
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // property has been defined.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('Transaction timeout started for transaction ' + o.id + '.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Start file upload.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai // Restore HTML form attributes to their original values.
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai abort: function() {
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('Transaction ' + o.id + ' aborted.', 'info', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai Y.log('Attempted to abort transaction ' + o.id + ' but transaction has completed.', 'warn', 'io');
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return false;
aa3181942968bcffcfc221a75d8bdc38c4c19b4bSatyen Desai return Y.one('#io_iframe' + o.id) ? true : false;