Database-BI
view release on metacpan or search on metacpan
templates/web/en-us/pivot.html.tt view on Meta::CPAN
</nav>
[%- END -%]
<script nonce="[% csp_nonce | html %]">
[% PROCESS "web/en/layouts/parse_accounting.html.tt" %]
// ---------------------------------------------------------------------------
// Pivot table: click-to-sort and (flat header only) drag-to-reorder columns
// ---------------------------------------------------------------------------
(function () {
var table = document.querySelector('table.pivot-table');
if (!table || !table.tHead || !table.tBodies[0]) return;
// NDIMS: number of row-dimension columns (always at the left of every tbody row)
// HASTWO: true when col-dimension headers produce a 2-level thead
var NDIMS = [% rows_dims.size %];
var HASTWO = [% (cols_dims.size > 0) ? 1 : 0 %];
// "Leaf" headers: the row of <th> cells that map 1:1 onto tbody columns.
// In the 2-level case this is tHead row 1 (dim headers span via rowspan=2).
// In the flat case this is tHead row 0.
var leafRow = table.tHead.rows[HASTWO ? 1 : 0];
var leafHeaders = Array.from(leafRow.cells);
var dragSrcIdx = null;
var sortColIdx = -1;
var sortAsc = true;
// What just happened, said out loud. Sorting and moving both change the
// table without changing the focus, so a screen reader has nothing else to
// notice.
var live = document.getElementById('pivot-live');
function announce(text) {
if (!live) return;
live.textContent = '';
setTimeout(function () { live.textContent = text; }, 30);
}
function headerName(leafIdx) {
var th = leafHeaders[leafIdx];
return th ? th.textContent.trim() : '';
}
// Compute the DOM cell index in tbody/tfoot for a leaf-header position.
// In the 2-level case the dim cells (rowspan=2) occupy positions 0..NDIMS-1
// in the tbody but are absent from tHead row 1, so offset by NDIMS.
function domColIdx(leafIdx) {
return HASTWO ? NDIMS + leafIdx : leafIdx;
}
function sortByCol(leafIdx, asc) {
var dIdx = domColIdx(leafIdx);
var tbody = table.tBodies[0];
var rows = Array.from(tbody.rows);
var nonEmpty = rows
.map(function (r) { return r.cells[dIdx] ? r.cells[dIdx].textContent.trim() : ''; })
.filter(Boolean);
var numeric = nonEmpty.length > 0 && nonEmpty.every(function (v) {
return !isNaN(parseAccounting(v));
});
rows.sort(function (a, b) {
var av = a.cells[dIdx] ? a.cells[dIdx].textContent.trim() : '';
var bv = b.cells[dIdx] ? b.cells[dIdx].textContent.trim() : '';
var cmp;
if (numeric) {
var an = parseAccounting(av), bn = parseAccounting(bv);
cmp = (isNaN(an) ? 0 : an) - (isNaN(bn) ? 0 : bn);
} else {
cmp = av.localeCompare(bv, undefined, { numeric: true, sensitivity: 'base' });
}
return asc ? cmp : -cmp;
});
rows.forEach(function (r) { tbody.appendChild(r); });
leafHeaders.forEach(function (h, i) {
h.setAttribute('aria-sort', i === leafIdx ? (asc ? 'ascending' : 'descending') : 'none');
});
announce('Sorted by ' + headerName(leafIdx) + ', ' + (asc ? 'ascending' : 'descending'));
}
// Move leaf column from -> to (flat mode only: no col-dim group structure to break).
function moveCol(from, to) {
var fd = domColIdx(from);
var td = domColIdx(to);
Array.from(table.rows).forEach(function (row) {
var cells = Array.from(row.children);
var moving = cells[fd];
var target = cells[td];
if (!moving || !target) return;
row.insertBefore(moving, from < to ? target.nextSibling : target);
});
var moved = leafHeaders.splice(from, 1)[0];
leafHeaders.splice(to, 0, moved);
if (sortColIdx === from) sortColIdx = to;
else if (from < to && sortColIdx > from && sortColIdx <= to) sortColIdx--;
else if (from > to && sortColIdx >= to && sortColIdx < from) sortColIdx++;
}
leafHeaders.forEach(function (th) {
th.setAttribute('aria-sort', 'none');
th.style.cursor = 'pointer';
th.title = 'Click to sort | Enter to sort'
+ (!HASTWO ? ' | Drag to reorder | Shift+Left/Right to move column' : '');
// A <th> is not a button, so the browser does not put it in the tab order
// or activate it by itself: it says here that it is a control, which
// header it controls, and that the instructions below apply to it.
th.setAttribute('tabindex', '0');
th.setAttribute('aria-describedby', 'pivot-head-help');
th.addEventListener('click', function () {
if (dragSrcIdx !== null) return;
var idx = leafHeaders.indexOf(th);
if (sortColIdx === idx) { sortAsc = !sortAsc; }
else { sortColIdx = idx; sortAsc = true; }
sortByCol(sortColIdx, sortAsc);
});
// The keyboard equivalent of the two pointer gestures. Enter and Space
// sort, because that is what a click does; Shift+Left/Right is the drag,
// and only where a drag is offered at all - a column moved inside a
// two-row header would leave its group label over the wrong measures.
th.addEventListener('keydown', function (e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
var idx = leafHeaders.indexOf(th);
( run in 1.170 second using v1.01-cache-2.11-cpan-60884a8df76 )