import { BaseComponent } from "./ave-base-component.js"; class AveTable extends BaseComponent { static get observedAttributes() { return ['caption', 'sortable', 'striped', 'hover', 'data', 'columns']; } constructor() { super(); this._handleSort = this._handleSort.bind(this); this._handleRowClick = this._handleRowClick.bind(this); this._sortColumn = null; this._sortDirection = 'asc'; } // Property getters/setters get caption() { return this.getAttribute('caption') || ''; } set caption(value) { this.setAttribute('caption', value); } get sortable() { return this.getAttribute('sortable') !== 'false'; } set sortable(value) { this.setAttribute('sortable', value); } get striped() { return this._getBooleanAttribute('striped'); } set striped(value) { this._setBooleanAttribute('striped', value); } get hover() { return this._getBooleanAttribute('hover'); } set hover(value) { this._setBooleanAttribute('hover', value); } get data() { const dataAttr = this.getAttribute('data'); if (dataAttr && typeof dataAttr === 'string') { try { return JSON.parse(dataAttr); } catch (e) { console.warn('Invalid JSON in data attribute:', dataAttr); return null; } } return null; } set data(value) { if (Array.isArray(value)) { this.setAttribute('data', JSON.stringify(value)); } else { this.setAttribute('data', value); } } get columns() { const columnsAttr = this.getAttribute('columns'); if (columnsAttr && typeof columnsAttr === 'string') { try { return JSON.parse(columnsAttr); } catch (e) { console.warn('Invalid JSON in columns attribute:', columnsAttr); return null; } } return null; } set columns(value) { if (Array.isArray(value)) { this.setAttribute('columns', JSON.stringify(value)); } else { this.setAttribute('columns', value); } } setupEventListeners() { if (this.sortable) { this._registerEvent('th[data-sortable]', 'click', this._handleSort); } this._registerEvent('tbody tr', 'click', this._handleRowClick); } _handleSort(e) { const th = e.target.closest('th'); if (!th || !th.dataset.sortable) return; const column = th.dataset.sortable; // Toggle sort direction if same column, otherwise start with asc if (this._sortColumn === column) { this._sortDirection = this._sortDirection === 'asc' ? 'desc' : 'asc'; } else { this._sortColumn = column; this._sortDirection = 'asc'; } this._updateSortIndicators(); this._sortTable(column, this._sortDirection); this._dispatch('ave-table-sort', { column: column, direction: this._sortDirection }); } _handleRowClick(e) { const row = e.target.closest('tr'); if (!row) return; const rowIndex = Array.from(row.parentNode.children).indexOf(row); const rowData = this._getRowData(row); this._dispatch('ave-table-row-click', { rowIndex: rowIndex, rowData: rowData, row: row }); } _updateSortIndicators() { // Remove all existing sort indicators this.shadowRoot.querySelectorAll('th[data-sortable]').forEach(th => { th.classList.remove('sort-asc', 'sort-desc'); const indicator = th.querySelector('.sort-indicator'); if (indicator) indicator.remove(); }); // Add indicator to current sort column const currentTh = this.shadowRoot.querySelector(`th[data-sortable="${this._sortColumn}"]`); if (currentTh) { currentTh.classList.add(`sort-${this._sortDirection}`); const indicator = document.createElement('span'); indicator.className = 'sort-indicator ml-1'; indicator.innerHTML = this._sortDirection === 'asc' ? '↑' : '↓'; currentTh.appendChild(indicator); } } _sortTable(column, direction) { const tbody = this.shadowRoot.querySelector('tbody'); if (!tbody) return; const rows = Array.from(tbody.querySelectorAll('tr')); const columnIndex = this._getColumnIndex(column); if (columnIndex === -1) return; rows.sort((a, b) => { const aValue = a.cells[columnIndex]?.textContent.trim() || ''; const bValue = b.cells[columnIndex]?.textContent.trim() || ''; // Try to parse as numbers const aNum = parseFloat(aValue.replace(/[^0-9.-]/g, '')); const bNum = parseFloat(bValue.replace(/[^0-9.-]/g, '')); let comparison = 0; if (!isNaN(aNum) && !isNaN(bNum)) { comparison = aNum - bNum; } else { comparison = aValue.localeCompare(bValue); } return direction === 'asc' ? comparison : -comparison; }); // Re-append sorted rows rows.forEach(row => tbody.appendChild(row)); } _getColumnIndex(column) { const headers = this.shadowRoot.querySelectorAll('th'); for (let i = 0; i < headers.length; i++) { if (headers[i].dataset.sortable === column) { return i; } } return -1; } _getRowData(row) { const data = {}; const headers = this.shadowRoot.querySelectorAll('th'); Array.from(row.cells).forEach((cell, index) => { const header = headers[index]; const key = header?.dataset.sortable || header?.textContent.trim().toLowerCase() || `column_${index}`; data[key] = cell.textContent.trim(); }); return data; } render() { const slotContent = this.innerHTML; const hasCustomContent = slotContent.trim().length > 0; this.shadowRoot.innerHTML = `
${this.caption ? `` : ''} ${hasCustomContent ? slotContent : this._getDefaultContent()}
${this.caption}
`; this.setupEventListeners(); } _getDefaultContent() { return ` Invoice Status Method Amount INV001 Paid Credit Card $250.00 INV002 Pending PayPal $150.00 INV003 Unpaid Bank Transfer $350.00 INV004 Paid Credit Card $450.00 INV005 Paid PayPal $550.00 INV006 Pending Bank Transfer $200.00 INV007 Unpaid Credit Card $300.00 Total $2,500.00 `; } } customElements.define('ave-table', AveTable);