vendor/assets/javascripts/jquery.iframe-transport.js in remotipart-1.3.1 vs vendor/assets/javascripts/jquery.iframe-transport.js in remotipart-1.4.0
- old
+ new
@@ -1,14 +1,14 @@
-// This [jQuery](http://jquery.com/) plugin implements an `<iframe>`
-// [transport](http://api.jquery.com/extending-ajax/#Transports) so that
+// This [jQuery](https://jquery.com/) plugin implements an `<iframe>`
+// [transport](https://api.jquery.com/jQuery.ajax/#extending-ajax) so that
// `$.ajax()` calls support the uploading of files using standard HTML file
// input fields. This is done by switching the exchange from `XMLHttpRequest`
// to a hidden `iframe` element containing a form that is submitted.
-// The [source for the plugin](http://github.com/cmlenz/jquery-iframe-transport)
-// is available on [Github](http://github.com/) and dual licensed under the MIT
-// or GPL Version 2 licenses.
+// The [source for the plugin](https://github.com/cmlenz/jquery-iframe-transport)
+// is available on [Github](https://github.com/) and licensed under the [MIT
+// license](https://github.com/cmlenz/jquery-iframe-transport/blob/master/LICENSE).
// ## Usage
// To use this plugin, you simply add an `iframe` option with the value `true`
// to the Ajax settings an `$.ajax()` call, and specify the file fields to
@@ -68,13 +68,13 @@
// Another problem with using an `iframe` for file uploads is that it is
// impossible for the javascript code to determine the HTTP status code of the
// servers response. Effectively, all of the calls you make will look like they
// are getting successful responses, and thus invoke the `done()` or
-// `complete()` callbacks. You can only determine communicate problems using
-// the content of the response payload. For example, consider using a JSON
-// response such as the following to indicate a problem with an uploaded file:
+// `complete()` callbacks. You can only communicate problems using the content
+// of the response payload. For example, consider using a JSON response such as
+// the following to indicate a problem with an uploaded file:
// <textarea data-type="application/json">
// {"ok": false, "message": "Please only upload reasonably sized files."}
// </textarea>
@@ -94,10 +94,11 @@
// Register a prefilter that checks whether the `iframe` option is set, and
// switches to the "iframe" data type if it is `true`.
$.ajaxPrefilter(function(options, origOptions, jqXHR) {
if (options.iframe) {
+ options.originalURL = options.url;
return "iframe";
}
});
// Register a transport for the "iframe" data type. It will only activate
@@ -107,33 +108,38 @@
var form = null,
iframe = null,
name = "iframe-" + $.now(),
files = $(options.files).filter(":file:enabled"),
markers = null,
- accepts;
+ accepts = null;
// This function gets called after a successful submission or an abortion
// and should revert all changes made to the page to enable the
// submission via this transport.
function cleanUp() {
- markers.each(function(i){
- $(this).replaceWith(files[i]);
- markers.splice(i, 1);
+ files.each(function(i, file) {
+ var $file = $(file);
+ $file.data("clone").replaceWith($file);
});
form.remove();
- iframe.bind("load", function() { iframe.remove(); });
+ iframe.one("load", function() { iframe.remove(); });
iframe.attr("src", "about:blank");
}
// Remove "iframe" from the data types list so that further processing is
// based on the content type returned by the server, without attempting an
// (unsupported) conversion from "iframe" to the actual type.
options.dataTypes.shift();
+ // Use the data from the original AJAX options, as it doesn't seem to be
+ // copied over since jQuery 1.7.
+ // See https://github.com/cmlenz/jquery-iframe-transport/issues/6
+ options.data = origOptions.data;
+
if (files.length) {
form = $("<form enctype='multipart/form-data' method='post'></form>").
- hide().attr({action: options.url, target: name});
+ hide().attr({action: options.originalURL, target: name});
// If there is any additional data specified via the `data` option,
// we add it as hidden fields to the form. This (currently) requires
// the `processData` option to be set to false so that the data doesn't
// get serialized to a string.
@@ -153,25 +159,31 @@
// field, to help server-side code to determine that the upload happened
// through this transport.
$("<input type='hidden' value='IFrame' name='X-Requested-With' />").
appendTo(form);
- // Borrowed straight from the JQuery source
- // Provides a way of specifying the accepted data type similar to HTTP_ACCEPTS
- accepts = options.dataTypes[ 0 ] && options.accepts[ options.dataTypes[0] ] ?
- options.accepts[ options.dataTypes[0] ] + ( options.dataTypes[ 0 ] !== "*" ? ", */*; q=0.01" : "" ) :
- options.accepts[ "*" ]
+ // Borrowed straight from the JQuery source.
+ // Provides a way of specifying the accepted data type similar to the
+ // HTTP "Accept" header
+ if (options.dataTypes[0] && options.accepts[options.dataTypes[0]]) {
+ accepts = options.accepts[options.dataTypes[0]] +
+ (options.dataTypes[0] !== "*" ? ", */*; q=0.01" : "");
+ } else {
+ accepts = options.accepts["*"];
+ }
+ $("<input type='hidden' name='X-HTTP-Accept'>").
+ attr("value", accepts).appendTo(form);
- $("<input type='hidden' name='X-Http-Accept'>")
- .attr("value", accepts).appendTo(form);
-
// Move the file fields into the hidden form, but first remember their
// original locations in the document by replacing them with disabled
// clones. This should also avoid introducing unwanted changes to the
// page layout during submission.
markers = files.after(function(idx) {
- return $(this).clone().prop("disabled", true);
+ var $this = $(this),
+ $clone = $this.clone().prop("disabled", true);
+ $this.data("clone", $clone);
+ return $clone;
}).next();
files.appendTo(form);
return {
@@ -181,17 +193,17 @@
iframe = $("<iframe src='about:blank' name='" + name +
"' id='" + name + "' style='display:none'></iframe>");
// The first load event gets fired after the iframe has been injected
// into the DOM, and is used to prepare the actual submission.
- iframe.bind("load", function() {
+ iframe.one("load", function() {
// The second load event gets fired when the response to the form
// submission is received. The implementation detects whether the
// actual payload is embedded in a `<textarea>` element, and
// prepares the required conversions to be made in that case.
- iframe.unbind("load").bind("load", function() {
+ iframe.one("load", function() {
var doc = this.contentWindow ? this.contentWindow.document :
(this.contentDocument ? this.contentDocument : this.document),
root = doc.documentElement ? doc.documentElement : doc.body,
textarea = root.getElementsByTagName("textarea")[0],
type = textarea && textarea.getAttribute("data-type") || null,
@@ -202,13 +214,10 @@
text: type ?
textarea.value :
root ? (root.textContent || root.innerText) : null
};
cleanUp();
- if (!jqXHR.responseText) {
- jqXHR.responseText = content.text;
- }
completeCallback(status, statusText, content, type ?
("Content-Type: " + type) :
null);
});
@@ -224,10 +233,10 @@
// The `abort` function is called by jQuery when the request should be
// aborted.
abort: function() {
if (iframe !== null) {
- iframe.unbind("load").attr("src", "javascript:false;");
+ iframe.unbind("load").attr("src", "about:blank");
cleanUp();
}
}
};