/** * Data table component. * * @augments IvComponent */ export class DataTable extends IvComponent { /** @type {Readonly} */ static defaults: Readonly; /** * Returns the data table instance attached to `el`, if any. * * @param {Element} el Host element. * @returns {DataTable|undefined} The instance or `undefined`. */ static get(el: Element): DataTable | undefined; /** * Returns the existing instance or creates one. * * @param {Element} el Host element. * @param {Partial} [options] Options passed in JavaScript. * @returns {DataTable} The instance. */ static getOrCreate(el: Element, options?: Partial): DataTable; /** * Instantiates every `[data-iv-component="datatable"]` inside `root`. * * @param {ParentNode} [root] Subtree to scan. * @returns {DataTable[]} Newly created instances. */ static initAll(root?: ParentNode): DataTable[]; /** * @param {Element} el A `
` element. * @param {Partial} [options] Options passed in JavaScript. * @throws {IvError} `invalid-element` when there is no table to enhance. */ constructor(el: Element, options?: Partial); /** @type {HTMLTableElement} The enhanced table. */ _table: HTMLTableElement; /** @type {TableBody[]} Sections and their served children. */ _bodies: TableBody[]; /** @type {HTMLTableRowElement[]} Every body row, in served order. */ _rows: HTMLTableRowElement[]; /** @type {SortableColumn[]} Sortable columns, in header order. */ _columns: SortableColumn[]; /** @type {HTMLTableRowElement} The "no rows match" row. */ _empty: HTMLTableRowElement; /** @type {HTMLElement} The announced row count. */ _status: HTMLElement; /** @type {HTMLInputElement|null} The filter input, when the author served one. */ _input: HTMLInputElement | null; /** @type {Map>} Attributes to restore. */ _saved: Map>; /** @type {Map} Attribute order to restore. */ _order: Map; /** @type {number} Index of the sorted column, or `-1`. */ _column: number; /** @type {SortDirection|null} Direction of the current sort. */ _direction: SortDirection | null; /** @type {string} Applied filter query. */ _query: string; /** @type {ReturnType|null} Pending debounced filter. */ _timer: ReturnType | null; /** * Resolved options. * * @returns {Readonly} The options. */ get options(): Readonly; /** * Index of the sorted column inside its header row. * * @returns {number} The index, or `-1` when the table is in served order. */ get sortColumn(): number; /** * Direction of the current sort. * * @returns {SortDirection|null} The direction, or `null` when unsorted. */ get sortDirection(): SortDirection | null; /** * Body rows in served order. The array is a copy; the rows are the live ones. * * @returns {HTMLTableRowElement[]} The rows. */ get rows(): HTMLTableRowElement[]; /** * How many rows pass the current filter. * * @returns {number} The count. */ get visibleRows(): number; /** * The query the rows are currently filtered by. * * @returns {string} The query, or an empty string. */ get query(): string; /** * Remembers the current value of an attribute, once per element. * * @param {Element} el Element to remember. * @param {string} name Attribute name. * @returns {void} */ _remember(el: Element, name: string): void; /** * Sets a managed attribute, remembering its previous value. * * @param {Element} el Target element. * @param {string} name Attribute name. * @param {string} value Attribute value. * @returns {void} */ _set(el: Element, name: string, value: string): void; /** * Removes a managed attribute, remembering its previous value. * * @param {Element} el Target element. * @param {string} name Attribute name. * @returns {void} */ _unset(el: Element, name: string): void; /** * Cancels a pending debounced filter. * * @returns {void} */ _clearTimer(): void; /** * How many columns the table has, so the empty row can span all of them. * * @returns {number} The column count, at least `1`. */ _columnCount(): number; /** * Locale used for text comparison: the option, else the `lang` of the * document, else the default of the runtime. * * @returns {string|undefined} The locale tag, or `undefined`. */ _locale(): string | undefined; /** * How many rows are not hidden by the filter. * * @returns {number} The count. */ _countVisible(): number; /** * Writes the announced row count. * * @param {number} visible Rows passing the filter. * @returns {void} */ _updateStatus(visible: number): void; /** * Resolves the argument of `sort` into a sortable column. * * @param {number|Element} target A column index or a header cell. * @returns {SortableColumn|null} The column, or `null` when it cannot sort. */ _resolveColumn(target: number | Element): SortableColumn | null; /** * Moves the rows of every section into the requested order and marks the * header. Rows whose cell holds no value of the column type go last in both * directions; ties keep the served order, so the sort is stable. * * @param {SortableColumn} column Column to sort by. * @param {SortDirection} direction Direction to sort in. * @returns {void} */ _applySort(column: SortableColumn, direction: SortDirection): void; /** * Puts the rows back in served order and drops the sort state. * * @returns {void} */ _restoreOrder(): void; /** * Writes `aria-sort` on the sorted header and removes it from the others. * * @param {number} index Index of the sorted column, or `-1`. * @param {SortDirection|null} direction Direction, or `null`. * @returns {void} */ _markSort(index: number, direction: SortDirection | null): void; /** * Hides the rows that do not match and updates the empty row and the count. * * @param {string} query Query to apply. * @returns {number} How many rows remain visible. */ _applyQuery(query: string): number; /** * Runs a filter: `iv:filter` (cancelable) → rows → `iv:filtered`. * * @param {string} query Query to apply. * @param {boolean} sync Whether the input must show the query. * @returns {void} */ _filterTo(query: string, sync: boolean): void; /** * Debounces typing in the filter input. * * @returns {void} */ _onFilterInput(): void; /** * Sorts by a column. Without a direction the click order applies: * ascending → descending → ascending. * * @param {number|Element} target Column index or header cell. * @param {SortDirection} [direction] Direction to sort in. * @returns {void} */ sort(target: number | Element, direction?: SortDirection): void; /** * Filters the rows immediately, skipping the `filterDelay` of typing. * * @param {string} query Text to look for. * @returns {void} */ filter(query: string): void; /** * Clears the filter and shows every row. * * @returns {void} */ clearFilter(): void; /** * Restores the served row order and clears the filter. Both steps are an * explicit API restore, so only the past-tense events are emitted. * * @returns {void} */ reset(): void; } export type DataTableOptions = { /** * Milliseconds waited after typing before the filter runs. */ filterDelay: number; /** * Text of the row shown when nothing matches. */ emptyText: string; /** * Announced row count; `{visible}` and `{total}` are replaced. */ statusText: string; /** * BCP 47 tag for text sorting; `null` falls back to the `lang` of the document. */ locale: string | null; }; export type SortDirection = "ascending" | "descending"; export type ColumnType = "text" | "number" | "date"; /** * A sortable column: the header cell, the button `init` put inside it and the * child nodes that button borrowed. */ export type SortableColumn = { /** * Header cell. */ th: HTMLTableCellElement; /** * Generated sort button. */ button: HTMLButtonElement; /** * Index of the cell inside its header row. */ index: number; /** * How the values of the column are compared. */ type: ColumnType; /** * Original child nodes of the header cell. */ content: ChildNode[]; }; /** * A `` and the children it had before `init` touched it. */ export type TableBody = { /** * The section. */ tbody: HTMLTableSectionElement; /** * Its rows, in served order. */ rows: HTMLTableRowElement[]; /** * Every child node, in served order. */ nodes: ChildNode[]; }; /** * One row decorated with the comparable value of the sorted column. */ export type SortEntry = { /** * The row. */ row: HTMLTableRowElement; /** * Position in the served order, used to break ties. */ index: number; /** * Raw cell value, for text comparison. */ text: string; /** * Numeric or time value, for the other types. */ value: number; /** * Whether the cell holds a value of the column type. */ valid: boolean; }; import { IvComponent } from "../core/component.js";