Merge pull request #5058 from VidVidex/master

Do not open preview for attachments that cannot be previewed
This commit is contained in:
Lauri Ojansivu 2023-08-03 20:12:05 +03:00 committed by GitHub
commit 8f8c045930
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -40,7 +40,7 @@ Template.attachmentGallery.events({
}),
});
function getNextAttachmentId(currentAttachmentId) {
function getNextAttachmentId(currentAttachmentId, offset = 0) {
const attachments = ReactiveCache.getAttachments({'meta.cardId': cardId});
let i = 0;
@ -49,10 +49,10 @@ function getNextAttachmentId(currentAttachmentId) {
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});
let i = 0;
@ -61,17 +61,34 @@ function getPrevAttachmentId(currentAttachmentId) {
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);
$("#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){
case (attachment.isImage):
$("#image-viewer").attr("src", attachment.link());
@ -106,7 +123,8 @@ function openAttachmentViewer(attachmentId){
break;
}
$("#viewer-overlay").removeClass("hidden");
$('#attachment-name').text(attachment.name);
$('#viewer-overlay').removeClass('hidden');
}
function closeAttachmentViewer() {
@ -152,16 +170,36 @@ Template.attachmentViewer.events({
closeAttachmentViewer();
},
'click #next-attachment'(event) {
closeAttachmentViewer()
const id = getNextAttachmentId(openAttachmentId);
closeAttachmentViewer();
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;
openAttachmentViewer(id);
break;
}
i++;
}
},
'click #prev-attachment'(event) {
closeAttachmentViewer()
const id = getPrevAttachmentId(openAttachmentId);
closeAttachmentViewer();
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;
openAttachmentViewer(id);
break;
}
i--;
}
}
});