-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.smartuploader.coffee
376 lines (308 loc) · 10.9 KB
/
jquery.smartuploader.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
###
jQuery Smart Uploader Plugin 0.4.0
https://github.com/kofon95/smartuploader
Licensed under the MIT license:
http://www.opensource.org/licenses/MIT
###
$ = jQuery
DefaultOptions = {
url: null
action: null
multiUploading: true
ifWrongFile: "show"
maxFileSize: Number.POSITIVE_INFINITY
autoUpload: no
fileNameMatcher: /.*/ # /^[^\.].*\..*[^\.]$/ # .wrong; also.wrong.; good.txt
fileMimeTypeMatcher: /.*/
wrapperForInvalidFile: (fileIndex) ->
"<p>File: \"#{this.files[fileIndex].name}\" doesn't support</p>'"
validateEach: (fileIndex) -> true # should return Boolean
validateAll: (files) -> files # should return array of valid files
# formData: (fileIndex, blob, filename) -> # if single upload, it's called once
# formData: (blobs, filenames) -> # if multi upload, it's called for each file
# ~70 line
uploadBegin: (fileIndex, blob) ->
uploadEnd: (fileIndex, blob) ->
done: () ->
ajaxSettings: (settings, fileIndex, blob)->
}
ifWrongFileParams = ["nothing", "error", "show", "ignore"]
DropZoneError = (message) ->
this.message = message
DropZoneError.prototype = new Error
extract = (field, self, args) ->
if typeof field is "function"
field.apply(self, args)
else
field
empty = ->
$.fn.withDropZone = (dropZone, options) ->
if this.attr("type") isnt "file"
throw new DropZoneError("You should call this method only on input[type=file] and send dropZone as argument")
dropZone = $(dropZone) unless dropZone instanceof $
# settings up options
if options
for key, value of DefaultOptions
options[key] = value unless options.hasOwnProperty(key)
if !options.ifWrongFile
options.ifWrongFile = "show"
else if ifWrongFileParams.indexOf(options.ifWrongFile) < 0
throw new DropZoneError('"ifWrongFile" should has one of these values: "nothing", "error", "show", "ignore"')
if !options.formData
if options.multiUploading
options.formData = (fileIndex, blob, filename) ->
formData = new FormData
formData.set(this.name, blob, filename)
formData
else
options.formData = (blobs, filenames) ->
formData = new FormData
for i in [0...blobs.length]
formData.append(this.name, blobs[i], filenames[i])
formData
else
options = {}
#####################################
workers = []
fileInput = this.get(0)
dropZone
.on("dragenter", ->
dropZone.addClass('hover')
)
.on("dragleave", (e) ->
dropZone.removeClass('hover')
)
.on('dragover',(e) ->
e.preventDefault()
)
.on("drop", (e) ->
e.preventDefault()
fileInput.files = e.originalEvent.dataTransfer.files
uploadImageFiles(this, fileInput, workers, fileInput.files, options)
)
.on("click", -> fileInput.click())
this.on("change", ->
uploadImageFiles(dropZone[0], this, workers, this.files, options)
)
return {
upload: ->
for worker in workers
worker()
autoUpload: (value)->
return options.autoUpload unless value?
options.autoUpload = value
waitingToUploadCount: -> workers.length
}
# both dropZone and fileInput are instances of HTMLElement
uploadImageFiles = (dropZone, fileInput, workers, files, options) ->
workers.length = 0;
dropZone.classList.remove 'hover'
dropZone.classList.remove 'drop'
dropZone.classList.remove 'error'
previewContainer = dropZone.getElementsByClassName("preview-container")[0]
previewContainer.innerHTML = "" if previewContainer
return if files.length is 0
droppedFiles = []
# when user drops files, they doesn't care about "multiple"
unless fileInput.multiple
files = files.slice 0, 1
# if user drops files, "accept" of file input doesn't work
accept = (fileInput.accept || "")
.split(",")
.map (accept) -> "(#{accept.replace('*', '.*')})"
.join("|")
accept = new RegExp accept
# validation
files = options.validateAll.call(fileInput, file for file in files)
isValidFile = []
nameMatcher = options.fileNameMatcher
mimeTypeMatcher = options.fileMimeTypeMatcher
msize = options.maxFileSize
anyIsInvalid = false
for file in files
isValid = options.validateEach(file) and
accept.test(file.type) and
nameMatcher.test(file.name) and
mimeTypeMatcher.test(file.type) and
msize >= file.size
isValidFile.push(isValid)
anyIsInvalid = true unless isValid
if anyIsInvalid
if options.ifWrongFile is "error"
fileInput.value = null
dropZone.classList.add 'error'
return
if options.ifWrongFile is "nothing"
fileInput.value = null
return
dropZone.classList.add('drop')
if previewContainer
previewContainer.innerHTML = ""
else
previewContainer = document.createElement("div")
previewContainer.className = "preview-container";
dropZone.append(previewContainer)
multiUploading = options.multiUploading
blobs = [] # only for single uploading
filenames = new Array(files.length)
uploadedFilesCount = 0
ignore = options.ifWrongFile is "ignore"
for i in [0...files.length]
if !isValidFile[i] and ignore
continue
`
let file = files[i]
let fileIndex = i
let wrapper = document.createElement("div")
let preview
let progressBar
`
wrapper.className = "wrapper uploading"
previewContainer.append(wrapper)
unless isValidFile[i]
wrapper.classList.add "invalid"
wrapper.innerHTML = options.wrapperForInvalidFile.call(fileInput, fileIndex)
continue
wrapper.innerHTML = '''
<div class="preview"></div>
<div class="file-name"></div>
<div class="file-uploader-progress-bar">
<div class="progress"></div>
</div>
'''
kids = wrapper.children
preview = kids[0]
kids[1].textContent = file.name
progressBar = kids[2].children[0]
# ajax request
uploadNext = (blob)->
workers.push ->
process = (progress) ->
progressBar.style.width = 100 * progress.loaded / progress.total + "%"
if multiUploading
formDataResult = options.formData.call(fileInput, fileIndex, blob, filenames[fileIndex])
uploadQuery(options, formDataResult, filenames[fileIndex], fileInput, fileIndex, blob, process).done ->
wrapper.classList.remove "uploading"
options.uploadEnd.call(fileInput, filenames[fileIndex], fileIndex, blob)
if ++uploadedFilesCount is files.length
options.done.call(fileInput, filenames)
else
blobs.push(blob)
if ++uploadedFilesCount is files.length
formDataResult = options.formData.call(fileInput, blobs, filenames)
uploadQuery(options, formDataResult, filenames, fileInput, "(multiUploading must be true)", blobs, process).done ->
options.uploadEnd.call(fileInput, filenames, fileIndex, blob)
options.done.call(fileInput, filenames)
workers[workers.length-1]() if options.autoUpload
filenames[fileIndex] = file.name
actionOption = extract(options.action, fileInput, [i])
if actionOption
action = actions[actionOption.name]
if typeof action isnt "function"
unless actionOption.name?
throw new DropZoneError('Please, specify "name" in "action" block')
throw new DropZoneError("There'no action with name \"#{actionOption.name}\"")
if actionOption.rename
dotPos = file.name.indexOf(".")
if dotPos < 0
name = file.name
ext = ""
else
name = file.name.substr(0, dotPos)
ext = file.name.substr(dotPos)
filenames[fileIndex] = actionOption.rename.call(fileInput, name, ext, fileIndex)
if !actionOption.params
throw new DropZoneError('You should specify "params" in "action" option')
action(options, actionOption.params, preview, file, uploadNext)
else
uploadNext(file)
null # end function
uploadQuery = (options, formDataResult, filename, fileInput, fileIndex, blob, loadingProgressCallback) ->
options.uploadBegin.call(fileInput, filename, fileIndex, blob)
settings = {
method: "POST"
cache: false
contentType: false
processData: false
data: formDataResult
xhr: ->
xhr = new XMLHttpRequest
xhr.upload.onprogress = loadingProgressCallback
xhr
}
options.ajaxSettings.call(fileInput, settings, fileIndex, filename, blob)
settings.url = options.url unless settings.url
$.ajax(settings)
prettyJoin = (sequence, delimiter, lastDelimiter)->
if sequence.length is 0
return ""
if sequence.length is 1
return sequence[0]
result = []
for i in [0...sequence.length-2]
result.push sequence[i]
last = sequence[sequence.length-2] + lastDelimiter + sequence[sequence.length-1]
return result.join(delimiter) + last
imageAction = (options, params, preview, file, blobCallback) ->
convertTo = params.convertTo
img = new Image
canvas = document.createElement("canvas")
img.onload = (e) ->
if params.preview
preview.append(canvas)
w = this.width
h = this.height
if !convertTo
canvas.width = w
canvas.height = h
canvas.getContext("2d").drawImage(this, 0, 0, w, h)
blobCallback(file)
return
if convertTo.maxWidth or convertTo.maxHeight
maxWidth = convertTo.maxWidth || this.width
maxHeight = convertTo.maxHeight || this.height
if typeof maxWidth is "string"
maxWidth = Number(maxWidth)
if typeof maxHeight is "string"
maxHeight = Number(maxHeight)
if w > maxWidth
w = maxWidth
h *= w / this.width
if h > maxHeight
h = maxHeight
w = this.width * h / this.height
else if h > maxHeight
h = maxHeight
w *= h / this.height
if w > maxWidth
w = maxWidth
h = this.height * w / this.width
if convertTo.width and convertTo.width < maxWidth
w = convertTo.width
if convertTo.height and convertTo.height < maxHeight
h = convertTo.height
else
if convertTo.width
w = convertTo.width
if convertTo.height
h = convertTo.height
canvas.width = w
canvas.height = h
canvas.getContext("2d").drawImage(this, 0, 0, w, h)
quality = convertTo.qualityArgument
if (!quality or `quality == 1`) and
w is this.width and
h is this.height and
file.type isnt convertTo.mimeType
blobCallback(file) # no need to resize or convert
return
quality = if quality is 1 then 1.1 else Number(quality) # 1.1 - bug
canvas.toBlob(blobCallback, convertTo.mimeType || file.type, quality)
reader = new FileReader
reader.onloadend = (e) ->
img.src = e.target.result
reader.readAsDataURL(file)
actions = $.fn.withDropZone.actions = {
image: imageAction
# more actions are gonna be here
}