button to generate plot pheno from plants
[sgn.git] / js / jqueryfileupload / iframe-transport.js
blobed2589506ec41c3c67243b30ce2e0512c25187cf
1 /*
2  * jQuery Iframe Transport Plugin 1.6.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://www.opensource.org/licenses/MIT
10  */
12 /*jslint unparam: true, nomen: true */
13 /*global define, window, document */
15 (function (factory) {
16     'use strict';
17     if (typeof define === 'function' && define.amd) {
18         // Register as an anonymous AMD module:
19         define(['jquery'], factory);
20     } else {
21         // Browser globals:
22         factory(window.jQuery);
23     }
24 }(function ($) {
25     'use strict';
27     // Helper variable to create unique names for the transport iframes:
28     var counter = 0;
30     // The iframe transport accepts three additional options:
31     // options.fileInput: a jQuery collection of file input fields
32     // options.paramName: the parameter name for the file form data,
33     //  overrides the name property of the file input field(s),
34     //  can be a string or an array of strings.
35     // options.formData: an array of objects with name and value properties,
36     //  equivalent to the return data of .serializeArray(), e.g.:
37     //  [{name: 'a', value: 1}, {name: 'b', value: 2}]
38     $.ajaxTransport('iframe', function (options) {
39         if (options.async) {
40             var form,
41                 iframe,
42                 addParamChar;
43             return {
44                 send: function (_, completeCallback) {
45                     form = $('<form style="display:none;"></form>');
46                     form.attr('accept-charset', options.formAcceptCharset);
47                     addParamChar = /\?/.test(options.url) ? '&' : '?';
48                     // XDomainRequest only supports GET and POST:
49                     if (options.type === 'DELETE') {
50                         options.url = options.url + addParamChar + '_method=DELETE';
51                         options.type = 'POST';
52                     } else if (options.type === 'PUT') {
53                         options.url = options.url + addParamChar + '_method=PUT';
54                         options.type = 'POST';
55                     } else if (options.type === 'PATCH') {
56                         options.url = options.url + addParamChar + '_method=PATCH';
57                         options.type = 'POST';
58                     }
59                     // javascript:false as initial iframe src
60                     // prevents warning popups on HTTPS in IE6.
61                     // IE versions below IE8 cannot set the name property of
62                     // elements that have already been added to the DOM,
63                     // so we set the name along with the iframe HTML markup:
64                     iframe = $(
65                         '<iframe src="javascript:false;" name="iframe-transport-' +
66                             (counter += 1) + '"></iframe>'
67                     ).bind('load', function () {
68                         var fileInputClones,
69                             paramNames = $.isArray(options.paramName) ?
70                                     options.paramName : [options.paramName];
71                         iframe
72                             .unbind('load')
73                             .bind('load', function () {
74                                 var response;
75                                 // Wrap in a try/catch block to catch exceptions thrown
76                                 // when trying to access cross-domain iframe contents:
77                                 try {
78                                     response = iframe.contents();
79                                     // Google Chrome and Firefox do not throw an
80                                     // exception when calling iframe.contents() on
81                                     // cross-domain requests, so we unify the response:
82                                     if (!response.length || !response[0].firstChild) {
83                                         throw new Error();
84                                     }
85                                 } catch (e) {
86                                     response = undefined;
87                                 }
88                                 // The complete callback returns the
89                                 // iframe content document as response object:
90                                 completeCallback(
91                                     200,
92                                     'success',
93                                     {'iframe': response}
94                                 );
95                                 // Fix for IE endless progress bar activity bug
96                                 // (happens on form submits to iframe targets):
97                                 $('<iframe src="javascript:false;"></iframe>')
98                                     .appendTo(form);
99                                 form.remove();
100                             });
101                         form
102                             .prop('target', iframe.prop('name'))
103                             .prop('action', options.url)
104                             .prop('method', options.type);
105                         if (options.formData) {
106                             $.each(options.formData, function (index, field) {
107                                 $('<input type="hidden"/>')
108                                     .prop('name', field.name)
109                                     .val(field.value)
110                                     .appendTo(form);
111                             });
112                         }
113                         if (options.fileInput && options.fileInput.length &&
114                                 options.type === 'POST') {
115                             fileInputClones = options.fileInput.clone();
116                             // Insert a clone for each file input field:
117                             options.fileInput.after(function (index) {
118                                 return fileInputClones[index];
119                             });
120                             if (options.paramName) {
121                                 options.fileInput.each(function (index) {
122                                     $(this).prop(
123                                         'name',
124                                         paramNames[index] || options.paramName
125                                     );
126                                 });
127                             }
128                             // Appending the file input fields to the hidden form
129                             // removes them from their original location:
130                             form
131                                 .append(options.fileInput)
132                                 .prop('enctype', 'multipart/form-data')
133                                 // enctype must be set as encoding for IE:
134                                 .prop('encoding', 'multipart/form-data');
135                         }
136                         form.submit();
137                         // Insert the file input fields at their original location
138                         // by replacing the clones with the originals:
139                         if (fileInputClones && fileInputClones.length) {
140                             options.fileInput.each(function (index, input) {
141                                 var clone = $(fileInputClones[index]);
142                                 $(input).prop('name', clone.prop('name'));
143                                 clone.replaceWith(input);
144                             });
145                         }
146                     });
147                     form.append(iframe).appendTo(document.body);
148                 },
149                 abort: function () {
150                     if (iframe) {
151                         // javascript:false as iframe src aborts the request
152                         // and prevents warning popups on HTTPS in IE6.
153                         // concat is used to avoid the "Script URL" JSLint error:
154                         iframe
155                             .unbind('load')
156                             .prop('src', 'javascript'.concat(':false;'));
157                     }
158                     if (form) {
159                         form.remove();
160                     }
161                 }
162             };
163         }
164     });
166     // The iframe transport returns the iframe content document as response.
167     // The following adds converters from iframe to text, json, html, and script:
168     $.ajaxSetup({
169         converters: {
170             'iframe text': function (iframe) {
171                 return iframe && $(iframe[0].body).text();
172             },
173             'iframe json': function (iframe) {
174                 return iframe && $.parseJSON($(iframe[0].body).text());
175             },
176             'iframe html': function (iframe) {
177                 return iframe && $(iframe[0].body).html();
178             },
179             'iframe script': function (iframe) {
180                 return iframe && $.globalEval($(iframe[0].body).text());
181             }
182         }
183     });
185 }));