Skip to content

Commit

Permalink
Show a toast message instead of submitting when a non-viewable file is
Browse files Browse the repository at this point in the history
double clicked.
  • Loading branch information
drgrice1 committed Oct 11, 2023
1 parent 5a38e76 commit 563f30e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
48 changes: 44 additions & 4 deletions htdocs/js/FileManager/filemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,47 @@

document.getElementsByName('directory')[0]?.addEventListener('change', () => doAction('Go'));
document.getElementsByName('dates')[0]?.addEventListener('click', () => doAction('Refresh'));
files?.addEventListener('dblclick', () => doAction('View'));
files?.addEventListener('dblclick', () => {
if (files.selectedOptions[0].dataset.type & 0b11010) doAction('View');
else {
const container = document.createElement('div');
container.classList.add('toast-container', 'top-50', 'start-50', 'translate-middle');

const toast = document.createElement('div');
toast.classList.add('toast');
toast.setAttribute('role', 'alert');
toast.setAttribute('aria-live', 'assertive');
toast.setAttribute('aria-atomit', 'true');
const toastContent = document.createElement('div');
toastContent.classList.add('d-flex', 'alert', 'alert-danger', 'mb-0', 'p-0');

const toastBody = document.createElement('div');
toastBody.classList.add('toast-body');
toastBody.textContent =
files.selectedOptions[0].dataset.type & 0b1
? files.dataset.linkMessage || 'Symbolic links can not be followed.'
: files.dataset.nonViewableMessage || 'This is not a viewable file type.';

const closeButton = document.createElement('button');
closeButton.type = 'button';
closeButton.classList.add('btn-close', 'me-2', 'm-auto');
closeButton.dataset.bsDismiss = 'toast';
closeButton.setAttribute('aria-label', files.dataset.closeTitle || 'Close');

toastContent.append(toastBody, closeButton);

toast.append(toastContent);
container.append(toast);
document.body.append(container);

const bsToast = new bootstrap.Toast(toast);
bsToast.show();
toast.addEventListener('hidden.bs.toast', () => {
bsToast.dispose();
container.remove();
});
}
});

// If on the confirmation page, then focus the "name" input.
form.querySelector('input[name="name"]')?.focus();
Expand All @@ -21,7 +61,7 @@
// The bits for types from least to most significant digit are set in the directoryListing method of
// lib/WeBWorK/ContentGenerator/Instructor/FileManager.pm to mean a file is a
// link, directory, regular file, text file, or image file.
const fileActionButtons = [
const fileActions = [
{ id: 'View', types: 0b11010, multiple: 0 },
{ id: 'Edit', types: 0b01000, multiple: 0 },
{ id: 'Download', types: 0b100, multiple: 0 },
Expand All @@ -30,13 +70,13 @@
{ id: 'Delete', types: 0b111, multiple: 1 },
{ id: 'MakeArchive', types: 0b111, multiple: 1 }
];
fileActionButtons.map((button) => (button.elt = document.getElementById(button.id)));
fileActions.map((button) => (button.elt = document.getElementById(button.id)));
const archiveButton = document.getElementById('MakeArchive');

const checkFiles = () => {
const selectedFiles = files.selectedOptions;

for (const button of fileActionButtons) {
for (const button of fileActions) {
if (!button.elt) continue;
if (selectedFiles.length) {
if (selectedFiles.length == 1 && !button.multiple)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@
<div class="row">
<div class="col-md-8 mb-2">
<%= select_field files => $files, id => 'files', class => 'form-select font-monospace h-100',
dir => 'ltr', size => 17, multiple => undef =%>
dir => 'ltr', size => 17, multiple => undef,
data => {
link_message => maketext('Symbolic links can not be followed.'),
non_viewable_message => maketext('This is not a viewable file type.'),
close_title => maketext('Close')
} =%>
</div>
<div class="col-md-4 mb-2">
<div class="d-flex flex-md-column flex-wrap flex-md-nowrap justify-content-evenly gap-1 gap-md-0">
Expand Down

0 comments on commit 563f30e

Please sign in to comment.