2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('io-upload-iframe', function(Y) {
11 * Extends the IO base class to enable file uploads, with HTML forms,
12 * using an iframe as the transport medium.
14 * @submodule io-upload-iframe
19 * @description Parses the POST data object and creates hidden form elements
20 * for each key-value, and appends them to the HTML form object.
24 * @param {object} f HTML form object.
25 * @param {string} s The key-value POST data.
26 * @return {array} e Array of created fields.
28 function _addData(f, s) {
33 for (i = 0, l = m.length - 1; i < l; i++) {
34 o[i] = document.createElement('input');
36 o[i].name = m[i].substring(m[i].lastIndexOf('&') + 1);
37 o[i].value = (i + 1 === l) ? m[i + 1] : m[i + 1].substring(0, (m[i + 1].lastIndexOf('&')));
45 * @description Removes the custom fields created to pass additional POST
46 * data, along with the HTML form fields.
50 * @param {object} f HTML form object.
51 * @param {object} o HTML form fields created from configuration.data.
54 function _removeData(f, o) {
57 for(i = 0, l = o.length; i < l; i++){
63 * @description Sets the appropriate attributes and values to the HTML
64 * form, in preparation of a file upload transaction.
68 * @param {object} f HTML form object.
69 * @param {object} id The Transaction ID.
70 * @param {object} uri Qualified path to transaction resource.
73 function _setAttrs(f, id, uri) {
74 var ie8 = (document.documentMode && document.documentMode === 8) ? true : false;
76 f.setAttribute('action', uri);
77 f.setAttribute('method', 'POST');
78 f.setAttribute('target', 'ioupload' + id );
79 f.setAttribute(Y.UA.ie && !ie8 ? 'encoding' : 'enctype', 'multipart/form-data');
83 * @description Sets the appropriate attributes and values to the HTML
84 * form, in preparation of a file upload transaction.
88 * @param {object} f HTML form object.
89 * @param {object} a Object of original attributes.
92 function _resetAttrs(f, a){
96 if (a.hasOwnProperty(a, p)) {
98 f.setAttribute(p, f[p]);
101 f.removeAttribute(p);
108 * @description Creates the iframe transported used in file upload
109 * transactions, and binds the response event handler.
114 * @param {object} o Transaction object generated by _create().
115 * @param {object} c Configuration object passed to YUI.io().
118 function _create(o, c) {
119 var i = Y.Node.create('<iframe id="ioupload' + o.id + '" name="ioupload' + o.id + '" />');
120 i._node.style.position = 'absolute';
121 i._node.style.top = '-1000px';
122 i._node.style.left = '-1000px';
124 Y.one('body').appendChild(i);
125 // Bind the onload handler to the iframe to detect the file upload response.
126 Y.on("load", function() { _handle(o, c) }, '#ioupload' + o.id);
130 * @description Bound to the iframe's Load event and processes
135 * @param {o} o The transaction object
136 * @param {object} c Configuration object for the transaction.
139 function _handle(o, c) {
140 var d = Y.one('#ioupload' + o.id).get('contentWindow.document'),
142 xml = (d._node.nodeType === 9),
150 // When a response Content-Type of "text/plain" is used, Firefox and Safari
151 // will wrap the response string with <pre></pre>.
152 p = b.query('pre:first-child');
153 o.c.responseText = p ? p.get('innerHTML') : b.get('innerHTML');
156 o.c.responseXML = d._node;
161 // The transaction is complete, so call _destroy to remove
162 // the event listener bound to the iframe transport, and then
163 // destroy the iframe.
164 w.setTimeout( function() { _destroy(o.id); }, 0);
168 * @description Starts timeout count if the configuration object
169 * has a defined timeout property.
171 * @method _startTimeout
174 * @param {object} o Transaction object generated by _create().
175 * @param {object} c Configuration object passed to YUI.io().
178 function _startTimeout(o, c) {
179 Y.io._timeout[o.id] = w.setTimeout(
181 var r = { id: o.id, status: 'timeout' };
189 * @description Clears the timeout interval started by _startTimeout().
190 * @method _clearTimeout
193 * @param {number} id - Transaction ID.
196 function _clearTimeout(id) {
197 w.clearTimeout(Y.io._timeout[id]);
198 delete Y.io._timeout[id];
206 * @param {o} o The transaction object
207 * @param {object} uri Qualified path to transaction resource.
208 * @param {object} c Configuration object for the transaction.
211 function _destroy(id) {
212 Y.Event.purgeElement('#ioupload' + id, false);
213 Y.one('body').removeChild(Y.one('#ioupload' + id));
218 * @description Uploads HTML form data, inclusive of files/attachments,
219 * using the iframe created in _create to facilitate the transaction.
223 * @param {o} o The transaction object
224 * @param {object} uri Qualified path to transaction resource.
225 * @param {object} c Configuration object for the transaction.
228 _upload: function(o, uri, c) {
229 var f = (typeof c.form.id === 'string') ? Y.config.doc.getElementById(c.form.id) : c.form.id,
231 // Track original HTML form attribute values.
233 action: f.getAttribute('action'),
234 target: f.getAttribute('target')
238 // Initialize the HTML form properties in case they are
239 // not defined in the HTML form.
240 _setAttrs(f, o.id, uri);
242 fields = _addData(f, c.data);
245 // Start polling if a callback is present and the timeout
246 // property has been defined.
251 // Start file upload.
255 _removeData(f, fields);
257 // Restore HTML form attributes to their original values.
258 _resetAttrs(f, attr);
263 var r = { id: o.id, status: 'abort' };
265 if (Y.one('#ioupload' + o.id)) {
274 isInProgress: function() {
275 return Y.one('#ioupload' + o.id) ? true : false;
283 }, '3.0.0' ,{requires:['io-base','node-base','event-base']});