Skip to content

Commit

Permalink
Merge pull request #178 from westonganger/master
Browse files Browse the repository at this point in the history
Update jquery iframe transport to v1.0.1
  • Loading branch information
mshibuya authored May 24, 2017
2 parents d9109eb + 20a7b87 commit ed507d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
4 changes: 2 additions & 2 deletions lib/remotipart/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def call env
end

# Override the accepted format, because it isn't what we really want
if params['X-Http-Accept']
env['HTTP_ACCEPT'] = params['X-Http-Accept']
if params['X-HTTP-Accept']
env['HTTP_ACCEPT'] = params['X-HTTP-Accept']
end
end

Expand Down
67 changes: 38 additions & 29 deletions vendor/assets/javascripts/jquery.iframe-transport.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// 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

Expand Down Expand Up @@ -70,9 +70,9 @@
// 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."}
Expand All @@ -96,6 +96,7 @@
// switches to the "iframe" data type if it is `true`.
$.ajaxPrefilter(function(options, origOptions, jqXHR) {
if (options.iframe) {
options.originalURL = options.url;
return "iframe";
}
});
Expand All @@ -109,18 +110,18 @@
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");
}

Expand All @@ -129,9 +130,14 @@
// (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
Expand All @@ -155,21 +161,27 @@
$("<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[ "*" ]

$("<input type='hidden' name='X-Http-Accept'>")
.attr("value", accepts).appendTo(form);
// 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);

// 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);

Expand All @@ -183,13 +195,13 @@

// 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,
Expand All @@ -204,9 +216,6 @@
root ? (root.textContent || root.innerText) : null
};
cleanUp();
if (!jqXHR.responseText) {
jqXHR.responseText = content.text;
}
completeCallback(status, statusText, content, type ?
("Content-Type: " + type) :
null);
Expand All @@ -226,7 +235,7 @@
// aborted.
abort: function() {
if (iframe !== null) {
iframe.unbind("load").attr("src", "javascript:false;");
iframe.unbind("load").attr("src", "about:blank");
cleanUp();
}
}
Expand Down

0 comments on commit ed507d2

Please sign in to comment.