Blame view

sources/apps/files/js/jquery.iframe-transport.js 9.81 KB
03e52840d   Kload   Init
1
  /*
31b7f2792   Kload   Upgrade to ownclo...
2
   * jQuery Iframe Transport Plugin 1.7
03e52840d   Kload   Init
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
   * https://github.com/blueimp/jQuery-File-Upload
   *
   * Copyright 2011, Sebastian Tschan
   * https://blueimp.net
   *
   * Licensed under the MIT license:
   * http://www.opensource.org/licenses/MIT
   */
  
  /*jslint unparam: true, nomen: true */
  /*global define, window, document */
  
  (function (factory) {
      'use strict';
      if (typeof define === 'function' && define.amd) {
          // Register as an anonymous AMD module:
          define(['jquery'], factory);
      } else {
          // Browser globals:
          factory(window.jQuery);
      }
  }(function ($) {
      'use strict';
  
      // Helper variable to create unique names for the transport iframes:
      var counter = 0;
  
      // The iframe transport accepts three additional options:
      // options.fileInput: a jQuery collection of file input fields
      // options.paramName: the parameter name for the file form data,
31b7f2792   Kload   Upgrade to ownclo...
33
34
      //  overrides the name property of the file input field(s),
      //  can be a string or an array of strings.
03e52840d   Kload   Init
35
36
37
38
      // options.formData: an array of objects with name and value properties,
      //  equivalent to the return data of .serializeArray(), e.g.:
      //  [{name: 'a', value: 1}, {name: 'b', value: 2}]
      $.ajaxTransport('iframe', function (options) {
31b7f2792   Kload   Upgrade to ownclo...
39
          if (options.async) {
03e52840d   Kload   Init
40
              var form,
31b7f2792   Kload   Upgrade to ownclo...
41
42
                  iframe,
                  addParamChar;
03e52840d   Kload   Init
43
44
45
              return {
                  send: function (_, completeCallback) {
                      form = $('<form style="display:none;"></form>');
31b7f2792   Kload   Upgrade to ownclo...
46
47
48
49
50
51
52
53
54
55
56
57
58
                      form.attr('accept-charset', options.formAcceptCharset);
                      addParamChar = /\?/.test(options.url) ? '&' : '?';
                      // XDomainRequest only supports GET and POST:
                      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';
                      }
03e52840d   Kload   Init
59
60
61
62
63
                      // javascript:false as initial iframe src
                      // prevents warning popups on HTTPS in IE6.
                      // IE versions below IE8 cannot set the name property of
                      // elements that have already been added to the DOM,
                      // so we set the name along with the iframe HTML markup:
31b7f2792   Kload   Upgrade to ownclo...
64
                      counter += 1;
03e52840d   Kload   Init
65
66
                      iframe = $(
                          '<iframe src="javascript:false;" name="iframe-transport-' +
31b7f2792   Kload   Upgrade to ownclo...
67
                              counter + '"></iframe>'
03e52840d   Kload   Init
68
                      ).bind('load', function () {
31b7f2792   Kload   Upgrade to ownclo...
69
70
71
                          var fileInputClones,
                              paramNames = $.isArray(options.paramName) ?
                                      options.paramName : [options.paramName];
03e52840d   Kload   Init
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
                          iframe
                              .unbind('load')
                              .bind('load', function () {
                                  var response;
                                  // Wrap in a try/catch block to catch exceptions thrown
                                  // when trying to access cross-domain iframe contents:
                                  try {
                                      response = iframe.contents();
                                      // Google Chrome and Firefox do not throw an
                                      // exception when calling iframe.contents() on
                                      // cross-domain requests, so we unify the response:
                                      if (!response.length || !response[0].firstChild) {
                                          throw new Error();
                                      }
                                  } catch (e) {
                                      response = undefined;
                                  }
                                  // The complete callback returns the
                                  // iframe content document as response object:
                                  completeCallback(
                                      200,
                                      'success',
                                      {'iframe': response}
                                  );
                                  // Fix for IE endless progress bar activity bug
                                  // (happens on form submits to iframe targets):
                                  $('<iframe src="javascript:false;"></iframe>')
                                      .appendTo(form);
31b7f2792   Kload   Upgrade to ownclo...
100
101
102
103
104
105
                                  window.setTimeout(function () {
                                      // Removing the form in a setTimeout call
                                      // allows Chrome's developer tools to display
                                      // the response result
                                      form.remove();
                                  }, 0);
03e52840d   Kload   Init
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
                              });
                          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();
                              // Insert a clone for each file input field:
                              options.fileInput.after(function (index) {
                                  return fileInputClones[index];
                              });
                              if (options.paramName) {
31b7f2792   Kload   Upgrade to ownclo...
127
128
129
130
131
                                  options.fileInput.each(function (index) {
                                      $(this).prop(
                                          'name',
                                          paramNames[index] || options.paramName
                                      );
03e52840d   Kload   Init
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
                                  });
                              }
                              // Appending the file input fields to the hidden form
                              // removes them from their original location:
                              form
                                  .append(options.fileInput)
                                  .prop('enctype', 'multipart/form-data')
                                  // enctype must be set as encoding for IE:
                                  .prop('encoding', 'multipart/form-data');
                          }
                          form.submit();
                          // Insert the file input fields at their original location
                          // by replacing the clones with the originals:
                          if (fileInputClones && fileInputClones.length) {
                              options.fileInput.each(function (index, input) {
                                  var clone = $(fileInputClones[index]);
                                  $(input).prop('name', clone.prop('name'));
                                  clone.replaceWith(input);
                              });
                          }
                      });
                      form.append(iframe).appendTo(document.body);
                  },
                  abort: function () {
                      if (iframe) {
                          // javascript:false as iframe src aborts the request
                          // and prevents warning popups on HTTPS in IE6.
                          // concat is used to avoid the "Script URL" JSLint error:
                          iframe
                              .unbind('load')
                              .prop('src', 'javascript'.concat(':false;'));
                      }
                      if (form) {
                          form.remove();
                      }
                  }
              };
          }
      });
  
      // The iframe transport returns the iframe content document as response.
31b7f2792   Kload   Upgrade to ownclo...
173
174
175
176
177
178
179
180
181
      // The following adds converters from iframe to text, json, html, xml
      // and script.
      // Please note that the Content-Type for JSON responses has to be text/plain
      // or text/html, if the browser doesn't include application/json in the
      // Accept header, else IE will show a download dialog.
      // The Content-Type for XML responses on the other hand has to be always
      // application/xml or text/xml, so IE properly parses the XML response.
      // See also
      // https://github.com/blueimp/jQuery-File-Upload/wiki/Setup#content-type-negotiation
03e52840d   Kload   Init
182
183
184
      $.ajaxSetup({
          converters: {
              'iframe text': function (iframe) {
31b7f2792   Kload   Upgrade to ownclo...
185
                  return iframe && $(iframe[0].body).text();
03e52840d   Kload   Init
186
187
              },
              'iframe json': function (iframe) {
31b7f2792   Kload   Upgrade to ownclo...
188
                  return iframe && $.parseJSON($(iframe[0].body).text());
03e52840d   Kload   Init
189
190
              },
              'iframe html': function (iframe) {
31b7f2792   Kload   Upgrade to ownclo...
191
192
193
194
195
196
197
                  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());
03e52840d   Kload   Init
198
199
              },
              'iframe script': function (iframe) {
31b7f2792   Kload   Upgrade to ownclo...
200
                  return iframe && $.globalEval($(iframe[0].body).text());
03e52840d   Kload   Init
201
202
203
              }
          }
      });
31b7f2792   Kload   Upgrade to ownclo...
204
  }));