Do not open preview for attachments that cannot be previewed

This commit is contained in:
Vid Smole 2023-08-03 18:57:12 +02:00
parent d0ba89d913
commit 21ffff3977
No known key found for this signature in database
GPG key ID: 85348A78371C84EB

View file

@ -40,7 +40,7 @@ Template.attachmentGallery.events({
}), }),
}); });
function getNextAttachmentId(currentAttachmentId) { function getNextAttachmentId(currentAttachmentId, offset = 0) {
const attachments = ReactiveCache.getAttachments({'meta.cardId': cardId}); const attachments = ReactiveCache.getAttachments({'meta.cardId': cardId});
let i = 0; let i = 0;
@ -49,10 +49,10 @@ function getNextAttachmentId(currentAttachmentId) {
break; break;
} }
} }
return attachments[(i + 1 + attachments.length) % attachments.length]._id; return attachments[(i + offset + 1 + attachments.length) % attachments.length]._id;
} }
function getPrevAttachmentId(currentAttachmentId) { function getPrevAttachmentId(currentAttachmentId, offset = 0) {
const attachments = ReactiveCache.getAttachments({'meta.cardId': cardId}); const attachments = ReactiveCache.getAttachments({'meta.cardId': cardId});
let i = 0; let i = 0;
@ -61,17 +61,34 @@ function getPrevAttachmentId(currentAttachmentId) {
break; break;
} }
} }
return attachments[(i - 1 + attachments.length) % attachments.length]._id; return attachments[(i + offset - 1 + attachments.length) % attachments.length]._id;
} }
function openAttachmentViewer(attachmentId){ function attachmentCanBeOpened(attachment) {
return (
attachment.isImage ||
attachment.isPDF ||
attachment.isText ||
attachment.isJSON ||
attachment.isVideo ||
attachment.isAudio
);
}
function openAttachmentViewer(attachmentId) {
const attachment = ReactiveCache.getAttachment(attachmentId); const attachment = ReactiveCache.getAttachment(attachmentId);
$("#attachment-name").text(attachment.name); // Check if we can open the attachment (if we have a viewer for it) and exit if not
if (!attachmentCanBeOpened(attachment)) {
return;
}
// IMPORTANT: if you ever add a new viewer, make sure you also implement /*
// cleanup in the closeAttachmentViewer() function Instructions for adding a new viewer:
- add a new case to the switch statement below
- implement cleanup in the closeAttachmentViewer() function, if necessary
- mark attachment type as openable by adding a new condition to the attachmentCanBeOpened function
*/
switch(true){ switch(true){
case (attachment.isImage): case (attachment.isImage):
$("#image-viewer").attr("src", attachment.link()); $("#image-viewer").attr("src", attachment.link());
@ -106,7 +123,8 @@ function openAttachmentViewer(attachmentId){
break; break;
} }
$("#viewer-overlay").removeClass("hidden"); $('#attachment-name').text(attachment.name);
$('#viewer-overlay').removeClass('hidden');
} }
function closeAttachmentViewer() { function closeAttachmentViewer() {
@ -152,16 +170,36 @@ Template.attachmentViewer.events({
closeAttachmentViewer(); closeAttachmentViewer();
}, },
'click #next-attachment'(event) { 'click #next-attachment'(event) {
closeAttachmentViewer() closeAttachmentViewer();
const id = getNextAttachmentId(openAttachmentId);
let i = 0;
// Find an attachment that can be opened
while (true) {
const id = getNextAttachmentId(openAttachmentId, i);
const attachment = ReactiveCache.getAttachment(id);
if (attachmentCanBeOpened(attachment)) {
openAttachmentId = id; openAttachmentId = id;
openAttachmentViewer(id); openAttachmentViewer(id);
break;
}
i++;
}
}, },
'click #prev-attachment'(event) { 'click #prev-attachment'(event) {
closeAttachmentViewer() closeAttachmentViewer();
const id = getPrevAttachmentId(openAttachmentId);
let i = 0;
// Find an attachment that can be opened
while (true) {
const id = getPrevAttachmentId(openAttachmentId, i);
const attachment = ReactiveCache.getAttachment(id);
if (attachmentCanBeOpened(attachment)) {
openAttachmentId = id; openAttachmentId = id;
openAttachmentViewer(id); openAttachmentViewer(id);
break;
}
i--;
}
} }
}); });