// The tree of the reorder page (D-09, UI-SPEC "Reorder page"): built from // the flat nodes GET …/reorder answers, edited by the page's moves and sent // back flat by POST …/reorder. Children always move with their parent. import type { AdminReorderNode, AdminReorderNodeInput } from '../../api/types' export interface TreeNode { id: number name: string children: TreeNode[] } /** Where a dragged node lands relative to the row under the pointer. */ export type Zone = 'before' | 'after' | 'inside' /** The keyboard and button moves: siblings, nest under the previous one, out one level. */ export type Move = 'up' | 'down' | 'indent' | 'outdent' /** A node as sent: the server's AdminReorderNodeInput, with a null parent at the top. */ export type FlatNode = Omit & { parent_id: number | null } /** The nested tree of flat nodes: by sort order, then id; an unknown parent puts a node at the top. */ export function buildTree(nodes: readonly AdminReorderNode[]): TreeNode[] { const byId = new Map() for (const node of nodes) { byId.set(node.id, { id: node.id, name: node.name ?? '', children: [] }) } const ordered = [...nodes].sort((a, b) => a.sort_order - b.sort_order || a.id - b.id) const roots: TreeNode[] = [] for (const flat of ordered) { const node = byId.get(flat.id)! const parentId = (flat.parent_id as number | null | undefined) ?? null const parent = parentId !== null ? byId.get(parentId) : undefined if (parent && parent !== node) { parent.children.push(node) } else { roots.push(node) } } // Nodes caught in a parent cycle reach no root: they join the top level. const reached = new Set() const walk = (list: TreeNode[]) => { for (const node of list) { reached.add(node.id) walk(node.children) } } walk(roots) for (const flat of ordered) { if (!reached.has(flat.id)) { const node = byId.get(flat.id)! for (const other of byId.values()) { other.children = other.children.filter((child) => child.id !== node.id) } roots.push(node) walk([node]) } } return roots } /** * The tree as sent: pre-order, sort_order counting from 1, parent null at the * top. A flat reorder (D-17) sends parent null on every node. */ export function flatten(tree: readonly TreeNode[], flat = false): FlatNode[] { const out: FlatNode[] = [] const walk = (list: readonly TreeNode[], parent: number | null) => { for (const node of list) { out.push({ id: node.id, parent_id: flat ? null : parent, sort_order: out.length + 1 }) walk(node.children, node.id) } } walk(tree, null) return out } export function cloneTree(tree: readonly TreeNode[]): TreeNode[] { return tree.map((node) => ({ id: node.id, name: node.name, children: cloneTree(node.children) })) } export interface Located { node: TreeNode list: TreeNode[] index: number parent: TreeNode | null /** 0 at the top level. */ depth: number } export function locate(tree: TreeNode[], id: number, parent: TreeNode | null = null, depth = 0): Located | null { for (let index = 0; index < tree.length; index++) { const node = tree[index]! if (node.id === id) { return { node, list: tree, index, parent, depth } } const found = locate(node.children, id, node, depth + 1) if (found) { return found } } return null } /** Whether id is node itself or anywhere below it. */ export function contains(node: TreeNode, id: number): boolean { return node.id === id || node.children.some((child) => contains(child, id)) } /** Whether a move can run: first or last among its siblings, at the top, or no previous sibling. */ export function canMove(tree: TreeNode[], id: number, move: Move): boolean { const at = locate(tree, id) if (!at) { return false } switch (move) { case 'up': case 'indent': return at.index > 0 case 'down': return at.index < at.list.length - 1 case 'outdent': return at.parent !== null } } /** Runs a move on tree in place; false when it cannot run. */ export function applyMove(tree: TreeNode[], id: number, move: Move): boolean { if (!canMove(tree, id, move)) { return false } const at = locate(tree, id)! switch (move) { case 'up': at.list.splice(at.index, 1) at.list.splice(at.index - 1, 0, at.node) return true case 'down': at.list.splice(at.index, 1) at.list.splice(at.index + 1, 0, at.node) return true case 'indent': { const previous = at.list[at.index - 1]! at.list.splice(at.index, 1) previous.children.push(at.node) return true } case 'outdent': { const parent = locate(tree, at.parent!.id)! at.list.splice(at.index, 1) parent.list.splice(parent.index + 1, 0, at.node) return true } } } /** The drop zone a flat reorder (D-17) uses: it never nests, so inside lands after the row. */ export function flatZone(zone: Zone, flat: boolean): Zone { return flat && zone === 'inside' ? 'after' : zone } /** The moves a flat reorder offers: siblings only, never indent or outdent. */ export function flatMove(move: Move, flat: boolean): boolean { return !flat || move === 'up' || move === 'down' } /** Whether node `dragged` may land on `target` in zone: never in its own subtree. */ export function canDrop(tree: TreeNode[], dragged: number, target: number): boolean { const at = locate(tree, dragged) return !!at && !contains(at.node, target) } /** Moves `dragged` before, after or inside (as last child of) `target`, in place. */ export function applyDrop(tree: TreeNode[], dragged: number, target: number, zone: Zone): boolean { if (!canDrop(tree, dragged, target)) { return false } const from = locate(tree, dragged)! from.list.splice(from.index, 1) const to = locate(tree, target)! if (zone === 'inside') { to.node.children.push(from.node) } else { to.list.splice(zone === 'before' ? to.index : to.index + 1, 0, from.node) } return true }