Simplify code to delete a cached attachment

We were already defining the links with data-remote and data-method, so
instead of manually doing an AJAX request we can rely on Rails to
perform the request and then handle the `ajax:complete` event.
This commit is contained in:
Javi Martín
2021-07-12 04:51:05 +02:00
parent 367d3f9d14
commit 6da8eeac6f
2 changed files with 10 additions and 57 deletions

View File

@@ -17,11 +17,11 @@
App.Documentable.lockUploads();
}
});
App.Documentable.initializeRemoveCachedDocumentLinks();
},
initializeDirectUploadInput: function(input) {
var inputData;
inputData = this.buildData([], input);
this.initializeRemoveCachedDocumentLink(input, inputData);
$(input).fileupload({
paramName: "attachment",
formData: null,
@@ -57,11 +57,6 @@
$(data.addAttachmentLabel).hide();
destroyAttachmentLink = $(data.result.destroy_link);
$(data.destroyAttachmentLinkContainer).html(destroyAttachmentLink);
$(destroyAttachmentLink).on("click", function(event) {
event.preventDefault();
event.stopPropagation();
App.Documentable.doDeleteCachedAttachmentRequest(this.href, data);
});
if (input.lockUpload) {
App.Documentable.showNotice();
}
@@ -76,7 +71,6 @@
buildData: function(data, input) {
var wrapper;
wrapper = $(input).closest(".direct-upload");
data.wrapper = wrapper;
data.progressBar = $(wrapper).find(".progress-bar-placeholder");
data.errorContainer = $(wrapper).find(".attachment-errors");
data.fileNameContainer = $(wrapper).find("p.file-name");
@@ -122,28 +116,10 @@
showNotice: function() {
$("#max-documents-notice").removeClass("hide");
},
doDeleteCachedAttachmentRequest: function(url, data) {
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: {
"_method": "delete"
},
complete: function() {
App.Documentable.unlockUploads();
$(data.wrapper).remove();
}
});
},
initializeRemoveCachedDocumentLink: function(input, data) {
var remove_document_link, wrapper;
wrapper = $(input).closest(".direct-upload");
remove_document_link = $(wrapper).find("a.remove-cached-attachment");
$(remove_document_link).on("click", function(e) {
e.preventDefault();
e.stopPropagation();
App.Documentable.doDeleteCachedAttachmentRequest(this.href, data);
initializeRemoveCachedDocumentLinks: function() {
$("#nested-documents").on("ajax:complete", "a.remove-cached-attachment", function() {
App.Documentable.unlockUploads();
$(this).closest(".direct-upload").remove();
});
},
removeDocument: function(id) {

View File

@@ -14,11 +14,11 @@
input = $(nested_image).find(".js-image-attachment");
App.Imageable.initializeDirectUploadInput(input);
});
App.Imageable.initializeRemoveCachedImageLinks();
},
initializeDirectUploadInput: function(input) {
var inputData;
inputData = this.buildData([], input);
this.initializeRemoveCachedImageLink(input, inputData);
$(input).fileupload({
paramName: "attachment",
formData: null,
@@ -56,11 +56,6 @@
App.Imageable.setPreview(data);
destroyAttachmentLink = $(data.result.destroy_link);
$(data.destroyAttachmentLinkContainer).html(destroyAttachmentLink);
$(destroyAttachmentLink).on("click", function(event) {
event.preventDefault();
event.stopPropagation();
App.Imageable.doDeleteCachedAttachmentRequest(this.href, data);
});
},
progress: function(e, data) {
var progress;
@@ -122,28 +117,10 @@
data.preview = $(data.wrapper).find(".image-preview");
}
},
doDeleteCachedAttachmentRequest: function(url, data) {
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: {
"_method": "delete"
},
complete: function() {
$("#new_image_link").removeClass("hide");
$(data.wrapper).remove();
}
});
},
initializeRemoveCachedImageLink: function(input, data) {
var remove_image_link, wrapper;
wrapper = $(input).closest(".direct-upload");
remove_image_link = $(wrapper).find("a.remove-cached-attachment");
$(remove_image_link).on("click", function(e) {
e.preventDefault();
e.stopPropagation();
App.Imageable.doDeleteCachedAttachmentRequest(this.href, data);
initializeRemoveCachedImageLinks: function() {
$("#nested-image").on("ajax:complete", "a.remove-cached-attachment", function() {
$("#new_image_link").removeClass("hide");
$(this).closest(".direct-upload").remove();
});
},
removeImage: function(id) {