@itwin/presentation-frontend
Version:
Frontend of iModel.js Presentation library
132 lines • 4.63 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable @typescript-eslint/no-deprecated */
/** @packageDocumentation
* @module UnifiedSelection
*/
import { DisposableList } from "@itwin/core-bentley";
/**
* A class that handles selection changes and helps to change
* internal the selection state.
*
* @public
* @deprecated in 5.0 - will not be removed until after 2026-06-13. Use `SelectionStorage` from [@itwin/unified-selection](https://github.com/iTwin/presentation/blob/master/packages/unified-selection/README.md#basic-usage) package instead.
*/
export class SelectionHandler {
_inSelect;
_disposables;
/** Selection manager used by this handler to manage selection */
manager;
/** Name that's used as `SelectionChangeEventArgs.source` when making selection changes */
name;
/** iModel whose selection is being handled */
imodel;
/**
* Id of a ruleset selection changes will be associated with.
* @see `SelectionHandlerProps.rulesetId`
*/
rulesetId;
/** Callback function called when selection changes */
onSelect;
/**
* Constructor.
*/
constructor(props) {
this._inSelect = false;
this.manager = props.manager;
this._disposables = new DisposableList();
this.name = props.name;
this.rulesetId = props.rulesetId;
this.imodel = props.imodel;
this.onSelect = props.onSelect;
this._disposables.add(this.manager.selectionChange.addListener(this.onSelectionChanged));
}
/**
* Destructor. Must be called before disposing this object to make sure it cleans
* up correctly.
*/
[Symbol.dispose]() {
this._disposables.dispose();
}
/** @deprecated in 5.0 - will not be removed until after 2026-06-13. Use [Symbol.dispose] instead. */
/* c8 ignore next 3 */
dispose() {
this[Symbol.dispose]();
}
/**
* Called when the selection changes. Handles this callback by first checking whether
* the event should be handled at all (using the `shouldHandle` method) and then calling `onSelect`
*/
onSelectionChanged = (evt, provider) => {
if (!this.onSelect || !this.shouldHandle(evt)) {
return;
}
this._inSelect = true;
this.onSelect(evt, provider);
this._inSelect = false;
};
/** Called to check whether the event should be handled by this handler */
shouldHandle(evt) {
if (this.name === evt.source) {
return false;
}
return true;
}
/** Get selection levels for the imodel managed by this handler */
getSelectionLevels() {
return this.manager.getSelectionLevels(this.imodel);
}
/**
* Get selection for the imodel managed by this handler.
* @param level Level of the selection to get. Defaults to 0.
*/
getSelection(level) {
return this.manager.getSelection(this.imodel, level);
}
/**
* Add to selection.
* @param keys The keys to add to selection.
* @param level Level of the selection.
*/
addToSelection(keys, level = 0) {
if (this._inSelect) {
return;
}
return this.manager.addToSelection(this.name, this.imodel, keys, level, this.rulesetId);
}
/**
* Remove from selection.
* @param keys The keys to remove from selection.
* @param level Level of the selection.
*/
removeFromSelection(keys, level = 0) {
if (this._inSelect) {
return;
}
return this.manager.removeFromSelection(this.name, this.imodel, keys, level, this.rulesetId);
}
/**
* Change selection.
* @param keys The keys indicating the new selection.
* @param level Level of the selection.
*/
replaceSelection(keys, level = 0) {
if (this._inSelect) {
return;
}
return this.manager.replaceSelection(this.name, this.imodel, keys, level, this.rulesetId);
}
/**
* Clear selection.
* @param level Level of the selection.
*/
clearSelection(level = 0) {
if (this._inSelect) {
return;
}
return this.manager.clearSelection(this.name, this.imodel, level, this.rulesetId);
}
}
//# sourceMappingURL=SelectionHandler.js.map