This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/assets/javascripts/file-upload/jquery.iframe-transport.js

186 lines
8.7 KiB
JavaScript
Raw Normal View History

2012-07-25 08:20:07 +00:00
/*
2013-07-17 10:45:44 +00:00
* jQuery Iframe Transport Plugin 1.6.1
2012-07-25 08:20:07 +00:00
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2011, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
2013-07-17 10:45:44 +00:00
* http://www.opensource.org/licenses/MIT
2012-07-25 08:20:07 +00:00
*/
2013-07-17 10:45:44 +00:00
/*jslint unparam: true, nomen: true */
/*global define, window, document */
2012-07-25 08:20:07 +00:00
2013-07-17 10:45:44 +00:00
(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// Register as an anonymous AMD module:
define(['jquery'], factory);
} else {
// Browser globals:
factory(window.jQuery);
}
}(function ($) {
2012-07-25 08:20:07 +00:00
'use strict';
// Helper variable to create unique names for the transport iframes:
var counter = 0;
// The iframe transport accepts three additional options:
// options.fileInput: a jQuery collection of file input fields
// options.paramName: the parameter name for the file form data,
2013-07-17 10:45:44 +00:00
// overrides the name property of the file input field(s),
// can be a string or an array of strings.
2012-07-25 08:20:07 +00:00
// options.formData: an array of objects with name and value properties,
// equivalent to the return data of .serializeArray(), e.g.:
2013-07-17 10:45:44 +00:00
// [{name: 'a', value: 1}, {name: 'b', value: 2}]
$.ajaxTransport('iframe', function (options) {
if (options.async) {
2012-07-25 08:20:07 +00:00
var form,
2013-07-17 10:45:44 +00:00
iframe,
addParamChar;
2012-07-25 08:20:07 +00:00
return {
2013-07-17 10:45:44 +00:00
send: function (_, completeCallback) {
2012-07-25 08:20:07 +00:00
form = $('<form style="display:none;"></form>');
2013-07-17 10:45:44 +00:00
form.attr('accept-charset', options.formAcceptCharset);
addParamChar = /\?/.test(options.url) ? '&' : '?';
// XDomainRequest only supports GET and POST:
if (options.type === 'DELETE') {
options.url = options.url + addParamChar + '_method=DELETE';
options.type = 'POST';
} else if (options.type === 'PUT') {
options.url = options.url + addParamChar + '_method=PUT';
options.type = 'POST';
} else if (options.type === 'PATCH') {
options.url = options.url + addParamChar + '_method=PATCH';
options.type = 'POST';
}
2012-07-25 08:20:07 +00:00
// javascript:false as initial iframe src
// prevents warning popups on HTTPS in IE6.
// IE versions below IE8 cannot set the name property of
// elements that have already been added to the DOM,
// so we set the name along with the iframe HTML markup:
iframe = $(
'<iframe src="javascript:false;" name="iframe-transport-' +
(counter += 1) + '"></iframe>'
).bind('load', function () {
2013-07-17 10:45:44 +00:00
var fileInputClones,
paramNames = $.isArray(options.paramName) ?
options.paramName : [options.paramName];
2012-07-25 08:20:07 +00:00
iframe
.unbind('load')
.bind('load', function () {
var response;
// Wrap in a try/catch block to catch exceptions thrown
// when trying to access cross-domain iframe contents:
try {
response = iframe.contents();
// Google Chrome and Firefox do not throw an
// exception when calling iframe.contents() on
// cross-domain requests, so we unify the response:
if (!response.length || !response[0].firstChild) {
throw new Error();
}
} catch (e) {
response = undefined;
}
// The complete callback returns the
// iframe content document as response object:
completeCallback(
200,
'success',
{'iframe': response}
);
// Fix for IE endless progress bar activity bug
// (happens on form submits to iframe targets):
$('<iframe src="javascript:false;"></iframe>')
.appendTo(form);
form.remove();
});
form
.prop('target', iframe.prop('name'))
.prop('action', options.url)
.prop('method', options.type);
if (options.formData) {
$.each(options.formData, function (index, field) {
$('<input type="hidden"/>')
.prop('name', field.name)
.val(field.value)
.appendTo(form);
});
}
if (options.fileInput && options.fileInput.length &&
options.type === 'POST') {
fileInputClones = options.fileInput.clone();
// Insert a clone for each file input field:
options.fileInput.after(function (index) {
return fileInputClones[index];
});
if (options.paramName) {
2013-07-17 10:45:44 +00:00
options.fileInput.each(function (index) {
$(this).prop(
'name',
paramNames[index] || options.paramName
);
2012-07-25 08:20:07 +00:00
});
}
// Appending the file input fields to the hidden form
// removes them from their original location:
form
.append(options.fileInput)
.prop('enctype', 'multipart/form-data')
// enctype must be set as encoding for IE:
.prop('encoding', 'multipart/form-data');
}
form.submit();
// Insert the file input fields at their original location
// by replacing the clones with the originals:
if (fileInputClones && fileInputClones.length) {
options.fileInput.each(function (index, input) {
var clone = $(fileInputClones[index]);
$(input).prop('name', clone.prop('name'));
clone.replaceWith(input);
});
}
});
2013-07-17 10:45:44 +00:00
form.append(iframe).appendTo(document.body);
2012-07-25 08:20:07 +00:00
},
abort: function () {
if (iframe) {
// javascript:false as iframe src aborts the request
// and prevents warning popups on HTTPS in IE6.
// concat is used to avoid the "Script URL" JSLint error:
iframe
.unbind('load')
.prop('src', 'javascript'.concat(':false;'));
}
if (form) {
form.remove();
}
}
};
}
});
// The iframe transport returns the iframe content document as response.
// The following adds converters from iframe to text, json, html, and script:
$.ajaxSetup({
converters: {
'iframe text': function (iframe) {
2013-07-17 10:45:44 +00:00
return iframe && $(iframe[0].body).text();
2012-07-25 08:20:07 +00:00
},
'iframe json': function (iframe) {
2013-07-17 10:45:44 +00:00
return iframe && $.parseJSON($(iframe[0].body).text());
2012-07-25 08:20:07 +00:00
},
'iframe html': function (iframe) {
2013-07-17 10:45:44 +00:00
return iframe && $(iframe[0].body).html();
2012-07-25 08:20:07 +00:00
},
'iframe script': function (iframe) {
2013-07-17 10:45:44 +00:00
return iframe && $.globalEval($(iframe[0].body).text());
2012-07-25 08:20:07 +00:00
}
}
});
2013-07-17 10:45:44 +00:00
}));