@itwin/presentation-frontend
Version:
Frontend of iModel.js Presentation library
93 lines • 4.08 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 { DEFAULT_KEYS_BATCH_SIZE, KeySet, } from "@itwin/presentation-common";
/**
* A manager that knows available [selection scopes]($docs/presentation/unified-selection/index#selection-scopes)
* and can compute logical selection based on element IDs and selection scope.
*
* @public
* @deprecated in 5.0 - will not be removed until after 2026-06-13. Use `computeSelection` from [@itwin/unified-selection](https://github.com/iTwin/presentation/blob/master/packages/unified-selection/README.md#selection-scopes) package instead.
*/
export class SelectionScopesManager {
_rpcRequestsHandler;
_getLocale;
_activeScope;
constructor(props) {
this._rpcRequestsHandler = props.rpcRequestsHandler;
this._getLocale = props.localeProvider ? props.localeProvider : () => undefined;
}
/** Get active locale */
get activeLocale() {
return this._getLocale();
}
/** The active selection scope or its id */
get activeScope() {
return this._activeScope;
}
set activeScope(scope) {
this._activeScope = scope;
}
/**
* Get available selection scopes.
* @param imodel The iModel to get selection scopes for
* @param locale Optional locale to use when localizing scopes' label and description
*/
async getSelectionScopes(imodel, locale) {
if (!locale) {
locale = this._getLocale();
}
return this._rpcRequestsHandler.getSelectionScopes({ imodel: imodel.getRpcProps(), locale });
}
/**
* Computes keys that need to be added to logical selection based on provided selection scope.
* @param ids Element IDs to compute selection for
* @param scope Selection scope to apply
*/
async computeSelection(imodel, ids, scope) {
const scopeProps = createSelectionScopeProps(scope);
// convert ids input to array
if (typeof ids === "string") {
ids = [ids];
}
else if (ids instanceof Set) {
ids = [...ids];
}
// compute selection in batches to avoid HTTP 413
const keys = new KeySet();
const batchSize = DEFAULT_KEYS_BATCH_SIZE;
const batchesCount = Math.ceil(ids.length / batchSize);
const batchKeyPromises = [];
for (let batchIndex = 0; batchIndex < batchesCount; ++batchIndex) {
const batchStart = batchSize * batchIndex;
const batchEnd = batchStart + batchSize > ids.length ? ids.length : batchStart + batchSize;
const batchIds = 0 === batchIndex && ids.length <= batchEnd ? ids : ids.slice(batchStart, batchEnd);
batchKeyPromises.push(this._rpcRequestsHandler.computeSelection({ imodel: imodel.getRpcProps(), elementIds: batchIds, scope: scopeProps }));
}
const batchKeys = (await Promise.all(batchKeyPromises)).map((json) => KeySet.fromJSON(json));
batchKeys.forEach((bk) => keys.add(bk));
return keys;
}
}
/**
* Normalizes given scope options and returns [[SelectionScopeProps]] that can be used for
* calculating selection with scope.
*
* @public
* @deprecated in 5.0 - will not be removed until after 2026-06-13. Use `computeSelection` from [@itwin/unified-selection](https://github.com/iTwin/presentation/blob/master/packages/unified-selection/README.md#selection-scopes) package instead.
*/
export function createSelectionScopeProps(scope) {
if (!scope) {
return { id: "element" };
}
if (typeof scope === "string") {
return { id: scope };
}
return scope;
}
//# sourceMappingURL=SelectionScopesManager.js.map