123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- (function (factory) {
- 'use strict';
- if (typeof define === 'function' && define.amd) {
-
- define(['jquery'], factory);
- } else {
-
- factory(window.jQuery);
- }
- }(function ($) {
- 'use strict';
-
- var counter = 0;
-
-
-
-
-
-
-
-
-
-
- $.ajaxTransport('iframe', function (options) {
- if (options.async) {
-
-
-
- var initialIframeSrc = options.initialIframeSrc || 'javascript:false;',
-
- form,
- iframe,
- addParamChar;
- return {
- send: function (_, completeCallback) {
- form = $('<form style="display:none;"></form>');
- form.attr('accept-charset', options.formAcceptCharset);
- addParamChar = /\?/.test(options.url) ? '&' : '?';
-
- 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';
- }
-
-
-
- counter += 1;
- iframe = $(
- '<iframe src="' + initialIframeSrc +
- '" name="iframe-transport-' + counter + '"></iframe>'
- ).bind('load', function () {
- var fileInputClones,
- paramNames = $.isArray(options.paramName) ?
- options.paramName : [options.paramName];
- iframe
- .unbind('load')
- .bind('load', function () {
- var response;
-
-
- try {
- response = iframe.contents();
-
-
-
- if (!response.length || !response[0].firstChild) {
- throw new Error();
- }
- } catch (e) {
- response = undefined;
- }
-
-
- completeCallback(
- 200,
- 'success',
- {'iframe': response}
- );
-
-
- $('<iframe src="' + initialIframeSrc + '"></iframe>')
- .appendTo(form);
- window.setTimeout(function () {
-
-
-
- form.remove();
- }, 0);
- });
- 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();
-
- options.fileInput.after(function (index) {
- return fileInputClones[index];
- });
- if (options.paramName) {
- options.fileInput.each(function (index) {
- $(this).prop(
- 'name',
- paramNames[index] || options.paramName
- );
- });
- }
-
-
- form
- .append(options.fileInput)
- .prop('enctype', 'multipart/form-data')
-
- .prop('encoding', 'multipart/form-data');
-
- options.fileInput.removeAttr('form');
- }
- form.submit();
-
-
- if (fileInputClones && fileInputClones.length) {
- options.fileInput.each(function (index, input) {
- var clone = $(fileInputClones[index]);
-
- $(input)
- .prop('name', clone.prop('name'))
- .attr('form', clone.attr('form'));
- clone.replaceWith(input);
- });
- }
- });
- form.append(iframe).appendTo(document.body);
- },
- abort: function () {
- if (iframe) {
-
-
-
- iframe
- .unbind('load')
- .prop('src', initialIframeSrc);
- }
- if (form) {
- form.remove();
- }
- }
- };
- }
- });
-
-
-
-
-
-
-
-
-
-
- $.ajaxSetup({
- converters: {
- 'iframe text': function (iframe) {
- return iframe && $(iframe[0].body).text();
- },
- 'iframe json': function (iframe) {
- return iframe && $.parseJSON($(iframe[0].body).text());
- },
- 'iframe html': function (iframe) {
- return iframe && $(iframe[0].body).html();
- },
- 'iframe xml': function (iframe) {
- var xmlDoc = iframe && iframe[0];
- return xmlDoc && $.isXMLDoc(xmlDoc) ? xmlDoc :
- $.parseXML((xmlDoc.XMLDocument && xmlDoc.XMLDocument.xml) ||
- $(xmlDoc.body).html());
- },
- 'iframe script': function (iframe) {
- return iframe && $.globalEval($(iframe[0].body).text());
- }
- }
- });
- }));
|