class UploadField { fieldContainer; isEditView; input; clearButton; deleteButton; editPreviewContainer; dropZone; options; form; checkConditionalState; constructor (fieldContainer, isEditView, checkConditionalState) { if (!fieldContainer) { return; } this.fieldContainer = fieldContainer; this.isEditView = isEditView ?? false; this.input = fieldContainer.querySelector('input[type="file"]'); this.clearButton = fieldContainer.querySelector('.clear-selection'); this.deleteButton = fieldContainer.querySelector('.deleteFile'); this.editPreviewContainer = fieldContainer.querySelector('.file-preview'); this.dropZone = fieldContainer.querySelector('.drop-zone'); this.fileNameHolder = fieldContainer.querySelector('.file-name-holder'); this.fileNameHolderP = fieldContainer.querySelector('.file-name-holder span'); this.form = fieldContainer.closest('form') this.checkConditionalState = checkConditionalState; this.handleImgFile = this.handleImgFile.bind(this); this.initUpload(); } initUpload(){ // hide dropzone in edit form view, if there is a stored uploaded file if (this.deleteButton && this.dropZone) { this.dropZone.classList.add('vishidden'); } // add event handler to deleteButton (edit form view) if (this.deleteButton) { if (this.input) { this.deleteButton.addEventListener('click', event => { event.stopPropagation(); if (this.deleteButton.checked) { this.input.disabled = false; this.input.removeAttribute('data-disabled'); this.input.removeAttribute('data-file-selected'); // add to validation this.input.classList.remove('ignore'); if (this.dropZone) { this.dropZone.classList.remove('vishidden'); } } else { // disable file upload input this.input.disabled = true; this.input.setAttribute('data-disabled', 'true'); this.input.setAttribute('data-file-selected', 'true'); // clear upload field this.input.value = null; if (this.dropZone) { this.dropZone.classList.add('vishidden'); } // force revalidation // upload fields have only 3 types of validation, file size, file type and required // after resetting the input to null, only the required validation matters // temporarily remove required validation const tmpRequired = this.input.required; if (tmpRequired) { this.input.required = false; this.input.removeAttribute('aria-required'); } // trigger revalidation jQuery(this.input).trigger('keyup'); // remove field from validation this.input.classList.add('ignore'); // add required validation again if (tmpRequired) { this.input.required = true; this.input.setAttribute('aria-required', 'true'); } } this.dispatchConditionalEvent(); }) } } if (this.input) { // add event handler to input // is also triggered if file selection in explorer is started from drop zone this.input.addEventListener('change', (event) => { if ((!(this.input.files.length === 0))) { this.input.setAttribute('data-file-selected', 'true'); this.handleImgFile(this.input.files[0]); // exists only, if upload with drag and drop is enabled if (this.fileNameHolder) { this.fileNameHolder.classList.remove('vishidden'); if (this.fileNameHolderP) { this.fileNameHolderP.textContent = this.input.files[0].name; } } this.showClearButton() } else { this.input.removeAttribute('data-file-selected'); if (this.fileNameHolder) { this.fileNameHolder.classList.add('vishidden'); if (this.fileNameHolderP) { this.fileNameHolderP.textContent = ''; } } this.hideClearButton() } // check conditional states uses the data-file-selected attribute in order to check if a file is selected // so we have dispatch it, after we have set the attribute this.dispatchConditionalEvent() // ToDo: Fix: To many error messages when image is resized // keyup event on jQuery element triggers validation if element is marked as invalid or as valid jQuery(this.input).trigger('keyup'); // Focus on jQuery element always trigger validation // jQuery(this.input).trigger('focus'); }) } // add click event handler to file input clear button (only available in form view) if (this.clearButton) { this.clearButton.classList.add('vishidden'); this.clearButton.addEventListener('click', (event) => { event.preventDefault(); if (this.input) { // file input value is cleared by setting it to null this.input.value = null; this.input.removeAttribute('data-file-selected'); this.dispatchConditionalEvent() // keyup event on jQuery element triggers validation if element is marked as invalid jQuery(this.input).trigger('keyup'); // hide the button event.target.classList.add('vishidden'); event.target.classList.remove('active'); } if (this.fileNameHolder) { this.fileNameHolder.classList.add('vishidden'); if (this.fileNameHolderP) { this.fileNameHolderP.textContent = ''; } } }) } if (this.dropZone) { this.dropZone.addEventListener('click', () => this.input.click()); this.dropZone.addEventListener('keydown', (e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault() this.input.click() } }) // make sure we only add the event handler once to the window and not for each upload field // remove listeners added by previous upload fields first window.removeEventListener('dragover', this.handleDefault) window.removeEventListener('drop', this.handleDefault) // add listener window.addEventListener("dragover", this.handleDefault); window.addEventListener("drop", this.handleDefault); this.dropZone.addEventListener("dragenter", (e) => { this.dropZone.classList.add('active-upload-dropzone'); this.handleDefault(e); }); this.dropZone.addEventListener("dragover", (e) => { this.dropZone.classList.add('active-upload-dropzone'); this.handleDefault(e); e.dataTransfer.dropEffect = "copy"; }); this.dropZone.addEventListener("drop", (e) => { this.dropZone.classList.remove('active-upload-dropzone'); if (e.dataTransfer.files.length > 0) { this.handleDefault(e); if (this.input) { this.input.setAttribute('data-file-selected', 'true'); // always set upload file into upload field this.input.files = e.dataTransfer.files; if (this.fileNameHolder) { this.fileNameHolder.classList.remove('vishidden'); } if (this.fileNameHolderP) { this.fileNameHolderP.textContent = e.dataTransfer.files[0].name; } // this.fileNameHolder.textContent = e.dataTransfer.files[0].name; // additional tasks, if the upload file is an image (like resize) // if the file is modified, this function sets the modified file into the upload field this.handleImgFile(e.dataTransfer.files[0]); // this.displayPreview(e.dataTransfer.files) this.showClearButton() // this.hideDropZone() // validate file // keyup event on jQuery element triggers validation if element is marked as invalid jQuery(this.input).trigger('keyup'); // focus event on jQuery element always triggers validation // jQuery(this.input).trigger('focus'); } } this.dispatchConditionalEvent() }) this.dropZone.addEventListener("dragleave", (e) => { this.handleDefault(e); this.dropZone.classList.remove('active-upload-dropzone'); }) } } displayPreview(files) { for (const file of files) { if (file.type.startsWith("image/")) { console.log(`[${file.type}] ${file.name}`); } } } handleImgFile(originalFile) { if (!originalFile || !this.input) { return; } const isImage = originalFile.type.startsWith('image'); if (!isImage) { return; } // get options from data attributes const {resizeImg, resizeImgMaxWidth, resizeImgMaxHeight} = this.input.dataset; // invalid options if (!resizeImg || !resizeImgMaxWidth || !resizeImgMaxHeight) { return; } let maxWidth = parseInt(resizeImgMaxWidth) let maxHeight = parseInt(resizeImgMaxHeight) // invalid options if (maxWidth < 0 || maxHeight < 0) { return; } if (maxWidth > 4096) { maxWidth = 4096; } if (maxHeight > 4096) { maxHeight = 4096; } const fileInput = this.input; const fileNameHolderP = this.fileNameHolderP; const img = document.createElement('img'); const canvas = document.createElement('canvas'); const reader = new FileReader(); reader.onload = function (e) { img.src = e.target.result img.onload = function () { if (img.width < maxWidth && img.height < maxHeight) { // Resize not required return; } const ratio = Math.min(maxWidth / img.width, maxHeight / img.height); const width = Math.round(img.width * ratio); const height = Math.round(img.height * ratio); canvas.width = width; canvas.height = height; const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0, width, height); canvas.toBlob(function (blob) { let resizedFile = new File([blob], originalFile.name, originalFile); let dataTransfer = new DataTransfer(); dataTransfer.items.add(resizedFile); fileInput.files = dataTransfer.files; if (fileNameHolderP) { fileNameHolderP.textContent = dataTransfer.files[0].name; } }, 'image/jpeg', 100); } } // process the file reader.readAsDataURL(originalFile); } showClearButton() { if (this.clearButton) { this.clearButton.classList.remove('vishidden'); this.clearButton.classList.add('active'); } } hideClearButton() { this.clearButton.classList.add('vishidden'); this.clearButton.classList.remove('active'); } handleDefault(event) { event.preventDefault(); event.stopPropagation(); } dispatchConditionalEvent() { if (!this.input) { return; } if (this.input.classList.contains('displayChanger')) { this.form.querySelectorAll('.conditional').forEach( element => { element.dispatchEvent(this.checkConditionalState); } ) } } } export {UploadField};