2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
7
YUI.add('io-upload-iframe', function(Y) {
10
* Extends the IO base class to enable file uploads, with HTML forms,
11
* using an iframe as the transport medium.
13
* @submodule io-upload-iframe
18
* @description Parses the POST data object and creates hidden form elements
19
* for each key-value, and appends them to the HTML form object.
23
* @param {object} d The key-value hash map.
24
* @return {array} e Array of created fields.
27
function _addData(f, d) {
30
if (d.hasOwnProperty(d, p)) {
31
e[i] = document.createElement('input');
42
function _removeData(f, e) {
44
if (e && e.length > 0) {
45
for(i = 0, l = e.length; i < l; i++){
51
function _create(o, c) {
52
var UPLOAD_IFRAME = '<iframe id="ioupload' + o.id + '" name="ioupload' + o.id + '" />',
58
i = Y.Node.create(UPLOAD_IFRAME);
61
Y.get('body').appendChild(i);
63
// Bind the onload handler to the iframe to detect the file upload response.
64
Y.on("load", function() { _handle(o, c) }, '#ioupload' + o.id);
67
// Create the upload callback handler that fires when the iframe
68
// receives the load event. Subsequently, the event handler is detached
69
// and the iframe removed from the document.
70
function _handle(o, c) {
76
var p, d = Y.get('#ioupload' + o.id).get('contentWindow.document'),
77
d = Y.get('#ioupload' + o.id).get('contentWindow'),
81
p = b.getElementsByTagName('pre');
82
o.c.responseText = (p) ? p.item(0).get('innerHTML') : b.get('innerHTML');
87
setTimeout( function() { _destroy(o.id); }, 100);
91
* @description Starts timeout count if the configuration object
92
* has a defined timeout property.
94
* @method _startTimeout
97
* @param {object} o - Transaction object generated by _create().
98
* @param {object} c - Configuration object passed to YUI.io().
101
function _startTimeout(o, c) {
102
Y.io._timeout[o.id] = w.setTimeout(function() { Y.io.abort(o, c); }, c.timeout);
106
* @description Clears the timeout interval started by _startTimeout().
108
* @method _clearTimeout
111
* @param {number} id - Transaction id.
114
function _clearTimeout(id) {
115
w.clearTimeout(Y.io._timeout[id]);
116
delete Y.io._timeout[id];
119
function _destroy(id) {
120
Y.Event.purgeElement('#ioupload' + id, false, "load");
121
Y.get('body').removeChild(Y.get('#ioupload' + id));
127
* @description Uploads HTML form, inclusive of files/attachments, using the
128
* iframe created in createFrame to facilitate the transaction.
132
* @param {o} o The transaction object
133
* @param {object} uri Qualified path to transaction resource.
134
* @param {object} c - configuration object for the transaction.
137
_upload: function(o, uri, c) {
138
var f = (typeof c.form.id === 'string') ? document.getElementById(c.form.id) : c.form.id,
139
e, fields, i, p, attr;
142
// Track original HTML form attribute values.
144
action: f.getAttribute('action'),
145
target: f.getAttribute('target')
148
// Initialize the HTML form properties in case they are
149
// not defined in the HTML form.
150
f.setAttribute('action', uri);
151
f.setAttribute('method', 'POST');
152
f.setAttribute('target', 'ioupload' + o.id );
153
f.setAttribute((Y.UA.ie) ? 'encoding' : 'enctype', 'multipart/form-data');
156
fields = _addData(f, c.data);
159
// Start polling if a callback is present and the timeout
160
// property has been defined.
165
// Start file upload.
171
_removeData(f, fields);
174
// Restore HTML form attributes to their original
175
// values prior to file upload.
177
if (attr.hasOwnProperty(attr, p)) {
179
f.setAttribute(p, f[prop]);
182
f.removeAttribute(p);
191
}, '3.0.0pr2' ,{requires:['io-base']});