UNPKG

@itwin/presentation-hierarchies

Version:

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

68 lines 5.25 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.LOGGING_NAMESPACE = void 0; exports.createHideNodesInHierarchyOperator = createHideNodesInHierarchyOperator; const rxjs_1 = require("rxjs"); const core_bentley_1 = require("@itwin/core-bentley"); const HierarchyNodeKey_js_1 = require("../../HierarchyNodeKey.js"); const Common_js_1 = require("../../internal/Common.js"); const LoggingUtils_js_1 = require("../../internal/LoggingUtils.js"); const Partition_js_1 = require("../../internal/operators/Partition.js"); const ReduceToMergeMap_js_1 = require("../../internal/operators/ReduceToMergeMap.js"); const IModelHierarchyNode_js_1 = require("../IModelHierarchyNode.js"); const Utils_js_1 = require("../Utils.js"); const OPERATOR_NAME = "HideNodesInHierarchy"; /** @internal */ exports.LOGGING_NAMESPACE = (0, Common_js_1.createOperatorLoggingNamespace)(OPERATOR_NAME, Common_js_1.LOGGING_NAMESPACE_INTERNAL); /** * Creates an operator that hides nodes and instead returns their children if the nodes have a `hideInHierarchy` handling param. * * @internal */ function createHideNodesInHierarchyOperator(getNodes, stopOnFirstChild) { return function (nodes) { const inputNodes = nodes.pipe((0, LoggingUtils_js_1.log)({ category: exports.LOGGING_NAMESPACE, message: /* c8 ignore next */ (n) => `in: ${(0, Common_js_1.createNodeIdentifierForLogging)(n)}` })); const [withFlag, withoutFlag] = (0, Partition_js_1.partition)(inputNodes, (n) => (IModelHierarchyNode_js_1.ProcessedHierarchyNode.isGeneric(n) || IModelHierarchyNode_js_1.ProcessedHierarchyNode.isInstancesNode(n)) && !!n.processingParams?.hideInHierarchy); // Defer to create a new seed for reduce on every subscribe const withLoadedChildren = (0, rxjs_1.defer)(() => withFlag.pipe((0, LoggingUtils_js_1.log)({ category: exports.LOGGING_NAMESPACE, message: /* c8 ignore next */ (n) => `${(0, Common_js_1.createNodeIdentifierForLogging)(n)} needs hide and needs children to be loaded`, }), (0, rxjs_1.filter)((node) => node.children !== false), (0, ReduceToMergeMap_js_1.reduceToMergeMapItem)((node) => createMergeMapKey(node), (node, mergedNode) => { if (mergedNode) { if (IModelHierarchyNode_js_1.ProcessedHierarchyNode.isGeneric(mergedNode)) { // we don't merge generic nodes, just return the first one return mergedNode; } (0, core_bentley_1.assert)(IModelHierarchyNode_js_1.ProcessedHierarchyNode.isInstancesNode(node)); return (0, Utils_js_1.mergeInstanceNodes)(mergedNode, node); } return node; }), (0, LoggingUtils_js_1.log)({ category: exports.LOGGING_NAMESPACE, message: /* c8 ignore next */ (mm) => `created a merge map of size ${mm.size}` }), (0, rxjs_1.mergeMap)((mm) => [...mm.values()].map((mergedNode) => (0, rxjs_1.defer)(() => getNodes(mergedNode)))), (0, rxjs_1.mergeAll)(), (0, rxjs_1.map)((n) => n))); return (0, rxjs_1.merge)(withoutFlag.pipe((0, LoggingUtils_js_1.log)({ category: exports.LOGGING_NAMESPACE, message: /* c8 ignore next */ (n) => `${(0, Common_js_1.createNodeIdentifierForLogging)(n)} doesn't need hide, return the node`, })), stopOnFirstChild ? (0, rxjs_1.concat)( // a small hack to handle situation when we're here to only check if parent node has children and one of them has `hideIfNoChildren` flag // with a `hasChildren = true` - we just return the hidden node itself in that case to avoid digging deeper into the hierarchy inputNodes.pipe((0, rxjs_1.filter)(Common_js_1.hasChildren), (0, LoggingUtils_js_1.log)({ category: exports.LOGGING_NAMESPACE, message: /* c8 ignore next */ (n) => `\`stopOnFirstChild = true\` and node ${(0, Common_js_1.createNodeIdentifierForLogging)(n)} is set to always have nodes - return the hidden node without loading children`, })), rxjs_1.EMPTY.pipe((0, rxjs_1.finalize)(() => (0, LoggingUtils_js_1.doLog)({ category: exports.LOGGING_NAMESPACE, message: /* c8 ignore next */ () => `\`stopOnFirstChild = true\` but none of the nodes had children determined to \`true\` - do load children`, }))), withLoadedChildren).pipe((0, rxjs_1.take)(1)) : withLoadedChildren).pipe((0, LoggingUtils_js_1.log)({ category: exports.LOGGING_NAMESPACE, message: /* c8 ignore next */ (n) => `out: ${(0, Common_js_1.createNodeIdentifierForLogging)(n)}` })); }; } function createMergeMapKey(node) { if (HierarchyNodeKey_js_1.HierarchyNodeKey.isGeneric(node.key)) { return `${node.key.source ? `${node.key.source}:` : ""}${node.key.id}`; } return node.key.instanceKeys[0].className; } //# sourceMappingURL=HideNodesInHierarchy.js.map