|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +function sortableValue(cell) { |
| 4 | + const text = cell.textContent.trim(); |
| 5 | + if (!text || text === '—') return null; |
| 6 | + |
| 7 | + const duration = text.match(/^(-?[\d,.]+)\s*([dh])(?:\s|$)/i); |
| 8 | + if (duration) { |
| 9 | + const hours = Number(duration[1].replaceAll(',', '')); |
| 10 | + return duration[2].toLowerCase() === 'd' ? hours * 24 : hours; |
| 11 | + } |
| 12 | + |
| 13 | + const numeric = text.match(/^-?[\d,.]+(?:%|\s|$)/); |
| 14 | + if (numeric) return Number(numeric[0].replaceAll(/[,%\s]/g, '')); |
| 15 | + return text.toLocaleLowerCase(); |
| 16 | +} |
| 17 | + |
| 18 | +function compareSortValues(left, right, direction) { |
| 19 | + if (left === null) return right === null ? 0 : 1; |
| 20 | + if (right === null) return -1; |
| 21 | + if (typeof left === 'number' && typeof right === 'number') { |
| 22 | + return (left - right) * direction; |
| 23 | + } |
| 24 | + return String(left).localeCompare(String(right), undefined, { numeric: true }) * direction; |
| 25 | +} |
| 26 | + |
| 27 | +function sortTable(table, column, direction) { |
| 28 | + const body = table.tBodies[0]; |
| 29 | + if (!body) return; |
| 30 | + const rows = Array.from(body.rows).map((row, index) => ({ row, index })); |
| 31 | + rows.sort((left, right) => ( |
| 32 | + compareSortValues( |
| 33 | + sortableValue(left.row.cells[column]), |
| 34 | + sortableValue(right.row.cells[column]), |
| 35 | + direction, |
| 36 | + ) || left.index - right.index |
| 37 | + )); |
| 38 | + rows.forEach(({ row }) => body.append(row)); |
| 39 | +} |
| 40 | + |
| 41 | +function resetSortIndicators(headers) { |
| 42 | + headers.forEach(header => { |
| 43 | + header.removeAttribute('aria-sort'); |
| 44 | + header.querySelector('.pr-metrics-sort-indicator').textContent = '↕'; |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +function activateSort(table, headers, header, indicator, column) { |
| 49 | + const ascending = header.getAttribute('aria-sort') !== 'ascending'; |
| 50 | + resetSortIndicators(headers); |
| 51 | + header.setAttribute('aria-sort', ascending ? 'ascending' : 'descending'); |
| 52 | + indicator.textContent = ascending ? '↑' : '↓'; |
| 53 | + sortTable(table, column, ascending ? 1 : -1); |
| 54 | +} |
| 55 | + |
| 56 | +function handleSortKey(event, activate) { |
| 57 | + if (['Enter', ' '].includes(event.key)) { |
| 58 | + event.preventDefault(); |
| 59 | + activate(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function initializeHeader(table, headers, header, column) { |
| 64 | + header.tabIndex = 0; |
| 65 | + header.style.cursor = 'pointer'; |
| 66 | + header.style.userSelect = 'none'; |
| 67 | + header.setAttribute('role', 'button'); |
| 68 | + header.setAttribute('title', 'Sort by this column'); |
| 69 | + const indicator = document.createElement('span'); |
| 70 | + indicator.className = 'ms-1 pr-metrics-sort-indicator'; |
| 71 | + indicator.setAttribute('aria-hidden', 'true'); |
| 72 | + indicator.textContent = '↕'; |
| 73 | + header.append(indicator); |
| 74 | + |
| 75 | + const activate = () => activateSort(table, headers, header, indicator, column); |
| 76 | + header.addEventListener('click', activate); |
| 77 | + header.addEventListener('keydown', event => handleSortKey(event, activate)); |
| 78 | +} |
| 79 | + |
| 80 | +function initializeTable(table) { |
| 81 | + if (table.dataset.sortableInitialized) return; |
| 82 | + table.dataset.sortableInitialized = 'true'; |
| 83 | + const headers = Array.from(table.querySelectorAll('thead th')); |
| 84 | + headers.forEach((header, column) => initializeHeader(table, headers, header, column)); |
| 85 | +} |
| 86 | + |
| 87 | +function initializeSortableTables(root = document) { |
| 88 | + root.querySelectorAll('table.pr-metrics-sortable').forEach(table => { |
| 89 | + initializeTable(table); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +document.addEventListener('DOMContentLoaded', () => initializeSortableTables()); |
| 94 | + |
| 95 | +/* istanbul ignore next */ |
| 96 | +if (typeof module !== 'undefined' && module.exports) { |
| 97 | + module.exports = { |
| 98 | + sortableValue, |
| 99 | + compareSortValues, |
| 100 | + sortTable, |
| 101 | + initializeSortableTables, |
| 102 | + }; |
| 103 | +} |
0 commit comments