Bug 14321: Integrate Upload.pm into Koha
[koha.git] / koha-tmpl / intranet-tmpl / lib / jquery / plugins / ajaxfileupload.js
1
2 jQuery.extend({
3
4     createUploadIframe: function(id, uri)
5         {
6                         //create frame
7             var frameId = 'jUploadFrame' + id;
8             
9             try {
10                 var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
11                 if(typeof uri== 'boolean'){
12                     io.src = 'javascript:false';
13                 }
14                 else if(typeof uri== 'string'){
15                     io.src = uri;
16                 }
17             }
18             catch(e) {
19                 var io = document.createElement('iframe');
20                 io.id = frameId;
21                 io.name = frameId;
22             }
23             io.style.position = 'absolute';
24             io.style.top = '-1000px';
25             io.style.left = '-1000px';
26
27             document.body.appendChild(io);
28
29             return io                   
30     },
31     createUploadForm: function(id, fileElementId)
32         {
33                 //create form   
34                 var formId = 'jUploadForm' + id;
35                 var fileId = 'jUploadFile' + id;
36                 var form = $('<form  action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>'); 
37                 var oldElement = $('#' + fileElementId);
38                 var newElement = $(oldElement).clone();
39                 $(oldElement).attr('id', fileId);
40                 $(oldElement).before(newElement);
41                 $(oldElement).appendTo(form);
42                 //set attributes
43                 $(form).css('position', 'absolute');
44                 $(form).css('top', '-1200px');
45                 $(form).css('left', '-1200px');
46                 $(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);
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                 {
61                         jQuery.event.trigger( "ajaxStart" );
62                 }            
63         var requestDone = false;
64         // Create the request object
65         var xml = {}   
66         if ( s.global )
67             jQuery.event.trigger("ajaxSend", [xml, s]);
68         // Wait for a response to come back
69         var uploadCallback = function(isTimeout)
70                 {                       
71                         var io = document.getElementById(frameId);
72             try 
73                         {                               
74                                 if(io.contentWindow)
75                                 {
76                                          xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
77                          xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
78                                          
79                                 }else if(io.contentDocument)
80                                 {
81                                          xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
82                         xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
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                                                                                 {
136                                                                                         jQuery.handleError(s, xml, null, e);
137                                                                                 }                                                                       
138
139                                                                         }, 100)
140
141                 xml = null
142
143             }
144         }
145         // Timeout checker
146         if ( s.timeout > 0 ) 
147                 {
148             setTimeout(function(){
149                 // Check to see if the request is still happening
150                 if( !requestDone ) uploadCallback( "timeout" );
151             }, s.timeout);
152         }
153         try 
154                 {
155            // var io = $('#' + frameId);
156                         var form = $('#' + formId);
157                         $(form).attr('action', s.url);
158                         $(form).attr('method', 'POST');
159                         $(form).attr('target', frameId);
160             if(form.encoding)
161                         {
162                 form.encoding = 'multipart/form-data';                          
163             }
164             else
165                         {                               
166                 form.enctype = 'multipart/form-data';
167             }                   
168             $(form).submit();
169
170         } catch(e) 
171                 {                       
172             jQuery.handleError(s, xml, null, e);
173         }
174         if(window.attachEvent){
175             document.getElementById(frameId).attachEvent('onload', uploadCallback);
176         }
177         else{
178             document.getElementById(frameId).addEventListener('load', uploadCallback, false);
179         }               
180         return {abort: function () {}}; 
181
182     },
183
184     uploadHttpData: function( r, type ) {
185         var data = !type;
186         data = type == "xml" || data ? r.responseXML : r.responseText;
187         // If the type is "script", eval it in global context
188         if ( type == "script" )
189             jQuery.globalEval( data );
190         // Get the JavaScript object, if JSON is used.
191         if ( type == "json" )
192             eval( "data = " + data );
193         // evaluate scripts within html
194         if ( type == "html" )
195             jQuery("<div>").html(data).evalScripts();
196                         //alert($('param', data).each(function(){alert($(this).attr('value'));}));
197         return data;
198     }
199 })
200