|
| 1 | +import { Table } from './table'; |
| 2 | + |
| 3 | +export const EVENT_DATA_TABLE_FILE_LOADED = 'EVENT_DATA_TABLE_FILE_LOADED'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @typedef {Object} config |
| 7 | + * @param {string} url - The URL to upload the file to. |
| 8 | + * @param {string} path - (Optional) Key in the response object used to extract nested data. |
| 9 | + * @param {string} inputSelector - (Optional) Selector to the input element (default .js-table-file-uploader). |
| 10 | + */ |
| 11 | +export class TableFileUploader { |
| 12 | + uploader = null; |
| 13 | + tableIds = null; |
| 14 | + |
| 15 | + staticClasses = { |
| 16 | + ajaxInitialized: 'ajax-initialized', |
| 17 | + isLoading: 'is-loading', |
| 18 | + }; |
| 19 | + |
| 20 | + constructor(data) { |
| 21 | + this.data = data; |
| 22 | + |
| 23 | + this.init(); |
| 24 | + } |
| 25 | + |
| 26 | + init() { |
| 27 | + this.setDefaults(); |
| 28 | + |
| 29 | + const selectors = document.querySelectorAll(this.data.config.inputSelector); |
| 30 | + |
| 31 | + this.uploader = selectors.length === 1 ? selectors[0] : this.findClosestInput(); |
| 32 | + |
| 33 | + if (!this.uploader) return; |
| 34 | + |
| 35 | + this.tableIds = Array.from(document.querySelectorAll(`table[id][${Table.FEATURES.uploader.attribute}]`)) |
| 36 | + .filter( |
| 37 | + (table) => |
| 38 | + JSON.parse(table.getAttribute(Table.FEATURES.uploader.attribute)).url === this.data.config.url, |
| 39 | + ) |
| 40 | + .map((table) => table.id); |
| 41 | + |
| 42 | + this.initializeAjax(); |
| 43 | + this.initializeUploadEvent(); |
| 44 | + } |
| 45 | + |
| 46 | + setDefaults() { |
| 47 | + this.data.config.inputSelector ??= '.js-table-file-uploader'; |
| 48 | + } |
| 49 | + |
| 50 | + initializeUploadEvent() { |
| 51 | + this.uploader.addEventListener(EVENT_DATA_TABLE_FILE_LOADED, (event) => { |
| 52 | + if (!event.detail.tableIds.includes(this.data.tableId)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const table = this.data.tables.get(this.data.tableId); |
| 57 | + const data = this.data.config.path ? event.detail.data[this.data.config.path] : event.detail.data; |
| 58 | + |
| 59 | + if (data?.length) { |
| 60 | + table.features.selectable.selectRowsByData(data); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + initializeAjax() { |
| 66 | + if (this.uploader.hasAttribute(this.staticClasses.ajaxInitialized)) return; |
| 67 | + |
| 68 | + this.uploader.setAttribute(this.staticClasses.ajaxInitialized, true); |
| 69 | + this.uploader.addEventListener('change', this.fetchData.bind(this)); |
| 70 | + } |
| 71 | + |
| 72 | + async fetchData(event) { |
| 73 | + const target = event.target; |
| 74 | + |
| 75 | + if (!target.value) return; |
| 76 | + |
| 77 | + const file = target.files[0]; |
| 78 | + target.classList.add(this.staticClasses.isLoading); |
| 79 | + |
| 80 | + try { |
| 81 | + const formData = new FormData(); |
| 82 | + formData.append('file', file, file.name); |
| 83 | + |
| 84 | + const response = await ( |
| 85 | + await fetch(this.data.config.url, { |
| 86 | + method: 'POST', |
| 87 | + body: formData, |
| 88 | + }) |
| 89 | + ).json(); |
| 90 | + |
| 91 | + this.uploader.dispatchEvent( |
| 92 | + new CustomEvent(EVENT_DATA_TABLE_FILE_LOADED, { |
| 93 | + detail: { |
| 94 | + tableIds: this.tableIds, |
| 95 | + data: response.data, |
| 96 | + }, |
| 97 | + }), |
| 98 | + ); |
| 99 | + } catch (error) { |
| 100 | + // eslint-disable-next-line no-console |
| 101 | + console.error('Upload failed:', error); |
| 102 | + } finally { |
| 103 | + target.classList.remove(this.staticClasses.isLoading); |
| 104 | + target.value = ''; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + findClosestInput() { |
| 109 | + let current = this.data.table; |
| 110 | + |
| 111 | + while (current) { |
| 112 | + const fileInput = current.querySelector(this.data.config.inputSelector); |
| 113 | + |
| 114 | + if (fileInput) return fileInput; |
| 115 | + |
| 116 | + current = current.parentElement; |
| 117 | + } |
| 118 | + |
| 119 | + return null; |
| 120 | + } |
| 121 | +} |
0 commit comments