{"version":3,"file":"custom-multifile.min.js","sources":["custom-multifile.js"],"sourcesContent":["AlectaFastigheter = window.AlectaFastigheter || {};\r\n\r\n/**\r\n * Provides functionality for selecting and uploading multiple files.\r\n * @namespace AlectaFastigheter.CustomMultifile\r\n */\r\nAlectaFastigheter.CustomMultifile = (function (document) {\r\n\r\n const uploadedFiles = [];\r\n const TEXT_FILE_TYPE = 'Filen kan inte bifogas, godkända filtyper är doc docx rtf xls xlsx csv txt pdf jpg jpeg png gif.'\r\n\r\n /**\r\n * Initializes the multifile component by adding event listeners to file input elements.\r\n */\r\n const init = (el) => {\r\n\r\n clearFiles()\r\n if (!el) {\r\n const elements = document.querySelectorAll(\r\n '[data-fn-trigger=\"multifile\"]'\r\n );\r\n\r\n elements.forEach((elm) => {\r\n handleFileInput(elm);\r\n });\r\n } else {\r\n handleFileInput(el);\r\n }\r\n };\r\n\r\n /**\r\n * Sets the ID and \"for\" attribute of the file input and label elements, respectively.\r\n * @param {HTMLElement} el - The container element that holds the file input and label elements.\r\n * @param {string} id - The ID to use for the file input element.\r\n */\r\n const setFileInputAttributes = (el, id) => {\r\n const fileInput = el.querySelector('input[type=\"file\"]');\r\n const fileLabel = el.querySelector('label');\r\n\r\n fileInput.setAttribute('id', `${id}-input`);\r\n fileLabel.setAttribute('for', `${id}-input`);\r\n };\r\n\r\n /**\r\n * Handles the upload of a single file.\r\n * @param {string} id - The ID of the file input element that triggered the upload.\r\n * @param {File} file - The file to upload.\r\n */\r\n const handleFileUpload = (file, id) => {\r\n const { name } = file;\r\n const regexp = new RegExp(document.getElementById('filtyper-pattern').value)\r\n const isValidFiletype = regexp.test(name.toLowerCase());\r\n\r\n if (\r\n !isFileInArray(uploadedFiles, file) && isValidFiletype\r\n ) {\r\n uploadedFiles.push(file);\r\n }\r\n\r\n if (!isValidFiletype) {\r\n alert(TEXT_FILE_TYPE);\r\n }\r\n };\r\n\r\n /**\r\n * Handles the change event on file input elements, triggering the upload of any selected files.\r\n * @param {HTMLElement} el - The element that is the file input component.\r\n */\r\n const handleFileInput = (el) => {\r\n const id = el.getAttribute('data-fn-target-id');\r\n const fileInput = el.querySelector('input[type=\"file\"]');\r\n\r\n setFileInputAttributes(el, id);\r\n\r\n fileInput.addEventListener('change', (e) => {\r\n const fileList = e.target.files;\r\n [...fileList].forEach((file) => {\r\n handleFileUpload(file, id);\r\n });\r\n\r\n updateFileList(id);\r\n });\r\n };\r\n\r\n /**\r\n * Updates the file list displayed on the page.\r\n * @param {string} id - The ID of the container element for the file list.\r\n */\r\n const updateFileList = (id) => {\r\n const el = document.querySelector('#' + id);\r\n const objectURL = (f) => URL.createObjectURL(f);\r\n const downloadLink = (f) =>\r\n '' +\r\n f.name +\r\n '';\r\n const listHtml = (item) => `