|
1 | 1 | /* |
2 | | - * jQuery File Upload User Interface Plugin 6.11 |
| 2 | + * jQuery File Upload User Interface Plugin 7.3 |
3 | 3 | * https://github.com/blueimp/jQuery-File-Upload |
4 | 4 | * |
5 | 5 | * Copyright 2010, Sebastian Tschan |
|
83 | 83 | // widget (via file input selection, drag & drop or add API call). |
84 | 84 | // See the basic file upload widget for more information: |
85 | 85 | add: function (e, data) { |
86 | | - var that = $(this).data('fileupload'), |
| 86 | + var that = $(this).data('blueimp-fileupload') || |
| 87 | + $(this).data('fileupload'), |
87 | 88 | options = that.options, |
88 | 89 | files = data.files; |
89 | 90 | $(this).fileupload('process', data).done(function () { |
|
94 | 95 | options.filesContainer[ |
95 | 96 | options.prependFiles ? 'prepend' : 'append' |
96 | 97 | ](data.context); |
97 | | - that._renderPreviews(files, data.context); |
| 98 | + that._renderPreviews(data); |
98 | 99 | that._forceReflow(data.context); |
99 | 100 | that._transition(data.context).done( |
100 | 101 | function () { |
|
109 | 110 | }, |
110 | 111 | // Callback for the start of each file upload request: |
111 | 112 | send: function (e, data) { |
112 | | - var that = $(this).data('fileupload'); |
| 113 | + var that = $(this).data('blueimp-fileupload') || |
| 114 | + $(this).data('fileupload'); |
113 | 115 | if (!data.isValidated) { |
114 | 116 | if (!data.maxNumberOfFilesAdjusted) { |
115 | 117 | that._adjustMaxNumberOfFiles(-data.files.length); |
|
138 | 140 | }, |
139 | 141 | // Callback for successful uploads: |
140 | 142 | done: function (e, data) { |
141 | | - var that = $(this).data('fileupload'), |
142 | | - template; |
| 143 | + var that = $(this).data('blueimp-fileupload') || |
| 144 | + $(this).data('fileupload'), |
| 145 | + files = that._getFilesFromResponse(data), |
| 146 | + template, |
| 147 | + deferred; |
143 | 148 | if (data.context) { |
144 | 149 | data.context.each(function (index) { |
145 | | - var file = ($.isArray(data.result) && |
146 | | - data.result[index]) || |
147 | | - {error: 'Empty file upload result'}; |
| 150 | + var file = files[index] || |
| 151 | + {error: 'Empty file upload result'}, |
| 152 | + deferred = that._addFinishedDeferreds(); |
148 | 153 | if (file.error) { |
149 | 154 | that._adjustMaxNumberOfFiles(1); |
150 | 155 | } |
|
158 | 163 | function () { |
159 | 164 | data.context = $(this); |
160 | 165 | that._trigger('completed', e, data); |
| 166 | + that._trigger('finished', e, data); |
| 167 | + deferred.resolve(); |
161 | 168 | } |
162 | 169 | ); |
163 | 170 | } |
164 | 171 | ); |
165 | 172 | }); |
166 | 173 | } else { |
167 | | - if ($.isArray(data.result)) { |
168 | | - $.each(data.result, function (index, file) { |
| 174 | + if (files.length) { |
| 175 | + $.each(files, function (index, file) { |
169 | 176 | if (data.maxNumberOfFilesAdjusted && file.error) { |
170 | 177 | that._adjustMaxNumberOfFiles(1); |
171 | 178 | } else if (!data.maxNumberOfFilesAdjusted && |
|
175 | 182 | }); |
176 | 183 | data.maxNumberOfFilesAdjusted = true; |
177 | 184 | } |
178 | | - template = that._renderDownload(data.result) |
| 185 | + template = that._renderDownload(files) |
179 | 186 | .appendTo(that.options.filesContainer); |
180 | 187 | that._forceReflow(template); |
| 188 | + deferred = that._addFinishedDeferreds(); |
181 | 189 | that._transition(template).done( |
182 | 190 | function () { |
183 | 191 | data.context = $(this); |
184 | 192 | that._trigger('completed', e, data); |
| 193 | + that._trigger('finished', e, data); |
| 194 | + deferred.resolve(); |
185 | 195 | } |
186 | 196 | ); |
187 | 197 | } |
188 | 198 | }, |
189 | 199 | // Callback for failed (abort or error) uploads: |
190 | 200 | fail: function (e, data) { |
191 | | - var that = $(this).data('fileupload'), |
192 | | - template; |
| 201 | + var that = $(this).data('blueimp-fileupload') || |
| 202 | + $(this).data('fileupload'), |
| 203 | + template, |
| 204 | + deferred; |
193 | 205 | if (data.maxNumberOfFilesAdjusted) { |
194 | 206 | that._adjustMaxNumberOfFiles(data.files.length); |
195 | 207 | } |
|
199 | 211 | var file = data.files[index]; |
200 | 212 | file.error = file.error || data.errorThrown || |
201 | 213 | true; |
| 214 | + deferred = that._addFinishedDeferreds(); |
202 | 215 | that._transition($(this)).done( |
203 | 216 | function () { |
204 | 217 | var node = $(this); |
|
209 | 222 | function () { |
210 | 223 | data.context = $(this); |
211 | 224 | that._trigger('failed', e, data); |
| 225 | + that._trigger('finished', e, data); |
| 226 | + deferred.resolve(); |
212 | 227 | } |
213 | 228 | ); |
214 | 229 | } |
215 | 230 | ); |
216 | 231 | } else { |
| 232 | + deferred = that._addFinishedDeferreds(); |
217 | 233 | that._transition($(this)).done( |
218 | 234 | function () { |
219 | 235 | $(this).remove(); |
220 | 236 | that._trigger('failed', e, data); |
| 237 | + that._trigger('finished', e, data); |
| 238 | + deferred.resolve(); |
221 | 239 | } |
222 | 240 | ); |
223 | 241 | } |
|
227 | 245 | .appendTo(that.options.filesContainer) |
228 | 246 | .data('data', data); |
229 | 247 | that._forceReflow(data.context); |
| 248 | + deferred = that._addFinishedDeferreds(); |
230 | 249 | that._transition(data.context).done( |
231 | 250 | function () { |
232 | 251 | data.context = $(this); |
233 | 252 | that._trigger('failed', e, data); |
| 253 | + that._trigger('finished', e, data); |
| 254 | + deferred.resolve(); |
234 | 255 | } |
235 | 256 | ); |
236 | 257 | } else { |
237 | 258 | that._trigger('failed', e, data); |
| 259 | + that._trigger('finished', e, data); |
| 260 | + that._addFinishedDeferreds().resolve(); |
238 | 261 | } |
239 | 262 | }, |
240 | 263 | // Callback for upload progress events: |
|
258 | 281 | .find('.progress-extended'); |
259 | 282 | if (extendedProgressNode.length) { |
260 | 283 | extendedProgressNode.html( |
261 | | - $this.data('fileupload')._renderExtendedProgress(data) |
| 284 | + ($this.data('blueimp-fileupload') || $this.data('fileupload')) |
| 285 | + ._renderExtendedProgress(data) |
262 | 286 | ); |
263 | 287 | } |
264 | 288 | globalProgressNode |
|
271 | 295 | }, |
272 | 296 | // Callback for uploads start, equivalent to the global ajaxStart event: |
273 | 297 | start: function (e) { |
274 | | - var that = $(this).data('fileupload'); |
| 298 | + var that = $(this).data('blueimp-fileupload') || |
| 299 | + $(this).data('fileupload'); |
| 300 | + that._resetFinishedDeferreds(); |
275 | 301 | that._transition($(this).find('.fileupload-progress')).done( |
276 | 302 | function () { |
277 | 303 | that._trigger('started', e); |
|
280 | 306 | }, |
281 | 307 | // Callback for uploads stop, equivalent to the global ajaxStop event: |
282 | 308 | stop: function (e) { |
283 | | - var that = $(this).data('fileupload'); |
| 309 | + var that = $(this).data('blueimp-fileupload') || |
| 310 | + $(this).data('fileupload'), |
| 311 | + deferred = that._addFinishedDeferreds(); |
| 312 | + $.when.apply($, that._getFinishedDeferreds()) |
| 313 | + .done(function () { |
| 314 | + that._trigger('stopped', e); |
| 315 | + }); |
284 | 316 | that._transition($(this).find('.fileupload-progress')).done( |
285 | 317 | function () { |
286 | 318 | $(this).find('.progress') |
287 | 319 | .attr('aria-valuenow', '0') |
288 | 320 | .find('.bar').css('width', '0%'); |
289 | 321 | $(this).find('.progress-extended').html(' '); |
290 | | - that._trigger('stopped', e); |
| 322 | + deferred.resolve(); |
291 | 323 | } |
292 | 324 | ); |
293 | 325 | }, |
294 | 326 | // Callback for file deletion: |
295 | 327 | destroy: function (e, data) { |
296 | | - var that = $(this).data('fileupload'); |
| 328 | + var that = $(this).data('blueimp-fileupload') || |
| 329 | + $(this).data('fileupload'); |
297 | 330 | if (data.url) { |
298 | 331 | $.ajax(data); |
299 | 332 | that._adjustMaxNumberOfFiles(1); |
|
307 | 340 | } |
308 | 341 | }, |
309 | 342 |
|
| 343 | + _resetFinishedDeferreds: function () { |
| 344 | + this._finishedUploads = []; |
| 345 | + }, |
| 346 | + |
| 347 | + _addFinishedDeferreds: function (deferred) { |
| 348 | + if (!deferred) { |
| 349 | + deferred = $.Deferred(); |
| 350 | + } |
| 351 | + this._finishedUploads.push(deferred); |
| 352 | + return deferred; |
| 353 | + }, |
| 354 | + |
| 355 | + _getFinishedDeferreds: function () { |
| 356 | + return this._finishedUploads; |
| 357 | + }, |
| 358 | + |
| 359 | + _getFilesFromResponse: function (data) { |
| 360 | + if (data.result && $.isArray(data.result.files)) { |
| 361 | + return data.result.files; |
| 362 | + } |
| 363 | + return []; |
| 364 | + }, |
| 365 | + |
310 | 366 | // Link handler, that allows to download files |
311 | 367 | // by drag & drop of the links to the desktop: |
312 | 368 | _enableDragToDesktop: function () { |
|
361 | 417 | if (bits >= 1000) { |
362 | 418 | return (bits / 1000).toFixed(2) + ' kbit/s'; |
363 | 419 | } |
364 | | - return bits + ' bit/s'; |
| 420 | + return bits.toFixed(2) + ' bit/s'; |
365 | 421 | }, |
366 | 422 |
|
367 | 423 | _formatTime: function (seconds) { |
|
472 | 528 | )) || dfd.resolveWith(node)) && dfd; |
473 | 529 | }, |
474 | 530 |
|
475 | | - _renderPreviews: function (files, nodes) { |
| 531 | + _renderPreviews: function (data) { |
476 | 532 | var that = this, |
477 | 533 | options = this.options; |
478 | | - nodes.find('.preview span').each(function (index, element) { |
479 | | - var file = files[index]; |
| 534 | + data.context.find('.preview span').each(function (index, element) { |
| 535 | + var file = data.files[index]; |
480 | 536 | if (options.previewSourceFileTypes.test(file.type) && |
481 | 537 | ($.type(options.previewSourceMaxFileSize) !== 'number' || |
482 | 538 | file.size < options.previewSourceMaxFileSize)) { |
483 | 539 | that._processingQueue = that._processingQueue.pipe(function () { |
484 | | - var dfd = $.Deferred(); |
| 540 | + var dfd = $.Deferred(), |
| 541 | + ev = $.Event('previewdone', { |
| 542 | + target: element |
| 543 | + }); |
485 | 544 | that._renderPreview(file, $(element)).done( |
486 | 545 | function () { |
| 546 | + that._trigger(ev.type, ev, data); |
487 | 547 | dfd.resolveWith(that); |
488 | 548 | } |
489 | 549 | ); |
|
691 | 751 | this._initRegExpOptions(); |
692 | 752 | }, |
693 | 753 |
|
| 754 | + _setOption: function (key, value) { |
| 755 | + this._super(key, value); |
| 756 | + if (key === 'maxNumberOfFiles') { |
| 757 | + this._adjustMaxNumberOfFiles(0); |
| 758 | + } |
| 759 | + }, |
| 760 | + |
694 | 761 | _create: function () { |
695 | 762 | this._super(); |
696 | 763 | this._refreshOptionsList.push( |
|
704 | 771 | return this._processingQueue; |
705 | 772 | }; |
706 | 773 | } |
| 774 | + this._resetFinishedDeferreds(); |
707 | 775 | }, |
708 | 776 |
|
709 | 777 | enable: function () { |
|
0 commit comments