1 2jQuery.extend({ 3 4 5 createUploadIframe: function (id, uri) { 6 //create frame 7 var frameId = 'jUploadFrame' + id; 8 var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"'; 9 if (window.ActiveXObject) { 10 if (typeof uri == 'boolean') { 11 iframeHtml += ' src="' + 'javascript:false' + '"'; 12 13 } 14 else if (typeof uri == 'string') { 15 iframeHtml += ' src="' + uri + '"'; 16 17 } 18 } 19 iframeHtml += ' />'; 20 jQuery(iframeHtml).appendTo(document.body); 21 22 return jQuery('#' + frameId).get(0); 23 }, 24 createUploadForm: function (id, fileElementId, data) { 25 //create form 26 var formId = 'jUploadForm' + id; 27 var fileId = 'jUploadFile' + id; 28 var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>'); 29 if (data) { 30 for (var i in data) { 31 jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form); 32 } 33 } 34 var oldElement = jQuery('#' + fileElementId); 35 var newElement = jQuery(oldElement).clone(); 36 jQuery(oldElement).attr('id', fileId); 37 jQuery(oldElement).before(newElement); 38 jQuery(oldElement).appendTo(form); 39 40 41 42 //set attributes 43 jQuery(form).css('position', 'absolute'); 44 jQuery(form).css('top', '-1200px'); 45 jQuery(form).css('left', '-1200px'); 46 jQuery(form).appendTo('body'); 47 return form; 48 }, 49 50 ajaxFileUpload: function (s) { 51 // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout 52 s = jQuery.extend({}, jQuery.ajaxSettings, s); 53 var id = new Date().getTime() 54 var form = jQuery.createUploadForm(id, s.fileElementId, (typeof (s.data) == 'undefined' ? false : s.data)); 55 var io = jQuery.createUploadIframe(id, s.secureuri); 56 var frameId = 'jUploadFrame' + id; 57 var formId = 'jUploadForm' + id; 58 // Watch for a new set of requests 59 if (s.global && !jQuery.active++) { 60 jQuery.event.trigger("ajaxStart"); 61 } 62 var requestDone = false; 63 // Create the request object 64 var xml = {} 65 if (s.global) 66 jQuery.event.trigger("ajaxSend", [xml, s]); 67 // Wait for a response to come back 68 var uploadCallback = function (isTimeout) { 69 var io = document.getElementById(frameId); 70 try { 71 if (io.contentWindow) { 72 xml.responseText = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML : null; 73 xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument : io.contentWindow.document; 74 75 } else if (io.contentDocument) { 76 xml.responseText = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML : null; 77 xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument : io.contentDocument.document; 78 } 79 } catch (e) { 80 jQuery.handleError(s, xml, null, e); 81 } 82 if (xml || isTimeout == "timeout") { 83 requestDone = true; 84 var status; 85 try { 86 status = isTimeout != "timeout" ? "success" : "error"; 87 // Make sure that the request was successful or notmodified 88 if (status != "error") { 89 // process the data (runs the xml through httpData regardless of callback) 90 var data = jQuery.uploadHttpData(xml, s.dataType); 91 // If a local callback was specified, fire it and pass it the data 92 93 if (s.success) 94 s.success(data, status); 95 96 97 98 // Fire the global callback 99 if (s.global) 100 jQuery.event.trigger("ajaxSuccess", [xml, s]); 101 } else 102 jQuery.handleError(s, xml, status); 103 } catch (e) { 104 status = "error"; 105 jQuery.handleError(s, xml, status, e); 106 } 107 108 // The request was completed 109 if (s.global) 110 jQuery.event.trigger("ajaxComplete", [xml, s]); 111 112 // Handle the global AJAX counter
113 if (s.global && ! --jQuery.active) 114 jQuery.event.trigger("ajaxStop"); 115 116 // Process result 117 if (s.complete) 118 s.complete(xml, status); 119 120 jQuery(io).unbind() 121 122 setTimeout(function () { 123 try { 124 jQuery(io).remove(); 125 jQuery(form).remove(); 126 127 } catch (e) { 128 jQuery.handleError(s, xml, null, e); 129 } 130 131 }, 100) 132 133 xml = null 134 135 } 136 } 137 // Timeout checker 138 if (s.timeout > 0) { 139 setTimeout(function () { 140 // Check to see if the request is still happening 141 if (!requestDone) uploadCallback("timeout"); 142 }, s.timeout); 143 } 144 try { 145 146 var form = jQuery('#' + formId); 147 jQuery(form).attr('action', s.url); 148 jQuery(form).attr('method', 'POST'); 149 jQuery(form).attr('target', frameId); 150 if (form.encoding) { 151 jQuery(form).attr('encoding', 'multipart/form-data'); 152 } 153 else { 154 jQuery(form).attr('enctype', 'multipart/form-data'); 155 } 156 jQuery(form).submit(); 157 158 } catch (e) { 159 jQuery.handleError(s, xml, null, e); 160 } 161 162 jQuery('#' + frameId).load(uploadCallback); 163 return { abort: function () { } }; 164 165 }, 166 167 uploadHttpData: function (r, type) { 168 var data = !type; 169 data = type == "xml" || data ? r.responseXML : r.responseText; 170 // If the type is "script", eval it in global context 171 if (type == "script") 172 jQuery.globalEval(data); 173 // Get the JavaScript object, if JSON is used. 174 if (type == "json") 175 data = $.parseJSON(data.replace(/<.*?>/ig, "")); 176 // evaluate scripts within html 177 if (type == "html") 178 jQuery("<div>").html(data).evalScripts(); 179 180 return data; 181 }, 182 handleError: function (s, xhr, status, e) { 183 // If a local callback was specified, fire it 184 if (s.error) { 185 s.error.call(s.context || s, xhr, status, e); 186 } 187 188 // Fire the global callback 189 if (s.global) { 190 (s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e]); 191 } 192 } 193}) 194
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.