UNPKG

@itwin/presentation-hierarchies

Version:

A package for creating hierarchies based on data in iTwin.js iModels.

93 lines 4.71 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.createPredicateBasedHierarchyDefinition = createPredicateBasedHierarchyDefinition; const rxjs_1 = require("rxjs"); const HierarchyNode_js_1 = require("../HierarchyNode.js"); /** * Creates an instance of `HierarchyDefinition` that uses a somewhat declarative approach to define the * hierarchy - each hierarchy level is defined by specifying a parent node predicate and child hierarchy * level definitions, that are used when the predicate passes. * * @public */ function createPredicateBasedHierarchyDefinition(props) { return new PredicateBasedHierarchyDefinition(props); } class PredicateBasedHierarchyDefinition { _props; parseNode; preProcessNode; postProcessNode; constructor(_props) { this._props = _props; if (this._props.parseNode) { this.parseNode = this._props.parseNode; } if (this._props.preProcessNode) { this.preProcessNode = this._props.preProcessNode; } if (this._props.postProcessNode) { this.postProcessNode = this._props.postProcessNode; } } /** * Create hierarchy level definitions for specific hierarchy level. * @param parentNode Parent node to create children definitions for. */ async defineHierarchyLevel(props) { const { parentNode } = props; if (parentNode && HierarchyNode_js_1.HierarchyNode.isGeneric(parentNode)) { return (0, rxjs_1.firstValueFrom)((0, rxjs_1.from)(this._props.hierarchy.childNodes).pipe((0, rxjs_1.filter)(isGenericNodeChildHierarchyLevelDefinition), (0, rxjs_1.mergeMap)(async (def) => { if (await def.parentGenericNodePredicate(parentNode.key)) { return def.definitions({ ...props, parentNode }); } return []; }), (0, rxjs_1.mergeAll)(), (0, rxjs_1.toArray)())); } if (parentNode && HierarchyNode_js_1.HierarchyNode.isInstancesNode(parentNode)) { const instancesParentNodeDefs = this._props.hierarchy.childNodes.filter(isInstancesNodeChildHierarchyLevelDefinition); return (0, rxjs_1.firstValueFrom)((0, rxjs_1.from)(groupInstanceIdsByClass(parentNode.key.instanceKeys).entries()).pipe((0, rxjs_1.mergeMap)(async ([parentNodeClassName, parentNodeInstanceIds]) => createHierarchyLevelDefinitions(this._props.classHierarchyInspector, instancesParentNodeDefs, { ...props, parentNodeClassName, parentNodeInstanceIds, parentNode, })), (0, rxjs_1.mergeAll)(), (0, rxjs_1.toArray)())); } return this._props.hierarchy.rootNodes(props); } } async function createHierarchyLevelDefinitions(classHierarchy, defs, requestProps) { return (0, rxjs_1.from)(defs).pipe((0, rxjs_1.mergeMap)(async (def, idx) => { if (typeof def.parentInstancesNodePredicate === "string" && (await classHierarchy.classDerivesFrom(requestProps.parentNodeClassName, def.parentInstancesNodePredicate))) { return { def, idx }; } if (typeof def.parentInstancesNodePredicate === "function" && (await def.parentInstancesNodePredicate(requestProps.parentNode.key))) { return { def, idx }; } return undefined; }), (0, rxjs_1.filter)((x) => !!x), (0, rxjs_1.toArray)(), (0, rxjs_1.concatMap)((x) => x.sort((a, b) => a.idx - b.idx).map(({ def }) => def)), (0, rxjs_1.filter)((def, idx) => !def.onlyIfNotHandled || idx === 0), (0, rxjs_1.mergeMap)(async (def) => def.definitions(requestProps)), (0, rxjs_1.mergeAll)()); } function groupInstanceIdsByClass(instanceKeys) { const instanceIdsByClass = new Map(); instanceKeys.forEach((key) => { let instanceIds = instanceIdsByClass.get(key.className); if (!instanceIds) { instanceIds = []; instanceIdsByClass.set(key.className, instanceIds); } instanceIds.push(key.id); }); return instanceIdsByClass; } function isGenericNodeChildHierarchyLevelDefinition(def) { return !!def.parentGenericNodePredicate; } function isInstancesNodeChildHierarchyLevelDefinition(def) { return !!def.parentInstancesNodePredicate; } //# sourceMappingURL=PredicateBasedHierarchyDefinition.js.map