fileupload-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,972 @@
1
+ /*
2
+ * jQuery File Upload Plugin 5.13
3
+ * https://github.com/blueimp/jQuery-File-Upload
4
+ *
5
+ * Copyright 2010, Sebastian Tschan
6
+ * https://blueimp.net
7
+ *
8
+ * Licensed under the MIT license:
9
+ * http://www.opensource.org/licenses/MIT
10
+ */
11
+
12
+ /*jslint nomen: true, unparam: true, regexp: true */
13
+ /*global define, window, document, Blob, FormData, location */
14
+
15
+ (function (factory) {
16
+ 'use strict';
17
+ if (typeof define === 'function' && define.amd) {
18
+ // Register as an anonymous AMD module:
19
+ define([
20
+ 'jquery',
21
+ 'jquery.ui.widget'
22
+ ], factory);
23
+ } else {
24
+ // Browser globals:
25
+ factory(window.jQuery);
26
+ }
27
+ }(function ($) {
28
+ 'use strict';
29
+
30
+ // The FileReader API is not actually used, but works as feature detection,
31
+ // as e.g. Safari supports XHR file uploads via the FormData API,
32
+ // but not non-multipart XHR file uploads:
33
+ $.support.xhrFileUpload = !!(window.XMLHttpRequestUpload && window.FileReader);
34
+ $.support.xhrFormDataFileUpload = !!window.FormData;
35
+
36
+ // The fileupload widget listens for change events on file input fields defined
37
+ // via fileInput setting and paste or drop events of the given dropZone.
38
+ // In addition to the default jQuery Widget methods, the fileupload widget
39
+ // exposes the "add" and "send" methods, to add or directly send files using
40
+ // the fileupload API.
41
+ // By default, files added via file input selection, paste, drag & drop or
42
+ // "add" method are uploaded immediately, but it is possible to override
43
+ // the "add" callback option to queue file uploads.
44
+ $.widget('blueimp.fileupload', {
45
+
46
+ options: {
47
+ // The namespace used for event handler binding on the dropZone and
48
+ // fileInput collections.
49
+ // If not set, the name of the widget ("fileupload") is used.
50
+ namespace: undefined,
51
+ // The drop target collection, by the default the complete document.
52
+ // Set to null or an empty collection to disable drag & drop support:
53
+ dropZone: $(document),
54
+ // The file input field collection, that is listened for change events.
55
+ // If undefined, it is set to the file input fields inside
56
+ // of the widget element on plugin initialization.
57
+ // Set to null or an empty collection to disable the change listener.
58
+ fileInput: undefined,
59
+ // By default, the file input field is replaced with a clone after
60
+ // each input field change event. This is required for iframe transport
61
+ // queues and allows change events to be fired for the same file
62
+ // selection, but can be disabled by setting the following option to false:
63
+ replaceFileInput: true,
64
+ // The parameter name for the file form data (the request argument name).
65
+ // If undefined or empty, the name property of the file input field is
66
+ // used, or "files[]" if the file input name property is also empty,
67
+ // can be a string or an array of strings:
68
+ paramName: undefined,
69
+ // By default, each file of a selection is uploaded using an individual
70
+ // request for XHR type uploads. Set to false to upload file
71
+ // selections in one request each:
72
+ singleFileUploads: true,
73
+ // To limit the number of files uploaded with one XHR request,
74
+ // set the following option to an integer greater than 0:
75
+ limitMultiFileUploads: undefined,
76
+ // Set the following option to true to issue all file upload requests
77
+ // in a sequential order:
78
+ sequentialUploads: false,
79
+ // To limit the number of concurrent uploads,
80
+ // set the following option to an integer greater than 0:
81
+ limitConcurrentUploads: undefined,
82
+ // Set the following option to true to force iframe transport uploads:
83
+ forceIframeTransport: false,
84
+ // Set the following option to the location of a redirect url on the
85
+ // origin server, for cross-domain iframe transport uploads:
86
+ redirect: undefined,
87
+ // The parameter name for the redirect url, sent as part of the form
88
+ // data and set to 'redirect' if this option is empty:
89
+ redirectParamName: undefined,
90
+ // Set the following option to the location of a postMessage window,
91
+ // to enable postMessage transport uploads:
92
+ postMessage: undefined,
93
+ // By default, XHR file uploads are sent as multipart/form-data.
94
+ // The iframe transport is always using multipart/form-data.
95
+ // Set to false to enable non-multipart XHR uploads:
96
+ multipart: true,
97
+ // To upload large files in smaller chunks, set the following option
98
+ // to a preferred maximum chunk size. If set to 0, null or undefined,
99
+ // or the browser does not support the required Blob API, files will
100
+ // be uploaded as a whole.
101
+ maxChunkSize: undefined,
102
+ // When a non-multipart upload or a chunked multipart upload has been
103
+ // aborted, this option can be used to resume the upload by setting
104
+ // it to the size of the already uploaded bytes. This option is most
105
+ // useful when modifying the options object inside of the "add" or
106
+ // "send" callbacks, as the options are cloned for each file upload.
107
+ uploadedBytes: undefined,
108
+ // By default, failed (abort or error) file uploads are removed from the
109
+ // global progress calculation. Set the following option to false to
110
+ // prevent recalculating the global progress data:
111
+ recalculateProgress: true,
112
+ // Interval in milliseconds to calculate and trigger progress events:
113
+ progressInterval: 100,
114
+ // Interval in milliseconds to calculate progress bitrate:
115
+ bitrateInterval: 500,
116
+
117
+ // Additional form data to be sent along with the file uploads can be set
118
+ // using this option, which accepts an array of objects with name and
119
+ // value properties, a function returning such an array, a FormData
120
+ // object (for XHR file uploads), or a simple object.
121
+ // The form of the first fileInput is given as parameter to the function:
122
+ formData: function (form) {
123
+ return form.serializeArray();
124
+ },
125
+
126
+ // The add callback is invoked as soon as files are added to the fileupload
127
+ // widget (via file input selection, drag & drop, paste or add API call).
128
+ // If the singleFileUploads option is enabled, this callback will be
129
+ // called once for each file in the selection for XHR file uplaods, else
130
+ // once for each file selection.
131
+ // The upload starts when the submit method is invoked on the data parameter.
132
+ // The data object contains a files property holding the added files
133
+ // and allows to override plugin options as well as define ajax settings.
134
+ // Listeners for this callback can also be bound the following way:
135
+ // .bind('fileuploadadd', func);
136
+ // data.submit() returns a Promise object and allows to attach additional
137
+ // handlers using jQuery's Deferred callbacks:
138
+ // data.submit().done(func).fail(func).always(func);
139
+ add: function (e, data) {
140
+ data.submit();
141
+ },
142
+
143
+ // Other callbacks:
144
+ // Callback for the submit event of each file upload:
145
+ // submit: function (e, data) {}, // .bind('fileuploadsubmit', func);
146
+ // Callback for the start of each file upload request:
147
+ // send: function (e, data) {}, // .bind('fileuploadsend', func);
148
+ // Callback for successful uploads:
149
+ // done: function (e, data) {}, // .bind('fileuploaddone', func);
150
+ // Callback for failed (abort or error) uploads:
151
+ // fail: function (e, data) {}, // .bind('fileuploadfail', func);
152
+ // Callback for completed (success, abort or error) requests:
153
+ // always: function (e, data) {}, // .bind('fileuploadalways', func);
154
+ // Callback for upload progress events:
155
+ // progress: function (e, data) {}, // .bind('fileuploadprogress', func);
156
+ // Callback for global upload progress events:
157
+ // progressall: function (e, data) {}, // .bind('fileuploadprogressall', func);
158
+ // Callback for uploads start, equivalent to the global ajaxStart event:
159
+ // start: function (e) {}, // .bind('fileuploadstart', func);
160
+ // Callback for uploads stop, equivalent to the global ajaxStop event:
161
+ // stop: function (e) {}, // .bind('fileuploadstop', func);
162
+ // Callback for change events of the fileInput collection:
163
+ // change: function (e, data) {}, // .bind('fileuploadchange', func);
164
+ // Callback for paste events to the dropZone collection:
165
+ // paste: function (e, data) {}, // .bind('fileuploadpaste', func);
166
+ // Callback for drop events of the dropZone collection:
167
+ // drop: function (e, data) {}, // .bind('fileuploaddrop', func);
168
+ // Callback for dragover events of the dropZone collection:
169
+ // dragover: function (e) {}, // .bind('fileuploaddragover', func);
170
+
171
+ // The plugin options are used as settings object for the ajax calls.
172
+ // The following are jQuery ajax settings required for the file uploads:
173
+ processData: false,
174
+ contentType: false,
175
+ cache: false
176
+ },
177
+
178
+ // A list of options that require a refresh after assigning a new value:
179
+ _refreshOptionsList: [
180
+ 'namespace',
181
+ 'dropZone',
182
+ 'fileInput',
183
+ 'multipart',
184
+ 'forceIframeTransport'
185
+ ],
186
+
187
+ _BitrateTimer: function () {
188
+ this.timestamp = +(new Date());
189
+ this.loaded = 0;
190
+ this.bitrate = 0;
191
+ this.getBitrate = function (now, loaded, interval) {
192
+ var timeDiff = now - this.timestamp;
193
+ if (!this.bitrate || !interval || timeDiff > interval) {
194
+ this.bitrate = (loaded - this.loaded) * (1000 / timeDiff) * 8;
195
+ this.loaded = loaded;
196
+ this.timestamp = now;
197
+ }
198
+ return this.bitrate;
199
+ };
200
+ },
201
+
202
+ _isXHRUpload: function (options) {
203
+ return !options.forceIframeTransport &&
204
+ ((!options.multipart && $.support.xhrFileUpload) ||
205
+ $.support.xhrFormDataFileUpload);
206
+ },
207
+
208
+ _getFormData: function (options) {
209
+ var formData;
210
+ if (typeof options.formData === 'function') {
211
+ return options.formData(options.form);
212
+ }
213
+ if ($.isArray(options.formData)) {
214
+ return options.formData;
215
+ }
216
+ if (options.formData) {
217
+ formData = [];
218
+ $.each(options.formData, function (name, value) {
219
+ formData.push({name: name, value: value});
220
+ });
221
+ return formData;
222
+ }
223
+ return [];
224
+ },
225
+
226
+ _getTotal: function (files) {
227
+ var total = 0;
228
+ $.each(files, function (index, file) {
229
+ total += file.size || 1;
230
+ });
231
+ return total;
232
+ },
233
+
234
+ _onProgress: function (e, data) {
235
+ if (e.lengthComputable) {
236
+ var now = +(new Date()),
237
+ total,
238
+ loaded;
239
+ if (data._time && data.progressInterval &&
240
+ (now - data._time < data.progressInterval) &&
241
+ e.loaded !== e.total) {
242
+ return;
243
+ }
244
+ data._time = now;
245
+ total = data.total || this._getTotal(data.files);
246
+ loaded = parseInt(
247
+ e.loaded / e.total * (data.chunkSize || total),
248
+ 10
249
+ ) + (data.uploadedBytes || 0);
250
+ this._loaded += loaded - (data.loaded || data.uploadedBytes || 0);
251
+ data.lengthComputable = true;
252
+ data.loaded = loaded;
253
+ data.total = total;
254
+ data.bitrate = data._bitrateTimer.getBitrate(
255
+ now,
256
+ loaded,
257
+ data.bitrateInterval
258
+ );
259
+ // Trigger a custom progress event with a total data property set
260
+ // to the file size(s) of the current upload and a loaded data
261
+ // property calculated accordingly:
262
+ this._trigger('progress', e, data);
263
+ // Trigger a global progress event for all current file uploads,
264
+ // including ajax calls queued for sequential file uploads:
265
+ this._trigger('progressall', e, {
266
+ lengthComputable: true,
267
+ loaded: this._loaded,
268
+ total: this._total,
269
+ bitrate: this._bitrateTimer.getBitrate(
270
+ now,
271
+ this._loaded,
272
+ data.bitrateInterval
273
+ )
274
+ });
275
+ }
276
+ },
277
+
278
+ _initProgressListener: function (options) {
279
+ var that = this,
280
+ xhr = options.xhr ? options.xhr() : $.ajaxSettings.xhr();
281
+ // Accesss to the native XHR object is required to add event listeners
282
+ // for the upload progress event:
283
+ if (xhr.upload) {
284
+ $(xhr.upload).bind('progress', function (e) {
285
+ var oe = e.originalEvent;
286
+ // Make sure the progress event properties get copied over:
287
+ e.lengthComputable = oe.lengthComputable;
288
+ e.loaded = oe.loaded;
289
+ e.total = oe.total;
290
+ that._onProgress(e, options);
291
+ });
292
+ options.xhr = function () {
293
+ return xhr;
294
+ };
295
+ }
296
+ },
297
+
298
+ _initXHRData: function (options) {
299
+ var formData,
300
+ file = options.files[0],
301
+ // Ignore non-multipart setting if not supported:
302
+ multipart = options.multipart || !$.support.xhrFileUpload,
303
+ paramName = options.paramName[0];
304
+ if (!multipart || options.blob) {
305
+ // For non-multipart uploads and chunked uploads,
306
+ // file meta data is not part of the request body,
307
+ // so we transmit this data as part of the HTTP headers.
308
+ // For cross domain requests, these headers must be allowed
309
+ // via Access-Control-Allow-Headers or removed using
310
+ // the beforeSend callback:
311
+ options.headers = $.extend(options.headers, {
312
+ 'X-File-Name': file.name,
313
+ 'X-File-Type': file.type,
314
+ 'X-File-Size': file.size
315
+ });
316
+ if (!options.blob) {
317
+ // Non-chunked non-multipart upload:
318
+ options.contentType = file.type;
319
+ options.data = file;
320
+ } else if (!multipart) {
321
+ // Chunked non-multipart upload:
322
+ options.contentType = 'application/octet-stream';
323
+ options.data = options.blob;
324
+ }
325
+ }
326
+ if (multipart && $.support.xhrFormDataFileUpload) {
327
+ if (options.postMessage) {
328
+ // window.postMessage does not allow sending FormData
329
+ // objects, so we just add the File/Blob objects to
330
+ // the formData array and let the postMessage window
331
+ // create the FormData object out of this array:
332
+ formData = this._getFormData(options);
333
+ if (options.blob) {
334
+ formData.push({
335
+ name: paramName,
336
+ value: options.blob
337
+ });
338
+ } else {
339
+ $.each(options.files, function (index, file) {
340
+ formData.push({
341
+ name: options.paramName[index] || paramName,
342
+ value: file
343
+ });
344
+ });
345
+ }
346
+ } else {
347
+ if (options.formData instanceof FormData) {
348
+ formData = options.formData;
349
+ } else {
350
+ formData = new FormData();
351
+ $.each(this._getFormData(options), function (index, field) {
352
+ formData.append(field.name, field.value);
353
+ });
354
+ }
355
+ if (options.blob) {
356
+ formData.append(paramName, options.blob, file.name);
357
+ } else {
358
+ $.each(options.files, function (index, file) {
359
+ // File objects are also Blob instances.
360
+ // This check allows the tests to run with
361
+ // dummy objects:
362
+ if (file instanceof Blob) {
363
+ formData.append(
364
+ options.paramName[index] || paramName,
365
+ file,
366
+ file.name
367
+ );
368
+ }
369
+ });
370
+ }
371
+ }
372
+ options.data = formData;
373
+ }
374
+ // Blob reference is not needed anymore, free memory:
375
+ options.blob = null;
376
+ },
377
+
378
+ _initIframeSettings: function (options) {
379
+ // Setting the dataType to iframe enables the iframe transport:
380
+ options.dataType = 'iframe ' + (options.dataType || '');
381
+ // The iframe transport accepts a serialized array as form data:
382
+ options.formData = this._getFormData(options);
383
+ // Add redirect url to form data on cross-domain uploads:
384
+ if (options.redirect && $('<a></a>').prop('href', options.url)
385
+ .prop('host') !== location.host) {
386
+ options.formData.push({
387
+ name: options.redirectParamName || 'redirect',
388
+ value: options.redirect
389
+ });
390
+ }
391
+ },
392
+
393
+ _initDataSettings: function (options) {
394
+ if (this._isXHRUpload(options)) {
395
+ if (!this._chunkedUpload(options, true)) {
396
+ if (!options.data) {
397
+ this._initXHRData(options);
398
+ }
399
+ this._initProgressListener(options);
400
+ }
401
+ if (options.postMessage) {
402
+ // Setting the dataType to postmessage enables the
403
+ // postMessage transport:
404
+ options.dataType = 'postmessage ' + (options.dataType || '');
405
+ }
406
+ } else {
407
+ this._initIframeSettings(options, 'iframe');
408
+ }
409
+ },
410
+
411
+ _getParamName: function (options) {
412
+ var fileInput = $(options.fileInput),
413
+ paramName = options.paramName;
414
+ if (!paramName) {
415
+ paramName = [];
416
+ fileInput.each(function () {
417
+ var input = $(this),
418
+ name = input.prop('name') || 'files[]',
419
+ i = (input.prop('files') || [1]).length;
420
+ while (i) {
421
+ paramName.push(name);
422
+ i -= 1;
423
+ }
424
+ });
425
+ if (!paramName.length) {
426
+ paramName = [fileInput.prop('name') || 'files[]'];
427
+ }
428
+ } else if (!$.isArray(paramName)) {
429
+ paramName = [paramName];
430
+ }
431
+ return paramName;
432
+ },
433
+
434
+ _initFormSettings: function (options) {
435
+ // Retrieve missing options from the input field and the
436
+ // associated form, if available:
437
+ if (!options.form || !options.form.length) {
438
+ options.form = $(options.fileInput.prop('form'));
439
+ }
440
+ options.paramName = this._getParamName(options);
441
+ if (!options.url) {
442
+ options.url = options.form.prop('action') || location.href;
443
+ }
444
+ // The HTTP request method must be "POST" or "PUT":
445
+ options.type = (options.type || options.form.prop('method') || '')
446
+ .toUpperCase();
447
+ if (options.type !== 'POST' && options.type !== 'PUT') {
448
+ options.type = 'POST';
449
+ }
450
+ },
451
+
452
+ _getAJAXSettings: function (data) {
453
+ var options = $.extend({}, this.options, data);
454
+ this._initFormSettings(options);
455
+ this._initDataSettings(options);
456
+ return options;
457
+ },
458
+
459
+ // Maps jqXHR callbacks to the equivalent
460
+ // methods of the given Promise object:
461
+ _enhancePromise: function (promise) {
462
+ promise.success = promise.done;
463
+ promise.error = promise.fail;
464
+ promise.complete = promise.always;
465
+ return promise;
466
+ },
467
+
468
+ // Creates and returns a Promise object enhanced with
469
+ // the jqXHR methods abort, success, error and complete:
470
+ _getXHRPromise: function (resolveOrReject, context, args) {
471
+ var dfd = $.Deferred(),
472
+ promise = dfd.promise();
473
+ context = context || this.options.context || promise;
474
+ if (resolveOrReject === true) {
475
+ dfd.resolveWith(context, args);
476
+ } else if (resolveOrReject === false) {
477
+ dfd.rejectWith(context, args);
478
+ }
479
+ promise.abort = dfd.promise;
480
+ return this._enhancePromise(promise);
481
+ },
482
+
483
+ // Uploads a file in multiple, sequential requests
484
+ // by splitting the file up in multiple blob chunks.
485
+ // If the second parameter is true, only tests if the file
486
+ // should be uploaded in chunks, but does not invoke any
487
+ // upload requests:
488
+ _chunkedUpload: function (options, testOnly) {
489
+ var that = this,
490
+ file = options.files[0],
491
+ fs = file.size,
492
+ ub = options.uploadedBytes = options.uploadedBytes || 0,
493
+ mcs = options.maxChunkSize || fs,
494
+ // Use the Blob methods with the slice implementation
495
+ // according to the W3C Blob API specification:
496
+ slice = file.webkitSlice || file.mozSlice || file.slice,
497
+ upload,
498
+ n,
499
+ jqXHR,
500
+ pipe;
501
+ if (!(this._isXHRUpload(options) && slice && (ub || mcs < fs)) ||
502
+ options.data) {
503
+ return false;
504
+ }
505
+ if (testOnly) {
506
+ return true;
507
+ }
508
+ if (ub >= fs) {
509
+ file.error = 'uploadedBytes';
510
+ return this._getXHRPromise(
511
+ false,
512
+ options.context,
513
+ [null, 'error', file.error]
514
+ );
515
+ }
516
+ // n is the number of blobs to upload,
517
+ // calculated via filesize, uploaded bytes and max chunk size:
518
+ n = Math.ceil((fs - ub) / mcs);
519
+ // The chunk upload method accepting the chunk number as parameter:
520
+ upload = function (i) {
521
+ if (!i) {
522
+ return that._getXHRPromise(true, options.context);
523
+ }
524
+ // Upload the blobs in sequential order:
525
+ return upload(i -= 1).pipe(function () {
526
+ // Clone the options object for each chunk upload:
527
+ var o = $.extend({}, options);
528
+ o.blob = slice.call(
529
+ file,
530
+ ub + i * mcs,
531
+ ub + (i + 1) * mcs
532
+ );
533
+ // Expose the chunk index:
534
+ o.chunkIndex = i;
535
+ // Expose the number of chunks:
536
+ o.chunksNumber = n;
537
+ // Store the current chunk size, as the blob itself
538
+ // will be dereferenced after data processing:
539
+ o.chunkSize = o.blob.size;
540
+ // Process the upload data (the blob and potential form data):
541
+ that._initXHRData(o);
542
+ // Add progress listeners for this chunk upload:
543
+ that._initProgressListener(o);
544
+ jqXHR = ($.ajax(o) || that._getXHRPromise(false, o.context))
545
+ .done(function () {
546
+ // Create a progress event if upload is done and
547
+ // no progress event has been invoked for this chunk:
548
+ if (!o.loaded) {
549
+ that._onProgress($.Event('progress', {
550
+ lengthComputable: true,
551
+ loaded: o.chunkSize,
552
+ total: o.chunkSize
553
+ }), o);
554
+ }
555
+ options.uploadedBytes = o.uploadedBytes +=
556
+ o.chunkSize;
557
+ });
558
+ return jqXHR;
559
+ });
560
+ };
561
+ // Return the piped Promise object, enhanced with an abort method,
562
+ // which is delegated to the jqXHR object of the current upload,
563
+ // and jqXHR callbacks mapped to the equivalent Promise methods:
564
+ pipe = upload(n);
565
+ pipe.abort = function () {
566
+ return jqXHR.abort();
567
+ };
568
+ return this._enhancePromise(pipe);
569
+ },
570
+
571
+ _beforeSend: function (e, data) {
572
+ if (this._active === 0) {
573
+ // the start callback is triggered when an upload starts
574
+ // and no other uploads are currently running,
575
+ // equivalent to the global ajaxStart event:
576
+ this._trigger('start');
577
+ // Set timer for global bitrate progress calculation:
578
+ this._bitrateTimer = new this._BitrateTimer();
579
+ }
580
+ this._active += 1;
581
+ // Initialize the global progress values:
582
+ this._loaded += data.uploadedBytes || 0;
583
+ this._total += this._getTotal(data.files);
584
+ },
585
+
586
+ _onDone: function (result, textStatus, jqXHR, options) {
587
+ if (!this._isXHRUpload(options)) {
588
+ // Create a progress event for each iframe load:
589
+ this._onProgress($.Event('progress', {
590
+ lengthComputable: true,
591
+ loaded: 1,
592
+ total: 1
593
+ }), options);
594
+ }
595
+ options.result = result;
596
+ options.textStatus = textStatus;
597
+ options.jqXHR = jqXHR;
598
+ this._trigger('done', null, options);
599
+ },
600
+
601
+ _onFail: function (jqXHR, textStatus, errorThrown, options) {
602
+ options.jqXHR = jqXHR;
603
+ options.textStatus = textStatus;
604
+ options.errorThrown = errorThrown;
605
+ this._trigger('fail', null, options);
606
+ if (options.recalculateProgress) {
607
+ // Remove the failed (error or abort) file upload from
608
+ // the global progress calculation:
609
+ this._loaded -= options.loaded || options.uploadedBytes || 0;
610
+ this._total -= options.total || this._getTotal(options.files);
611
+ }
612
+ },
613
+
614
+ _onAlways: function (jqXHRorResult, textStatus, jqXHRorError, options) {
615
+ this._active -= 1;
616
+ options.textStatus = textStatus;
617
+ if (jqXHRorError && jqXHRorError.always) {
618
+ options.jqXHR = jqXHRorError;
619
+ options.result = jqXHRorResult;
620
+ } else {
621
+ options.jqXHR = jqXHRorResult;
622
+ options.errorThrown = jqXHRorError;
623
+ }
624
+ this._trigger('always', null, options);
625
+ if (this._active === 0) {
626
+ // The stop callback is triggered when all uploads have
627
+ // been completed, equivalent to the global ajaxStop event:
628
+ this._trigger('stop');
629
+ // Reset the global progress values:
630
+ this._loaded = this._total = 0;
631
+ this._bitrateTimer = null;
632
+ }
633
+ },
634
+
635
+ _onSend: function (e, data) {
636
+ var that = this,
637
+ jqXHR,
638
+ slot,
639
+ pipe,
640
+ options = that._getAJAXSettings(data),
641
+ send = function (resolve, args) {
642
+ that._sending += 1;
643
+ // Set timer for bitrate progress calculation:
644
+ options._bitrateTimer = new that._BitrateTimer();
645
+ jqXHR = jqXHR || (
646
+ (resolve !== false &&
647
+ that._trigger('send', e, options) !== false &&
648
+ (that._chunkedUpload(options) || $.ajax(options))) ||
649
+ that._getXHRPromise(false, options.context, args)
650
+ ).done(function (result, textStatus, jqXHR) {
651
+ that._onDone(result, textStatus, jqXHR, options);
652
+ }).fail(function (jqXHR, textStatus, errorThrown) {
653
+ that._onFail(jqXHR, textStatus, errorThrown, options);
654
+ }).always(function (jqXHRorResult, textStatus, jqXHRorError) {
655
+ that._sending -= 1;
656
+ that._onAlways(
657
+ jqXHRorResult,
658
+ textStatus,
659
+ jqXHRorError,
660
+ options
661
+ );
662
+ if (options.limitConcurrentUploads &&
663
+ options.limitConcurrentUploads > that._sending) {
664
+ // Start the next queued upload,
665
+ // that has not been aborted:
666
+ var nextSlot = that._slots.shift();
667
+ while (nextSlot) {
668
+ if (!nextSlot.isRejected()) {
669
+ nextSlot.resolve();
670
+ break;
671
+ }
672
+ nextSlot = that._slots.shift();
673
+ }
674
+ }
675
+ });
676
+ return jqXHR;
677
+ };
678
+ this._beforeSend(e, options);
679
+ if (this.options.sequentialUploads ||
680
+ (this.options.limitConcurrentUploads &&
681
+ this.options.limitConcurrentUploads <= this._sending)) {
682
+ if (this.options.limitConcurrentUploads > 1) {
683
+ slot = $.Deferred();
684
+ this._slots.push(slot);
685
+ pipe = slot.pipe(send);
686
+ } else {
687
+ pipe = (this._sequence = this._sequence.pipe(send, send));
688
+ }
689
+ // Return the piped Promise object, enhanced with an abort method,
690
+ // which is delegated to the jqXHR object of the current upload,
691
+ // and jqXHR callbacks mapped to the equivalent Promise methods:
692
+ pipe.abort = function () {
693
+ var args = [undefined, 'abort', 'abort'];
694
+ if (!jqXHR) {
695
+ if (slot) {
696
+ slot.rejectWith(args);
697
+ }
698
+ return send(false, args);
699
+ }
700
+ return jqXHR.abort();
701
+ };
702
+ return this._enhancePromise(pipe);
703
+ }
704
+ return send();
705
+ },
706
+
707
+ _onAdd: function (e, data) {
708
+ var that = this,
709
+ result = true,
710
+ options = $.extend({}, this.options, data),
711
+ limit = options.limitMultiFileUploads,
712
+ paramName = this._getParamName(options),
713
+ paramNameSet,
714
+ paramNameSlice,
715
+ fileSet,
716
+ i;
717
+ if (!(options.singleFileUploads || limit) ||
718
+ !this._isXHRUpload(options)) {
719
+ fileSet = [data.files];
720
+ paramNameSet = [paramName];
721
+ } else if (!options.singleFileUploads && limit) {
722
+ fileSet = [];
723
+ paramNameSet = [];
724
+ for (i = 0; i < data.files.length; i += limit) {
725
+ fileSet.push(data.files.slice(i, i + limit));
726
+ paramNameSlice = paramName.slice(i, i + limit);
727
+ if (!paramNameSlice.length) {
728
+ paramNameSlice = paramName;
729
+ }
730
+ paramNameSet.push(paramNameSlice);
731
+ }
732
+ } else {
733
+ paramNameSet = paramName;
734
+ }
735
+ data.originalFiles = data.files;
736
+ $.each(fileSet || data.files, function (index, element) {
737
+ var newData = $.extend({}, data);
738
+ newData.files = fileSet ? element : [element];
739
+ newData.paramName = paramNameSet[index];
740
+ newData.submit = function () {
741
+ newData.jqXHR = this.jqXHR =
742
+ (that._trigger('submit', e, this) !== false) &&
743
+ that._onSend(e, this);
744
+ return this.jqXHR;
745
+ };
746
+ return (result = that._trigger('add', e, newData));
747
+ });
748
+ return result;
749
+ },
750
+
751
+ // File Normalization for Gecko 1.9.1 (Firefox 3.5) support:
752
+ _normalizeFile: function (index, file) {
753
+ if (file.name === undefined && file.size === undefined) {
754
+ file.name = file.fileName;
755
+ file.size = file.fileSize;
756
+ }
757
+ },
758
+
759
+ _replaceFileInput: function (input) {
760
+ var inputClone = input.clone(true);
761
+ $('<form></form>').append(inputClone)[0].reset();
762
+ // Detaching allows to insert the fileInput on another form
763
+ // without loosing the file input value:
764
+ input.after(inputClone).detach();
765
+ // Avoid memory leaks with the detached file input:
766
+ $.cleanData(input.unbind('remove'));
767
+ // Replace the original file input element in the fileInput
768
+ // collection with the clone, which has been copied including
769
+ // event handlers:
770
+ this.options.fileInput = this.options.fileInput.map(function (i, el) {
771
+ if (el === input[0]) {
772
+ return inputClone[0];
773
+ }
774
+ return el;
775
+ });
776
+ // If the widget has been initialized on the file input itself,
777
+ // override this.element with the file input clone:
778
+ if (input[0] === this.element[0]) {
779
+ this.element = inputClone;
780
+ }
781
+ },
782
+
783
+ _getFileInputFiles: function (fileInput) {
784
+ fileInput = $(fileInput);
785
+ var files = $.each($.makeArray(fileInput.prop('files')), this._normalizeFile),
786
+ value;
787
+ if (!files.length) {
788
+ value = fileInput.prop('value');
789
+ if (!value) {
790
+ return [];
791
+ }
792
+ // If the files property is not available, the browser does not
793
+ // support the File API and we add a pseudo File object with
794
+ // the input value as name with path information removed:
795
+ files = [{name: value.replace(/^.*\\/, '')}];
796
+ }
797
+ return files;
798
+ },
799
+
800
+ _onChange: function (e) {
801
+ var that = e.data.fileupload,
802
+ data = {
803
+ fileInput: $(e.target),
804
+ form: $(e.target.form)
805
+ };
806
+ data.files = that._getFileInputFiles(data.fileInput);
807
+ if (that.options.replaceFileInput) {
808
+ that._replaceFileInput(data.fileInput);
809
+ }
810
+ if (that._trigger('change', e, data) === false ||
811
+ that._onAdd(e, data) === false) {
812
+ return false;
813
+ }
814
+ },
815
+
816
+ _onPaste: function (e) {
817
+ var that = e.data.fileupload,
818
+ cbd = e.originalEvent.clipboardData,
819
+ items = (cbd && cbd.items) || [],
820
+ data = {files: []};
821
+ $.each(items, function (index, item) {
822
+ var file = item.getAsFile && item.getAsFile();
823
+ if (file) {
824
+ data.files.push(file);
825
+ }
826
+ });
827
+ if (that._trigger('paste', e, data) === false ||
828
+ that._onAdd(e, data) === false) {
829
+ return false;
830
+ }
831
+ },
832
+
833
+ _onDrop: function (e) {
834
+ var that = e.data.fileupload,
835
+ dataTransfer = e.dataTransfer = e.originalEvent.dataTransfer,
836
+ data = {
837
+ files: $.each(
838
+ $.makeArray(dataTransfer && dataTransfer.files),
839
+ that._normalizeFile
840
+ )
841
+ };
842
+ if (that._trigger('drop', e, data) === false ||
843
+ that._onAdd(e, data) === false) {
844
+ return false;
845
+ }
846
+ e.preventDefault();
847
+ },
848
+
849
+ _onDragOver: function (e) {
850
+ var that = e.data.fileupload,
851
+ dataTransfer = e.dataTransfer = e.originalEvent.dataTransfer;
852
+ if (that._trigger('dragover', e) === false) {
853
+ return false;
854
+ }
855
+ if (dataTransfer) {
856
+ dataTransfer.dropEffect = 'copy';
857
+ }
858
+ e.preventDefault();
859
+ },
860
+
861
+ _initEventHandlers: function () {
862
+ var ns = this.options.namespace;
863
+ if (this._isXHRUpload(this.options)) {
864
+ this.options.dropZone
865
+ .bind('dragover.' + ns, {fileupload: this}, this._onDragOver)
866
+ .bind('drop.' + ns, {fileupload: this}, this._onDrop)
867
+ .bind('paste.' + ns, {fileupload: this}, this._onPaste);
868
+ }
869
+ this.options.fileInput
870
+ .bind('change.' + ns, {fileupload: this}, this._onChange);
871
+ },
872
+
873
+ _destroyEventHandlers: function () {
874
+ var ns = this.options.namespace;
875
+ this.options.dropZone
876
+ .unbind('dragover.' + ns, this._onDragOver)
877
+ .unbind('drop.' + ns, this._onDrop)
878
+ .unbind('paste.' + ns, this._onPaste);
879
+ this.options.fileInput
880
+ .unbind('change.' + ns, this._onChange);
881
+ },
882
+
883
+ _setOption: function (key, value) {
884
+ var refresh = $.inArray(key, this._refreshOptionsList) !== -1;
885
+ if (refresh) {
886
+ this._destroyEventHandlers();
887
+ }
888
+ $.Widget.prototype._setOption.call(this, key, value);
889
+ if (refresh) {
890
+ this._initSpecialOptions();
891
+ this._initEventHandlers();
892
+ }
893
+ },
894
+
895
+ _initSpecialOptions: function () {
896
+ var options = this.options;
897
+ if (options.fileInput === undefined) {
898
+ options.fileInput = this.element.is('input:file') ?
899
+ this.element : this.element.find('input:file');
900
+ } else if (!(options.fileInput instanceof $)) {
901
+ options.fileInput = $(options.fileInput);
902
+ }
903
+ if (!(options.dropZone instanceof $)) {
904
+ options.dropZone = $(options.dropZone);
905
+ }
906
+ },
907
+
908
+ _create: function () {
909
+ var options = this.options;
910
+ // Initialize options set via HTML5 data-attributes:
911
+ $.extend(options, $(this.element[0].cloneNode(false)).data());
912
+ options.namespace = options.namespace || this.widgetName;
913
+ this._initSpecialOptions();
914
+ this._slots = [];
915
+ this._sequence = this._getXHRPromise(true);
916
+ this._sending = this._active = this._loaded = this._total = 0;
917
+ this._initEventHandlers();
918
+ },
919
+
920
+ destroy: function () {
921
+ this._destroyEventHandlers();
922
+ $.Widget.prototype.destroy.call(this);
923
+ },
924
+
925
+ enable: function () {
926
+ $.Widget.prototype.enable.call(this);
927
+ this._initEventHandlers();
928
+ },
929
+
930
+ disable: function () {
931
+ this._destroyEventHandlers();
932
+ $.Widget.prototype.disable.call(this);
933
+ },
934
+
935
+ // This method is exposed to the widget API and allows adding files
936
+ // using the fileupload API. The data parameter accepts an object which
937
+ // must have a files property and can contain additional options:
938
+ // .fileupload('add', {files: filesList});
939
+ add: function (data) {
940
+ if (!data || this.options.disabled) {
941
+ return;
942
+ }
943
+ if (data.fileInput && !data.files) {
944
+ data.files = this._getFileInputFiles(data.fileInput);
945
+ } else {
946
+ data.files = $.each($.makeArray(data.files), this._normalizeFile);
947
+ }
948
+ this._onAdd(null, data);
949
+ },
950
+
951
+ // This method is exposed to the widget API and allows sending files
952
+ // using the fileupload API. The data parameter accepts an object which
953
+ // must have a files property and can contain additional options:
954
+ // .fileupload('send', {files: filesList});
955
+ // The method returns a Promise object for the file upload call.
956
+ send: function (data) {
957
+ if (data && !this.options.disabled) {
958
+ if (data.fileInput && !data.files) {
959
+ data.files = this._getFileInputFiles(data.fileInput);
960
+ } else {
961
+ data.files = $.each($.makeArray(data.files), this._normalizeFile);
962
+ }
963
+ if (data.files.length) {
964
+ return this._onSend(null, data);
965
+ }
966
+ }
967
+ return this._getXHRPromise(false, data && data.context);
968
+ }
969
+
970
+ });
971
+
972
+ }));