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