Skip to content

Commit e8a712d

Browse files
committed
Fix #371: Ability to replace files in the preview. New autoReplace property.
1 parent eff5d11 commit e8a712d

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

CHANGE.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Change Log: `bootstrap-fileinput`
77

88
1. (enh #362): Add Bulgarian translations.
99
2. (bug #370): Reverts #342 with better fix.
10-
3. (enh #372): Create new event `filepreajax`.
10+
3. (enh #371): Ability to replace files in the preview. New `autoReplace` property.
11+
4. (enh #372): Create new event `filepreajax`.
1112

1213
## version 4.2.4
1314

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ _boolean_ whether to display the file upload cancel button. Defaults to `true`.
176176
### showUploadedThumbs
177177
_boolean_ whether to persist display of the uploaded file thumbnails in the preview window (for ajax uploads) until the remove/clear button is pressed. Defaults to `true`. When set to `false`, a next batch of files selected for upload will clear these thumbnails from preview.
178178

179+
### autoReplace
180+
_boolean_ whether to automatically replace the files in the preview after the `maxFileCount` limit is reached and a new set of file(s) is/are selected. This will only work if a valid `maxFileCount` is set. Defaults to `false`.
181+
179182
### captionClass
180183
_string_ any additional CSS class to append to the caption container.
181184

@@ -1270,7 +1273,7 @@ $('#input-id').on('fileunlock', function(event, filestack) {
12701273
```
12711274

12721275
#### filepreajax
1273-
This event is triggered before submission of the upload ajax request. You could use this event to manipulate the uploadExtraData before its submitted via ajax. The following additional parameters are also available but only if the upload is triggered via each thumbnail upload button.
1276+
This event is triggered before submission of the upload ajax request. You could use this event to manipulate the `uploadExtraData` before its submitted via ajax. The following additional parameters are also available specifically and only if the upload is triggered via each thumbnail upload button.
12741277

12751278
- `previewId`: the identifier of the preview thumbnail container.
12761279
- `index`: the zero-based index of the file in the preview container.

js/fileinput.js

+44-25
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,22 @@
11851185
self.initCaption();
11861186
}
11871187
},
1188+
resetPreviewThumbs: function (isAjax) {
1189+
var self = this, out;
1190+
if (isAjax) {
1191+
self.clearPreview();
1192+
self.filestack = [];
1193+
return;
1194+
}
1195+
if (self.hasInitialPreview()) {
1196+
out = previewCache.out(self.id);
1197+
self.$preview.html(out.content);
1198+
self.setCaption(out.caption);
1199+
self.initPreviewDeletes();
1200+
} else {
1201+
self.clearPreview();
1202+
}
1203+
},
11881204
reset: function () {
11891205
var self = this;
11901206
self.resetPreview();
@@ -1205,7 +1221,8 @@
12051221
self.raise('filedisabled');
12061222
self.$element.attr('disabled', 'disabled');
12071223
self.$container.find(".kv-fileinput-caption").addClass("file-caption-disabled");
1208-
self.$container.find(".btn-file, .fileinput-remove, .kv-fileinput-upload, .file-preview-frame button").attr("disabled", true);
1224+
self.$container.find(".btn-file, .fileinput-remove, .kv-fileinput-upload, .file-preview-frame button").attr("disabled",
1225+
true);
12091226
self.initDragDrop();
12101227
},
12111228
enable: function () {
@@ -1907,8 +1924,9 @@
19071924
self.fileInputCleared = false;
19081925
var tfiles, msg, total, $preview = self.$preview, isDragDrop = arguments.length > 1,
19091926
files = isDragDrop ? e.originalEvent.dataTransfer.files : $el.get(0).files,
1910-
isSingleUpload = isEmpty($el.attr('multiple')), i = 0, f, m, folders = 0,
1911-
ctr = self.filestack.length, isAjaxUpload = self.isUploadable,
1927+
isSingleUpload = isEmpty($el.attr('multiple')), i = 0, f, n, folders = 0,
1928+
ctr = self.filestack.length, isAjaxUpload = self.isUploadable, len,
1929+
flagSingle = (isSingleUpload && ctr > 0),
19121930
throwError = function (mesg, file, previewId, index) {
19131931
var p1 = $.extend(self.getOutData({}, {}, files), {id: previewId, index: index}),
19141932
p2 = {id: previewId, index: index, file: file, files: files};
@@ -1949,32 +1967,32 @@
19491967
return;
19501968
}
19511969
self.resetErrors();
1952-
total = self.isUploadable ? self.getFileStack().length + tfiles.length : tfiles.length;
1970+
len = tfiles.length;
1971+
total = self.isUploadable ? self.getFileStack().length + len : len;
19531972
if (self.maxFileCount > 0 && total > self.maxFileCount) {
1954-
msg = self.msgFilesTooMany.replace('{m}', self.maxFileCount).replace('{n}', total);
1955-
self.isError = throwError(msg, null, null, null);
1956-
self.$captionContainer.find('.kv-caption-icon').hide();
1957-
self.setCaption('', true);
1958-
self.setEllipsis();
1959-
self.$container.removeClass('file-input-new file-input-ajax-new');
1960-
return;
1961-
}
1962-
if (!isAjaxUpload || (isSingleUpload && ctr > 0)) {
1963-
if (self.hasInitialPreview()) {
1964-
var out = previewCache.out(self.id);
1965-
$preview.html(out.content);
1966-
self.setCaption(out.caption);
1967-
self.initPreviewDeletes();
1968-
} else {
1969-
self.clearPreview();
1973+
if (!self.autoReplace || len > self.maxFileCount) {
1974+
n = (self.autoReplace && len > self.maxFileCount) ? len : total;
1975+
msg = self.msgFilesTooMany.replace('{m}', self.maxFileCount).replace('{n}', n);
1976+
self.isError = throwError(msg, null, null, null);
1977+
self.$captionContainer.find('.kv-caption-icon').hide();
1978+
self.setCaption('', true);
1979+
self.setEllipsis();
1980+
self.$container.removeClass('file-input-new file-input-ajax-new');
1981+
return;
19701982
}
1971-
if (isSingleUpload && ctr > 0) {
1972-
self.filestack = [];
1983+
if (total > self.maxFileCount) {
1984+
self.resetPreviewThumbs(isAjaxUpload);
19731985
}
19741986
} else {
1975-
if (isAjaxUpload && ctr === 0 && (!previewCache.count(self.id) || self.overwriteInitial)) {
1976-
self.clearPreview();
1977-
self.filestack = [];
1987+
if (!isAjaxUpload || flagSingle) {
1988+
self.resetPreviewThumbs(false);
1989+
if (flagSingle) {
1990+
self.filestack = [];
1991+
}
1992+
} else {
1993+
if (isAjaxUpload && ctr === 0 && (!previewCache.count(self.id) || self.overwriteInitial)) {
1994+
self.resetPreviewThumbs(true);
1995+
}
19781996
}
19791997
}
19801998
if (self.isPreviewable) {
@@ -2168,6 +2186,7 @@
21682186
showUpload: true,
21692187
showCancel: true,
21702188
showUploadedThumbs: true,
2189+
autoReplace: false,
21712190
mainClass: '',
21722191
previewClass: '',
21732192
captionClass: '',

js/fileinput.min.js

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)