Skip to content

Commit

Permalink
Updates to release v4.3.9 fix #890 fix #893 fix #894 fix #895
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Mar 6, 2017
1 parent 20ca9e6 commit 9e7edc0
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 100 deletions.
5 changes: 4 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Change Log: `bootstrap-fileinput`

## version 4.3.9

**Date:** 02-Mar-2017 (_under development_)
**Date:** 06-Mar-2017 (_under development_)

1. (enh #863): New plugin method `zoom` with parameter `frameId` to allow custom triggering of zoomed preview for each thumbnail frame.
2. (enh #881): Update Spanish Translations.
Expand All @@ -14,6 +14,9 @@ Change Log: `bootstrap-fileinput`
7. Enhance events like `fileclear` and `filepreajax` to be aborted via `event.preventDefault()`.
8. (enh #887): New properties `msgUploadBegin` and `msgUploadEnd` to display a better progress status. The `layoutTemplates.progress` will support a new token `{status}`.
9. Enhance uploaded thumb frames to not reset or change the frame identifier after successful upload.
10. (bug #890): Fix doubling of images for async bulk uploads when initial preview is returned via ajax response.
11. (bug #893): Correct `file-success-remove` event handling.
12. (enh #894, #895): Correct file size validation for empty files.

## version 4.3.8

Expand Down
240 changes: 144 additions & 96 deletions js/fileinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
MODAL_ID: 'kvFileinputModal',
MODAL_EVENTS: ['show', 'shown', 'hide', 'hidden', 'loaded'],
objUrl: window.URL || window.webkitURL,
arrayFix: function(arr) {
var len = arr.length;
if (len === 0) {
return arr;
}
return arr.filter(function(_, i) {
return (i + 1) % 2 !== 0;
});
},
compare: function (input, str, exact) {
return input !== undefined && (exact ? input === str : input.match(str));
},
Expand Down Expand Up @@ -1424,94 +1433,6 @@
});
});
},
_initPreviewActions: function () {
var self = this, $preview = self.$preview, deleteExtraData = self.deleteExtraData || {},
btnRemove = $h.FRAMES + ' .kv-file-remove',
resetProgress = function () {
var hasFiles = self.isUploadable ? self.previewCache.count() : self.$element.get(0).files.length;
if ($preview.find(btnRemove).length === 0 && !hasFiles) {
self.reset();
self.initialCaption = '';
}
};
self._initZoomButton();
$preview.find(btnRemove).each(function () {
var $el = $(this), vUrl = $el.data('url') || self.deleteUrl, vKey = $el.data('key');
if ($h.isEmpty(vUrl) || vKey === undefined) {
return;
}
var $frame = $el.closest($h.FRAMES), cache = self.previewCache.data,
settings, params, index = $frame.data('fileindex'), config, extraData;
index = parseInt(index.replace('init_', ''));
config = $h.isEmpty(cache.config) && $h.isEmpty(cache.config[index]) ? null : cache.config[index];
extraData = $h.isEmpty(config) || $h.isEmpty(config.extra) ? deleteExtraData : config.extra;
if (typeof extraData === "function") {
extraData = extraData();
}
params = {id: $el.attr('id'), key: vKey, extra: extraData};
settings = $.extend(true, {}, {
url: vUrl,
type: 'POST',
dataType: 'json',
data: $.extend(true, {}, {key: vKey}, extraData),
beforeSend: function (jqXHR) {
self.ajaxAborted = false;
self._raise('filepredelete', [vKey, jqXHR, extraData]);
if (self.ajaxAborted) {
jqXHR.abort();
} else {
$h.addCss($frame, 'file-uploading');
$h.addCss($el, 'disabled');
}
},
success: function (data, textStatus, jqXHR) {
var n, cap;
if ($h.isEmpty(data) || $h.isEmpty(data.error)) {
self.previewCache.init();
index = parseInt(($frame.data('fileindex')).replace('init_', ''));
self.previewCache.unset(index);
n = self.previewCache.count();
cap = n > 0 ? self._getMsgSelected(n) : '';
self._raise('filedeleted', [vKey, jqXHR, extraData]);
self._setCaption(cap);
} else {
params.jqXHR = jqXHR;
params.response = data;
self._showError(data.error, params, 'filedeleteerror');
$frame.removeClass('file-uploading');
$el.removeClass('disabled');
resetProgress();
return;
}
$frame.removeClass('file-uploading').addClass('file-deleted');
$frame.fadeOut('slow', function () {
$h.cleanZoomCache($preview.find('#zoom-' + $frame.attr('id')));
self._clearObjects($frame);
$frame.remove();
resetProgress();
if (!n && self.getFileStack().length === 0) {
self._setCaption('');
self.reset();
}
});
},
error: function (jqXHR, textStatus, errorThrown) {
var op = self.ajaxOperations.deleteThumb, errMsg = self._parseError(op, jqXHR, errorThrown);
params.jqXHR = jqXHR;
params.response = {};
self._showError(errMsg, params, 'filedeleteerror');
$frame.removeClass('file-uploading');
resetProgress();
}
}, self.ajaxDeleteSettings);
self._handler($el, 'click', function () {
if (!self._validateMinCount()) {
return false;
}
$.ajax(settings);
});
});
},
_clearObjects: function ($el) {
$el.find('video audio').each(function () {
this.pause();
Expand Down Expand Up @@ -1775,7 +1696,7 @@
if (!self.showPreview) {
return;
}
self._getThumbs($h.FRAMES + ' .file-preview-success').each(function () {
self._getThumbs($h.FRAMES + '.file-preview-success').each(function () {
var $thumb = $(this), $preview = self.$preview, $remove = $thumb.find('.kv-file-remove');
$remove.removeAttr('disabled');
self._handler($remove, 'click', function () {
Expand Down Expand Up @@ -1839,14 +1760,20 @@
}
};
chkComplete = function () {
var u = self.uploadCache, $initThumbs, i, j, len, data = self.cacheInitialPreview;
var u = self.uploadCache, $initThumbs, i, j, len = 0, data = self.cacheInitialPreview;
if (data && data.content) {
data.content = $h.arrayFix(data.content || []);
data.config = $h.arrayFix(data.config || []);
data.tags = $h.arrayFix(data.tags || []);
len = data.content.length;
self.cacheInitialPreview = data;
}
if (!self.fileBatchCompleted) {
return;
}
setTimeout(function () {
if (self.showPreview) {
self.previewCache.set(u.content, u.config, u.tags, u.append);
len = data && data.content ? data.content.length : 0;
if (len) {
for (i = 0; i < u.content.length; i++) {
j = i + len;
Expand Down Expand Up @@ -2125,6 +2052,34 @@
};
self._ajaxSubmit(fnBefore, fnSuccess, fnComplete, fnError);
},
_deleteFileIndex: function($frame) {
var self = this, ind = $frame.attr('data-fileindex');
if (ind.substring(0, 5) === 'init_') {
ind = parseInt(ind.replace('init_', ''));
self.initialPreview.splice(ind, 1);
self.initialPreviewConfig.splice(ind, 1);
if (self.initialPreviewThumbTags[ind]) {
self.initialPreviewThumbTags.splice(ind, 1);
}
self.$preview.find($h.FRAMES).each(function() {
var $nFrame = $(this), nInd = $nFrame.attr('data-fileindex');
if (nInd.substring(0, 5) === 'init_') {
nInd = parseInt(nInd.replace('init_', ''));
if (nInd > ind) {
nInd--;
$nFrame.attr('data-fileindex', 'init_' + nInd);
}
}
});
if (self.uploadAsync) {
self.cacheInitialPreview = {
content: self.initialPreview,
config: self.initialPreviewConfig,
tags: self.initialPreviewThumbTags
};
}
}
},
_initFileActions: function () {
var self = this, $preview = self.$preview;
if (!self.showPreview) {
Expand Down Expand Up @@ -2179,6 +2134,95 @@
});
});
},
_initPreviewActions: function () {
var self = this, $preview = self.$preview, deleteExtraData = self.deleteExtraData || {},
btnRemove = $h.FRAMES + ' .kv-file-remove',
resetProgress = function () {
var hasFiles = self.isUploadable ? self.previewCache.count() : self.$element.get(0).files.length;
if ($preview.find(btnRemove).length === 0 && !hasFiles) {
self.reset();
self.initialCaption = '';
}
};
self._initZoomButton();
$preview.find(btnRemove).each(function () {
var $el = $(this), vUrl = $el.data('url') || self.deleteUrl, vKey = $el.data('key');
if ($h.isEmpty(vUrl) || vKey === undefined) {
return;
}
var $frame = $el.closest($h.FRAMES), cache = self.previewCache.data,
settings, params, index = $frame.data('fileindex'), config, extraData;
index = parseInt(index.replace('init_', ''));
config = $h.isEmpty(cache.config) && $h.isEmpty(cache.config[index]) ? null : cache.config[index];
extraData = $h.isEmpty(config) || $h.isEmpty(config.extra) ? deleteExtraData : config.extra;
if (typeof extraData === "function") {
extraData = extraData();
}
params = {id: $el.attr('id'), key: vKey, extra: extraData};
settings = $.extend(true, {}, {
url: vUrl,
type: 'POST',
dataType: 'json',
data: $.extend(true, {}, {key: vKey}, extraData),
beforeSend: function (jqXHR) {
self.ajaxAborted = false;
self._raise('filepredelete', [vKey, jqXHR, extraData]);
if (self.ajaxAborted) {
jqXHR.abort();
} else {
$h.addCss($frame, 'file-uploading');
$h.addCss($el, 'disabled');
}
},
success: function (data, textStatus, jqXHR) {
var n, cap;
if ($h.isEmpty(data) || $h.isEmpty(data.error)) {
self.previewCache.init();
index = parseInt(($frame.data('fileindex')).replace('init_', ''));
self.previewCache.unset(index);
n = self.previewCache.count();
cap = n > 0 ? self._getMsgSelected(n) : '';
self._raise('filedeleted', [vKey, jqXHR, extraData]);
self._setCaption(cap);
self._deleteFileIndex($frame);
} else {
params.jqXHR = jqXHR;
params.response = data;
self._showError(data.error, params, 'filedeleteerror');
$frame.removeClass('file-uploading');
$el.removeClass('disabled');
resetProgress();
return;
}
$frame.removeClass('file-uploading').addClass('file-deleted');
$frame.fadeOut('slow', function () {
$h.cleanZoomCache($preview.find('#zoom-' + $frame.attr('id')));
self._clearObjects($frame);
$frame.remove();
resetProgress();
if (!n && self.getFileStack().length === 0) {
self._setCaption('');
self.reset();
}
});
},
error: function (jqXHR, textStatus, errorThrown) {
var op = self.ajaxOperations.deleteThumb, errMsg = self._parseError(op, jqXHR, errorThrown);
params.jqXHR = jqXHR;
params.response = {};
self._showError(errMsg, params, 'filedeleteerror');
$frame.removeClass('file-uploading');
resetProgress();
}
}, self.ajaxDeleteSettings);
self._handler($el, 'click', function () {
if (!self._validateMinCount()) {
return false;
}
$.ajax(settings);
});
});
},
_hideFileIcon: function () {
if (this.overwriteInitial) {
this.$captionContainer.find('.kv-caption-icon').hide();
Expand All @@ -2195,9 +2239,13 @@
if (typeof func === 'function') {
out = func(size);
} else {
i = Math.floor(Math.log(size) / Math.log(1024));
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
out = (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + sizes[i];
if (size === 0) {
out = '0.00 B';
} else {
i = Math.floor(Math.log(size) / Math.log(1024));
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
out = (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + sizes[i];
}
}
return self._getLayoutTemplate('size').replace('{sizeText}', out);
},
Expand Down Expand Up @@ -2240,7 +2288,7 @@
if (!self.showPreview) {
return;
}
var fname = file ? file.name : '', ftype = file ? file.type : '', content, size = file.size,
var fname = file ? file.name : '', ftype = file ? file.type : '', content, size = file.size || 0,
caption = self.slug(fname), isError = isDisabled === true && !self.isUploadable,
data = $h.objUrl.createObjectURL(file);
self._clearDefaultPreview();
Expand All @@ -2257,7 +2305,7 @@
}
var self = this, cat = self._parseFileType(file), fname = file ? file.name : '', caption = self.slug(fname),
types = self.allowedPreviewTypes, mimes = self.allowedPreviewMimeTypes, $preview = self.$preview,
chkTypes = types && types.indexOf(cat) >= 0, size = file.size,
chkTypes = types && types.indexOf(cat) >= 0, size = file.size || 0,
iData = (cat === 'text' || cat === 'html' || cat === 'image') ? theFile.target.result : data, content,
chkMimes = mimes && mimes.indexOf(file.type) !== -1;
/** @namespace window.DOMPurify */
Expand Down
6 changes: 3 additions & 3 deletions js/fileinput.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 9e7edc0

Please sign in to comment.