ifixkart-admin/components/ui/StretchableTables.tsx

131 lines
4.6 KiB
TypeScript

'use client';
import { useEffect } from 'react';
const MIN_WIDTH = 72;
const MAX_WIDTH = 720;
function columnKey(th: HTMLTableCellElement) {
const label = (th.textContent || `col-${th.cellIndex}`).replace(/\s+/g, ' ').trim().slice(0, 48);
return `${window.location.pathname}::${th.cellIndex}::${label || 'col'}`;
}
function isSelectionHeader(th: HTMLTableCellElement) {
const label = (th.textContent || '').trim();
return label.length === 0 && Boolean(th.querySelector('input[type="checkbox"], button'));
}
function isActionHeader(th: HTMLTableCellElement) {
return /^actions?$/i.test((th.textContent || '').replace(/\s+/g, ' ').trim());
}
function applyWidth(th: HTMLTableCellElement, width: number) {
const next = `${Math.round(width)}px`;
th.style.width = next;
th.style.minWidth = next;
th.style.maxWidth = next;
}
function lockColumns(table: HTMLTableElement) {
if (table.dataset.stretchLocked === '1') return;
const headers = table.querySelectorAll<HTMLTableCellElement>(':scope > thead > tr:first-child > th');
headers.forEach((th) => {
if (!th.style.width) applyWidth(th, th.getBoundingClientRect().width);
});
table.style.tableLayout = 'fixed';
table.style.width = 'max-content';
table.style.minWidth = '100%';
table.dataset.stretchLocked = '1';
}
function attach(table: HTMLTableElement) {
const headers = table.querySelectorAll<HTMLTableCellElement>(':scope > thead > tr:first-child > th');
headers.forEach((th) => {
if (isSelectionHeader(th)) return;
if (isActionHeader(th)) {
localStorage.removeItem(`ifixkart.col.${columnKey(th)}`);
th.style.width = '';
th.style.minWidth = '';
th.style.maxWidth = '';
}
if (th.querySelector(':scope > .crm-col-resizer')) return;
const saved = Number(localStorage.getItem(`ifixkart.col.${columnKey(th)}`));
if (Number.isFinite(saved) && saved >= MIN_WIDTH) {
lockColumns(table);
applyWidth(th, saved);
}
th.classList.add('crm-stretch-th');
const handle = document.createElement('span');
handle.className = 'crm-col-resizer';
handle.title = 'Drag to resize column';
handle.addEventListener('pointerdown', (event) => {
event.preventDefault();
event.stopPropagation();
lockColumns(table);
const startX = event.clientX;
const startWidth = th.getBoundingClientRect().width;
const direction = document.documentElement.dir === 'rtl' ? -1 : 1;
handle.classList.add('is-active');
document.body.style.cursor = 'col-resize';
document.body.style.userSelect = 'none';
const move = (ev: PointerEvent) => {
const next = Math.max(MIN_WIDTH, Math.min(MAX_WIDTH, startWidth + (ev.clientX - startX) * direction));
applyWidth(th, next);
};
const up = () => {
handle.classList.remove('is-active');
document.body.style.cursor = '';
document.body.style.userSelect = '';
localStorage.setItem(`ifixkart.col.${columnKey(th)}`, String(Math.round(th.getBoundingClientRect().width)));
window.removeEventListener('pointermove', move);
window.removeEventListener('pointerup', up);
};
window.addEventListener('pointermove', move);
window.addEventListener('pointerup', up);
});
th.appendChild(handle);
});
}
export function StretchableTables() {
useEffect(() => {
const root = document.getElementById('main-content') || document.body;
let frame = 0;
let applying = false;
const scan = () => {
applying = true;
root.querySelectorAll<HTMLTableElement>('table.crm-data-table').forEach(attach);
applying = false;
};
const schedule = () => {
if (applying || frame) return;
frame = window.requestAnimationFrame(() => {
frame = 0;
scan();
});
};
schedule();
const observer = new MutationObserver((mutations) => {
if (applying) return;
const relevant = mutations.some((mutation) =>
[...mutation.addedNodes].some((node) => {
if (!(node instanceof Element)) return false;
if (node.classList.contains('crm-col-resizer')) return false;
return node.matches('table, thead, tbody, tr, th') || Boolean(node.querySelector('table.crm-data-table, thead, th'));
})
);
if (relevant) schedule();
});
observer.observe(root, { childList: true, subtree: true });
return () => {
observer.disconnect();
if (frame) window.cancelAnimationFrame(frame);
};
}, []);
return null;
}