Skip to content

Commit 25872ab

Browse files
author
Neil Peyssard
committed
Add pre remove JS callback
1 parent 2e22c7f commit 25872ab

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

Form/Type/FileType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function configureOptions(OptionsResolver $resolver)
7575
'remove_uri_path' => $this->urlGenerator->generate('sherlockode_afb_remove'),
7676
'remove_tmp_uri_path' => $url = $this->urlGenerator->generate('sherlockode_afb_remove_tmp'),
7777
'js_callback' => null,
78+
'js_pre_remove_callback' => null,
7879
'js_error_callback' => null,
7980
'mapped' => function (Options $options) {
8081
return $options['upload_mode'] != 'immediate';
@@ -103,6 +104,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
103104
$view->vars['multiple'] = $isMultiple;
104105
$view->vars['async'] = $options['async'];
105106
$view->vars['jsCallback'] = $options['js_callback'];
107+
$view->vars['jsPreRemoveCallback'] = $options['js_pre_remove_callback'];
106108
$view->vars['jsErrorCallback'] = $options['js_error_callback'];
107109
$view->vars['subject'] = $subject;
108110
$view->vars['mapping'] = $options['mapping'];

Resources/doc/options_overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Options overview
1212
## Optional options
1313

1414
* `js_callback`: A string representing a JavaScript function name for a callback after an upload. `null` by default.
15+
* `js_pre_remove_callback`: A string representing a JavaScript function name for a callback before removing a file. `null` by default.
1516
* `js_error_callback`: A string representing a JavaScript function name for a callback after an error on upload. `null` by default.
1617
* `image_preview`: A boolean which determine the default callback after an upload. Set it to true if you want a preview for your pictures. `false` by default.
1718
* `remove_uri_path`: The url for removing files. It can be useful if you want to override the default controller.

Resources/public/js/ajax-uploader.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if (typeof module === "object" && module.exports) {
1111
let settings = $.extend({
1212
onXhrFail: baseOnXhrFail,
1313
uploadCallback: null,
14+
onPreRemove: null,
1415
}, options);
1516

1617
function baseOnXhrFail(jqXhr){
@@ -28,6 +29,7 @@ if (typeof module === "object" && module.exports) {
2829
return this.each(function(){
2930
var container = $(this),
3031
uploadCallback = container.data('callback') ? container.data('callback') : settings.uploadCallback,
32+
preRemoveCallback = container.data('preRemoveCallback'),
3133
ajaxErrorCallback = container.data('errorcallback'),
3234
isImgPreview = container.data('imgpreview'),
3335
uploadMode = container.data('uploadMode'),
@@ -43,6 +45,7 @@ if (typeof module === "object" && module.exports) {
4345
isAsync = container.data('async');
4446

4547
let onXhrFail = ajaxErrorCallback && "function" === typeof(window[ajaxErrorCallback]) ? window[ajaxErrorCallback] : settings.onXhrFail;
48+
let onPreRemove = preRemoveCallback && "function" === typeof(window[preRemoveCallback]) ? window[preRemoveCallback] : settings.onPreRemove;
4649

4750
function onDropFile(event) {
4851
var files = event.dataTransfer.files;
@@ -193,6 +196,16 @@ if (typeof module === "object" && module.exports) {
193196
}
194197
}
195198

199+
function triggerRemove(element, next) {
200+
if (onPreRemove && "function" === typeof(onPreRemove)) {
201+
onPreRemove.call(null, element, function() {
202+
return next();
203+
});
204+
} else {
205+
return next();
206+
}
207+
}
208+
196209
function removeFile(id, isTmp){
197210
var formData = new FormData();
198211
var url = removeUrl;
@@ -231,7 +244,10 @@ if (typeof module === "object" && module.exports) {
231244
});
232245
container.find('.afb_upload_container').on('click', '.afb_remove_file', function(event) {
233246
event.preventDefault();
234-
deletePreview($(this), true);
247+
let element = $(this);
248+
triggerRemove(element, function() {
249+
return deletePreview(element, true);
250+
});
235251
});
236252
});
237253
};

Resources/views/Form/upload_file.html.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
{% if jsCallback %}
3434
data-callback="{{ jsCallback|e }}"
3535
{% endif %}
36+
{% if jsPreRemoveCallback %}
37+
data-pre-remove-callback="{{ jsPreRemoveCallback|e }}"
38+
{% endif %}
3639
{% if jsErrorCallback %}
3740
data-errorcallback="{{ jsErrorCallback|e }}"
3841
{% endif %}

0 commit comments

Comments
 (0)