import { ID } from "../../types/index.mjs"; import { GroupContext, GroupContextOptions, GroupOptions, GroupTicket, GroupTicketInput } from "../createGroup/index.mjs"; import { ComputedRef, MaybeRefOrGetter, Reactive, Ref } from "vue"; //#region src/composables/createNested/types.d.ts /** * Input type for nested tickets - what users provide to register(). * Extend this interface to add custom properties. * * @template V The type of the ticket value. */ interface NestedTicketInput extends GroupTicketInput { /** ID of the parent ticket, or undefined if this is a root item */ parentId?: ID; /** Whether this ticket is initially active */ active?: boolean; /** Reference to the DOM element for focus management */ el?: MaybeRefOrGetter; } /** * Output type for nested tickets - what users receive from get(). * Includes all input properties plus tree traversal and state methods. * * @template Z The input ticket type that extends NestedTicketInput. */ type NestedTicket = GroupTicket & { /** ID of the parent ticket, or undefined if this is a root item. Static — set at registration time. */ parentId: ID | undefined; /** Reference to the DOM element for focus management */ el: MaybeRefOrGetter | undefined; /** Whether this ticket is currently open/expanded */ isOpen: Readonly>; /** Whether this ticket is currently active/highlighted */ isActive: Readonly>; /** Whether this ticket is a leaf node (has no children) */ isLeaf: Readonly>; /** Depth in tree (0 = root) */ depth: Readonly>; /** Open/expand this ticket */ open: () => void; /** Close/collapse this ticket */ close: () => void; /** Flip this ticket's open/closed state */ flip: () => void; /** Reveal this ticket by opening all ancestors */ reveal: () => void; /** Activate/highlight this ticket */ activate: () => void; /** Deactivate/unhighlight this ticket */ deactivate: () => void; /** Get path from root to self (includes self) */ getPath: () => ID[]; /** Get ancestors excluding self */ getAncestors: () => ID[]; /** Get all descendants */ getDescendants: () => ID[]; /** Check if this ticket is an ancestor of the given ID */ isAncestorOf: (descendantId: ID) => boolean; /** Check if this ticket has the given ID as an ancestor */ hasAncestor: (ancestorId: ID) => boolean; /** Get sibling IDs (including self) */ siblings: () => ID[]; /** Get 1-indexed position among siblings (for aria-posinset) */ position: () => number; /** Drop the element and keep this ticket registered */ release: () => void; }; /** * Registration input for nested items. * Allows inline children definition for easier tree construction. * * @template Z The input ticket type that extends NestedTicketInput. */ type NestedRegistration = Partial & { /** Inline children to register with this item as parent */ children?: NestedRegistration[]; }; /** * Context for managing nested/hierarchical item collections. * * @template Z The input ticket type. * @template E The output ticket type. * * @remarks * Extends GroupContext with parent-child relationship tracking, open/close state, * and hierarchical traversal methods. Perfect for tree structures, nested menus, * file explorers, and organizational charts. */ interface NestedContext = NestedTicket> extends Omit, 'register' | 'onboard' | 'offboard' | 'select' | 'unselect' | 'toggle'> { /** Map of parent IDs to arrays of child IDs. Use register/unregister to modify. */ readonly children: ReadonlyMap; /** Map of child IDs to their parent ID (or undefined for roots). Use register/unregister to modify. */ readonly parents: ReadonlyMap; /** Reactive Set of opened/expanded item IDs. Use open/close/flip to modify. */ readonly openedIds: Reactive>; /** Computed Set of opened/expanded item instances */ openedItems: ComputedRef>; /** Reactive Set of active/highlighted item IDs. Use activate/deactivate to modify. */ readonly activeIds: Reactive>; /** Computed Set of active/highlighted item instances */ activeItems: ComputedRef>; /** Computed Set of active item indexes (position-based) */ activeIndexes: ComputedRef>; /** Activate/highlight one or more items by ID */ activate: (ids: ID | ID[]) => void; /** Deactivate/unhighlight one or more items by ID */ deactivate: (ids: ID | ID[]) => void; /** Check if an item is active by ID */ activated: (id: ID) => boolean; /** Deactivate all items */ deactivateAll: () => void; /** Open/expand one or more items by ID */ open: (ids: ID | ID[]) => void; /** Close/collapse one or more items by ID */ close: (ids: ID | ID[]) => void; /** Flip one or more items' open/closed state by ID */ flip: (ids: ID | ID[]) => void; /** Check if an item is open by ID */ opened: (id: ID) => boolean; /** Open node(s) and their immediate non-leaf children */ unfold: (ids: ID | ID[]) => void; /** Reveal node(s) by opening all ancestors (makes node visible without opening it) */ reveal: (ids: ID | ID[]) => void; /** Fully expand node(s) and all their non-leaf descendants */ expand: (ids: ID | ID[]) => void; /** Expand all non-leaf nodes */ expandAll: () => void; /** Collapse all nodes */ collapseAll: () => void; /** Convert tree to flat array with parentId references */ toFlat: () => Array<{ id: ID; parentId: ID | undefined; value: Z extends NestedTicketInput ? V : unknown; }>; /** Get the path from root to the specified item (inclusive) */ getPath: (id: ID) => ID[]; /** Get all descendants of an item */ getDescendants: (id: ID) => ID[]; /** Get all ancestors of an item (excluding self) */ getAncestors: (id: ID) => ID[]; /** Check if an item is a leaf node */ isLeaf: (id: ID) => boolean; /** Get the depth level of an item in the tree */ getDepth: (id: ID) => number; /** Check if ancestorId is an ancestor of descendantId */ isAncestorOf: (ancestorId: ID, descendantId: ID) => boolean; /** Check if id has ancestorId as an ancestor (semantic alias for isAncestorOf) */ hasAncestor: (id: ID, ancestorId: ID) => boolean; /** Get sibling IDs (including self). For roots, returns all root IDs. */ siblings: (id: ID) => ID[]; /** Get 1-indexed position among siblings (for aria-posinset). Returns 0 if not found. */ position: (id: ID) => number; /** Computed array of root items (items with no parent) */ roots: ComputedRef; /** Computed array of leaf items (items with no children) */ leaves: ComputedRef; /** Returns depth-first traversal of visible (open) nodes for keyboard navigation */ visibleItems: () => E[]; /** Whether multiple items can be selected */ readonly multiple: boolean; /** Select item(s) and all descendants, updating ancestor mixed states */ select: (ids: ID | ID[]) => void; /** Unselect item(s) and all descendants, updating ancestor mixed states */ unselect: (ids: ID | ID[]) => void; /** Toggle selection with cascading behavior */ toggle: (ids: ID | ID[]) => void; /** Register a node with optional inline children (accepts input type, returns output type) */ register: (registration?: NestedRegistration) => E; /** Batch register nodes with optional inline children */ onboard: (registrations: NestedRegistration[]) => E[]; /** Unregister a node, optionally cascading to descendants */ unregister: (id: ID, cascade?: boolean) => void; /** Drop a node's element and keep its ticket, parent link, and selection */ release: (id: ID) => void; /** Offboard multiple nodes, optionally cascading */ offboard: (ids: ID[], cascade?: boolean) => void; /** Clear all nodes and nested state (children, parents, openedIds) */ clear: () => void; } /** * Open mode for nested items. * - `'multiple'` (default): Multiple nodes can be open simultaneously * - `'single'`: Only one node can be open at a time (accordion behavior) */ type NestedOpenMode = 'single' | 'multiple'; /** * Selection mode for nested items. * - `'cascade'` (default): Selecting a parent selects all descendants; ancestors show mixed state * - `'independent'`: Each node is selected independently, no cascading * - `'leaf'`: Only leaf nodes can be selected; selecting parent selects all leaf descendants */ type NestedSelectionMode = 'cascade' | 'independent' | 'leaf'; /** * Active mode for nested items. * - `'single'` (default): Only one item can be active at a time * - `'multiple'`: Multiple items can be active simultaneously */ type NestedActiveMode = 'single' | 'multiple'; /** * Options for creating a nested instance. */ interface NestedOptions extends GroupOptions { /** * Controls how nodes expand/collapse. * - `'multiple'` (default): Multiple nodes can be open simultaneously * - `'single'`: Only one node open at a time (accordion behavior) */ open?: NestedOpenMode; /** * When true, parent nodes automatically open when children are registered. * Similar to `enroll` in selection composables but for open state. */ openAll?: boolean; /** * When true, opening a node also opens all its ancestors. * Ensures the opened node is always visible in the tree. */ reveal?: boolean; /** * Controls how selection cascades through the hierarchy. * - `'cascade'` (default): Selecting parent selects descendants; ancestors show mixed state * - `'independent'`: Each node selected independently * - `'leaf'`: Only leaf nodes selectable; parent selection selects leaf descendants */ selection?: NestedSelectionMode; /** * Controls how many items can be active/highlighted simultaneously. * - `'single'` (default): Only one item active at a time * - `'multiple'`: Multiple items can be active simultaneously */ active?: NestedActiveMode; } /** * Options for creating a nested context. */ interface NestedContextOptions extends GroupContextOptions { /** * Controls how nodes expand/collapse. * - `'multiple'` (default): Multiple nodes can be open simultaneously * - `'single'`: Only one node open at a time (accordion behavior) */ open?: NestedOpenMode; /** * When true, parent nodes automatically open when children are registered. * Similar to `enroll` in selection composables but for open state. */ openAll?: boolean; /** * When true, opening a node also opens all its ancestors. * Ensures the opened node is always visible in the tree. */ reveal?: boolean; /** * Controls how selection cascades through the hierarchy. * - `'cascade'` (default): Selecting parent selects descendants; ancestors show mixed state * - `'independent'`: Each node selected independently * - `'leaf'`: Only leaf nodes selectable; parent selection selects leaf descendants */ selection?: NestedSelectionMode; /** * Controls how many items can be active/highlighted simultaneously. * - `'single'` (default): Only one item active at a time * - `'multiple'`: Multiple items can be active simultaneously */ active?: NestedActiveMode; } //#endregion export { NestedActiveMode, NestedContext, NestedContextOptions, NestedOpenMode, NestedOptions, NestedRegistration, NestedSelectionMode, NestedTicket, NestedTicketInput };