OnlyOffice在线文档

jquery.iframe-transport.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * jQuery Iframe Transport Plugin 1.2
  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://creativecommons.org/licenses/MIT/
  10. */
  11. /*jslint unparam: true */
  12. /*global jQuery */
  13. (function ($) {
  14. 'use strict';
  15. // Helper variable to create unique names for the transport iframes:
  16. var counter = 0;
  17. // The iframe transport accepts three additional options:
  18. // options.fileInput: a jQuery collection of file input fields
  19. // options.paramName: the parameter name for the file form data,
  20. // overrides the name property of the file input field(s)
  21. // options.formData: an array of objects with name and value properties,
  22. // equivalent to the return data of .serializeArray(), e.g.:
  23. // [{name: a, value: 1}, {name: b, value: 2}]
  24. $.ajaxTransport('iframe', function (options, originalOptions, jqXHR) {
  25. if (options.type === 'POST' || options.type === 'GET') {
  26. var form,
  27. iframe;
  28. return {
  29. send: function (headers, completeCallback) {
  30. form = $('<form style="display:none;"></form>');
  31. // javascript:false as initial iframe src
  32. // prevents warning popups on HTTPS in IE6.
  33. // IE versions below IE8 cannot set the name property of
  34. // elements that have already been added to the DOM,
  35. // so we set the name along with the iframe HTML markup:
  36. iframe = $(
  37. '<iframe src="javascript:false;" name="iframe-transport-' +
  38. (counter += 1) + '"></iframe>'
  39. ).bind('load', function () {
  40. var fileInputClones;
  41. iframe
  42. .unbind('load')
  43. .bind('load', function () {
  44. // The complete callback returns the
  45. // iframe content document as response object:
  46. completeCallback(
  47. 200,
  48. 'success',
  49. {'iframe': iframe.contents()}
  50. );
  51. // Fix for IE endless progress bar activity bug
  52. // (happens on form submits to iframe targets):
  53. $('<iframe src="javascript:false;"></iframe>')
  54. .appendTo(form);
  55. form.remove();
  56. });
  57. form
  58. .prop('target', iframe.prop('name'))
  59. .prop('action', options.url)
  60. .prop('method', options.type);
  61. if (options.formData) {
  62. $.each(options.formData, function (index, field) {
  63. $('<input type="hidden"/>')
  64. .prop('name', field.name)
  65. .val(field.value)
  66. .appendTo(form);
  67. });
  68. }
  69. if (options.fileInput && options.fileInput.length &&
  70. options.type === 'POST') {
  71. fileInputClones = options.fileInput.clone();
  72. // Insert a clone for each file input field:
  73. options.fileInput.after(function (index) {
  74. return fileInputClones[index];
  75. });
  76. if (options.paramName) {
  77. options.fileInput.each(function () {
  78. $(this).prop('name', options.paramName);
  79. });
  80. }
  81. // Appending the file input fields to the hidden form
  82. // removes them from their original location:
  83. form
  84. .append(options.fileInput)
  85. .prop('enctype', 'multipart/form-data')
  86. // enctype must be set as encoding for IE:
  87. .prop('encoding', 'multipart/form-data');
  88. }
  89. form.submit();
  90. // Insert the file input fields at their original location
  91. // by replacing the clones with the originals:
  92. if (fileInputClones && fileInputClones.length) {
  93. options.fileInput.each(function (index, input) {
  94. var clone = $(fileInputClones[index]);
  95. $(input).prop('name', clone.prop('name'));
  96. clone.replaceWith(input);
  97. });
  98. }
  99. });
  100. form.append(iframe).appendTo('body');
  101. },
  102. abort: function () {
  103. if (iframe) {
  104. // javascript:false as iframe src aborts the request
  105. // and prevents warning popups on HTTPS in IE6.
  106. // concat is used to avoid the "Script URL" JSLint error:
  107. iframe
  108. .unbind('load')
  109. .prop('src', 'javascript'.concat(':false;'));
  110. }
  111. if (form) {
  112. form.remove();
  113. }
  114. }
  115. };
  116. }
  117. });
  118. // The iframe transport returns the iframe content document as response.
  119. // The following adds converters from iframe to text, json, html, and script:
  120. $.ajaxSetup({
  121. converters: {
  122. 'iframe text': function (iframe) {
  123. return iframe.text();
  124. },
  125. 'iframe json': function (iframe) {
  126. return $.parseJSON(iframe.text());
  127. },
  128. 'iframe html': function (iframe) {
  129. return iframe.find('body').html();
  130. },
  131. 'iframe script': function (iframe) {
  132. return $.globalEval(iframe.text());
  133. }
  134. }
  135. });
  136. }(jQuery));