import { HidingMap, IndexesSequence, TrimmingMap } from './maps'; import type { IndexMap } from './maps/indexMap'; import type { LinkedPhysicalIndexToValueMap } from './maps/linkedPhysicalIndexToValueMap'; import type { PhysicalIndexToValueMap } from './maps/physicalIndexToValueMap'; import { AggregatedCollection, MapCollection } from './mapCollections'; import { ChangesObservable } from './changesObservable/observable'; /** * @class IndexMapper * @description * * Index mapper stores, registers and manages the indexes on the basis of calculations collected from the subsidiary maps. * It should be seen as a single source of truth (regarding row and column indexes, for example, their sequence, information if they are skipped in the process of rendering (hidden or trimmed), values linked to them) * for any operation that considers CRUD actions such as **insertion**, **movement**, **removal** etc, and is used to properly calculate physical and visual indexes translations in both ways. * It has a built-in cache that is updated only when the data or structure changes. * * **Physical index** is a type of an index from the sequence of indexes assigned to the data source rows or columns * (from 0 to n, where n is number of the cells on the axis of data set). * **Visual index** is a type of an index from the sequence of indexes assigned to rows or columns existing in {@link DataMap} (from 0 to n, where n is number of the cells on the axis of data set). * **Renderable index** is a type of an index from the sequence of indexes assigned to rows or columns whose may be rendered (when they are in a viewport; from 0 to n, where n is number of the cells renderable on the axis). * * There are different kinds of index maps which may be registered in the collections and can be used by a reference. * They also expose public API and trigger two local hooks such as `init` (on initialization) and `change` (on change). * * These are: {@link IndexesSequence}, {@link PhysicalIndexToValueMap}, {@link LinkedPhysicalIndexToValueMap}, {@link HidingMap}, and {@link TrimmingMap}. */ export declare class IndexMapper { #private; /** * Internal storage map for local hook callbacks, keyed by hook name. */ _localHooks: Record; /** * Registers a callback function for the given local hook name on this index mapper. */ addLocalHook: (key: string, callback: Function) => this; /** * Removes a previously registered callback function for the given local hook name. */ removeLocalHook: (key: string, callback: Function) => this; /** * Triggers all callbacks registered under the given local hook name, passing any additional arguments. */ runLocalHooks: (key: string, ...args: unknown[]) => void; /** * Map for storing the sequence of indexes. * * It is registered by default and may be used from API methods. * * @private * @type {IndexesSequence} */ indexesSequence: IndexesSequence; /** * Collection for different trimming maps. Indexes marked as trimmed in any map WILL NOT be included in * the {@link DataMap} and won't be rendered. * * @private * @type {MapCollection} */ trimmingMapsCollection: AggregatedCollection; /** * Collection for different hiding maps. Indexes marked as hidden in any map WILL be included in the {@link DataMap}, * but won't be rendered. * * @private * @type {MapCollection} */ hidingMapsCollection: AggregatedCollection; /** * Collection for another kind of maps. There are stored mappings from indexes (visual or physical) to values. * * @private * @type {MapCollection} */ variousMapsCollection: MapCollection; /** * The class instance collects row and column index changes that happen while the Handsontable * is running. The object allows creating observers that you can subscribe. Each event represents * the index change (e.g., insert, removing, change index value), which can be consumed by a * developer to update its logic. * * @private * @type {ChangesObservable} */ hidingChangesObservable: ChangesObservable; /** * Cache for list of not trimmed indexes, respecting the indexes sequence (physical indexes). * * Note: Please keep in mind that trimmed index can be also hidden. * * @private * @type {Array} */ notTrimmedIndexesCache: number[]; /** * Cache for list of not hidden indexes, respecting the indexes sequence (physical indexes). * * Note: Please keep in mind that hidden index can be also trimmed. * * @private * @type {Array} */ notHiddenIndexesCache: number[]; /** * Flag determining whether actions performed on index mapper have been batched. It's used for cache management. * * @private * @type {boolean} */ isBatched: boolean; /** * Flag determining whether any action on indexes sequence has been performed. It's used for cache management. * * @private * @type {boolean} */ indexesSequenceChanged: boolean; /** * Flag informing about source of the change. * * @type {undefined|string} */ indexesChangeSource: string | undefined; /** * Flag determining whether any action on trimmed indexes has been performed. It's used for cache management. * * @private * @type {boolean} */ trimmedIndexesChanged: boolean; /** * Flag determining whether any action on hidden indexes has been performed. It's used for cache management. * * @private * @type {boolean} */ hiddenIndexesChanged: boolean; /** * Physical indexes (respecting the sequence of indexes) which may be rendered (when they are in a viewport). * * @private * @type {Array} */ renderablePhysicalIndexesCache: number[]; /** * Visual indexes (native map's value) corresponding to physical indexes (native map's index). * * @private * @type {Map} */ fromPhysicalToVisualIndexesCache: Map; /** * Visual indexes (native map's value) corresponding to physical indexes (native map's index). * * @private * @type {Map} */ fromVisualToRenderableIndexesCache: Map; /** * Initializes the IndexMapper by wiring change listeners on the indexes sequence, trimming, and hiding map collections to keep caches up to date. */ constructor(); /** * Suspends the cache update for this map. The method is helpful to group multiple * operations, which affects the cache. In this case, the cache will be updated once after * calling the `resumeOperations` method. */ suspendOperations(): void; /** * Resumes the cache update for this map. It recalculates the cache and restores the * default behavior where each map modification updates the cache. */ resumeOperations(): void; /** * It creates and returns the new instance of the ChangesObserver object. The object * allows listening to the index changes that happen while the Handsontable is running. * * @param {string} indexMapType The index map type which we want to observe. * Currently, only the 'hiding' index map types are observable. * @returns {ChangesObserver} */ createChangesObserver(indexMapType: string): import("./changesObservable/observer").ChangesObserver; /** * Creates and registers a new `IndexMap` for a specified `IndexMapper` instance. For a known * `mapType` literal the return type narrows to the concrete map class ('hiding' -> `HidingMap`, * 'trimming' -> `TrimmingMap`, etc.); any other string falls back to the base `IndexMap`. * * @param {string} indexName A unique index name. * @param {string} mapType The index map type (e.g., "hiding", "trimming", "physicalIndexToValue"). * @param {*} [initValueOrFn] The initial value for the index map. * @returns {IndexMap} */ createAndRegisterIndexMap(indexName: string, mapType: 'hiding', initValueOrFn?: unknown): HidingMap; createAndRegisterIndexMap(indexName: string, mapType: 'trimming', initValueOrFn?: unknown): TrimmingMap; createAndRegisterIndexMap(indexName: string, mapType: 'physicalIndexToValue', initValueOrFn?: unknown): PhysicalIndexToValueMap; createAndRegisterIndexMap(indexName: string, mapType: 'linkedPhysicalIndexToValue', initValueOrFn?: unknown): LinkedPhysicalIndexToValueMap; createAndRegisterIndexMap(indexName: string, mapType: string, initValueOrFn?: unknown): IndexMap; /** * Register map which provide some index mappings. Type of map determining to which collection it will be added. * * @param {string} uniqueName Name of the index map. It should be unique. * @param {IndexMap} indexMap Registered index map updated on items removal and insertion. * @returns {IndexMap} */ registerMap(uniqueName: string, indexMap: T): T; /** * Unregister a map with given name. * * @param {string} name Name of the index map. */ unregisterMap(name: string): void; /** * Unregisters all collected index map instances from all map collection types. */ unregisterAll(): void; /** * Get a physical index corresponding to the given visual index. * * @param {number} visualIndex Visual index. * @returns {number|null} Returns translated index mapped by passed visual index. */ getPhysicalFromVisualIndex(visualIndex: number): number | null; /** * Get a physical index corresponding to the given renderable index. * * @param {number} renderableIndex Renderable index. * @returns {null|number} */ getPhysicalFromRenderableIndex(renderableIndex: number): number | null; /** * Get a visual index corresponding to the given physical index. * * @param {number} physicalIndex Physical index to search. * @returns {number|null} Returns a visual index of the index mapper. */ getVisualFromPhysicalIndex(physicalIndex: number): number | null; /** * Get a visual index corresponding to the given renderable index. * * @param {number} renderableIndex Renderable index. * @returns {null|number} */ getVisualFromRenderableIndex(renderableIndex: number): number | null; /** * Get a renderable index corresponding to the given visual index. * * @param {number} visualIndex Visual index. * @returns {null|number} */ getRenderableFromVisualIndex(visualIndex: number): number | null; /** * Search for the nearest not-hidden row or column. * * @param {number} fromVisualIndex The visual index of the row or column from which the search starts.

* If the row or column from which the search starts is not hidden, the method simply returns the `fromVisualIndex` number. * @param {number} searchDirection The search direction.

`1`: search from `fromVisualIndex` to the end of the dataset.

* `-1`: search from `fromVisualIndex` to the beginning of the dataset (i.e., to the row or column at visual index `0`). * @param {boolean} searchAlsoOtherWayAround `true`: if a search in a first direction failed, try the opposite direction.

* `false`: search in one direction only. * * @returns {number|null} A visual index of a row or column, or `null`. */ getNearestNotHiddenIndex(fromVisualIndex: number, searchDirection: 1 | -1, searchAlsoOtherWayAround?: boolean): number | null; /** * Set default values for all indexes in registered index maps. * * @param {number} [length] Destination length for all stored index maps. */ initToLength(length?: number): void; /** * Trim/extend the mappers to fit the desired length. * * @param {number} length New mapper length. */ fitToLength(length: number): void; /** * Get sequence of indexes. * * @returns {Array} Physical indexes. */ getIndexesSequence(): number[]; /** * Set completely new indexes sequence. * * @param {Array} indexes Physical indexes. */ setIndexesSequence(indexes: number[]): void; /** * Get all NOT trimmed indexes. * * Note: Indexes marked as trimmed aren't included in a {@link DataMap} and aren't rendered. * * @param {boolean} [readFromCache=true] Determine if read indexes from cache. * @returns {Array} List of physical indexes. Index of this native array is a "visual index", * value of this native array is a "physical index". */ getNotTrimmedIndexes(readFromCache?: boolean): number[]; /** * Get length of all NOT trimmed indexes. * * Note: Indexes marked as trimmed aren't included in a {@link DataMap} and aren't rendered. * * @returns {number} */ getNotTrimmedIndexesLength(): number; /** * Get all NOT hidden indexes. * * Note: Indexes marked as hidden are included in a {@link DataMap}, but aren't rendered. * * @param {boolean} [readFromCache=true] Determine if read indexes from cache. * @returns {Array} List of physical indexes. Please keep in mind that index of this native array IS NOT a "visual index". */ getNotHiddenIndexes(readFromCache?: boolean): number[]; /** * Get length of all NOT hidden indexes. * * Note: Indexes marked as hidden are included in a {@link DataMap}, but aren't rendered. * * @returns {number} */ getNotHiddenIndexesLength(): number; /** * Get list of physical indexes (respecting the sequence of indexes) which may be rendered (when they are in a viewport). * * @param {boolean} [readFromCache=true] Determine if read indexes from cache. * @returns {Array} List of physical indexes. Index of this native array is a "renderable index", * value of this native array is a "physical index". */ getRenderableIndexes(readFromCache?: boolean): number[]; /** * Get length of all NOT trimmed and NOT hidden indexes. * * @returns {number} */ getRenderableIndexesLength(): number; /** * Get number of all indexes. * * @returns {number} */ getNumberOfIndexes(): number; /** * Move indexes in the index mapper. * * @param {number|Array} movedIndexes Visual index(es) to move. * @param {number} finalIndex Visual index being a start index for the moved elements. */ moveIndexes(movedIndexes: number | number[], finalIndex: number): void; /** * Get whether index is trimmed. Index marked as trimmed isn't included in a {@link DataMap} and isn't rendered. * * @param {number} physicalIndex Physical index. * @returns {boolean} */ isTrimmed(physicalIndex: number): boolean; /** * Get whether index is hidden. Index marked as hidden is included in a {@link DataMap}, but isn't rendered. * * @param {number} physicalIndex Physical index. * @returns {boolean} */ isHidden(physicalIndex: number): boolean; /** * Insert new indexes and corresponding mapping and update values of the others, for all stored index maps. * * @private * @param {number} firstInsertedVisualIndex First inserted visual index. * @param {number} amountOfIndexes Amount of inserted indexes. * @param {'start' | 'end'} [mode] Sets where the column is inserted: at the start of the passed index or at the end. */ insertIndexes(firstInsertedVisualIndex: number, amountOfIndexes: number, mode?: 'start' | 'end'): void; /** * Remove some indexes and corresponding mappings and update values of the others, for all stored index maps. * * @private * @param {Array} removedIndexes List of removed indexes. */ removeIndexes(removedIndexes: number[]): void; /** * Registers an observer for batched changes on a specific index map. During batched * operations ({@link IndexMapper#suspendOperations}/{@link IndexMapper#resumeOperations}), * changes are coalesced and the callback fires once when the batch completes. Outside of * batching, the callback fires immediately on each change. * * Works with maps registered in the various maps collection. * * @param {IndexMap} map The map instance to observe. * @param {Function} callback Called when the observed map's values change (coalesced during batches). * @returns {Function} Disposer function that removes the observer. */ observeMapChange(map: IndexMap, callback: (map: IndexMap) => void): () => void; /** * Rebuild cache for some indexes. Every action on indexes sequence or indexes skipped in the process of rendering * by default reset cache, thus batching some index maps actions is recommended. * * @private * @param {boolean} [force=false] Determine if force cache update. */ updateCache(force?: boolean): void; /** * Update cache for translations from physical to visual indexes. * * @private */ cacheFromPhysicalToVisualIndexes(): void; /** * Update cache for translations from visual to renderable indexes. * * @private */ cacheFromVisualToRenderableIndexes(): void; /** * Destroys the IndexMapper instance. Clears all registered map observers * and unregisters all maps from all collections. */ destroy(): void; }