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