io-upload-iframe-debug.js revision a2d0f15986afef30b833db3884d70718716a6a56
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * Extends the IO base class to enable file uploads, with HTML forms,
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * using an iframe as the transport medium.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @module io
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @submodule io-upload-iframe
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney _std = (d.documentMode && d.documentMode >= 8),
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @description Parses the POST data object and creates hidden form elements
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * for each key-value, and appends them to the HTML form object.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @method appendData
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} f HTML form object.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {string} s The key-value POST data.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @return {array} e Array of created fields.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney function _addData(f, s) {
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney o[i].name = _d(m[i].substring(m[i].lastIndexOf('&') + 1));
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney o[i].value = (i + 1 === l) ? _d(m[i + 1]) : _d(m[i + 1].substring(0, (m[i + 1].lastIndexOf('&'))));
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney Y.log('key: ' + o[i].name + ' and value: ' + o[i].value + ' added as form data.', 'info', 'io');
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @description Removes the custom fields created to pass additional POST
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * data, along with the HTML form fields.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} f HTML form object.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} o HTML form fields created from configuration.data.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @return {void}
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney function _removeData(f, o) {
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @description Sets the appropriate attributes and values to the HTML
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * form, in preparation of a file upload transaction.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @method _setAttrs
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} f HTML form object.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} id The Transaction ID.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} uri Qualified path to transaction resource.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @return {void}
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney f.setAttribute(Y.UA.ie && !_std ? 'encoding' : 'enctype', 'multipart/form-data');
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @description Sets the appropriate attributes and values to the HTML
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * form, in preparation of a file upload transaction.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @method _resetAttrs
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} f HTML form object.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} a Object of original attributes.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @return {void}
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney function _resetAttrs(f, a){
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney for (p in a) {
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @description Starts timeout count if the configuration object
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * has a defined timeout property.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @method _startTimeout
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} o Transaction object generated by _create().
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} c Configuration object passed to YUI.io().
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @return {void}
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney function _startTimeout(o, c) {
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney function() {
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney Y.log('Transaction ' + o.id + ' timeout.', 'info', 'io');
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @description Clears the timeout interval started by _startTimeout().
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @method _clearTimeout
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {number} id - Transaction ID.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @return {void}
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @description
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @method _destroy
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {o} o The transaction object
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} uri Qualified path to transaction resource.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} c Configuration object for the transaction.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @return {void}
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney Y.one('body').removeChild(Y.one('#ioupload' + id));
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney Y.log('The iframe transport for transaction ' + id + ' has been destroyed.', 'info', 'io');
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @description Bound to the iframe's Load event and processes
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * the response data.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @method _handle
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {o} o The transaction object
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} c Configuration object for the transaction.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @return {void}
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney function _handle(o, c) {
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney var d = Y.one('#ioupload' + o.id).get('contentWindow.document'),
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // When a response Content-Type of "text/plain" is used, Firefox and Safari
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // will wrap the response string with <pre></pre>.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney o.c.responseText = p ? p.get('text') : b.get('text');
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney Y.log('The responseText value for transaction ' + o.id + ' is: ' + o.c.responseText + '.', 'info', 'io');
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney Y.log('The response for transaction ' + o.id + ' is an XML document.', 'info', 'io');
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // The transaction is complete, so call _destroy to remove
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // the event listener bound to the iframe transport, and then
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // destroy the iframe.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney w.setTimeout( function() { _destroy(o.id); }, 0);
c31e28da00e9fe86d7e9aebaa5ff25933fec30b4Matt Sweeney * @description Creates the iframe transported used in file upload
c31e28da00e9fe86d7e9aebaa5ff25933fec30b4Matt Sweeney * transactions, and binds the response event handler.
c31e28da00e9fe86d7e9aebaa5ff25933fec30b4Matt Sweeney * @method _create
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} o Transaction object generated by _create().
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} c Configuration object passed to YUI.io().
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @return {void}
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney function _create(o, c) {
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney var i = Y.Node.create('<iframe id="ioupload' + o.id + '" name="ioupload' + o.id + '" />');
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // Bind the onload handler to the iframe to detect the file upload response.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney Y.on("load", function() { _handle(o, c); }, '#ioupload' + o.id);
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @description Uploads HTML form data, inclusive of files/attachments,
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * using the iframe created in _create to facilitate the transaction.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @method _upload
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {o} o The transaction object
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} uri Qualified path to transaction resource.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @param {object} c Configuration object for the transaction.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney * @return {void}
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney var f = (typeof c.form.id === 'string') ? d.getElementById(c.form.id) : c.form.id,
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // Track original HTML form attribute values.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // Initialize the HTML form properties in case they are
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // not defined in the HTML form.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // Start polling if a callback is present and the timeout
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney // property has been defined.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney Y.log('Transaction timeout started for transaction ' + o.id + '.', 'info', 'io');
82f3694450ab9870df1b580e54d8cb58968869f9Matt Sweeney // Start file upload.
82f3694450ab9870df1b580e54d8cb58968869f9Matt Sweeney // Restore HTML form attributes to their original values.
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney abort: function() {
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney Y.log('Transaction ' + o.id + ' aborted.', 'info', 'io');
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney Y.log('Attempted to abort transaction ' + o.id + ' but transaction has completed.', 'warn', 'io');
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney return false;
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney return Y.one('#ioupload' + o.id) ? true : false;
fdfbb58f76522ce41b2c8af7f0a9274dcf82cd4aMatt Sweeney}, '@VERSION@' ,{requires:['io-base','node-base']});