fix: Show img even if URL ending is unknown - let html handle the stuff

it knows

* Hide Preview if request is errored. EAFP.
* Set file attachment limit based on how many attachments have already
  been added. If 1 attachment exists and the limit is 3, File uploader
will allow you to select only 2 more files.
This commit is contained in:
Gavin D'souza 2022-06-08 12:05:06 +05:30
parent 56d97da32e
commit e22ab86651
2 changed files with 4 additions and 14 deletions

View file

@ -9,19 +9,9 @@ frappe.ui.form.on("File", "refresh", function(frm) {
}, "fa fa-download");
}
var wrapper = frm.get_field("preview_html").$wrapper;
var is_viewable = frappe.utils.is_image_file(frm.doc.file_url);
frm.toggle_display("preview", is_viewable);
frm.toggle_display("preview_html", is_viewable);
if(is_viewable){
wrapper.html('<div class="img_preview">\
<img class="img-responsive" src="'+frm.doc.file_url+'"></img>\
</div>');
} else {
wrapper.empty();
}
frm.get_field("preview_html").$wrapper.html(`<div class="img_preview">
<img class="img-responsive" src="${frm.doc.file_url}" onerror="cur_frm.toggle_display('preview', false)" />
</div>`);
var is_raster_image = (/\.(gif|jpg|jpeg|tiff|png)$/i).test(frm.doc.file_url);
var is_optimizable = !frm.doc.is_folder && is_raster_image && frm.doc.file_size > 0;

View file

@ -185,7 +185,7 @@ frappe.ui.form.Attachments = class Attachments {
this.attachment_uploaded(file_doc);
},
restrictions: {
max_number_of_files: this.frm.meta.max_attachments,
max_number_of_files: this.frm.meta.max_attachments - this.frm.attachments.get_attachments().length,
}
});
}