@itwin/presentation-frontend
Version:
Frontend of iModel.js Presentation library
103 lines • 4.06 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module UnifiedSelection
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectionScopesManager = void 0;
exports.createSelectionScopeProps = createSelectionScopeProps;
exports.getScopeId = getScopeId;
const presentation_common_1 = require("@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
*/
class SelectionScopesManager {
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 presentation_common_1.KeySet();
const batchSize = presentation_common_1.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) => presentation_common_1.KeySet.fromJSON(json));
batchKeys.forEach((bk) => keys.add(bk));
return keys;
}
}
exports.SelectionScopesManager = SelectionScopesManager;
/**
* Normalizes given scope options and returns [[SelectionScopeProps]] that can be used for
* calculating selection with scope.
*
* @public
*/
function createSelectionScopeProps(scope) {
if (!scope) {
return { id: "element" };
}
if (typeof scope === "string") {
return { id: scope };
}
return scope;
}
/**
* Determines the scope id
* @param scope Selection scope
* @public
* @deprecated in 3.x. This is an internal utility that should've never become public.
*/
// istanbul ignore next
function getScopeId(scope) {
return createSelectionScopeProps(scope).id;
}
//# sourceMappingURL=SelectionScopesManager.js.map