Mercurial > hg > orthanc
annotate OrthancExplorer/libs/jquery-file-upload/js/jquery.iframe-transport.js @ 3066:5713952f60c0 update-jquery
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
author | am@osimis.io |
---|---|
date | Wed, 26 Dec 2018 16:58:21 +0100 |
parents | 4bc019d2f969 |
children |
rev | line source |
---|---|
0 | 1 /* |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
2 * jQuery Iframe Transport Plugin 1.7 |
0 | 3 * https://github.com/blueimp/jQuery-File-Upload |
4 * | |
5 * Copyright 2011, Sebastian Tschan | |
6 * https://blueimp.net | |
7 * | |
8 * Licensed under the MIT license: | |
9 * http://www.opensource.org/licenses/MIT | |
10 */ | |
11 | |
12 /*jslint unparam: true, nomen: true */ | |
13 /*global define, window, document */ | |
14 | |
15 (function (factory) { | |
16 'use strict'; | |
17 if (typeof define === 'function' && define.amd) { | |
18 // Register as an anonymous AMD module: | |
19 define(['jquery'], factory); | |
20 } else { | |
21 // Browser globals: | |
22 factory(window.jQuery); | |
23 } | |
24 }(function ($) { | |
25 'use strict'; | |
26 | |
27 // Helper variable to create unique names for the transport iframes: | |
28 var counter = 0; | |
29 | |
30 // The iframe transport accepts three additional options: | |
31 // options.fileInput: a jQuery collection of file input fields | |
32 // options.paramName: the parameter name for the file form data, | |
33 // overrides the name property of the file input field(s), | |
34 // can be a string or an array of strings. | |
35 // options.formData: an array of objects with name and value properties, | |
36 // equivalent to the return data of .serializeArray(), e.g.: | |
37 // [{name: 'a', value: 1}, {name: 'b', value: 2}] | |
38 $.ajaxTransport('iframe', function (options) { | |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
39 if (options.async) { |
0 | 40 var form, |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
41 iframe, |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
42 addParamChar; |
0 | 43 return { |
44 send: function (_, completeCallback) { | |
45 form = $('<form style="display:none;"></form>'); | |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
46 form.attr('accept-charset', options.formAcceptCharset); |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
47 addParamChar = /\?/.test(options.url) ? '&' : '?'; |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
48 // XDomainRequest only supports GET and POST: |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
49 if (options.type === 'DELETE') { |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
50 options.url = options.url + addParamChar + '_method=DELETE'; |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
51 options.type = 'POST'; |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
52 } else if (options.type === 'PUT') { |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
53 options.url = options.url + addParamChar + '_method=PUT'; |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
54 options.type = 'POST'; |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
55 } else if (options.type === 'PATCH') { |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
56 options.url = options.url + addParamChar + '_method=PATCH'; |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
57 options.type = 'POST'; |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
58 } |
0 | 59 // javascript:false as initial iframe src |
60 // prevents warning popups on HTTPS in IE6. | |
61 // IE versions below IE8 cannot set the name property of | |
62 // elements that have already been added to the DOM, | |
63 // so we set the name along with the iframe HTML markup: | |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
64 counter += 1; |
0 | 65 iframe = $( |
66 '<iframe src="javascript:false;" name="iframe-transport-' + | |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
67 counter + '"></iframe>' |
0 | 68 ).bind('load', function () { |
69 var fileInputClones, | |
70 paramNames = $.isArray(options.paramName) ? | |
71 options.paramName : [options.paramName]; | |
72 iframe | |
73 .unbind('load') | |
74 .bind('load', function () { | |
75 var response; | |
76 // Wrap in a try/catch block to catch exceptions thrown | |
77 // when trying to access cross-domain iframe contents: | |
78 try { | |
79 response = iframe.contents(); | |
80 // Google Chrome and Firefox do not throw an | |
81 // exception when calling iframe.contents() on | |
82 // cross-domain requests, so we unify the response: | |
83 if (!response.length || !response[0].firstChild) { | |
84 throw new Error(); | |
85 } | |
86 } catch (e) { | |
87 response = undefined; | |
88 } | |
89 // The complete callback returns the | |
90 // iframe content document as response object: | |
91 completeCallback( | |
92 200, | |
93 'success', | |
94 {'iframe': response} | |
95 ); | |
96 // Fix for IE endless progress bar activity bug | |
97 // (happens on form submits to iframe targets): | |
98 $('<iframe src="javascript:false;"></iframe>') | |
99 .appendTo(form); | |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
100 window.setTimeout(function () { |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
101 // Removing the form in a setTimeout call |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
102 // allows Chrome's developer tools to display |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
103 // the response result |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
104 form.remove(); |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
105 }, 0); |
0 | 106 }); |
107 form | |
108 .prop('target', iframe.prop('name')) | |
109 .prop('action', options.url) | |
110 .prop('method', options.type); | |
111 if (options.formData) { | |
112 $.each(options.formData, function (index, field) { | |
113 $('<input type="hidden"/>') | |
114 .prop('name', field.name) | |
115 .val(field.value) | |
116 .appendTo(form); | |
117 }); | |
118 } | |
119 if (options.fileInput && options.fileInput.length && | |
120 options.type === 'POST') { | |
121 fileInputClones = options.fileInput.clone(); | |
122 // Insert a clone for each file input field: | |
123 options.fileInput.after(function (index) { | |
124 return fileInputClones[index]; | |
125 }); | |
126 if (options.paramName) { | |
127 options.fileInput.each(function (index) { | |
128 $(this).prop( | |
129 'name', | |
130 paramNames[index] || options.paramName | |
131 ); | |
132 }); | |
133 } | |
134 // Appending the file input fields to the hidden form | |
135 // removes them from their original location: | |
136 form | |
137 .append(options.fileInput) | |
138 .prop('enctype', 'multipart/form-data') | |
139 // enctype must be set as encoding for IE: | |
140 .prop('encoding', 'multipart/form-data'); | |
141 } | |
142 form.submit(); | |
143 // Insert the file input fields at their original location | |
144 // by replacing the clones with the originals: | |
145 if (fileInputClones && fileInputClones.length) { | |
146 options.fileInput.each(function (index, input) { | |
147 var clone = $(fileInputClones[index]); | |
148 $(input).prop('name', clone.prop('name')); | |
149 clone.replaceWith(input); | |
150 }); | |
151 } | |
152 }); | |
153 form.append(iframe).appendTo(document.body); | |
154 }, | |
155 abort: function () { | |
156 if (iframe) { | |
157 // javascript:false as iframe src aborts the request | |
158 // and prevents warning popups on HTTPS in IE6. | |
159 // concat is used to avoid the "Script URL" JSLint error: | |
160 iframe | |
161 .unbind('load') | |
162 .prop('src', 'javascript'.concat(':false;')); | |
163 } | |
164 if (form) { | |
165 form.remove(); | |
166 } | |
167 } | |
168 }; | |
169 } | |
170 }); | |
171 | |
172 // The iframe transport returns the iframe content document as response. | |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
173 // The following adds converters from iframe to text, json, html, xml |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
174 // and script. |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
175 // Please note that the Content-Type for JSON responses has to be text/plain |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
176 // or text/html, if the browser doesn't include application/json in the |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
177 // Accept header, else IE will show a download dialog. |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
178 // The Content-Type for XML responses on the other hand has to be always |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
179 // application/xml or text/xml, so IE properly parses the XML response. |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
180 // See also |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
181 // https://github.com/blueimp/jQuery-File-Upload/wiki/Setup#content-type-negotiation |
0 | 182 $.ajaxSetup({ |
183 converters: { | |
184 'iframe text': function (iframe) { | |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
185 return iframe && $(iframe[0].body).text(); |
0 | 186 }, |
187 'iframe json': function (iframe) { | |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
188 return iframe && $.parseJSON($(iframe[0].body).text()); |
0 | 189 }, |
190 'iframe html': function (iframe) { | |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
191 return iframe && $(iframe[0].body).html(); |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
192 }, |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
193 'iframe xml': function (iframe) { |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
194 var xmlDoc = iframe && iframe[0]; |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
195 return xmlDoc && $.isXMLDoc(xmlDoc) ? xmlDoc : |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
196 $.parseXML((xmlDoc.XMLDocument && xmlDoc.XMLDocument.xml) || |
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
197 $(xmlDoc.body).html()); |
0 | 198 }, |
199 'iframe script': function (iframe) { | |
3066
5713952f60c0
upgraded jquery, jquery-mobile, jquery-file-upload. Everything seems to be working as before. Theme has changed !
am@osimis.io
parents:
57
diff
changeset
|
200 return iframe && $.globalEval($(iframe[0].body).text()); |
0 | 201 } |
202 } | |
203 }); | |
204 | |
205 })); |