Blame view

sources/apps/files/js/jquery.fileupload.js 56 KB
03e52840d   Kload   Init
1
  /*
31b7f2792   Kload   Upgrade to ownclo...
2
   * jQuery File Upload Plugin 5.32.2
03e52840d   Kload   Init
3
4
5
6
7
8
9
10
11
12
   * https://github.com/blueimp/jQuery-File-Upload
   *
   * Copyright 2010, Sebastian Tschan
   * https://blueimp.net
   *
   * Licensed under the MIT license:
   * http://www.opensource.org/licenses/MIT
   */
  
  /*jslint nomen: true, unparam: true, regexp: true */
31b7f2792   Kload   Upgrade to ownclo...
13
  /*global define, window, document, location, File, Blob, FormData */
03e52840d   Kload   Init
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  
  (function (factory) {
      'use strict';
      if (typeof define === 'function' && define.amd) {
          // Register as an anonymous AMD module:
          define([
              'jquery',
              'jquery.ui.widget'
          ], factory);
      } else {
          // Browser globals:
          factory(window.jQuery);
      }
  }(function ($) {
      'use strict';
31b7f2792   Kload   Upgrade to ownclo...
29
30
31
32
33
34
35
36
37
38
39
      // Detect file input support, based on
      // http://viljamis.com/blog/2012/file-upload-support-on-mobile/
      $.support.fileInput = !(new RegExp(
          // Handle devices which give false positives for the feature detection:
          '(Android (1\\.[0156]|2\\.[01]))' +
              '|(Windows Phone (OS 7|8\\.0))|(XBLWP)|(ZuneWP)|(WPDesktop)' +
              '|(w(eb)?OSBrowser)|(webOS)' +
              '|(Kindle/(1\\.0|2\\.[05]|3\\.0))'
      ).test(window.navigator.userAgent) ||
          // Feature detection for all other devices:
          $('<input type="file">').prop('disabled'));
03e52840d   Kload   Init
40
41
42
43
44
      // The FileReader API is not actually used, but works as feature detection,
      // as e.g. Safari supports XHR file uploads via the FormData API,
      // but not non-multipart XHR file uploads:
      $.support.xhrFileUpload = !!(window.XMLHttpRequestUpload && window.FileReader);
      $.support.xhrFormDataFileUpload = !!window.FormData;
31b7f2792   Kload   Upgrade to ownclo...
45
46
47
      // Detect support for Blob slicing (required for chunked uploads):
      $.support.blobSlice = window.Blob && (Blob.prototype.slice ||
          Blob.prototype.webkitSlice || Blob.prototype.mozSlice);
03e52840d   Kload   Init
48
49
50
51
52
53
54
55
56
57
58
      // The fileupload widget listens for change events on file input fields defined
      // via fileInput setting and paste or drop events of the given dropZone.
      // In addition to the default jQuery Widget methods, the fileupload widget
      // exposes the "add" and "send" methods, to add or directly send files using
      // the fileupload API.
      // By default, files added via file input selection, paste, drag & drop or
      // "add" method are uploaded immediately, but it is possible to override
      // the "add" callback option to queue file uploads.
      $.widget('blueimp.fileupload', {
  
          options: {
31b7f2792   Kload   Upgrade to ownclo...
59
60
              // The drop target element(s), by the default the complete document.
              // Set to null to disable drag & drop support:
03e52840d   Kload   Init
61
              dropZone: $(document),
31b7f2792   Kload   Upgrade to ownclo...
62
63
64
65
              // The paste target element(s), by the default the complete document.
              // Set to null to disable paste support:
              pasteZone: $(document),
              // The file input field(s), that are listened to for change events.
03e52840d   Kload   Init
66
67
              // If undefined, it is set to the file input fields inside
              // of the widget element on plugin initialization.
31b7f2792   Kload   Upgrade to ownclo...
68
              // Set to null to disable the change listener.
03e52840d   Kload   Init
69
70
71
72
73
74
75
76
              fileInput: undefined,
              // By default, the file input field is replaced with a clone after
              // each input field change event. This is required for iframe transport
              // queues and allows change events to be fired for the same file
              // selection, but can be disabled by setting the following option to false:
              replaceFileInput: true,
              // The parameter name for the file form data (the request argument name).
              // If undefined or empty, the name property of the file input field is
31b7f2792   Kload   Upgrade to ownclo...
77
78
              // used, or "files[]" if the file input name property is also empty,
              // can be a string or an array of strings:
03e52840d   Kload   Init
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
              paramName: undefined,
              // By default, each file of a selection is uploaded using an individual
              // request for XHR type uploads. Set to false to upload file
              // selections in one request each:
              singleFileUploads: true,
              // To limit the number of files uploaded with one XHR request,
              // set the following option to an integer greater than 0:
              limitMultiFileUploads: undefined,
              // Set the following option to true to issue all file upload requests
              // in a sequential order:
              sequentialUploads: false,
              // To limit the number of concurrent uploads,
              // set the following option to an integer greater than 0:
              limitConcurrentUploads: undefined,
              // Set the following option to true to force iframe transport uploads:
              forceIframeTransport: false,
              // Set the following option to the location of a redirect url on the
              // origin server, for cross-domain iframe transport uploads:
              redirect: undefined,
              // The parameter name for the redirect url, sent as part of the form
              // data and set to 'redirect' if this option is empty:
              redirectParamName: undefined,
              // Set the following option to the location of a postMessage window,
              // to enable postMessage transport uploads:
              postMessage: undefined,
              // By default, XHR file uploads are sent as multipart/form-data.
              // The iframe transport is always using multipart/form-data.
              // Set to false to enable non-multipart XHR uploads:
              multipart: true,
              // To upload large files in smaller chunks, set the following option
              // to a preferred maximum chunk size. If set to 0, null or undefined,
              // or the browser does not support the required Blob API, files will
              // be uploaded as a whole.
              maxChunkSize: undefined,
              // When a non-multipart upload or a chunked multipart upload has been
              // aborted, this option can be used to resume the upload by setting
              // it to the size of the already uploaded bytes. This option is most
              // useful when modifying the options object inside of the "add" or
              // "send" callbacks, as the options are cloned for each file upload.
              uploadedBytes: undefined,
              // By default, failed (abort or error) file uploads are removed from the
              // global progress calculation. Set the following option to false to
              // prevent recalculating the global progress data:
              recalculateProgress: true,
31b7f2792   Kload   Upgrade to ownclo...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
              // Interval in milliseconds to calculate and trigger progress events:
              progressInterval: 100,
              // Interval in milliseconds to calculate progress bitrate:
              bitrateInterval: 500,
              // By default, uploads are started automatically when adding files:
              autoUpload: true,
  
              // Error and info messages:
              messages: {
                  uploadedBytes: 'Uploaded bytes exceed file size'
              },
  
              // Translation function, gets the message key to be translated
              // and an object with context specific data as arguments:
              i18n: function (message, context) {
                  message = this.messages[message] || message.toString();
                  if (context) {
                      $.each(context, function (key, value) {
                          message = message.replace('{' + key + '}', value);
                      });
                  }
                  return message;
              },
03e52840d   Kload   Init
146
147
148
149
150
151
152
153
154
155
156
157
158
  
              // Additional form data to be sent along with the file uploads can be set
              // using this option, which accepts an array of objects with name and
              // value properties, a function returning such an array, a FormData
              // object (for XHR file uploads), or a simple object.
              // The form of the first fileInput is given as parameter to the function:
              formData: function (form) {
                  return form.serializeArray();
              },
  
              // The add callback is invoked as soon as files are added to the fileupload
              // widget (via file input selection, drag & drop, paste or add API call).
              // If the singleFileUploads option is enabled, this callback will be
31b7f2792   Kload   Upgrade to ownclo...
159
              // called once for each file in the selection for XHR file uploads, else
03e52840d   Kload   Init
160
              // once for each file selection.
31b7f2792   Kload   Upgrade to ownclo...
161
              //
03e52840d   Kload   Init
162
163
              // The upload starts when the submit method is invoked on the data parameter.
              // The data object contains a files property holding the added files
31b7f2792   Kload   Upgrade to ownclo...
164
165
              // and allows you to override plugin options as well as define ajax settings.
              //
03e52840d   Kload   Init
166
167
              // Listeners for this callback can also be bound the following way:
              // .bind('fileuploadadd', func);
31b7f2792   Kload   Upgrade to ownclo...
168
              //
03e52840d   Kload   Init
169
170
171
172
              // data.submit() returns a Promise object and allows to attach additional
              // handlers using jQuery's Deferred callbacks:
              // data.submit().done(func).fail(func).always(func);
              add: function (e, data) {
31b7f2792   Kload   Upgrade to ownclo...
173
174
175
176
177
178
                  if (data.autoUpload || (data.autoUpload !== false &&
                          $(this).fileupload('option', 'autoUpload'))) {
                      data.process().done(function () {
                          data.submit();
                      });
                  }
03e52840d   Kload   Init
179
180
181
              },
  
              // Other callbacks:
31b7f2792   Kload   Upgrade to ownclo...
182

03e52840d   Kload   Init
183
184
              // Callback for the submit event of each file upload:
              // submit: function (e, data) {}, // .bind('fileuploadsubmit', func);
31b7f2792   Kload   Upgrade to ownclo...
185

03e52840d   Kload   Init
186
187
              // Callback for the start of each file upload request:
              // send: function (e, data) {}, // .bind('fileuploadsend', func);
31b7f2792   Kload   Upgrade to ownclo...
188

03e52840d   Kload   Init
189
190
              // Callback for successful uploads:
              // done: function (e, data) {}, // .bind('fileuploaddone', func);
31b7f2792   Kload   Upgrade to ownclo...
191

03e52840d   Kload   Init
192
193
              // Callback for failed (abort or error) uploads:
              // fail: function (e, data) {}, // .bind('fileuploadfail', func);
31b7f2792   Kload   Upgrade to ownclo...
194

03e52840d   Kload   Init
195
196
              // Callback for completed (success, abort or error) requests:
              // always: function (e, data) {}, // .bind('fileuploadalways', func);
31b7f2792   Kload   Upgrade to ownclo...
197

03e52840d   Kload   Init
198
199
              // Callback for upload progress events:
              // progress: function (e, data) {}, // .bind('fileuploadprogress', func);
31b7f2792   Kload   Upgrade to ownclo...
200

03e52840d   Kload   Init
201
202
              // Callback for global upload progress events:
              // progressall: function (e, data) {}, // .bind('fileuploadprogressall', func);
31b7f2792   Kload   Upgrade to ownclo...
203

03e52840d   Kload   Init
204
205
              // Callback for uploads start, equivalent to the global ajaxStart event:
              // start: function (e) {}, // .bind('fileuploadstart', func);
31b7f2792   Kload   Upgrade to ownclo...
206

03e52840d   Kload   Init
207
208
              // Callback for uploads stop, equivalent to the global ajaxStop event:
              // stop: function (e) {}, // .bind('fileuploadstop', func);
31b7f2792   Kload   Upgrade to ownclo...
209
210
  
              // Callback for change events of the fileInput(s):
03e52840d   Kload   Init
211
              // change: function (e, data) {}, // .bind('fileuploadchange', func);
31b7f2792   Kload   Upgrade to ownclo...
212
213
  
              // Callback for paste events to the pasteZone(s):
03e52840d   Kload   Init
214
              // paste: function (e, data) {}, // .bind('fileuploadpaste', func);
31b7f2792   Kload   Upgrade to ownclo...
215
216
  
              // Callback for drop events of the dropZone(s):
03e52840d   Kload   Init
217
              // drop: function (e, data) {}, // .bind('fileuploaddrop', func);
31b7f2792   Kload   Upgrade to ownclo...
218
219
  
              // Callback for dragover events of the dropZone(s):
03e52840d   Kload   Init
220
              // dragover: function (e) {}, // .bind('fileuploaddragover', func);
31b7f2792   Kload   Upgrade to ownclo...
221
222
223
224
225
226
227
228
229
230
231
              // Callback for the start of each chunk upload request:
              // chunksend: function (e, data) {}, // .bind('fileuploadchunksend', func);
  
              // Callback for successful chunk uploads:
              // chunkdone: function (e, data) {}, // .bind('fileuploadchunkdone', func);
  
              // Callback for failed (abort or error) chunk uploads:
              // chunkfail: function (e, data) {}, // .bind('fileuploadchunkfail', func);
  
              // Callback for completed (success, abort or error) chunk upload requests:
              // chunkalways: function (e, data) {}, // .bind('fileuploadchunkalways', func);
03e52840d   Kload   Init
232
233
234
235
236
237
              // The plugin options are used as settings object for the ajax calls.
              // The following are jQuery ajax settings required for the file uploads:
              processData: false,
              contentType: false,
              cache: false
          },
31b7f2792   Kload   Upgrade to ownclo...
238
239
240
          // A list of options that require reinitializing event listeners and/or
          // special initialization code:
          _specialOptions: [
03e52840d   Kload   Init
241
              'fileInput',
31b7f2792   Kload   Upgrade to ownclo...
242
243
              'dropZone',
              'pasteZone',
03e52840d   Kload   Init
244
245
246
              'multipart',
              'forceIframeTransport'
          ],
31b7f2792   Kload   Upgrade to ownclo...
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
          _blobSlice: $.support.blobSlice && function () {
              var slice = this.slice || this.webkitSlice || this.mozSlice;
              return slice.apply(this, arguments);
          },
  
          _BitrateTimer: function () {
              this.timestamp = ((Date.now) ? Date.now() : (new Date()).getTime());
              this.loaded = 0;
              this.bitrate = 0;
              this.getBitrate = function (now, loaded, interval) {
                  var timeDiff = now - this.timestamp;
                  if (!this.bitrate || !interval || timeDiff > interval) {
                      this.bitrate = (loaded - this.loaded) * (1000 / timeDiff) * 8;
                      this.loaded = loaded;
                      this.timestamp = now;
                  }
                  return this.bitrate;
              };
          },
03e52840d   Kload   Init
266
267
268
269
270
271
272
273
274
275
          _isXHRUpload: function (options) {
              return !options.forceIframeTransport &&
                  ((!options.multipart && $.support.xhrFileUpload) ||
                  $.support.xhrFormDataFileUpload);
          },
  
          _getFormData: function (options) {
              var formData;
              if (typeof options.formData === 'function') {
                  return options.formData(options.form);
31b7f2792   Kload   Upgrade to ownclo...
276
277
              }
              if ($.isArray(options.formData)) {
03e52840d   Kload   Init
278
                  return options.formData;
31b7f2792   Kload   Upgrade to ownclo...
279
280
              }
              if ($.type(options.formData) === 'object') {
03e52840d   Kload   Init
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
                  formData = [];
                  $.each(options.formData, function (name, value) {
                      formData.push({name: name, value: value});
                  });
                  return formData;
              }
              return [];
          },
  
          _getTotal: function (files) {
              var total = 0;
              $.each(files, function (index, file) {
                  total += file.size || 1;
              });
              return total;
          },
31b7f2792   Kload   Upgrade to ownclo...
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
          _initProgressObject: function (obj) {
              var progress = {
                  loaded: 0,
                  total: 0,
                  bitrate: 0
              };
              if (obj._progress) {
                  $.extend(obj._progress, progress);
              } else {
                  obj._progress = progress;
              }
          },
  
          _initResponseObject: function (obj) {
              var prop;
              if (obj._response) {
                  for (prop in obj._response) {
                      if (obj._response.hasOwnProperty(prop)) {
                          delete obj._response[prop];
                      }
                  }
              } else {
                  obj._response = {};
              }
          },
03e52840d   Kload   Init
322
323
          _onProgress: function (e, data) {
              if (e.lengthComputable) {
31b7f2792   Kload   Upgrade to ownclo...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
                  var now = ((Date.now) ? Date.now() : (new Date()).getTime()),
                      loaded;
                  if (data._time && data.progressInterval &&
                          (now - data._time < data.progressInterval) &&
                          e.loaded !== e.total) {
                      return;
                  }
                  data._time = now;
                  loaded = Math.floor(
                      e.loaded / e.total * (data.chunkSize || data._progress.total)
                  ) + (data.uploadedBytes || 0);
                  // Add the difference from the previously loaded state
                  // to the global loaded counter:
                  this._progress.loaded += (loaded - data._progress.loaded);
                  this._progress.bitrate = this._bitrateTimer.getBitrate(
                      now,
                      this._progress.loaded,
                      data.bitrateInterval
                  );
                  data._progress.loaded = data.loaded = loaded;
                  data._progress.bitrate = data.bitrate = data._bitrateTimer.getBitrate(
                      now,
                      loaded,
                      data.bitrateInterval
                  );
03e52840d   Kload   Init
349
350
351
352
353
354
                  // Trigger a custom progress event with a total data property set
                  // to the file size(s) of the current upload and a loaded data
                  // property calculated accordingly:
                  this._trigger('progress', e, data);
                  // Trigger a global progress event for all current file uploads,
                  // including ajax calls queued for sequential file uploads:
31b7f2792   Kload   Upgrade to ownclo...
355
                  this._trigger('progressall', e, this._progress);
03e52840d   Kload   Init
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
              }
          },
  
          _initProgressListener: function (options) {
              var that = this,
                  xhr = options.xhr ? options.xhr() : $.ajaxSettings.xhr();
              // Accesss to the native XHR object is required to add event listeners
              // for the upload progress event:
              if (xhr.upload) {
                  $(xhr.upload).bind('progress', function (e) {
                      var oe = e.originalEvent;
                      // Make sure the progress event properties get copied over:
                      e.lengthComputable = oe.lengthComputable;
                      e.loaded = oe.loaded;
                      e.total = oe.total;
                      that._onProgress(e, options);
                  });
                  options.xhr = function () {
                      return xhr;
                  };
              }
          },
31b7f2792   Kload   Upgrade to ownclo...
378
379
380
381
          _isInstanceOf: function (type, obj) {
              // Cross-frame instanceof check
              return Object.prototype.toString.call(obj) === '[object ' + type + ']';
          },
03e52840d   Kload   Init
382
          _initXHRData: function (options) {
31b7f2792   Kload   Upgrade to ownclo...
383
384
              var that = this,
                  formData,
03e52840d   Kload   Init
385
386
                  file = options.files[0],
                  // Ignore non-multipart setting if not supported:
31b7f2792   Kload   Upgrade to ownclo...
387
388
389
390
391
392
393
394
395
                  multipart = options.multipart || !$.support.xhrFileUpload,
                  paramName = options.paramName[0];
              options.headers = options.headers || {};
              if (options.contentRange) {
                  options.headers['Content-Range'] = options.contentRange;
              }
              if (!multipart || options.blob || !this._isInstanceOf('File', file)) {
                  options.headers['Content-Disposition'] = 'attachment; filename="' +
                      encodeURI(file.name) + '"';
03e52840d   Kload   Init
396
              }
31b7f2792   Kload   Upgrade to ownclo...
397
398
399
400
              if (!multipart) {
                  options.contentType = file.type;
                  options.data = options.blob || file;
              } else if ($.support.xhrFormDataFileUpload) {
03e52840d   Kload   Init
401
402
403
404
405
406
407
408
                  if (options.postMessage) {
                      // window.postMessage does not allow sending FormData
                      // objects, so we just add the File/Blob objects to
                      // the formData array and let the postMessage window
                      // create the FormData object out of this array:
                      formData = this._getFormData(options);
                      if (options.blob) {
                          formData.push({
31b7f2792   Kload   Upgrade to ownclo...
409
                              name: paramName,
03e52840d   Kload   Init
410
411
412
413
414
                              value: options.blob
                          });
                      } else {
                          $.each(options.files, function (index, file) {
                              formData.push({
31b7f2792   Kload   Upgrade to ownclo...
415
                                  name: options.paramName[index] || paramName,
03e52840d   Kload   Init
416
417
418
419
420
                                  value: file
                              });
                          });
                      }
                  } else {
31b7f2792   Kload   Upgrade to ownclo...
421
                      if (that._isInstanceOf('FormData', options.formData)) {
03e52840d   Kload   Init
422
423
424
425
426
427
428
429
                          formData = options.formData;
                      } else {
                          formData = new FormData();
                          $.each(this._getFormData(options), function (index, field) {
                              formData.append(field.name, field.value);
                          });
                      }
                      if (options.blob) {
31b7f2792   Kload   Upgrade to ownclo...
430
                          formData.append(paramName, options.blob, file.name);
03e52840d   Kload   Init
431
432
                      } else {
                          $.each(options.files, function (index, file) {
03e52840d   Kload   Init
433
434
                              // This check allows the tests to run with
                              // dummy objects:
31b7f2792   Kload   Upgrade to ownclo...
435
436
437
438
439
440
441
                              if (that._isInstanceOf('File', file) ||
                                      that._isInstanceOf('Blob', file)) {
                                  formData.append(
                                      options.paramName[index] || paramName,
                                      file,
                                      file.name
                                  );
03e52840d   Kload   Init
442
443
444
445
446
447
448
449
450
451
452
                              }
                          });
                      }
                  }
                  options.data = formData;
              }
              // Blob reference is not needed anymore, free memory:
              options.blob = null;
          },
  
          _initIframeSettings: function (options) {
31b7f2792   Kload   Upgrade to ownclo...
453
              var targetHost = $('<a></a>').prop('href', options.url).prop('host');
03e52840d   Kload   Init
454
455
456
457
458
              // Setting the dataType to iframe enables the iframe transport:
              options.dataType = 'iframe ' + (options.dataType || '');
              // The iframe transport accepts a serialized array as form data:
              options.formData = this._getFormData(options);
              // Add redirect url to form data on cross-domain uploads:
31b7f2792   Kload   Upgrade to ownclo...
459
              if (options.redirect && targetHost && targetHost !== location.host) {
03e52840d   Kload   Init
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
                  options.formData.push({
                      name: options.redirectParamName || 'redirect',
                      value: options.redirect
                  });
              }
          },
  
          _initDataSettings: function (options) {
              if (this._isXHRUpload(options)) {
                  if (!this._chunkedUpload(options, true)) {
                      if (!options.data) {
                          this._initXHRData(options);
                      }
                      this._initProgressListener(options);
                  }
                  if (options.postMessage) {
                      // Setting the dataType to postmessage enables the
                      // postMessage transport:
                      options.dataType = 'postmessage ' + (options.dataType || '');
                  }
              } else {
31b7f2792   Kload   Upgrade to ownclo...
481
                  this._initIframeSettings(options);
03e52840d   Kload   Init
482
483
              }
          },
31b7f2792   Kload   Upgrade to ownclo...
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
          _getParamName: function (options) {
              var fileInput = $(options.fileInput),
                  paramName = options.paramName;
              if (!paramName) {
                  paramName = [];
                  fileInput.each(function () {
                      var input = $(this),
                          name = input.prop('name') || 'files[]',
                          i = (input.prop('files') || [1]).length;
                      while (i) {
                          paramName.push(name);
                          i -= 1;
                      }
                  });
                  if (!paramName.length) {
                      paramName = [fileInput.prop('name') || 'files[]'];
                  }
              } else if (!$.isArray(paramName)) {
                  paramName = [paramName];
              }
              return paramName;
          },
03e52840d   Kload   Init
506
507
508
509
510
          _initFormSettings: function (options) {
              // Retrieve missing options from the input field and the
              // associated form, if available:
              if (!options.form || !options.form.length) {
                  options.form = $(options.fileInput.prop('form'));
31b7f2792   Kload   Upgrade to ownclo...
511
512
513
514
515
                  // If the given file input doesn't have an associated form,
                  // use the default widget file input's form:
                  if (!options.form.length) {
                      options.form = $(this.options.fileInput.prop('form'));
                  }
03e52840d   Kload   Init
516
              }
31b7f2792   Kload   Upgrade to ownclo...
517
              options.paramName = this._getParamName(options);
03e52840d   Kload   Init
518
519
520
521
522
523
              if (!options.url) {
                  options.url = options.form.prop('action') || location.href;
              }
              // The HTTP request method must be "POST" or "PUT":
              options.type = (options.type || options.form.prop('method') || '')
                  .toUpperCase();
31b7f2792   Kload   Upgrade to ownclo...
524
525
              if (options.type !== 'POST' && options.type !== 'PUT' &&
                      options.type !== 'PATCH') {
03e52840d   Kload   Init
526
527
                  options.type = 'POST';
              }
31b7f2792   Kload   Upgrade to ownclo...
528
529
530
              if (!options.formAcceptCharset) {
                  options.formAcceptCharset = options.form.attr('accept-charset');
              }
03e52840d   Kload   Init
531
532
533
534
535
536
537
538
          },
  
          _getAJAXSettings: function (data) {
              var options = $.extend({}, this.options, data);
              this._initFormSettings(options);
              this._initDataSettings(options);
              return options;
          },
31b7f2792   Kload   Upgrade to ownclo...
539
540
541
542
543
544
545
546
547
548
549
550
551
552
          // jQuery 1.6 doesn't provide .state(),
          // while jQuery 1.8+ removed .isRejected() and .isResolved():
          _getDeferredState: function (deferred) {
              if (deferred.state) {
                  return deferred.state();
              }
              if (deferred.isResolved()) {
                  return 'resolved';
              }
              if (deferred.isRejected()) {
                  return 'rejected';
              }
              return 'pending';
          },
03e52840d   Kload   Init
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
          // Maps jqXHR callbacks to the equivalent
          // methods of the given Promise object:
          _enhancePromise: function (promise) {
              promise.success = promise.done;
              promise.error = promise.fail;
              promise.complete = promise.always;
              return promise;
          },
  
          // Creates and returns a Promise object enhanced with
          // the jqXHR methods abort, success, error and complete:
          _getXHRPromise: function (resolveOrReject, context, args) {
              var dfd = $.Deferred(),
                  promise = dfd.promise();
              context = context || this.options.context || promise;
              if (resolveOrReject === true) {
                  dfd.resolveWith(context, args);
              } else if (resolveOrReject === false) {
                  dfd.rejectWith(context, args);
              }
              promise.abort = dfd.promise;
              return this._enhancePromise(promise);
          },
31b7f2792   Kload   Upgrade to ownclo...
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
          // Adds convenience methods to the data callback argument:
          _addConvenienceMethods: function (e, data) {
              var that = this,
                  getPromise = function (data) {
                      return $.Deferred().resolveWith(that, [data]).promise();
                  };
              data.process = function (resolveFunc, rejectFunc) {
                  if (resolveFunc || rejectFunc) {
                      data._processQueue = this._processQueue =
                          (this._processQueue || getPromise(this))
                              .pipe(resolveFunc, rejectFunc);
                  }
                  return this._processQueue || getPromise(this);
              };
              data.submit = function () {
                  if (this.state() !== 'pending') {
                      data.jqXHR = this.jqXHR =
                          (that._trigger('submit', e, this) !== false) &&
                          that._onSend(e, this);
                  }
                  return this.jqXHR || that._getXHRPromise();
              };
              data.abort = function () {
                  if (this.jqXHR) {
                      return this.jqXHR.abort();
                  }
                  return that._getXHRPromise();
              };
              data.state = function () {
                  if (this.jqXHR) {
                      return that._getDeferredState(this.jqXHR);
                  }
                  if (this._processQueue) {
                      return that._getDeferredState(this._processQueue);
                  }
              };
              data.progress = function () {
                  return this._progress;
              };
              data.response = function () {
                  return this._response;
              };
          },
  
          // Parses the Range header from the server response
          // and returns the uploaded bytes:
          _getUploadedBytes: function (jqXHR) {
              var range = jqXHR.getResponseHeader('Range'),
                  parts = range && range.split('-'),
                  upperBytesPos = parts && parts.length > 1 &&
                      parseInt(parts[1], 10);
              return upperBytesPos && upperBytesPos + 1;
          },
03e52840d   Kload   Init
629
630
631
632
633
634
          // Uploads a file in multiple, sequential requests
          // by splitting the file up in multiple blob chunks.
          // If the second parameter is true, only tests if the file
          // should be uploaded in chunks, but does not invoke any
          // upload requests:
          _chunkedUpload: function (options, testOnly) {
31b7f2792   Kload   Upgrade to ownclo...
635
              options.uploadedBytes = options.uploadedBytes || 0;
03e52840d   Kload   Init
636
637
638
              var that = this,
                  file = options.files[0],
                  fs = file.size,
31b7f2792   Kload   Upgrade to ownclo...
639
                  ub = options.uploadedBytes,
03e52840d   Kload   Init
640
                  mcs = options.maxChunkSize || fs,
31b7f2792   Kload   Upgrade to ownclo...
641
642
643
                  slice = this._blobSlice,
                  dfd = $.Deferred(),
                  promise = dfd.promise(),
03e52840d   Kload   Init
644
                  jqXHR,
31b7f2792   Kload   Upgrade to ownclo...
645
                  upload;
03e52840d   Kload   Init
646
647
648
649
650
651
652
653
              if (!(this._isXHRUpload(options) && slice && (ub || mcs < fs)) ||
                      options.data) {
                  return false;
              }
              if (testOnly) {
                  return true;
              }
              if (ub >= fs) {
31b7f2792   Kload   Upgrade to ownclo...
654
                  file.error = options.i18n('uploadedBytes');
03e52840d   Kload   Init
655
656
657
658
659
660
                  return this._getXHRPromise(
                      false,
                      options.context,
                      [null, 'error', file.error]
                  );
              }
31b7f2792   Kload   Upgrade to ownclo...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
              // The chunk upload method:
              upload = function () {
                  // Clone the options object for each chunk upload:
                  var o = $.extend({}, options),
                      currentLoaded = o._progress.loaded;
                  o.blob = slice.call(
                      file,
                      ub,
                      ub + mcs,
                      file.type
                  );
                  // Store the current chunk size, as the blob itself
                  // will be dereferenced after data processing:
                  o.chunkSize = o.blob.size;
                  // Expose the chunk bytes position range:
                  o.contentRange = 'bytes ' + ub + '-' +
                      (ub + o.chunkSize - 1) + '/' + fs;
                  // Process the upload data (the blob and potential form data):
                  that._initXHRData(o);
                  // Add progress listeners for this chunk upload:
                  that._initProgressListener(o);
                  jqXHR = ((that._trigger('chunksend', null, o) !== false && $.ajax(o)) ||
                          that._getXHRPromise(false, o.context))
                      .done(function (result, textStatus, jqXHR) {
                          ub = that._getUploadedBytes(jqXHR) ||
                              (ub + o.chunkSize);
                          // Create a progress event if no final progress event
                          // with loaded equaling total has been triggered
                          // for this chunk:
                          if (currentLoaded + o.chunkSize - o._progress.loaded) {
                              that._onProgress($.Event('progress', {
                                  lengthComputable: true,
                                  loaded: ub - o.uploadedBytes,
                                  total: ub - o.uploadedBytes
                              }), o);
                          }
                          options.uploadedBytes = o.uploadedBytes = ub;
                          o.result = result;
                          o.textStatus = textStatus;
                          o.jqXHR = jqXHR;
                          that._trigger('chunkdone', null, o);
                          that._trigger('chunkalways', null, o);
                          if (ub < fs) {
                              // File upload not yet complete,
                              // continue with the next chunk:
                              upload();
                          } else {
                              dfd.resolveWith(
                                  o.context,
                                  [result, textStatus, jqXHR]
                              );
                          }
                      })
                      .fail(function (jqXHR, textStatus, errorThrown) {
                          o.jqXHR = jqXHR;
                          o.textStatus = textStatus;
                          o.errorThrown = errorThrown;
                          that._trigger('chunkfail', null, o);
                          that._trigger('chunkalways', null, o);
                          dfd.rejectWith(
                              o.context,
                              [jqXHR, textStatus, errorThrown]
                          );
                      });
03e52840d   Kload   Init
725
              };
31b7f2792   Kload   Upgrade to ownclo...
726
727
              this._enhancePromise(promise);
              promise.abort = function () {
03e52840d   Kload   Init
728
729
                  return jqXHR.abort();
              };
31b7f2792   Kload   Upgrade to ownclo...
730
731
              upload();
              return promise;
03e52840d   Kload   Init
732
733
734
735
736
737
738
739
          },
  
          _beforeSend: function (e, data) {
              if (this._active === 0) {
                  // the start callback is triggered when an upload starts
                  // and no other uploads are currently running,
                  // equivalent to the global ajaxStart event:
                  this._trigger('start');
31b7f2792   Kload   Upgrade to ownclo...
740
741
742
743
744
                  // Set timer for global bitrate progress calculation:
                  this._bitrateTimer = new this._BitrateTimer();
                  // Reset the global progress values:
                  this._progress.loaded = this._progress.total = 0;
                  this._progress.bitrate = 0;
03e52840d   Kload   Init
745
              }
31b7f2792   Kload   Upgrade to ownclo...
746
747
748
749
750
751
752
753
              // Make sure the container objects for the .response() and
              // .progress() methods on the data object are available
              // and reset to their initial state:
              this._initResponseObject(data);
              this._initProgressObject(data);
              data._progress.loaded = data.loaded = data.uploadedBytes || 0;
              data._progress.total = data.total = this._getTotal(data.files) || 1;
              data._progress.bitrate = data.bitrate = 0;
03e52840d   Kload   Init
754
755
              this._active += 1;
              // Initialize the global progress values:
31b7f2792   Kload   Upgrade to ownclo...
756
757
              this._progress.loaded += data.loaded;
              this._progress.total += data.total;
03e52840d   Kload   Init
758
759
760
          },
  
          _onDone: function (result, textStatus, jqXHR, options) {
31b7f2792   Kload   Upgrade to ownclo...
761
762
763
764
765
              var total = options._progress.total,
                  response = options._response;
              if (options._progress.loaded < total) {
                  // Create a progress event if no final progress event
                  // with loaded equaling total has been triggered:
03e52840d   Kload   Init
766
767
                  this._onProgress($.Event('progress', {
                      lengthComputable: true,
31b7f2792   Kload   Upgrade to ownclo...
768
769
                      loaded: total,
                      total: total
03e52840d   Kload   Init
770
771
                  }), options);
              }
31b7f2792   Kload   Upgrade to ownclo...
772
773
774
              response.result = options.result = result;
              response.textStatus = options.textStatus = textStatus;
              response.jqXHR = options.jqXHR = jqXHR;
03e52840d   Kload   Init
775
776
777
778
              this._trigger('done', null, options);
          },
  
          _onFail: function (jqXHR, textStatus, errorThrown, options) {
31b7f2792   Kload   Upgrade to ownclo...
779
              var response = options._response;
03e52840d   Kload   Init
780
781
782
              if (options.recalculateProgress) {
                  // Remove the failed (error or abort) file upload from
                  // the global progress calculation:
31b7f2792   Kload   Upgrade to ownclo...
783
784
                  this._progress.loaded -= options._progress.loaded;
                  this._progress.total -= options._progress.total;
03e52840d   Kload   Init
785
              }
31b7f2792   Kload   Upgrade to ownclo...
786
787
788
789
              response.jqXHR = options.jqXHR = jqXHR;
              response.textStatus = options.textStatus = textStatus;
              response.errorThrown = options.errorThrown = errorThrown;
              this._trigger('fail', null, options);
03e52840d   Kload   Init
790
791
792
          },
  
          _onAlways: function (jqXHRorResult, textStatus, jqXHRorError, options) {
31b7f2792   Kload   Upgrade to ownclo...
793
794
              // jqXHRorResult, textStatus and jqXHRorError are added to the
              // options object via done and fail callbacks
03e52840d   Kload   Init
795
              this._trigger('always', null, options);
03e52840d   Kload   Init
796
797
798
          },
  
          _onSend: function (e, data) {
31b7f2792   Kload   Upgrade to ownclo...
799
800
801
              if (!data.submit) {
                  this._addConvenienceMethods(e, data);
              }
03e52840d   Kload   Init
802
803
              var that = this,
                  jqXHR,
31b7f2792   Kload   Upgrade to ownclo...
804
                  aborted,
03e52840d   Kload   Init
805
806
807
                  slot,
                  pipe,
                  options = that._getAJAXSettings(data),
31b7f2792   Kload   Upgrade to ownclo...
808
                  send = function () {
03e52840d   Kload   Init
809
                      that._sending += 1;
31b7f2792   Kload   Upgrade to ownclo...
810
811
                      // Set timer for bitrate progress calculation:
                      options._bitrateTimer = new that._BitrateTimer();
03e52840d   Kload   Init
812
                      jqXHR = jqXHR || (
31b7f2792   Kload   Upgrade to ownclo...
813
814
815
                          ((aborted || that._trigger('send', e, options) === false) &&
                          that._getXHRPromise(false, options.context, aborted)) ||
                          that._chunkedUpload(options) || $.ajax(options)
03e52840d   Kload   Init
816
817
818
819
820
                      ).done(function (result, textStatus, jqXHR) {
                          that._onDone(result, textStatus, jqXHR, options);
                      }).fail(function (jqXHR, textStatus, errorThrown) {
                          that._onFail(jqXHR, textStatus, errorThrown, options);
                      }).always(function (jqXHRorResult, textStatus, jqXHRorError) {
03e52840d   Kload   Init
821
822
823
824
825
826
                          that._onAlways(
                              jqXHRorResult,
                              textStatus,
                              jqXHRorError,
                              options
                          );
31b7f2792   Kload   Upgrade to ownclo...
827
828
                          that._sending -= 1;
                          that._active -= 1;
03e52840d   Kload   Init
829
830
831
832
833
834
                          if (options.limitConcurrentUploads &&
                                  options.limitConcurrentUploads > that._sending) {
                              // Start the next queued upload,
                              // that has not been aborted:
                              var nextSlot = that._slots.shift();
                              while (nextSlot) {
31b7f2792   Kload   Upgrade to ownclo...
835
                                  if (that._getDeferredState(nextSlot) === 'pending') {
03e52840d   Kload   Init
836
837
838
839
840
841
                                      nextSlot.resolve();
                                      break;
                                  }
                                  nextSlot = that._slots.shift();
                              }
                          }
31b7f2792   Kload   Upgrade to ownclo...
842
843
844
845
846
                          if (that._active === 0) {
                              // The stop callback is triggered when all uploads have
                              // been completed, equivalent to the global ajaxStop event:
                              that._trigger('stop');
                          }
03e52840d   Kload   Init
847
848
849
850
851
852
853
854
855
856
857
858
                      });
                      return jqXHR;
                  };
              this._beforeSend(e, options);
              if (this.options.sequentialUploads ||
                      (this.options.limitConcurrentUploads &&
                      this.options.limitConcurrentUploads <= this._sending)) {
                  if (this.options.limitConcurrentUploads > 1) {
                      slot = $.Deferred();
                      this._slots.push(slot);
                      pipe = slot.pipe(send);
                  } else {
31b7f2792   Kload   Upgrade to ownclo...
859
860
                      this._sequence = this._sequence.pipe(send, send);
                      pipe = this._sequence;
03e52840d   Kload   Init
861
862
863
864
865
                  }
                  // Return the piped Promise object, enhanced with an abort method,
                  // which is delegated to the jqXHR object of the current upload,
                  // and jqXHR callbacks mapped to the equivalent Promise methods:
                  pipe.abort = function () {
31b7f2792   Kload   Upgrade to ownclo...
866
                      aborted = [undefined, 'abort', 'abort'];
03e52840d   Kload   Init
867
868
                      if (!jqXHR) {
                          if (slot) {
31b7f2792   Kload   Upgrade to ownclo...
869
                              slot.rejectWith(options.context, aborted);
03e52840d   Kload   Init
870
                          }
31b7f2792   Kload   Upgrade to ownclo...
871
                          return send();
03e52840d   Kload   Init
872
873
874
875
876
877
878
879
880
881
882
883
884
                      }
                      return jqXHR.abort();
                  };
                  return this._enhancePromise(pipe);
              }
              return send();
          },
  
          _onAdd: function (e, data) {
              var that = this,
                  result = true,
                  options = $.extend({}, this.options, data),
                  limit = options.limitMultiFileUploads,
31b7f2792   Kload   Upgrade to ownclo...
885
886
887
                  paramName = this._getParamName(options),
                  paramNameSet,
                  paramNameSlice,
03e52840d   Kload   Init
888
889
890
891
892
                  fileSet,
                  i;
              if (!(options.singleFileUploads || limit) ||
                      !this._isXHRUpload(options)) {
                  fileSet = [data.files];
31b7f2792   Kload   Upgrade to ownclo...
893
                  paramNameSet = [paramName];
03e52840d   Kload   Init
894
895
              } else if (!options.singleFileUploads && limit) {
                  fileSet = [];
31b7f2792   Kload   Upgrade to ownclo...
896
                  paramNameSet = [];
03e52840d   Kload   Init
897
898
                  for (i = 0; i < data.files.length; i += limit) {
                      fileSet.push(data.files.slice(i, i + limit));
31b7f2792   Kload   Upgrade to ownclo...
899
900
901
902
903
                      paramNameSlice = paramName.slice(i, i + limit);
                      if (!paramNameSlice.length) {
                          paramNameSlice = paramName;
                      }
                      paramNameSet.push(paramNameSlice);
03e52840d   Kload   Init
904
                  }
31b7f2792   Kload   Upgrade to ownclo...
905
906
              } else {
                  paramNameSet = paramName;
03e52840d   Kload   Init
907
908
909
              }
              data.originalFiles = data.files;
              $.each(fileSet || data.files, function (index, element) {
31b7f2792   Kload   Upgrade to ownclo...
910
911
912
913
914
915
916
917
                  var newData = $.extend({}, data);
                  newData.files = fileSet ? element : [element];
                  newData.paramName = paramNameSet[index];
                  that._initResponseObject(newData);
                  that._initProgressObject(newData);
                  that._addConvenienceMethods(e, newData);
                  result = that._trigger('add', e, newData);
                  return result;
03e52840d   Kload   Init
918
919
920
              });
              return result;
          },
03e52840d   Kload   Init
921
922
923
924
925
926
927
928
929
          _replaceFileInput: function (input) {
              var inputClone = input.clone(true);
              $('<form></form>').append(inputClone)[0].reset();
              // Detaching allows to insert the fileInput on another form
              // without loosing the file input value:
              input.after(inputClone).detach();
              // Avoid memory leaks with the detached file input:
              $.cleanData(input.unbind('remove'));
              // Replace the original file input element in the fileInput
31b7f2792   Kload   Upgrade to ownclo...
930
              // elements set with the clone, which has been copied including
03e52840d   Kload   Init
931
932
933
934
935
936
937
938
939
940
941
942
943
              // event handlers:
              this.options.fileInput = this.options.fileInput.map(function (i, el) {
                  if (el === input[0]) {
                      return inputClone[0];
                  }
                  return el;
              });
              // If the widget has been initialized on the file input itself,
              // override this.element with the file input clone:
              if (input[0] === this.element[0]) {
                  this.element = inputClone;
              }
          },
31b7f2792   Kload   Upgrade to ownclo...
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
          _handleFileTreeEntry: function (entry, path) {
              var that = this,
                  dfd = $.Deferred(),
                  errorHandler = function (e) {
                      if (e && !e.entry) {
                          e.entry = entry;
                      }
                      // Since $.when returns immediately if one
                      // Deferred is rejected, we use resolve instead.
                      // This allows valid files and invalid items
                      // to be returned together in one set:
                      dfd.resolve([e]);
                  },
                  dirReader;
              path = path || '';
              if (entry.isFile) {
                  if (entry._file) {
                      // Workaround for Chrome bug #149735
                      entry._file.relativePath = path;
                      dfd.resolve(entry._file);
                  } else {
                      entry.file(function (file) {
                          file.relativePath = path;
                          dfd.resolve(file);
                      }, errorHandler);
                  }
              } else if (entry.isDirectory) {
                  dirReader = entry.createReader();
                  dirReader.readEntries(function (entries) {
                      that._handleFileTreeEntries(
                          entries,
                          path + entry.name + '/'
                      ).done(function (files) {
                          dfd.resolve(files);
                      }).fail(errorHandler);
                  }, errorHandler);
              } else {
                  // Return an empy list for file system items
                  // other than files or directories:
                  dfd.resolve([]);
              }
              return dfd.promise();
          },
  
          _handleFileTreeEntries: function (entries, path) {
              var that = this;
              return $.when.apply(
                  $,
                  $.map(entries, function (entry) {
                      return that._handleFileTreeEntry(entry, path);
                  })
              ).pipe(function () {
                  return Array.prototype.concat.apply(
                      [],
                      arguments
                  );
              });
          },
  
          _getDroppedFiles: function (dataTransfer) {
              dataTransfer = dataTransfer || {};
              var items = dataTransfer.items;
              if (items && items.length && (items[0].webkitGetAsEntry ||
                      items[0].getAsEntry)) {
                  return this._handleFileTreeEntries(
                      $.map(items, function (item) {
                          var entry;
                          if (item.webkitGetAsEntry) {
                              entry = item.webkitGetAsEntry();
                              if (entry) {
                                  // Workaround for Chrome bug #149735:
                                  entry._file = item.getAsFile();
                              }
                              return entry;
                          }
                          return item.getAsEntry();
                      })
                  );
              }
              return $.Deferred().resolve(
                  $.makeArray(dataTransfer.files)
              ).promise();
          },
  
          _getSingleFileInputFiles: function (fileInput) {
              fileInput = $(fileInput);
              var entries = fileInput.prop('webkitEntries') ||
                      fileInput.prop('entries'),
                  files,
                  value;
              if (entries && entries.length) {
                  return this._handleFileTreeEntries(entries);
              }
              files = $.makeArray(fileInput.prop('files'));
              if (!files.length) {
                  value = fileInput.prop('value');
                  if (!value) {
                      return $.Deferred().resolve([]).promise();
                  }
03e52840d   Kload   Init
1043
1044
1045
                  // If the files property is not available, the browser does not
                  // support the File API and we add a pseudo File object with
                  // the input value as name with path information removed:
31b7f2792   Kload   Upgrade to ownclo...
1046
1047
1048
1049
1050
1051
1052
                  files = [{name: value.replace(/^.*\\/, '')}];
              } else if (files[0].name === undefined && files[0].fileName) {
                  // File normalization for Safari 4 and Firefox 3:
                  $.each(files, function (index, file) {
                      file.name = file.fileName;
                      file.size = file.fileSize;
                  });
03e52840d   Kload   Init
1053
              }
31b7f2792   Kload   Upgrade to ownclo...
1054
1055
1056
1057
1058
1059
              return $.Deferred().resolve(files).promise();
          },
  
          _getFileInputFiles: function (fileInput) {
              if (!(fileInput instanceof $) || fileInput.length === 1) {
                  return this._getSingleFileInputFiles(fileInput);
03e52840d   Kload   Init
1060
              }
31b7f2792   Kload   Upgrade to ownclo...
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
              return $.when.apply(
                  $,
                  $.map(fileInput, this._getSingleFileInputFiles)
              ).pipe(function () {
                  return Array.prototype.concat.apply(
                      [],
                      arguments
                  );
              });
          },
  
          _onChange: function (e) {
              var that = this,
                  data = {
                      fileInput: $(e.target),
                      form: $(e.target.form)
                  };
              this._getFileInputFiles(data.fileInput).always(function (files) {
                  data.files = files;
                  if (that.options.replaceFileInput) {
                      that._replaceFileInput(data.fileInput);
                  }
                  if (that._trigger('change', e, data) !== false) {
                      that._onAdd(e, data);
                  }
              });
03e52840d   Kload   Init
1087
1088
1089
          },
  
          _onPaste: function (e) {
31b7f2792   Kload   Upgrade to ownclo...
1090
1091
              var items = e.originalEvent && e.originalEvent.clipboardData &&
                      e.originalEvent.clipboardData.items,
03e52840d   Kload   Init
1092
                  data = {files: []};
31b7f2792   Kload   Upgrade to ownclo...
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
              if (items && items.length) {
                  $.each(items, function (index, item) {
                      var file = item.getAsFile && item.getAsFile();
                      if (file) {
                          data.files.push(file);
                      }
                  });
                  if (this._trigger('paste', e, data) === false ||
                          this._onAdd(e, data) === false) {
                      return false;
03e52840d   Kload   Init
1103
                  }
03e52840d   Kload   Init
1104
1105
1106
1107
              }
          },
  
          _onDrop: function (e) {
31b7f2792   Kload   Upgrade to ownclo...
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
              e.dataTransfer = e.originalEvent && e.originalEvent.dataTransfer;
              var that = this,
                  dataTransfer = e.dataTransfer,
                  data = {};
              if (dataTransfer && dataTransfer.files && dataTransfer.files.length) {
                  e.preventDefault();
                  this._getDroppedFiles(dataTransfer).always(function (files) {
                      data.files = files;
                      if (that._trigger('drop', e, data) !== false) {
                          that._onAdd(e, data);
                      }
                  });
03e52840d   Kload   Init
1120
              }
03e52840d   Kload   Init
1121
1122
1123
          },
  
          _onDragOver: function (e) {
31b7f2792   Kload   Upgrade to ownclo...
1124
1125
              e.dataTransfer = e.originalEvent && e.originalEvent.dataTransfer;
              var dataTransfer = e.dataTransfer;
03e52840d   Kload   Init
1126
              if (dataTransfer) {
31b7f2792   Kload   Upgrade to ownclo...
1127
1128
1129
1130
1131
1132
1133
                  if (this._trigger('dragover', e) === false) {
                      return false;
                  }
                  if ($.inArray('Files', dataTransfer.types) !== -1) {
                      dataTransfer.dropEffect = 'copy';
                      e.preventDefault();
                  }
03e52840d   Kload   Init
1134
              }
03e52840d   Kload   Init
1135
1136
1137
          },
  
          _initEventHandlers: function () {
03e52840d   Kload   Init
1138
              if (this._isXHRUpload(this.options)) {
31b7f2792   Kload   Upgrade to ownclo...
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
                  this._on(this.options.dropZone, {
                      dragover: this._onDragOver,
                      drop: this._onDrop
                  });
                  this._on(this.options.pasteZone, {
                      paste: this._onPaste
                  });
              }
              if ($.support.fileInput) {
                  this._on(this.options.fileInput, {
                      change: this._onChange
                  });
03e52840d   Kload   Init
1151
              }
03e52840d   Kload   Init
1152
1153
1154
          },
  
          _destroyEventHandlers: function () {
31b7f2792   Kload   Upgrade to ownclo...
1155
1156
1157
              this._off(this.options.dropZone, 'dragover drop');
              this._off(this.options.pasteZone, 'paste');
              this._off(this.options.fileInput, 'change');
03e52840d   Kload   Init
1158
1159
1160
          },
  
          _setOption: function (key, value) {
31b7f2792   Kload   Upgrade to ownclo...
1161
1162
              var reinit = $.inArray(key, this._specialOptions) !== -1;
              if (reinit) {
03e52840d   Kload   Init
1163
1164
                  this._destroyEventHandlers();
              }
31b7f2792   Kload   Upgrade to ownclo...
1165
1166
              this._super(key, value);
              if (reinit) {
03e52840d   Kload   Init
1167
1168
1169
1170
1171
1172
1173
1174
                  this._initSpecialOptions();
                  this._initEventHandlers();
              }
          },
  
          _initSpecialOptions: function () {
              var options = this.options;
              if (options.fileInput === undefined) {
31b7f2792   Kload   Upgrade to ownclo...
1175
1176
                  options.fileInput = this.element.is('input[type="file"]') ?
                          this.element : this.element.find('input[type="file"]');
03e52840d   Kload   Init
1177
1178
1179
1180
1181
1182
              } else if (!(options.fileInput instanceof $)) {
                  options.fileInput = $(options.fileInput);
              }
              if (!(options.dropZone instanceof $)) {
                  options.dropZone = $(options.dropZone);
              }
31b7f2792   Kload   Upgrade to ownclo...
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
              if (!(options.pasteZone instanceof $)) {
                  options.pasteZone = $(options.pasteZone);
              }
          },
  
          _getRegExp: function (str) {
              var parts = str.split('/'),
                  modifiers = parts.pop();
              parts.shift();
              return new RegExp(parts.join('/'), modifiers);
          },
  
          _isRegExpOption: function (key, value) {
              return key !== 'url' && $.type(value) === 'string' &&
                  /^\/.*\/[igm]{0,3}$/.test(value);
          },
  
          _initDataAttributes: function () {
              var that = this,
                  options = this.options;
              // Initialize options set via HTML5 data-attributes:
              $.each(
                  $(this.element[0].cloneNode(false)).data(),
                  function (key, value) {
                      if (that._isRegExpOption(key, value)) {
                          value = that._getRegExp(value);
                      }
                      options[key] = value;
                  }
              );
03e52840d   Kload   Init
1213
1214
1215
          },
  
          _create: function () {
31b7f2792   Kload   Upgrade to ownclo...
1216
              this._initDataAttributes();
03e52840d   Kload   Init
1217
1218
1219
              this._initSpecialOptions();
              this._slots = [];
              this._sequence = this._getXHRPromise(true);
31b7f2792   Kload   Upgrade to ownclo...
1220
1221
              this._sending = this._active = 0;
              this._initProgressObject(this);
03e52840d   Kload   Init
1222
1223
              this._initEventHandlers();
          },
31b7f2792   Kload   Upgrade to ownclo...
1224
1225
1226
1227
          // This method is exposed to the widget API and allows to query
          // the number of active uploads:
          active: function () {
              return this._active;
03e52840d   Kload   Init
1228
          },
31b7f2792   Kload   Upgrade to ownclo...
1229
1230
1231
1232
1233
1234
          // This method is exposed to the widget API and allows to query
          // the widget upload progress.
          // It returns an object with loaded, total and bitrate properties
          // for the running uploads:
          progress: function () {
              return this._progress;
03e52840d   Kload   Init
1235
1236
1237
1238
1239
1240
1241
          },
  
          // This method is exposed to the widget API and allows adding files
          // using the fileupload API. The data parameter accepts an object which
          // must have a files property and can contain additional options:
          // .fileupload('add', {files: filesList});
          add: function (data) {
31b7f2792   Kload   Upgrade to ownclo...
1242
              var that = this;
03e52840d   Kload   Init
1243
1244
1245
              if (!data || this.options.disabled) {
                  return;
              }
31b7f2792   Kload   Upgrade to ownclo...
1246
1247
1248
1249
1250
1251
1252
1253
1254
              if (data.fileInput && !data.files) {
                  this._getFileInputFiles(data.fileInput).always(function (files) {
                      data.files = files;
                      that._onAdd(null, data);
                  });
              } else {
                  data.files = $.makeArray(data.files);
                  this._onAdd(null, data);
              }
03e52840d   Kload   Init
1255
1256
1257
1258
          },
  
          // This method is exposed to the widget API and allows sending files
          // using the fileupload API. The data parameter accepts an object which
31b7f2792   Kload   Upgrade to ownclo...
1259
          // must have a files or fileInput property and can contain additional options:
03e52840d   Kload   Init
1260
1261
1262
1263
          // .fileupload('send', {files: filesList});
          // The method returns a Promise object for the file upload call.
          send: function (data) {
              if (data && !this.options.disabled) {
31b7f2792   Kload   Upgrade to ownclo...
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
                  if (data.fileInput && !data.files) {
                      var that = this,
                          dfd = $.Deferred(),
                          promise = dfd.promise(),
                          jqXHR,
                          aborted;
                      promise.abort = function () {
                          aborted = true;
                          if (jqXHR) {
                              return jqXHR.abort();
                          }
                          dfd.reject(null, 'abort', 'abort');
                          return promise;
                      };
                      this._getFileInputFiles(data.fileInput).always(
                          function (files) {
                              if (aborted) {
                                  return;
                              }
                              if (!files.length) {
                                  dfd.reject();
                                  return;
                              }
                              data.files = files;
                              jqXHR = that._onSend(null, data).then(
                                  function (result, textStatus, jqXHR) {
                                      dfd.resolve(result, textStatus, jqXHR);
                                  },
                                  function (jqXHR, textStatus, errorThrown) {
                                      dfd.reject(jqXHR, textStatus, errorThrown);
                                  }
                              );
                          }
                      );
                      return this._enhancePromise(promise);
                  }
                  data.files = $.makeArray(data.files);
03e52840d   Kload   Init
1301
1302
1303
1304
1305
1306
1307
1308
                  if (data.files.length) {
                      return this._onSend(null, data);
                  }
              }
              return this._getXHRPromise(false, data && data.context);
          }
  
      });
31b7f2792   Kload   Upgrade to ownclo...
1309
  }));