-
Notifications
You must be signed in to change notification settings - Fork 10
/
uploader.js
1592 lines (1305 loc) · 50.3 KB
/
uploader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
322
323
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
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
/**
* UploaderJS v0.9.0
* https://github.com/tamaspap/uploader
*
* Copyright 2014, Pap Tamas
* Released under the MIT license
* http://www.opensource.org/licenses/MIT
*
* Please report issues at: https://github.com/tamaspap/uploader/issues
*/
/**
* Create the Uploader.
*/
(function ($, window) {
"use strict";
/**
* Create the Uploader class
*
* @param options
*/
function Uploader(options) {
this.init(options);
}
/**
* Define the static properties of Uploader.
*/
$.extend(Uploader, {
/**
* Default values
*/
defaults: {
/**
* The button to launch the file browser dialog.
* Possible values: a jQuery selector/object or an HTMLElement.
*/
selectButton: null,
/**
* The drop zone where the user can drag & drop files to.
* Possible values: a jQuery selector/object or an HTMLElement.
*/
dropZone: null,
/**
* Name of the file input.
*/
name: "file",
/**
* Whether selecting multiple files at once in allowed.
* Supported only in modern browsers.
*/
multiple: true,
/**
* The url to upload the files to.
*/
url: null,
/**
* Upload method.
*/
method: "POST",
/**
* Whether to automatically upload files after they were selected or drag & dropped to the drop zone.
*/
autoUpload: true,
/**
* Whether to remove the file from the file list if upload failed.
*/
removeOnFail: true,
/**
* Additional headers to send with the files.
* Supported only in modern browsers.
*/
headers: {},
/**
* Additional data to send with the files.
*/
data: {},
/**
* Array of the accepted file types, ex. [".jpg", ".jpeg", ".png"]. By default all types are accepted.
*/
acceptType: null,
/**
* The accepted file size interval in KB. By default any size is accepted.
* Supported only in modern browsers.
*/
acceptSize: [null, null],
/**
* The maximum number of files the user can upload. By default there is no limit.
*/
maxFiles: null,
/**
* The maximum number of simultaneous uploads.
*/
simultaneousUploads: 3,
/**
* Whether to upload files using iFrames even if XHR uploads are supported (useful for testing purposes).
*/
degrade: false,
/**
* The css classes that will be added to different elements on different events.
*/
cssClasses: {
/**
* Drop zone.
*/
dropZoneDragOver: "drop-zone-drag-over"
},
/**
* Error messages.
*/
errors: {
invalidType: "The file '{{fileName}}' is not valid. Please upload only files with the following extensions: {{allowedExtensions}}.",
sizeTooSmall: "The file '{{fileName}}' is too small. Please upload only files bigger than {{allowedMinSize}}.",
sizeTooLarge: "The file '{{fileName}}' is too large. Please upload only files smaller than {{allowedMaxSize}}.",
tooManyFiles: "Can not upload the file '{{fileName}}'', because you can upload only {{maxFiles}} file(s).",
networkError: "There was a problem uploading the file '{{fileName}}'. Please try uploading that file again."
},
/**
* Prefix to use when creating unique ids.
*/
uniqueIdPrefix: "upload_"
},
/**
* Counter for generating unique ids.
*/
uniqueIncrement: 1,
/**
* Check browser support for different features.
*/
support: (function(window) {
var support = {};
// Whether the browser supports uploading files with XMLHttpRequest.
support.xhrUpload = !! window.FormData && !! window.XMLHttpRequest && "upload" in new XMLHttpRequest();
// Whether the browser supports selecting multiple files at once.
support.selectMultiple = !! window.FileList && "multiple" in document.createElement("input");
// Whether the browser supports dropping files to the drop zone.
var div = document.createElement("div");
support.dropFiles = "ondragstart" in div && "ondrop" in div && !! window.FileList;
return support;
}(window)),
/**
* Define statuses.
*
* STATUS_ADDED: The file was added to the file list.
* STATUS_UPLOADING: The file is uploading.
* STATUS_COMPLETED: The file is uploaded.
* STATUS_PENDING: The file is in the pending list and waits to be uploaded.
* STATUS_FAILED: The upload failed due a network error, or the server side validation failed.
*/
STATUS_ADDED: "ADDED",
STATUS_UPLOADING: "UPLOADING",
STATUS_COMPLETED: "COMPLETED",
STATUS_PENDING: "PENDING",
STATUS_FAILED: "FAILED"
});
window.Uploader = Uploader;
}(jQuery, window));
/**
* Implement initialization.
*/
(function (Uploader, $) {
"use strict";
$.extend(Uploader.prototype, {
/**
* Init uploader instance.
*
* @param options
*/
init: function(options) {
// Merge the options with the default values.
this.options = $.extend(true, {}, this.constructor.defaults, options);
/**
* Whether the current browser can be considered modern (in this case we consider a browser to be modern if
* it supports XHR uploads, and `this.options.degrade` is not set to true.
*/
this.isModernBrowser = this.constructor.support.xhrUpload && ! this.options.degrade;
/**
* All the files selected or drag & dropped by the user are added one by one to the file list (if they pass the validation).
*
* Each item in the file list is added with a unique key (generated by the `uniqueId` method), and has the
* following properties:
*
* - name: the file's name
*
* - file: a File object for modern browsers supporting XHR uploads, and a jQuery object with
* a file input element for older browsers
*
* - status: STATUS_ADDED | STATUS_UPLOADING | STATUS_COMPLETED | STATUS_PENDING | STATUS_FAILED
*
* - request: an XHR object for modern browsers supporting XHR uploads, a jQuery object with an iFrame element
* for older browsers
*
* - progress: information about the upload
*
* NOTE! The `request` and `progress` properties doesn't exist until the file starts uploading.
*/
this.fileList = {};
/**
* To not exceed the maximum allowed number of simultaneous uploads, some files are added to the pending list,
* and wait until other files finish uploading.
*/
this.pendingList = [];
// Init the file count (this number must not exceed the `this.options.maxFiles` value).
this.fileCount = 0;
// Init the list of event listeners
this.eventHandlers = {};
// Init elements.
this.initElements();
},
/**
* Create and init elements.
*
* Because most of the browsers doesn't allow launching the file browser dialog from javascript, we must implement
* the transparent input trick.
*
* Basically we place a transparent file input over the upload button. This way users will see the select button,
* but will actually click the transparent file input, thus the file browser dialog will be opened.
*/
initElements: function() {
// Init the select button
this.$selectButton = $(this.options.selectButton);
// Create the transparent file input and append it to the select button
this.createFileInput();
// Init the drop zone
if (this.options.dropZone && this.constructor.support.dropFiles && this.isModernBrowser) {
this.initDropZone();
}
},
/**
* Create and init a new file input.
*/
createFileInput: function() {
// Create the file input
this.$fileInput = $("<input/>", {
name: this.options.name,
accept: (this.options.acceptType || []).join(),
type: "file"
}).appendTo(this.$selectButton);
// Set the multiple attribute
if (this.options.multiple && this.constructor.support.selectMultiple && this.isModernBrowser) {
this.$fileInput.attr("multiple", "multiple");
}
// Listen to file input's `onchange` event
this.$fileInput.on("change", $.proxy(this.onFileSelect, this));
},
/**
* Init drop zone.
*
* We register all the event handlers in the ".Uploader" namespace, so it will be easier to remove them later.
*/
initDropZone: function() {
this.$dropZone = $(this.options.dropZone);
// On drag over
this.$dropZone.on("dragover.Uploader", $.proxy(function(e) {
e.preventDefault();
this.$dropZone.addClass(this.options.cssClasses.dropZoneDragOver);
return false;
}, this));
// On drag end
this.$dropZone.on("dragend.Uploader", $.proxy(function(e) {
e.preventDefault();
this.$dropZone.removeClass(this.options.cssClasses.dropZoneDragOver);
return false;
}, this));
// On drag leave
this.$dropZone.on("dragleave.Uploader", $.proxy(function(e) {
e.preventDefault();
this.$dropZone.removeClass(this.options.cssClasses.dropZoneDragOver);
return false;
}, this));
// On drop
this.$dropZone.on("drop.Uploader", $.proxy(function(e) {
e.preventDefault();
this.$dropZone.removeClass(this.options.cssClasses.dropZoneDragOver);
// Add the files to the file list
if (e.originalEvent.dataTransfer && e.originalEvent.dataTransfer.files) {
this.addToList(e.originalEvent.dataTransfer.files);
}
}, this));
}
});
}(window.Uploader, jQuery));
// Extend prototype: Implement event handlers.
(function (Uploader, $) {
"use strict";
$.extend(Uploader.prototype, {
/**
* One or more files were selected by the user.
*/
onFileSelect: function() {
// The list of File objects for modern browsers, the file input element for the older ones.
var files = this.isModernBrowser ? this.$fileInput[0].files : [this.$fileInput];
// Add the files to the file list.
this.addToList(files);
if ( ! this.isModernBrowser) {
// Detach the file input, but don't remove it (we will need it when submitting the file to the server).
this.$fileInput.off().detach();
}
else {
// Remove the file input (we got the File objects from it, so we don't need it anymore).
this.$fileInput.remove();
}
// Create new file input element, to make selecting new files possible.
this.createFileInput();
},
/**
* A new file was added to the list.
*
* @param uniqueId
* @param name
* @param file
*/
onFileAdd: function(uniqueId, name, file) {
// Trigger "fileAdd"
this.trigger("fileAdd", uniqueId, name, file);
},
/**
* A file was removed from the list.
*
* @param uniqueId
* @param name
*/
onFileRemove: function(uniqueId, name) {
// Trigger "fileRemove"
this.trigger("fileRemove", uniqueId, name);
},
/**
* A file validation failed.
*
* @param name
* @param error
*/
onFileInvalid: function(name, error) {
// Trigger "fileInvalid"
this.trigger("fileInvalid", name, error);
},
/**
* The max files limit was reached.
*
* @param name
* @param message
*/
onTooManyFiles: function(name, message) {
// Trigger "tooManyFiles"
this.trigger("tooManyFiles", name, message);
},
/**
* Everything is ready to upload.
*
* Some final setup can be made via event handlers.
*
* @param uniqueId
* @param name
* @param data
* @param xhr
*/
onBeforeUpload: function(uniqueId, name, data, xhr) {
// Trigger "beforeUpload"
this.trigger("beforeUpload", uniqueId, name, data, xhr);
},
/**
* A file started uploading.
*
* @param uniqueId
* @param name
*/
onUploadStart: function(uniqueId, name) {
// Init the progress object
var now = new Date().getTime();
this.fileList[uniqueId].progress = {
startTime: now,
endTime: null,
previous: {
time: null,
bytes: 0
},
current: {
time: now,
bytes: 0
}
};
// Trigger uploadStart
this.trigger("uploadStart", uniqueId, name);
},
/**
* Upload progress.
*
* @param uniqueId
* @param name
* @param loaded
* @param total
*/
onUploadProgress: function(uniqueId, name, loaded, total) {
// Update progress
var now = new Date().getTime();
var progress = this.fileList[uniqueId].progress;
progress.previous = {
time: progress.current.time,
bytes: progress.current.bytes
};
progress.current = {
time: now,
bytes: loaded
};
if (loaded == total) {
progress.endTime = now;
}
// Trigger "uploadProgress"
this.trigger("uploadProgress", uniqueId, name, loaded, total);
},
/**
* The upload was completed.
*
* @param uniqueId
* @param name
* @param response
* @param status
* @param xhr
*/
onUploadComplete: function(uniqueId, name, response, status, xhr) {
/**
* When an upload is completed it means that the browser has sent the file to the server successfully, and
* also got a response from it. By default we assume that a completed upload is also a successful upload.
*
* Therefore when an upload is completed, it's the developers job to check if the upload was successful or not,
* based on the server's response. If the upload was unsuccessful, developers must throw an error with the
* name: `UploadError`, and a message to call the `onUploadFail` method with.
*
* NOTE! For iFrame uploads `status` and `xhr` are null.
*/
try {
// Trigger "uploadComplete"
this.trigger("uploadComplete", uniqueId, name, response, status, xhr);
// Change status to: "COMPLETED"
this.fileList[uniqueId].status = this.constructor.STATUS_COMPLETED;
// Remove the hidden form and iFrame from the DOM
if ( ! this.isModernBrowser) {
this.iFrameCleanUp(uniqueId);
}
// Upload next file from the pending list (if any)
this.uploadNext();
}
catch (error) {
if (error.name == "UploadError") {
// There was an error, call onUploadFail
this.onUploadFail(uniqueId, name, this.errorMessage(error.message, this.fileList[uniqueId].file));
}
else {
// This is not an UploadError, re-throw it
throw error;
}
}
},
/**
* The upload was failed.
*
* This happens when a network error occurs (the server is down or the user looses the internet connection), but
* also when the upload is completed but it isn't successful. For example: the browser has sent the file to the
* server, but the server was unable to save the file and responded with an error message.
*
* @param uniqueId
* @param name
* @param message
*/
onUploadFail: function(uniqueId, name, message) {
// Change status to: "FAILED"
this.fileList[uniqueId].status = this.constructor.STATUS_FAILED;
// Trigger "uploadFail"
this.trigger("uploadFail", uniqueId, name, message);
// Upload was unsuccessful, update the file count
this.fileCount--;
// Remove the hidden form and iFrame from the DOM
if ( ! this.isModernBrowser) {
this.iFrameCleanUp(uniqueId);
}
if (this.options.removeOnFail) {
// Remove the file from the file list
this.removeFromList(uniqueId);
}
// Upload next file from the pending list (if any)
this.uploadNext();
},
/**
* The upload was aborted by the user.
*
* @param uniqueId
* @param name
*/
onUploadAbort: function(uniqueId, name) {
// Trigger "uploadAbort"
this.trigger("uploadAbort", uniqueId, name);
// Upload wasn't finished, update the file count
this.fileCount--;
// Remove the hidden form and iFrame from the DOM
if ( ! this.isModernBrowser) {
this.iFrameCleanUp(uniqueId);
}
// Remove file from file list
this.removeFromList(uniqueId);
// Upload next file from the pending list (if any)
this.uploadNext();
},
/**
* Register an event handler for an event.
*
* @param event
* @param callback
* @return {*}
*/
on: function(event, callback) {
this.eventHandlers[event] = this.eventHandlers[event] || [];
this.eventHandlers[event].push(callback);
// Make chaining possible
return this;
},
/**
* Remove all the handlers of an event.
*
* If no event is specified, all the handlers of all events will be removed.
*
* @param event
*/
off: function(event) {
if (event) {
delete this.eventHandlers[event];
}
else {
this.eventHandlers = {};
}
},
/**
* Execute all the handlers attached to an event.
*
* NOTE! Some event handlers may throw errors intentionally. For example the handlers of the "uploadComplete" event
* may throw an error after processing the server's response, if they consider the upload unsuccessful.
* We want to catch these errors to make sure all handlers are executed, then re-throw the latest
* error.
*/
trigger: function() {
var lastError, event = arguments[0];
this.eventHandlers[event] = this.eventHandlers[event] || [];
// Try to execute all handlers associated with this event
for (var i = 0; i < this.eventHandlers[event].length; i++) {
try {
this.eventHandlers[event][i].apply(this, Array.prototype.slice.call(arguments, 1));
}
catch (error) {
if (error.name == "UploadError") {
// Update last error
lastError = error;
}
else {
// This is not an upload error, re-throw it
throw error;
}
}
}
if (lastError) {
// We have an error, let's re-throw it
throw lastError;
}
}
});
}(window.Uploader, jQuery));
/**
* Ajax vs. iFrame uploads
* ===========================================
*
* In modern browsers (supporting XHR uploads) files are uploaded using ajax, in older browsers using hidden iFrames.
*
* The workflow in modern browsers
* ================================
*
* 1. User selects files or drops files to the drop zone
* 2. After they are validated, the file objects from the file input or the drop zone are added to the file list
*
* Uploading a file:
*
* 3. A form data is created
* 4. The file and additional data (from this.options.data) are appended to the form data
* 5. The form data is sent to the server via a XMLHttpRequest
*
*
* The workflow in older browsers
* ================================
*
* 1. User selects a file (no dropping files is supported in this case)
* 2. The file input element used to select the file is added to the file list and detached from the DOM. Then it's
* replaced in DOM by a new file input element, to make selecting new files possible
*
* Uploading a file:
*
* 3. A hidden form and a hidden iFrame are created
* 4. The iFrame is set as the target for the form
* 5. The file input element 'holding' the file, and additional data (from this.options.data) are appended to the form
* 6. The form is submitted to the server
* 7. When upload finishes, the form and the iFrame are removed from the DOM
*/
// Extend prototype: Implement API methods.
(function (Uploader, $, window, undefined) {
"use strict";
$.extend(Uploader.prototype, {
/**
* Add files to the file list.
*
* This method is called when new files are selected or drag & dropped to the drop zone.
*
* @param files
*/
addToList: function(files) {
$.each(files, $.proxy(function(i, file) {
// Get the filename
var fileName = this.isModernBrowser ? file.name : this.getFileName(file.val());
// Make sure the max files limit wasn't reached yet
if ( ! $.isNumeric(this.options.maxFiles) || this.fileCount < this.options.maxFiles) {
try {
// Check if the file is valid (whether it's type and size are allowed)
this.validateFile(file);
// Create a unique id for the file
var uniqueId = this.uniqueId("file");
this.$fileInput.attr("id", uniqueId);
// Add the file to the file list
this.fileList[uniqueId] = {
name: fileName,
file: file,
status: this.constructor.STATUS_ADDED
};
// Increment file count
this.fileCount++;
// Trigger onFileAdd
this.onFileAdd(uniqueId, fileName, file);
// Upload the file if autoUpload is true
if (this.options.autoUpload) {
this.upload(uniqueId);
}
}
catch (error) {
if (error.name == "FileValidationError") {
// The file validation failed
this.onFileInvalid(fileName, error);
}
else {
// This is not a file validation error, re-throw it
throw error;
}
}
}
else {
// The max files limit was reached
this.onTooManyFiles(fileName, this.errorMessage(this.options.errors.tooManyFiles, file));
}
}, this));
},
/**
* Check if a file is valid (whether it's type and size are allowed).
*
* @param file
* @return {boolean}
*/
validateFile: function(file) {
// Validation errors
var errors = [];
var fileName = this.isModernBrowser ? file.name : this.getFileName(file.val());
var fileSize = this.isModernBrowser ? file.size : null;
var fileExtension = this.getFileExtension(fileName);
var allowedExtensions = this.options.acceptType;
// We are multiplying with 1024 because the values in `this.options.acceptSize` are in KB
var allowedMinSize = $.isNumeric(this.options.acceptSize[0]) ? this.options.acceptSize[0] * 1024 : null;
var allowedMaxSize = $.isNumeric(this.options.acceptSize[1]) ? this.options.acceptSize[1] * 1024 : null;
// Check if file type is accepted
if (allowedExtensions && $.inArray("." + fileExtension, allowedExtensions) < 0) {
// Invalid file type
errors.push({
code: 1,
message: this.errorMessage(this.options.errors.invalidType, file)
});
}
// Check if file size is accepted
if ($.isNumeric(fileSize)) {
if ($.isNumeric(allowedMinSize) && fileSize < allowedMinSize) {
// File size too small
errors.push({
code: 2,
message: this.errorMessage(this.options.errors.sizeTooSmall, file)
});
}
if ($.isNumeric(allowedMaxSize) && fileSize > allowedMaxSize) {
// File size too large
errors.push({
code: 3,
message: this.errorMessage(this.options.errors.sizeTooLarge, file)
});
}
}
if (errors.length) {
// The file validation failed. We throw a FileValidationError.
throw {
name: "FileValidationError",
message: "Invalid file.",
errors: errors
};
}
return true;
},
/**
* Upload a file.
*
* @param uniqueId
*/
upload: function(uniqueId) {
// Make sure the file isn't already uploading
if ( ! this.isUploading(uniqueId)) {
// Get the number of uploading files
var filesUploading = this.countFiles(this.constructor.STATUS_UPLOADING);
// Check if we can upload the file, or have to put it in the pending list
if (filesUploading < this.options.simultaneousUploads) {
// If the file is in the pending list, remove it from there
if (this.isPending(uniqueId)) {
this.removeFromPendingList(uniqueId);
}
// Execute the appropriate "upload" method
this[this.isModernBrowser ? "ajaxUpload" : "iFrameUpload"](uniqueId);
// Change status to: "UPLOADING"
this.fileList[uniqueId].status = this.constructor.STATUS_UPLOADING;
// Upload was started
this.onUploadStart(uniqueId, this.fileList[uniqueId].name);
}
else {
// Put the file in the pending list and change it's status to "PENDING"
if ( ! this.isPending(uniqueId)) {
this.addToPendingList(uniqueId);
this.fileList[uniqueId].status = this.constructor.STATUS_PENDING;
}
}
}
},
/**
* Upload all files.
*/
uploadAll: function() {
// Get the files from the file list with the status: "ADDED", and upload them one by one
for (var uniqueId in this.fileList) {
if (this.fileList[uniqueId].status == this.constructor.STATUS_ADDED) {
this.upload(uniqueId);
}
}
},
/**
* Abort an upload request.
*
* @param uniqueId
*/
abort: function(uniqueId) {
// Make sure the file is uploading
if (this.isUploading(uniqueId)) {
// Call the appropriate abort method
this[this.isModernBrowser ? "ajaxAbort" : "iFrameAbort"](uniqueId);
}
},
/**
* Abort all active requests.
*/
abortAll: function() {
// Get the files from the file list with the status: "UPLOADING", and abort their upload request
for (var uniqueId in this.fileList) {
if (this.fileList[uniqueId].status == this.constructor.STATUS_UPLOADING) {
this.abort(uniqueId);
}
}
},
/**
* Check if a file is in the file list.
*
* @param uniqueId
* @return {boolean}
*/
inList: function(uniqueId) {
return (typeof this.fileList[uniqueId] != "undefined");
},
/**
* Remove a file from the file list.
*
* @param uniqueId
*/
removeFromList: function(uniqueId) {
var name = this.fileList[uniqueId].name;
if (this.inList(uniqueId)) {
delete this.fileList[uniqueId];
}
// A file was removed from the list
this.onFileRemove(uniqueId, name);
},
/**
* Count the files in the file list having the given status.
* If no status is specified, the size of the whole list is returned.
*
* @param status
* @returns {number}
*/
countFiles: function(status) {
var c = 0;
for (var uniqueId in this.fileList) {
if ( ! status || this.fileList[uniqueId].status == status) c++;
}
return c;
},
/**
* Check if the file with the given uniqueId has the status: "ADDED".
*
* @param uniqueId
* @returns {boolean}
*/
isAdded: function(uniqueId) {
return this.fileList[uniqueId].status == this.constructor.STATUS_ADDED;
},
/**
* Check if the file with the given uniqueId has the status: "PENDING".
*
* @param uniqueId
* @returns {boolean}