Skip to content

Commit 276de2e

Browse files
committed
Make the Image Processing plugin as well as the LoadImage and Templates functions optional dependencies.
Added the filesContainer option. Allow setting the uploadTemplateId and downloadTemplateId options after widget initialization.
1 parent 0eeaac1 commit 276de2e

File tree

1 file changed

+70
-46
lines changed

1 file changed

+70
-46
lines changed

js/jquery.fileupload-ui.js

+70-46
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload User Interface Plugin 6.5.5
2+
* jQuery File Upload User Interface Plugin 6.6
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -33,9 +33,10 @@
3333
}(function ($, tmpl, loadImage) {
3434
'use strict';
3535

36-
// The UI version extends the IP (image processing) version and adds
37-
// complete user interface interaction:
38-
$.widget('blueimpUI.fileupload', $.blueimpIP.fileupload, {
36+
// The UI version extends the IP (image processing) version or the basic
37+
// file upload widget and adds complete user interface interaction:
38+
var parentWidget = ($.blueimpIP || $.blueimp).fileupload;
39+
$.widget('blueimpUI.fileupload', parentWidget, {
3940

4041
options: {
4142
// By default, files added to the widget are uploaded as soon
@@ -85,11 +86,10 @@
8586
$(this).fileupload('resize', data).done(data, function () {
8687
data.files.valid = data.isValidated = that._validate(files);
8788
data.context = that._renderUpload(files)
88-
.appendTo(that._files)
89+
.appendTo(options.filesContainer)
8990
.data('data', data);
9091
that._renderPreviews(files, data.context);
91-
// Force reflow:
92-
that._reflow = $.support.transition && data.context[0].offsetWidth;
92+
that._forceReflow(data.context);
9393
that._transition(data.context).done(
9494
function () {
9595
if ((that._trigger('added', e, data) !== false) &&
@@ -146,9 +146,7 @@
146146
template = that._renderDownload([file])
147147
.css('height', node.height())
148148
.replaceAll(node);
149-
// Force reflow:
150-
that._reflow = $.support.transition &&
151-
template[0].offsetWidth;
149+
that._forceReflow(template);
152150
that._transition(template).done(
153151
function () {
154152
data.context = $(this);
@@ -160,9 +158,8 @@
160158
});
161159
} else {
162160
template = that._renderDownload(data.result)
163-
.appendTo(that._files);
164-
// Force reflow:
165-
that._reflow = $.support.transition && template[0].offsetWidth;
161+
.appendTo(that.options.filesContainer);
162+
that._forceReflow(template);
166163
that._transition(template).done(
167164
function () {
168165
data.context = $(this);
@@ -187,9 +184,7 @@
187184
var node = $(this);
188185
template = that._renderDownload([file])
189186
.replaceAll(node);
190-
// Force reflow:
191-
that._reflow = $.support.transition &&
192-
template[0].offsetWidth;
187+
that._forceReflow(template);
193188
that._transition(template).done(
194189
function () {
195190
data.context = $(this);
@@ -210,10 +205,9 @@
210205
} else if (data.errorThrown !== 'abort') {
211206
that._adjustMaxNumberOfFiles(-data.files.length);
212207
data.context = that._renderUpload(data.files)
213-
.appendTo(that._files)
208+
.appendTo(that.options.filesContainer)
214209
.data('data', data);
215-
// Force reflow:
216-
that._reflow = $.support.transition && data.context[0].offsetWidth;
210+
that._forceReflow(data.context);
217211
that._transition(data.context).done(
218212
function () {
219213
data.context = $(this);
@@ -357,24 +351,29 @@
357351
},
358352

359353
_renderTemplate: function (func, files) {
360-
return $(this.options.templateContainer).html(func({
354+
if (!func) {
355+
return $();
356+
}
357+
var result = func({
361358
files: files,
362359
formatFileSize: this._formatFileSize,
363360
options: this.options
364-
})).children();
361+
});
362+
if (result instanceof $) {
363+
return result;
364+
}
365+
return $(this.options.templatesContainer).html(result).children();
365366
},
366367

367368
_renderPreview: function (file, node) {
368369
var that = this,
369370
options = this.options,
370371
deferred = $.Deferred();
371-
return (loadImage(
372+
return ((loadImage && loadImage(
372373
file,
373374
function (img) {
374375
node.append(img);
375-
// Force reflow:
376-
that._reflow = $.support.transition &&
377-
node[0].offsetWidth;
376+
that._forceReflow(node);
378377
that._transition(node).done(function () {
379378
deferred.resolveWith(node);
380379
});
@@ -384,7 +383,7 @@
384383
maxHeight: options.previewMaxHeight,
385384
canvas: options.previewAsCanvas
386385
}
387-
) || deferred.resolveWith(node)) && deferred;
386+
)) || deferred.resolveWith(node)) && deferred;
388387
},
389388

390389
_renderPreviews: function (files, nodes) {
@@ -451,11 +450,16 @@
451450
e.data.fileupload._trigger('destroy', e, {
452451
context: button.closest('.template-download'),
453452
url: button.attr('data-url'),
454-
type: button.attr('data-type'),
453+
type: button.attr('data-type') || 'DELETE',
455454
dataType: e.data.fileupload.options.dataType
456455
});
457456
},
458457

458+
_forceReflow: function (node) {
459+
this._reflow = $.support.transition &&
460+
node.length && node[0].offsetWidth;
461+
},
462+
459463
_transition: function (node) {
460464
var that = this,
461465
deferred = $.Deferred();
@@ -480,7 +484,7 @@
480484

481485
_initButtonBarEventHandlers: function () {
482486
var fileUploadButtonBar = this.element.find('.fileupload-buttonbar'),
483-
filesList = this._files,
487+
filesList = this.options.filesContainer,
484488
ns = this.options.namespace;
485489
fileUploadButtonBar.find('.start')
486490
.bind('click.' + ns, function (e) {
@@ -517,9 +521,9 @@
517521
},
518522

519523
_initEventHandlers: function () {
520-
$.blueimpIP.fileupload.prototype._initEventHandlers.call(this);
524+
parentWidget.prototype._initEventHandlers.call(this);
521525
var eventData = {fileupload: this};
522-
this._files
526+
this.options.filesContainer
523527
.delegate(
524528
'.start button',
525529
'click.' + this.options.namespace,
@@ -542,12 +546,13 @@
542546
},
543547

544548
_destroyEventHandlers: function () {
549+
var options = this.options;
545550
this._destroyButtonBarEventHandlers();
546-
this._files
547-
.undelegate('.start button', 'click.' + this.options.namespace)
548-
.undelegate('.cancel button', 'click.' + this.options.namespace)
549-
.undelegate('.delete button', 'click.' + this.options.namespace);
550-
$.blueimpIP.fileupload.prototype._destroyEventHandlers.call(this);
551+
options.filesContainer
552+
.undelegate('.start button', 'click.' + options.namespace)
553+
.undelegate('.cancel button', 'click.' + options.namespace)
554+
.undelegate('.delete button', 'click.' + options.namespace);
555+
parentWidget.prototype._destroyEventHandlers.call(this);
551556
},
552557

553558
_enableFileInputButton: function () {
@@ -564,33 +569,52 @@
564569

565570
_initTemplates: function () {
566571
var options = this.options;
567-
options.templateContainer = document.createElement(
568-
this._files.prop('nodeName')
572+
options.templatesContainer = document.createElement(
573+
options.filesContainer.prop('nodeName')
569574
);
570-
options.uploadTemplate = tmpl(options.uploadTemplateId);
571-
options.downloadTemplate = tmpl(options.downloadTemplateId);
575+
if (tmpl) {
576+
options.uploadTemplate = tmpl(options.uploadTemplateId);
577+
options.downloadTemplate = tmpl(options.downloadTemplateId);
578+
}
572579
},
573580

574-
_initFiles: function () {
575-
this._files = this.element.find('.files');
581+
_initFilesContainer: function () {
582+
if (!this.options.filesContainer) {
583+
this.options.filesContainer = this.element.find('.files');
584+
}
576585
},
577586

578-
_create: function () {
579-
this._initFiles();
580-
$.blueimpIP.fileupload.prototype._create.call(this);
587+
_initSpecialOptions: function () {
588+
parentWidget.prototype._initSpecialOptions.call(this);
581589
this._initTemplates();
582590
},
583591

592+
_create: function () {
593+
this._initFilesContainer();
594+
parentWidget.prototype._create.call(this);
595+
this._refreshOptionsList.push(
596+
'filesContainer',
597+
'uploadTemplateId',
598+
'downloadTemplateId'
599+
);
600+
if (!$.blueimpIP) {
601+
this._processingQueue = $.Deferred().resolveWith(this).promise();
602+
this.resize = function () {
603+
return this._processingQueue;
604+
};
605+
}
606+
},
607+
584608
enable: function () {
585-
$.blueimpIP.fileupload.prototype.enable.call(this);
609+
parentWidget.prototype.enable.call(this);
586610
this.element.find('input, button').prop('disabled', false);
587611
this._enableFileInputButton();
588612
},
589613

590614
disable: function () {
591615
this.element.find('input, button').prop('disabled', true);
592616
this._disableFileInputButton();
593-
$.blueimpIP.fileupload.prototype.disable.call(this);
617+
parentWidget.prototype.disable.call(this);
594618
}
595619

596620
});

0 commit comments

Comments
 (0)