Files
2026-10-29 11:57:29 +01:00

58 lines
2.3 KiB
JavaScript

// Generated, do not edit. sha256:50118a60294dbc8f28d9357f89a87533bb1550041305794ac6ceb8f28c573fcc
import { compare } from './table-rows.js';
const SEP = '\u0000';
export function groupSections(rows, { columns, groupKey, groupKey2 = null, groupKey3 = null, sort = null, collapsed = null }) {
const col = columns.find(c => c.key === groupKey);
const col2 = groupKey2 ? columns.find(c => c.key === groupKey2) : null;
const col3 = groupKey3 ? columns.find(c => c.key === groupKey3) : null;
const orderOf = (c, key) => ((sort && sort.key === key) ? sort.dir : (c.group.order || 'asc'));
const order = orderOf(col, groupKey);
const order2 = col2 ? orderOf(col2, groupKey2) : 'asc';
const order3 = col3 ? orderOf(col3, groupKey3) : 'asc';
const buckets = new Map();
for (const r of rows) {
const v1 = col.group.value(r);
const v2 = col2 ? col2.group.value(r) : null;
const v3 = col3 ? col3.group.value(r) : null;
const key = [String(v1), col2 ? String(v2) : '', col3 ? String(v3) : ''].join(SEP);
if (!buckets.has(key)) buckets.set(key, { v1, v2, v3, items: [] });
buckets.get(key).items.push(r);
}
const entries = [...buckets.values()].sort((a, b) => {
const c1 = compare(a.v1, b.v1, order);
if (c1 !== 0 || !col2) return c1;
const c2 = compare(a.v2, b.v2, order2);
return (c2 !== 0 || !col3) ? c2 : compare(a.v3, b.v3, order3);
});
let itemsByV1 = null;
if (collapsed) {
itemsByV1 = new Map();
for (const e of entries) {
const tk = String(e.v1);
if (!itemsByV1.has(tk)) itemsByV1.set(tk, []);
itemsByV1.get(tk).push(...e.items);
}
}
const shownCollapsed = new Set();
const out = [];
for (const { v1, v2, v3, items } of entries) {
const token = String(v1);
const l1 = col.group.label ? col.group.label(v1) : v1;
if (collapsed && collapsed.has(token)) {
if (shownCollapsed.has(token)) continue;
shownCollapsed.add(token);
out.push({ token, value: v1, label: l1, items: itemsByV1.get(token) || items, collapsed: true });
continue;
}
let label = l1;
if (col2) label += ` - ${col2.group.label ? col2.group.label(v2) : v2}`;
if (col3) label += ` - ${col3.group.label ? col3.group.label(v3) : v3}`;
out.push({ token, value: v1, label, items, collapsed: false });
}
return out;
}