import { isDefined } from 'ts-is-present'; import { Graph } from '../../Graph'; import { StaticLayout } from './StaticLayout'; class RowLayout extends StaticLayout { private _groups: string[][]; constructor () { super(); this._groups = []; } apply (graph: Graph): void { const bbox = graph.boundingBox(); const cx = (bbox[1] + bbox[3]) / 2; const cy = (bbox[0] + bbox[2]) / 2; const w = 0.8 * Math.abs(bbox[1] - bbox[3]); const h = 0.8 * Math.abs(bbox[0] - bbox[2]); const groups = []; const nodemap = new Map(graph.nodes().map(node => [node.id, node])); this._groups.forEach(group => { const nodes = group .map(id => { const node = nodemap.get(id); nodemap.delete(id); return node; }) .filter(isDefined); if (nodes.length) groups.push(nodes); }); groups.push(Array.from(nodemap.values())); const ngroup = groups.length || 1; const t = cy + h / 2; const l = cx - w / 2; const dx = w / Math.max(...groups.map(group => group.length)); const dy = h / ((ngroup - 1) || 1); groups.forEach((group, g) => { const y = t - g * dy; group.forEach((node, n) => { node.x = l + dx * n; node.y = y; }); }) } groups (groups: string[][]): this { this._groups = groups; return this; } } export { RowLayout }