class MultipleUploader { #multipleUploader; #$imagesUploadInput; constructor( multiUploaderSelector ) { this.#multipleUploader = document.querySelector(multiUploaderSelector); this.#$imagesUploadInput = document.createElement('input') } init( { maxUpload = 10 , maxSize = 2 , formSelector = 'form' , filesInpName = 'images' } = {} ) { const form = document.querySelector(formSelector); if (! this.#multipleUploader ) // check if the end user didnt write the multiple uploader div throw new Error('The multiple uploader element doesnt exist'); if (! form ) // check if there is no form with this selector throw new Error('We couldn\'t find a form with this selector: ' + formSelector); // ensure that the form has enctype attribute with the value multipart/form-data form.enctype = 'multipart/form-data' if ( document.getElementById('max-upload-number') ) document.getElementById('max-upload-number').innerHTML = `Upload up to ${ maxUpload } files`; // create multiple file input and make it hidden this.#$imagesUploadInput.type = 'file'; this.#$imagesUploadInput.name = `${filesInpName}[]`; this.#$imagesUploadInput.multiple = true; this.#$imagesUploadInput.accept = "image/*"; this.#$imagesUploadInput.style.setProperty('display','none','important'); // create multiple file input and make it hidden // append the newly created input to the form with the help of the formSelector provided by the user document.querySelector(formSelector).append( this.#$imagesUploadInput ); this.#multipleUploader.addEventListener("click", (e) => { if ( e.target.className === 'multiple-uploader' || e.target.className === 'mup-msg' || e.target.className === 'mup-main-msg' ) this.#$imagesUploadInput.click() // trigger the input file to upload images }); const self = this; // preview the uploaded images this.#$imagesUploadInput.addEventListener("change",function () { if (this.files.length > 0) { self.#multipleUploader.querySelectorAll('.image-container').forEach( image => image.remove() ); // clear the previous rendered images self.#multipleUploader.querySelector('.mup-msg').style.setProperty('display', 'none'); // hide the hint texts inside drop zone // if the length of uploaded images greater than the images uploaded by the user, the maximum uploaded will be considered const uploadedImagesCount = this.files.length > maxUpload ? maxUpload : this.files.length; const unAcceptableImagesIndices = []; for (let index = 0; index < uploadedImagesCount; index++) { const imageSize = self.#bytesToSize( this.files[ index ].size ); const isImageSizeAcceptable = self.#checkImageSize( index , imageSize , maxSize , 'MB' ); // appended the newly created image to the multiple uploader self.#multipleUploader.innerHTML += `