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