Skip to content

Commit d6ec905

Browse files
derrekbertrandlinkesch
authored andcommitted
deleteScript can now be a callback (linkesch#428)
* Delete Script can now be a callback * Fixed backwards compat issue
1 parent 63c865b commit d6ec905

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/js/images.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@
546546

547547
if (images.length) {
548548
for (i = 0; i < images.length; i++) {
549-
this.deleteFile(images[i].attr('src'));
549+
this.deleteFile(images[i].attr('src'), images[i]);
550550

551551
$parent = images[i].closest('.medium-insert-images');
552552
images[i].closest('figure').remove();
@@ -577,17 +577,25 @@
577577
/**
578578
* Makes ajax call to deleteScript
579579
*
580-
* @param {String} file File name
580+
* @param {string} file The name of the file to delete
581+
* @param {jQuery} $el The jQuery element of the file to delete
581582
* @returns {void}
582583
*/
583584

584-
Images.prototype.deleteFile = function (file) {
585+
Images.prototype.deleteFile = function (file, $el) {
586+
// only take action if there is a truthy value
585587
if (this.options.deleteScript) {
586-
$.ajax($.extend(true, {}, {
587-
url: this.options.deleteScript,
588-
type: this.options.deleteMethod || 'POST',
589-
data: { file: file }
590-
}, this.options.fileDeleteOptions));
588+
// try to run it as a callback
589+
if (typeof this.options.deleteScript === 'function') {
590+
this.options.deleteScript(file, $el);
591+
// otherwise, it's probably a string, call it as ajax
592+
} else {
593+
$.ajax($.extend(true, {}, {
594+
url: this.options.deleteScript,
595+
type: this.options.deleteMethod || 'POST',
596+
data: { file: file }
597+
}, this.options.fileDeleteOptions));
598+
}
591599
}
592600
};
593601

0 commit comments

Comments
 (0)