Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions assets/js/field-file-upload.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { toggleVisibilityClasses } from './helpers';

const eaFileUploadHandler = (event) => {
document.querySelectorAll('.ea-fileupload input[type="file"]').forEach((fileUploadElement) => {
const initFileUploadFields = (root) => {
root.querySelectorAll('.ea-fileupload input[type="file"]').forEach((fileUploadElement) => {
if (fileUploadElement.dataset.eaFileuploadInitialized === '1') {
return;
}
fileUploadElement.dataset.eaFileuploadInitialized = '1';
new FileUploadField(fileUploadElement);
});
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just store the FileUploadField so we can easily access it if needed?

Suggested change
};
const initFileUploadFields = (root) => {
root.querySelectorAll('.ea-fileupload input[type="file"]').forEach((fileUploadElement) => {
fileUploadElement.eaFileUploadField ??= new FileUploadField(fileUploadElement);
});
};


window.addEventListener('DOMContentLoaded', eaFileUploadHandler);
document.addEventListener('ea.collection.item-added', eaFileUploadHandler);
window.addEventListener('DOMContentLoaded', () => initFileUploadFields(document));
document.addEventListener('ea.collection.item-added', (event) => {
// only initialise the file upload fields contained in the newly added collection item:
// re-running the query on the whole document would re-attach listeners to already
// processed fields and double-fire their handlers (see #6637). The dataset flag
// already protects against double-init if `event.detail.newElement` is missing.
initFileUploadFields(event?.detail?.newElement ?? document);
});

class FileUploadField {
#fieldContainerElement;
Expand Down
Loading