xref: /openbsd-src/usr.sbin/httpd/js.h.in (revision ef0dceb5e3695222c9515e080415663c4574353a)
1static const char *js =
2const cellVal = (e) => e.getAttribute('title') || e.getAttribute('data-o') ||
3    e.innerText || e.textContent;
4const rowValue = (tr, idx) => cellVal(tr.children[idx]);
5const compare = (idx, asc) => (a, b) => ((v1, v2) =>
6    v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
7    )(rowValue(asc ? a : b, idx), rowValue(asc ? b : a, idx));
8
9// set up the listener
10document.querySelectorAll('tr.sort th').forEach(th => th.addEventListener('click', (() => {
11    const table = th.closest('table');
12    // make the sorted column bold
13    table.querySelectorAll('tr.sort th').forEach(th2 =>
14    	th2.className = th2 == th ? 'sorted' : 'unsorted');
15    const body = table.querySelector('tbody');
16    Array.from(body.querySelectorAll('tr'))
17        .sort(compare(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc))
18        .forEach(tr => body.appendChild(tr) );
19})))
20