forked from dropzone/dropzone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropzone.coffee
1455 lines (1097 loc) · 48.1 KB
/
dropzone.coffee
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
###
#
# More info at [www.dropzonejs.com](http://www.dropzonejs.com)
#
# Copyright (c) 2012, Matias Meno
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###
# Dependencies
Em = Emitter ? require "emitter" # Can't be the same name because it will lead to a local variable
noop = ->
class Dropzone extends Em
###
This is a list of all available events you can register on a dropzone object.
You can register an event handler like this:
dropzone.on("dragEnter", function() { });
###
events: [
"drop"
"dragstart"
"dragend"
"dragenter"
"dragover"
"dragleave"
"addedfile"
"removedfile"
"thumbnail"
"error"
"errormultiple"
"processing"
"processingmultiple"
"uploadprogress"
"totaluploadprogress"
"sending"
"sendingmultiple"
"success"
"successmultiple"
"canceled"
"canceledmultiple"
"complete"
"completemultiple"
"reset"
"maxfilesexceeded"
"maxfilesreached"
]
defaultOptions:
url: null
method: "post"
withCredentials: no
parallelUploads: 2
uploadMultiple: no # Whether to send multiple files in one request.
maxFilesize: 256 # in MB
paramName: "file" # The name of the file param that gets transferred.
createImageThumbnails: true
maxThumbnailFilesize: 10 # in MB. When the filename exceeds this limit, the thumbnail will not be generated.
thumbnailWidth: 100
thumbnailHeight: 100
# Can be used to limit the maximum number of files that will be handled
# by this Dropzone
maxFiles: null
# Can be an object of additional parameters to transfer to the server.
# This is the same as adding hidden input fields in the form element.
params: { }
# If true, the dropzone will present a file selector when clicked.
clickable: yes
# Whether hidden files in directories should be ignored.
ignoreHiddenFiles: yes
# You can set accepted mime types here.
#
# The default implementation of the `accept()` function will check this
# property, and if the Dropzone is clickable this will be used as
# `accept` attribute.
#
# This is a comma separated list of mime types or extensions. E.g.:
#
# audio/*,video/*,image/png,.pdf
#
# See https://developer.mozilla.org/en-US/docs/HTML/Element/input#attr-accept
# for a reference.
acceptedFiles: null
# @deprecated
# Use acceptedFiles instead.
acceptedMimeTypes: null
# If false, files will be added to the queue but the queue will not be
# processed automatically.
# This can be useful if you need some additional user input before sending
# files (or if you want want all files sent at once).
# If you're ready to send the file simply call myDropzone.processQueue()
autoProcessQueue: on
# If false, files added to the dropzone will not be queued by default.
# You'll have to call `enqueueFile(file)` manually.
autoQueue: on
# If true, Dropzone will add a link to each file preview to cancel/remove
# the upload.
# See dictCancelUpload and dictRemoveFile to use different words.
addRemoveLinks: no
# A CSS selector or HTML element for the file previews container.
# If null, the dropzone element itself will be used.
# If false, previews won't be rendered.
previewsContainer: null
# Dictionary
# The text used before any files are dropped
dictDefaultMessage: "Drop files here to upload"
# The text that replaces the default message text it the browser is not supported
dictFallbackMessage: "Your browser does not support drag'n'drop file uploads."
# The text that will be added before the fallback form
# If null, no text will be added at all.
dictFallbackText: "Please use the fallback form below to upload your files like in the olden days."
# If the filesize is too big.
dictFileTooBig: "File is too big ({{filesize}}MiB). Max filesize: {{maxFilesize}}MiB."
# If the file doesn't match the file type.
dictInvalidFileType: "You can't upload files of this type."
# If the server response was invalid.
dictResponseError: "Server responded with {{statusCode}} code."
# If used, the text to be used for the cancel upload link.
dictCancelUpload: "Cancel upload"
# If used, the text to be used for confirmation when cancelling upload.
dictCancelUploadConfirmation: "Are you sure you want to cancel this upload?"
# If used, the text to be used to remove a file.
dictRemoveFile: "Remove file"
# If this is not null, then the user will be prompted before removing a file.
dictRemoveFileConfirmation: null
# Displayed when the maxFiles have been exceeded
# You can use {{maxFiles}} here, which will be replaced by the option.
dictMaxFilesExceeded: "You can not upload any more files."
# If `done()` is called without argument the file is accepted
# If you call it with an error message, the file is rejected
# (This allows for asynchronous validation).
accept: (file, done) -> done()
tryRemoveFile: (file, done) -> done()
# Called when dropzone initialized
# You can add event listeners here
init: -> noop
# Used to debug dropzone and force the fallback form.
forceFallback: off
# Called when the browser does not support drag and drop
fallback: ->
# This code should pass in IE7... :(
@element.className = "#{@element.className} dz-browser-not-supported"
for child in @element.getElementsByTagName "div"
if /(^| )dz-message($| )/.test child.className
messageElement = child
child.className = "dz-message" # Removes the 'dz-default' class
continue
unless messageElement
messageElement = Dropzone.createElement """<div class="dz-message"><span></span></div>"""
@element.appendChild messageElement
span = messageElement.getElementsByTagName("span")[0]
span.textContent = @options.dictFallbackMessage if span
@element.appendChild @getFallbackForm()
# Gets called to calculate the thumbnail dimensions.
#
# You can use file.width, file.height, options.thumbnailWidth and
# options.thumbnailHeight to calculate the dimensions.
#
# The dimensions are going to be used like this:
#
# var info = @options.resize.call(this, file);
# ctx.drawImage(img, info.srcX, info.srcY, info.srcWidth, info.srcHeight, info.trgX, info.trgY, info.trgWidth, info.trgHeight);
#
# srcX, srcy, trgX and trgY can be omitted (in which case 0 is assumed).
# trgWidth and trgHeight can be omitted (in which case the options.thumbnailWidth / options.thumbnailHeight are used)
resize: (file) ->
info =
srcX: 0
srcY: 0
srcWidth: file.width
srcHeight: file.height
srcRatio = file.width / file.height
info.optWidth = @options.thumbnailWidth
info.optHeight = @options.thumbnailHeight
# automatically calculate dimensions if not specified
if !info.optWidth? and !info.optHeight?
info.optWidth = info.srcWidth
info.optHeight = info.srcHeight
else if !info.optWidth?
info.optWidth = srcRatio * info.optHeight
else if !info.optHeight?
info.optHeight = (1/srcRatio) * info.optWidth
trgRatio = info.optWidth / info.optHeight
if file.height < info.optHeight or file.width < info.optWidth
# This image is smaller than the canvas
info.trgHeight = info.srcHeight
info.trgWidth = info.srcWidth
else
# Image is bigger and needs rescaling
if srcRatio > trgRatio
info.srcHeight = file.height
info.srcWidth = info.srcHeight * trgRatio
else
info.srcWidth = file.width
info.srcHeight = info.srcWidth / trgRatio
info.srcX = (file.width - info.srcWidth) / 2
info.srcY = (file.height - info.srcHeight) / 2
return info
###
Those functions register themselves to the events on init and handle all
the user interface specific stuff. Overwriting them won't break the upload
but can break the way it's displayed.
You can overwrite them if you don't like the default behavior. If you just
want to add an additional event handler, register it on the dropzone object
and don't overwrite those options.
###
# Those are self explanatory and simply concern the DragnDrop.
drop: (e) -> @element.classList.remove "dz-drag-hover"
dragstart: noop
dragend: (e) -> @element.classList.remove "dz-drag-hover"
dragenter: (e) -> @element.classList.add "dz-drag-hover"
dragover: (e) -> @element.classList.add "dz-drag-hover"
dragleave: (e) -> @element.classList.remove "dz-drag-hover"
paste: noop
# Called whenever there are no files left in the dropzone anymore, and the
# dropzone should be displayed as if in the initial state.
reset: ->
@element.classList.remove "dz-started"
# Called when a file is added to the queue
# Receives `file`
addedfile: (file) ->
@element.classList.add "dz-started" if @element == @previewsContainer
if @previewsContainer
file.previewElement = Dropzone.createElement @options.previewTemplate.trim()
file.previewTemplate = file.previewElement # Backwards compatibility
@previewsContainer.appendChild file.previewElement
node.textContent = file.name for node in file.previewElement.querySelectorAll("[data-dz-name]")
node.innerHTML = @filesize file.size for node in file.previewElement.querySelectorAll("[data-dz-size]")
if @options.addRemoveLinks
file._removeLink = Dropzone.createElement """<a class="dz-remove" href="javascript:undefined;" data-dz-remove>#{@options.dictRemoveFile}</a>"""
file.previewElement.appendChild file._removeLink
removeFileEvent = (e) =>
e.preventDefault()
e.stopPropagation()
if file.status == Dropzone.UPLOADING
Dropzone.confirm @options.dictCancelUploadConfirmation, => @removeFile file
else
if @options.dictRemoveFileConfirmation
Dropzone.confirm @options.dictRemoveFileConfirmation, => @removeFile file
else
@removeFile file
removeLink.addEventListener "click", removeFileEvent for removeLink in file.previewElement.querySelectorAll("[data-dz-remove]")
# Called whenever a file is removed.
removedfile: (file) ->
file.previewElement?.parentNode.removeChild file.previewElement if file.previewElement
@_updateMaxFilesReachedClass()
# Called when a thumbnail has been generated
# Receives `file` and `dataUrl`
thumbnail: (file, dataUrl) ->
if file.previewElement
file.previewElement.classList.remove "dz-file-preview"
file.previewElement.classList.add "dz-image-preview"
for thumbnailElement in file.previewElement.querySelectorAll("[data-dz-thumbnail]")
thumbnailElement.alt = file.name
thumbnailElement.src = dataUrl
# Called whenever an error occurs
# Receives `file` and `message`
error: (file, message) ->
if file.previewElement
file.previewElement.classList.add "dz-error"
message = message.error if typeof message != "String" and message.error
node.textContent = message for node in file.previewElement.querySelectorAll("[data-dz-errormessage]")
errormultiple: noop
# Called when a file gets processed. Since there is a cue, not all added
# files are processed immediately.
# Receives `file`
processing: (file) ->
if file.previewElement
file.previewElement.classList.add "dz-processing"
file._removeLink.textContent = @options.dictCancelUpload if file._removeLink
processingmultiple: noop
# Called whenever the upload progress gets updated.
# Receives `file`, `progress` (percentage 0-100) and `bytesSent`.
# To get the total number of bytes of the file, use `file.size`
uploadprogress: (file, progress, bytesSent) ->
if file.previewElement
node.style.width = "#{progress}%" for node in file.previewElement.querySelectorAll("[data-dz-uploadprogress]")
# Called whenever the total upload progress gets updated.
# Called with totalUploadProgress (0-100), totalBytes and totalBytesSent
totaluploadprogress: noop
# Called just before the file is sent. Gets the `xhr` object as second
# parameter, so you can modify it (for example to add a CSRF token) and a
# `formData` object to add additional information.
sending: noop
sendingmultiple: noop
# When the complete upload is finished and successfull
# Receives `file`
success: (file) ->
if file.previewElement
file.previewElement.classList.add "dz-success"
successmultiple: noop
# When the upload is canceled.
canceled: (file) -> @emit "error", file, "Upload canceled."
canceledmultiple: noop
# When the upload is finished, either with success or an error.
# Receives `file`
complete: (file) ->
file._removeLink.textContent = @options.dictRemoveFile if file._removeLink
completemultiple: noop
maxfilesexceeded: noop
maxfilesreached: noop
# This template will be chosen when a new file is dropped.
previewTemplate: """
<div class="dz-preview dz-file-preview">
<div class="dz-details">
<div class="dz-filename"><span data-dz-name></span></div>
<div class="dz-size" data-dz-size></div>
<img data-dz-thumbnail />
</div>
<div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div>
<div class="dz-success-mark"><span>✔</span></div>
<div class="dz-error-mark"><span>✘</span></div>
<div class="dz-error-message"><span data-dz-errormessage></span></div>
</div>
"""
# global utility
extend = (target, objects...) ->
for object in objects
target[key] = val for key, val of object
target
constructor: (@element, options) ->
# For backwards compatibility since the version was in the prototype previously
@version = Dropzone.version
@defaultOptions.previewTemplate = @defaultOptions.previewTemplate.replace /\n*/g, ""
@clickableElements = [ ]
@listeners = [ ]
@files = [] # All files
@element = document.querySelector @element if typeof @element == "string"
# Not checking if instance of HTMLElement or Element since IE9 is extremely weird.
throw new Error "Invalid dropzone element." unless @element and @element.nodeType?
throw new Error "Dropzone already attached." if @element.dropzone
# Now add this dropzone to the instances.
Dropzone.instances.push @
# Put the dropzone inside the element itself.
@element.dropzone = @
elementOptions = Dropzone.optionsForElement(@element) ? { }
@options = extend { }, @defaultOptions, elementOptions, options ? { }
# If the browser failed, just call the fallback and leave
return @options.fallback.call this if @options.forceFallback or !Dropzone.isBrowserSupported()
# @options.url = @element.getAttribute "action" unless @options.url?
@options.url = @element.getAttribute "action" unless @options.url?
throw new Error "No URL provided." unless @options.url
throw new Error "You can't provide both 'acceptedFiles' and 'acceptedMimeTypes'. 'acceptedMimeTypes' is deprecated." if @options.acceptedFiles and @options.acceptedMimeTypes
# Backwards compatibility
if @options.acceptedMimeTypes
@options.acceptedFiles = @options.acceptedMimeTypes
delete @options.acceptedMimeTypes
@options.method = @options.method.toUpperCase()
if (fallback = @getExistingFallback()) and fallback.parentNode
# Remove the fallback
fallback.parentNode.removeChild fallback
# Display previews in the previewsContainer element or the Dropzone element unless explicitly set to false
if @options.previewsContainer != false
if @options.previewsContainer
@previewsContainer = Dropzone.getElement @options.previewsContainer, "previewsContainer"
else
@previewsContainer = @element
if @options.clickable
if @options.clickable == yes
@clickableElements = [ @element ]
else
@clickableElements = Dropzone.getElements @options.clickable, "clickable"
@init()
# Returns all files that have been accepted
getAcceptedFiles: -> file for file in @files when file.accepted
# Returns all files that have been rejected
# Not sure when that's going to be useful, but added for completeness.
getRejectedFiles: -> file for file in @files when not file.accepted
getFilesWithStatus: (status) -> file for file in @files when file.status == status
# Returns all files that are in the queue
getQueuedFiles: -> @getFilesWithStatus Dropzone.QUEUED
getUploadingFiles: -> @getFilesWithStatus Dropzone.UPLOADING
# Files that are either queued or uploading
getActiveFiles: -> file for file in @files when file.status == Dropzone.UPLOADING or file.status == Dropzone.QUEUED
init: ->
# In case it isn't set already
@element.setAttribute("enctype", "multipart/form-data") if @element.tagName == "form"
if @element.classList.contains("dropzone") and [email protected](".dz-message")
@element.appendChild Dropzone.createElement """<div class="dz-default dz-message"><span>#{@options.dictDefaultMessage}</span></div>"""
if @clickableElements.length
setupHiddenFileInput = =>
document.body.removeChild @hiddenFileInput if @hiddenFileInput
@hiddenFileInput = document.createElement "input"
@hiddenFileInput.setAttribute "type", "file"
@hiddenFileInput.setAttribute "multiple", "multiple" if [email protected]? || @options.maxFiles > 1
@hiddenFileInput.className = "dz-hidden-input"
@hiddenFileInput.setAttribute "accept", @options.acceptedFiles if @options.acceptedFiles?
# Not setting `display="none"` because some browsers don't accept clicks
# on elements that aren't displayed.
@hiddenFileInput.style.visibility = "hidden"
@hiddenFileInput.style.position = "absolute"
@hiddenFileInput.style.top = "0"
@hiddenFileInput.style.left = "0"
@hiddenFileInput.style.height = "0"
@hiddenFileInput.style.width = "0"
document.body.appendChild @hiddenFileInput
@hiddenFileInput.addEventListener "change", =>
files = @hiddenFileInput.files
@addFile file for file in files if files.length
setupHiddenFileInput()
setupHiddenFileInput()
@URL = window.URL ? window.webkitURL
# Setup all event listeners on the Dropzone object itself.
# They're not in @setupEventListeners() because they shouldn't be removed
# again when the dropzone gets disabled.
@on eventName, @options[eventName] for eventName in @events
@on "uploadprogress", => @updateTotalUploadProgress()
@on "removedfile", => @updateTotalUploadProgress()
@on "canceled", (file) => @emit "complete", file
# Emit a `queuecomplete` event if all files finished uploading.
@on "complete", (file) =>
if @getUploadingFiles().length == 0 and @getQueuedFiles().length == 0
# This needs to be deferred so that `queuecomplete` really triggers after `complete`
setTimeout (=> @emit "queuecomplete"), 0
noPropagation = (e) ->
e.stopPropagation()
if e.preventDefault
e.preventDefault()
else
e.returnValue = false
# Create the listeners
@listeners = [
{
element: @element
events:
"dragstart": (e) =>
@emit "dragstart", e
"dragenter": (e) =>
noPropagation e
@emit "dragenter", e
"dragover": (e) =>
# Makes it possible to drag files from chrome's download bar
# http://stackoverflow.com/questions/19526430/drag-and-drop-file-uploads-from-chrome-downloads-bar
# Try is required to prevent bug in Internet Explorer 11 (SCRIPT65535 exception)
try efct = e.dataTransfer.effectAllowed
e.dataTransfer.dropEffect = if 'move' == efct or 'linkMove' == efct then 'move' else 'copy'
noPropagation e
@emit "dragover", e
"dragleave": (e) =>
@emit "dragleave", e
"drop": (e) =>
noPropagation e
@drop e
"dragend": (e) =>
@emit "dragend", e
# This is disabled right now, because the browsers don't implement it properly.
# "paste": (e) =>
# noPropagation e
# @paste e
}
]
@clickableElements.forEach (clickableElement) =>
@listeners.push
element: clickableElement
events:
"click": (evt) =>
# Only the actual dropzone or the message element should trigger file selection
if (clickableElement != @element) or (evt.target == @element or Dropzone.elementInside evt.target, @element.querySelector ".dz-message")
@hiddenFileInput.click() # Forward the click
@enable()
@options.init.call @
# Not fully tested yet
destroy: ->
@disable()
@removeAllFiles true
if @hiddenFileInput?.parentNode
@hiddenFileInput.parentNode.removeChild @hiddenFileInput
@hiddenFileInput = null
delete @element.dropzone
Dropzone.instances.splice Dropzone.instances.indexOf(this), 1
updateTotalUploadProgress: ->
totalBytesSent = 0
totalBytes = 0
activeFiles = @getActiveFiles()
if activeFiles.length
for file in @getActiveFiles()
totalBytesSent += file.upload.bytesSent
totalBytes += file.upload.total
totalUploadProgress = 100 * totalBytesSent / totalBytes
else
totalUploadProgress = 100
@emit "totaluploadprogress", totalUploadProgress, totalBytes, totalBytesSent
# @options.paramName can be a function taking one parameter rather than a string.
# A parameter name for a file is obtained simply by calling this with an index number.
_getParamName: (n) ->
if typeof @options.paramName is "function"
@options.paramName n
else
"#{@options.paramName}#{if @options.uploadMultiple then "[#{n}]" else ""}"
# Returns a form that can be used as fallback if the browser does not support DragnDrop
#
# If the dropzone is already a form, only the input field and button are returned. Otherwise a complete form element is provided.
# This code has to pass in IE7 :(
getFallbackForm: ->
return existingFallback if existingFallback = @getExistingFallback()
fieldsString = """<div class="dz-fallback">"""
fieldsString += """<p>#{@options.dictFallbackText}</p>""" if @options.dictFallbackText
fieldsString += """<input type="file" name="#{@_getParamName 0}" #{if @options.uploadMultiple then 'multiple="multiple"' } /><input type="submit" value="Upload!"></div>"""
fields = Dropzone.createElement fieldsString
if @element.tagName isnt "FORM"
form = Dropzone.createElement("""<form action="#{@options.url}" enctype="multipart/form-data" method="#{@options.method}"></form>""")
form.appendChild fields
else
# Make sure that the enctype and method attributes are set properly
@element.setAttribute "enctype", "multipart/form-data"
@element.setAttribute "method", @options.method
form ? fields
# Returns the fallback elements if they exist already
#
# This code has to pass in IE7 :(
getExistingFallback: ->
getFallback = (elements) -> return el for el in elements when /(^| )fallback($| )/.test el.className
for tagName in [ "div", "form" ]
return fallback if fallback = getFallback @element.getElementsByTagName tagName
# Activates all listeners stored in @listeners
setupEventListeners: ->
for elementListeners in @listeners
elementListeners.element.addEventListener event, listener, false for event, listener of elementListeners.events
# Deactivates all listeners stored in @listeners
removeEventListeners: ->
for elementListeners in @listeners
elementListeners.element.removeEventListener event, listener, false for event, listener of elementListeners.events
# Removes all event listeners and cancels all files in the queue or being processed.
disable: ->
@clickableElements.forEach (element) -> element.classList.remove "dz-clickable"
@removeEventListeners()
@cancelUpload file for file in @files
enable: ->
@clickableElements.forEach (element) -> element.classList.add "dz-clickable"
@setupEventListeners()
# Returns a nicely formatted filesize
filesize: (size) ->
if size >= 1024 * 1024 * 1024 * 1024 / 10
size = size / (1024 * 1024 * 1024 * 1024 / 10)
string = "TiB"
else if size >= 1024 * 1024 * 1024 / 10
size = size / (1024 * 1024 * 1024 / 10)
string = "GiB"
else if size >= 1024 * 1024 / 10
size = size / (1024 * 1024 / 10)
string = "MiB"
else if size >= 1024 / 10
size = size / (1024 / 10)
string = "KiB"
else
size = size * 10
string = "b"
"<strong>#{Math.round(size)/10}</strong> #{string}"
# Adds or removes the `dz-max-files-reached` class from the form.
_updateMaxFilesReachedClass: ->
if @options.maxFiles? and @getAcceptedFiles().length >= @options.maxFiles
@emit 'maxfilesreached', @files if @getAcceptedFiles().length == @options.maxFiles
@element.classList.add "dz-max-files-reached"
else
@element.classList.remove "dz-max-files-reached"
drop: (e) ->
return unless e.dataTransfer
@emit "drop", e
files = e.dataTransfer.files
# Even if it's a folder, files.length will contain the folders.
if files.length
items = e.dataTransfer.items
if items and items.length and (items[0].webkitGetAsEntry?)
# The browser supports dropping of folders, so handle items instead of files
@_addFilesFromItems items
else
@handleFiles files
return
paste: (e) ->
return unless e?.clipboardData?.items?
@emit "paste", e
items = e.clipboardData.items
@_addFilesFromItems items if items.length
handleFiles: (files) ->
@addFile file for file in files
# When a folder is dropped (or files are pasted), items must be handled
# instead of files.
_addFilesFromItems: (items) ->
for item in items
if item.webkitGetAsEntry? and entry = item.webkitGetAsEntry()
if entry.isFile
@addFile item.getAsFile()
else if entry.isDirectory
# Append all files from that directory to files
@_addFilesFromDirectory entry, entry.name
else if item.getAsFile?
if !item.kind? or item.kind == "file"
@addFile item.getAsFile()
# Goes through the directory, and adds each file it finds recursively
_addFilesFromDirectory: (directory, path) ->
dirReader = directory.createReader()
entriesReader = (entries) =>
for entry in entries
if entry.isFile
entry.file (file) =>
return if @options.ignoreHiddenFiles and file.name.substring(0, 1) is '.'
file.fullPath = "#{path}/#{file.name}"
@addFile file
else if entry.isDirectory
@_addFilesFromDirectory entry, "#{path}/#{entry.name}"
return
dirReader.readEntries entriesReader, (error) -> console?.log? error
# If `done()` is called without argument the file is accepted
# If you call it with an error message, the file is rejected
# (This allows for asynchronous validation)
#
# This function checks the filesize, and if the file.type passes the
# `acceptedFiles` check.
accept: (file, done) ->
if file.size > @options.maxFilesize * 1024 * 1024
done @options.dictFileTooBig.replace("{{filesize}}", Math.round(file.size / 1024 / 10.24) / 100).replace("{{maxFilesize}}", @options.maxFilesize)
else unless Dropzone.isValidFile file, @options.acceptedFiles
done @options.dictInvalidFileType
else if @options.maxFiles? and @getAcceptedFiles().length >= @options.maxFiles
done @options.dictMaxFilesExceeded.replace "{{maxFiles}}", @options.maxFiles
@emit "maxfilesexceeded", file
else
@options.accept.call this, file, done
addFile: (file) ->
file.upload =
progress: 0
# Setting the total upload size to file.size for the beginning
# It's actual different than the size to be transmitted.
total: file.size
bytesSent: 0
@files.push file
file.status = Dropzone.ADDED
@emit "addedfile", file
@_enqueueThumbnail file
@accept file, (error) =>
if error
file.accepted = false
@_errorProcessing [ file ], error # Will set the file.status
else
file.accepted = true
@enqueueFile file if @options.autoQueue # Will set .accepted = true
@_updateMaxFilesReachedClass()
# Wrapper for enqueueFile
enqueueFiles: (files) -> @enqueueFile file for file in files; null
enqueueFile: (file) ->
if file.status == Dropzone.ADDED and file.accepted == true
file.status = Dropzone.QUEUED
if @options.autoProcessQueue
setTimeout (=> @processQueue()), 0 # Deferring the call
else
throw new Error "This file can't be queued because it has already been processed or was rejected."
_thumbnailQueue: [ ]
_processingThumbnail: no
_enqueueThumbnail: (file) ->
if @options.createImageThumbnails and file.type.match(/image.*/) and file.size <= @options.maxThumbnailFilesize * 1024 * 1024
@_thumbnailQueue.push(file)
setTimeout (=> @_processThumbnailQueue()), 0 # Deferring the call
_processThumbnailQueue: ->
return if @_processingThumbnail or @_thumbnailQueue.length == 0
@_processingThumbnail = yes
@createThumbnail @_thumbnailQueue.shift(), =>
@_processingThumbnail = no
@_processThumbnailQueue()
# Can be called by the user to remove a file
removeFileInternal: (file) ->
@cancelUpload file if file.status == Dropzone.UPLOADING
@files = without @files, file
@emit "removedfile", file
@emit "reset" if @files.length == 0
tryRemoveFile: (file, done) ->
return @options.tryRemoveFile.call this, file, done
# Can be called by the user to remove a file
removeFile: (file) ->
@cancelUpload file if file.status == Dropzone.UPLOADING
@tryRemoveFile file, (error) =>
if error
@_errorProcessing([file], error)
else
@removeFileInternal(file)
@_updateMaxFilesReachedClass()
# Removes all files that aren't currently processed from the list
removeAllFiles: (cancelIfNecessary = off) ->
# Create a copy of files since removeFile() changes the @files array.
for file in @files.slice()
@removeFile file if file.status != Dropzone.UPLOADING || cancelIfNecessary
return null
createThumbnail: (file, callback) ->
fileReader = new FileReader
fileReader.onload = =>
# Not using `new Image` here because of a bug in latest Chrome versions.
# See https://github.com/enyo/dropzone/pull/226
img = document.createElement "img"
img.onload = =>
file.width = img.width
file.height = img.height
resizeInfo = @options.resize.call @, file
resizeInfo.trgWidth ?= resizeInfo.optWidth
resizeInfo.trgHeight ?= resizeInfo.optHeight
canvas = document.createElement "canvas"
ctx = canvas.getContext "2d"
canvas.width = resizeInfo.trgWidth
canvas.height = resizeInfo.trgHeight
# This is a bugfix for iOS' scaling bug.
drawImageIOSFix ctx, img, resizeInfo.srcX ? 0, resizeInfo.srcY ? 0, resizeInfo.srcWidth, resizeInfo.srcHeight, resizeInfo.trgX ? 0, resizeInfo.trgY ? 0, resizeInfo.trgWidth, resizeInfo.trgHeight
thumbnail = canvas.toDataURL "image/png"
@emit "thumbnail", file, thumbnail
callback() if callback?
img.src = fileReader.result
fileReader.readAsDataURL file
# Goes through the queue and processes files if there aren't too many already.
processQueue: ->
parallelUploads = @options.parallelUploads
processingLength = @getUploadingFiles().length
i = processingLength
# There are already at least as many files uploading than should be
return if processingLength >= parallelUploads
queuedFiles = @getQueuedFiles()
return unless queuedFiles.length > 0
if @options.uploadMultiple
# The files should be uploaded in one request
@processFiles queuedFiles.slice 0, (parallelUploads - processingLength)
else
while i < parallelUploads
return unless queuedFiles.length # Nothing left to process
@processFile queuedFiles.shift()
i++
# Wrapper for `processFiles`
processFile: (file) -> @processFiles [ file ]
# Loads the file, then calls finishedLoading()
processFiles: (files) ->
for file in files
file.processing = yes # Backwards compatibility
file.status = Dropzone.UPLOADING
@emit "processing", file
@emit "processingmultiple", files if @options.uploadMultiple
@uploadFiles files
_getFilesWithXhr: (xhr) -> files = (file for file in @files when file.xhr == xhr)
# Cancels the file upload and sets the status to CANCELED
# **if** the file is actually being uploaded.
# If it's still in the queue, the file is being removed from it and the status
# set to CANCELED.
cancelUpload: (file) ->
if file.status == Dropzone.UPLOADING
groupedFiles = @_getFilesWithXhr file.xhr
groupedFile.status = Dropzone.CANCELED for groupedFile in groupedFiles
file.xhr.abort()
@emit "canceled", groupedFile for groupedFile in groupedFiles
@emit "canceledmultiple", groupedFiles if @options.uploadMultiple
else if file.status in [ Dropzone.ADDED, Dropzone.QUEUED ]
file.status = Dropzone.CANCELED
@emit "canceled", file
@emit "canceledmultiple", [ file ] if @options.uploadMultiple
@processQueue() if @options.autoProcessQueue
# Wrapper for uploadFiles()
uploadFile: (file) -> @uploadFiles [ file ]