@itwin/presentation-testing
Version:
Testing utilities for iTwin.js Presentation library
76 lines • 3.67 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 Hierarchies
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.HierarchyBuilder = exports.defaultNodeMappingFunc = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const presentation_components_1 = require("@itwin/presentation-components");
const presentation_frontend_1 = require("@itwin/presentation-frontend");
/**
* Default [[NodeMappingFunc]] implementation that outputs the whole `TreeNodeItem` object.
* @public
* @deprecated in 5.2. All tree-related APIs have been deprecated in favor of the new generation hierarchy
* building APIs (see https://github.com/iTwin/presentation/blob/33e79ee8d77f30580a9bab81a72884bda008db25/README.md#the-packages).
*/
const defaultNodeMappingFunc = (node) => {
// Skip properties 'id', 'parentId' as they contain internal stuff
// that callers are most likely not interested in. Otherwise they can supply
// a custom `NodeMappingFunc` that does return those properties as well.
const { id, parentId, ...resultNode } = node; // eslint-disable-line @typescript-eslint/no-unused-vars
return resultNode;
};
exports.defaultNodeMappingFunc = defaultNodeMappingFunc;
/**
* A class that constructs simple node hierarchy from specified
* imodel and ruleset.
*
* @public
* @deprecated in 5.2. All tree-related APIs have been deprecated in favor of the new generation hierarchy
* building APIs (see https://github.com/iTwin/presentation/blob/33e79ee8d77f30580a9bab81a72884bda008db25/README.md#the-packages).
*/
class HierarchyBuilder {
_iModel;
_nodeMappingFunc;
/** Constructor */
constructor(props) {
this._iModel = props.imodel;
this._nodeMappingFunc = props.nodeMappingFunc ?? exports.defaultNodeMappingFunc;
}
async createSubHierarchy(nodes, dataProvider) {
const hierarchy = [];
for (const node of nodes) {
const { key: _key, ...nodeNoKey } = node;
const nodeIndex = hierarchy.push(this._nodeMappingFunc(nodeNoKey)) - 1;
const childNodes = await dataProvider.getNodes(node);
if (childNodes.length > 0) {
hierarchy[nodeIndex].children = await this.createSubHierarchy(childNodes, dataProvider);
}
}
return hierarchy;
}
async doCreateHierarchy(rulesetId) {
const dataProvider = new presentation_components_1.PresentationTreeDataProvider({ imodel: this._iModel, ruleset: rulesetId });
const rootNodes = await dataProvider.getNodes();
return this.createSubHierarchy(rootNodes, dataProvider);
}
/**
* Create a hierarchy using the supplied presentation ruleset.
* @param rulesetOrId Either a [Ruleset]($presentation-common) object or a ruleset id.
*/
async createHierarchy(rulesetOrId) {
if (typeof rulesetOrId === "string") {
return this.doCreateHierarchy(rulesetOrId);
}
return (0, core_bentley_1.using)(await presentation_frontend_1.Presentation.presentation.rulesets().add(rulesetOrId), async (ruleset) => {
return this.doCreateHierarchy(ruleset.id);
});
}
}
exports.HierarchyBuilder = HierarchyBuilder;
//# sourceMappingURL=HierarchyBuilder.js.map