aboutsummaryrefslogtreecommitdiff
path: root/src/parts/end.html
blob: 204188c70e1d777ef95382373f85ba239fdf4393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
      </tbody>
    </table>
  </div>
</body>

<script type="text/javascript">
  const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
  prevColumnSorted = document.getElementsByClassName("current-sort")[0];
  
  const comparer = (idx, asc) => (a, b) => ((v1, v2) => 
    v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
    )(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
  
  function doSort(th) {
    console.log("Doing sort to: ", th.textContent, th);
    
    const table = th.closest('table');
    const tbody = table.querySelector('tbody');
    const columnIndex = Array.from(th.parentNode.children).indexOf(th);
    
    // Toggle sort direction
    if (prevColumnSorted == th) {
      this.asc = !this.asc;
    } else {
      this.asc = true;
    }
    
    // Get all rows, sort them, then reappend to tbody
    const rows = Array.from(tbody.querySelectorAll('tr'));
    const sortedRows = rows.sort(comparer(columnIndex, this.asc));
    
    sortedRows.forEach(row => tbody.appendChild(row));
    prevColumnSorted = th;
  }
  
  // Event listeners and initial sort
  document.querySelectorAll('th').forEach(th => th.addEventListener('click', () => {
    document.querySelectorAll('.current-sort').forEach(el => el.classList.remove('current-sort'));
    th.classList.add('current-sort');
    doSort(th);
  }));

  // Column and row highlighting on hover
  const table = document.querySelector('table');
  const allCells = table.querySelectorAll('td, th');
  let currentCol = -1;
  let currentRow = null;
  
  allCells.forEach(cell => {
    cell.addEventListener('mouseenter', function() {
      const colIndex = Array.from(this.parentNode.children).indexOf(this);
      const row = this.parentNode;
      const isHeader = this.tagName === 'TH';
      
      // Only update if we're entering a different column or row
      if (colIndex !== currentCol || row !== currentRow) {
        // Remove previous highlights
        table.querySelectorAll('.highlight-col').forEach(el => {
          el.classList.remove('highlight-col');
        });
        table.querySelectorAll('.highlight-row').forEach(el => {
          el.classList.remove('highlight-row');
        });
        
        // Add new column highlights (only tbody cells)
        table.querySelector('tbody').querySelectorAll('tr').forEach(r => {
          if (r.children[colIndex]) {
            r.children[colIndex].classList.add('highlight-col');
          }
        });
        
        // Add row highlight (only for tbody rows, not header)
        if (!isHeader) {
          row.classList.add('highlight-row');
        }
        
        currentCol = colIndex;
        currentRow = row;
      }
    });
  });
  
  // Clear highlights when leaving the table entirely
  table.addEventListener('mouseleave', function() {
    table.querySelectorAll('.highlight-col').forEach(el => {
      el.classList.remove('highlight-col');
    });
    table.querySelectorAll('.highlight-row').forEach(el => {
      el.classList.remove('highlight-row');
    });
    currentCol = -1;
    currentRow = null;
  });
</script>

</html>