comparison PalanthirExplorer/libs/jquery-file-upload/js/jquery.iframe-transport.js @ 45:33d67e1ab173

r
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Sep 2012 13:24:59 +0200
parents PalantirExplorer/libs/jquery-file-upload/js/jquery.iframe-transport.js@3959d33612cc
children
comparison
equal deleted inserted replaced
43:9be852ad33d2 45:33d67e1ab173
1 /*
2 * jQuery Iframe Transport Plugin 1.4
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) {
39 if (options.async && (options.type === 'POST' || options.type === 'GET')) {
40 var form,
41 iframe;
42 return {
43 send: function (_, completeCallback) {
44 form = $('<form style="display:none;"></form>');
45 // javascript:false as initial iframe src
46 // prevents warning popups on HTTPS in IE6.
47 // IE versions below IE8 cannot set the name property of
48 // elements that have already been added to the DOM,
49 // so we set the name along with the iframe HTML markup:
50 iframe = $(
51 '<iframe src="javascript:false;" name="iframe-transport-' +
52 (counter += 1) + '"></iframe>'
53 ).bind('load', function () {
54 var fileInputClones,
55 paramNames = $.isArray(options.paramName) ?
56 options.paramName : [options.paramName];
57 iframe
58 .unbind('load')
59 .bind('load', function () {
60 var response;
61 // Wrap in a try/catch block to catch exceptions thrown
62 // when trying to access cross-domain iframe contents:
63 try {
64 response = iframe.contents();
65 // Google Chrome and Firefox do not throw an
66 // exception when calling iframe.contents() on
67 // cross-domain requests, so we unify the response:
68 if (!response.length || !response[0].firstChild) {
69 throw new Error();
70 }
71 } catch (e) {
72 response = undefined;
73 }
74 // The complete callback returns the
75 // iframe content document as response object:
76 completeCallback(
77 200,
78 'success',
79 {'iframe': response}
80 );
81 // Fix for IE endless progress bar activity bug
82 // (happens on form submits to iframe targets):
83 $('<iframe src="javascript:false;"></iframe>')
84 .appendTo(form);
85 form.remove();
86 });
87 form
88 .prop('target', iframe.prop('name'))
89 .prop('action', options.url)
90 .prop('method', options.type);
91 if (options.formData) {
92 $.each(options.formData, function (index, field) {
93 $('<input type="hidden"/>')
94 .prop('name', field.name)
95 .val(field.value)
96 .appendTo(form);
97 });
98 }
99 if (options.fileInput && options.fileInput.length &&
100 options.type === 'POST') {
101 fileInputClones = options.fileInput.clone();
102 // Insert a clone for each file input field:
103 options.fileInput.after(function (index) {
104 return fileInputClones[index];
105 });
106 if (options.paramName) {
107 options.fileInput.each(function (index) {
108 $(this).prop(
109 'name',
110 paramNames[index] || options.paramName
111 );
112 });
113 }
114 // Appending the file input fields to the hidden form
115 // removes them from their original location:
116 form
117 .append(options.fileInput)
118 .prop('enctype', 'multipart/form-data')
119 // enctype must be set as encoding for IE:
120 .prop('encoding', 'multipart/form-data');
121 }
122 form.submit();
123 // Insert the file input fields at their original location
124 // by replacing the clones with the originals:
125 if (fileInputClones && fileInputClones.length) {
126 options.fileInput.each(function (index, input) {
127 var clone = $(fileInputClones[index]);
128 $(input).prop('name', clone.prop('name'));
129 clone.replaceWith(input);
130 });
131 }
132 });
133 form.append(iframe).appendTo(document.body);
134 },
135 abort: function () {
136 if (iframe) {
137 // javascript:false as iframe src aborts the request
138 // and prevents warning popups on HTTPS in IE6.
139 // concat is used to avoid the "Script URL" JSLint error:
140 iframe
141 .unbind('load')
142 .prop('src', 'javascript'.concat(':false;'));
143 }
144 if (form) {
145 form.remove();
146 }
147 }
148 };
149 }
150 });
151
152 // The iframe transport returns the iframe content document as response.
153 // The following adds converters from iframe to text, json, html, and script:
154 $.ajaxSetup({
155 converters: {
156 'iframe text': function (iframe) {
157 return $(iframe[0].body).text();
158 },
159 'iframe json': function (iframe) {
160 return $.parseJSON($(iframe[0].body).text());
161 },
162 'iframe html': function (iframe) {
163 return $(iframe[0].body).html();
164 },
165 'iframe script': function (iframe) {
166 return $.globalEval($(iframe[0].body).text());
167 }
168 }
169 });
170
171 }));