UNPKG

@itwin/presentation-hierarchies-react

Version:

React components based on `@itwin/presentation-hierarchies`

131 lines 5.8 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.useUnifiedTreeSelection = useUnifiedTreeSelection; const react_1 = require("react"); const core_bentley_1 = require("@itwin/core-bentley"); const presentation_hierarchies_1 = require("@itwin/presentation-hierarchies"); const unified_selection_1 = require("@itwin/unified-selection"); const UnifiedSelectionContext_js_1 = require("../UnifiedSelectionContext.js"); const TreeModel_js_1 = require("./TreeModel.js"); /** @internal */ function useUnifiedTreeSelection({ sourceName, selectionStorage, getTreeModelNode, }) { const [options, setOptions] = (0, react_1.useState)(() => ({ isNodeSelected: /* c8 ignore next */ () => false, selectNodes: /* c8 ignore next */ () => { }, })); const deprecatedSelectionStorage = (0, UnifiedSelectionContext_js_1.useUnifiedSelectionStorage)(); selectionStorage ??= deprecatedSelectionStorage; (0, react_1.useEffect)(() => { if (!selectionStorage) { // TODO: make selectionStorage prop required setOptions({ isNodeSelected: () => false, selectNodes: () => { }, }); return; } setOptions(createOptions(sourceName, selectionStorage, getTreeModelNode)); return selectionStorage.selectionChangeEvent.addListener((args) => { if (args.level > 0) { return; } setOptions(createOptions(sourceName, selectionStorage, getTreeModelNode)); }); }, [selectionStorage, getTreeModelNode, sourceName]); return options; } function createOptions(source, storage, getNode) { return { isNodeSelected: (nodeId) => { const node = getNode(nodeId); if (!node || !(0, TreeModel_js_1.isTreeModelHierarchyNode)(node)) { return false; } return Object.entries(groupNodeSelectablesByIModelKey(node)).some(([imodelKey, nodeSelectables]) => { const storageSelectables = storage.getSelection({ imodelKey, level: 0 }); return unified_selection_1.Selectables.hasAny(storageSelectables, nodeSelectables); }); }, selectNodes: (nodeIds, changeType) => { const imodelSelectables = {}; for (const nodeId of nodeIds) { const node = getNode(nodeId); if (!node || !(0, TreeModel_js_1.isTreeModelHierarchyNode)(node)) { return; } Object.entries(groupNodeSelectablesByIModelKey(node)).forEach(([imodelKey, nodeSelectables]) => { let selectablesList = imodelSelectables[imodelKey]; if (!selectablesList) { selectablesList = []; imodelSelectables[imodelKey] = selectablesList; } nodeSelectables.forEach((selectable) => selectablesList.push(selectable)); }); } Object.entries(imodelSelectables).forEach(([imodelKey, selectables]) => { const actionProps = { imodelKey, source, selectables, level: 0 }; switch (changeType) { case "add": storage.addToSelection(actionProps); return; case "remove": storage.removeFromSelection(actionProps); return; case "replace": storage.replaceSelection(actionProps); return; } }); }, }; } function groupNodeSelectablesByIModelKey(modelNode) { const hierarchyNode = modelNode.nodeData; if (presentation_hierarchies_1.HierarchyNode.isInstancesNode(hierarchyNode)) { return groupIModelInstanceKeys(hierarchyNode.key.instanceKeys); } if (presentation_hierarchies_1.HierarchyNode.isGroupingNode(hierarchyNode)) { return Object.entries(groupIModelInstanceKeys(hierarchyNode.groupedInstanceKeys)).reduce((imodelSelectables, [imodelKey, instanceKeys]) => ({ ...imodelSelectables, [imodelKey]: [ { identifier: modelNode.id, data: hierarchyNode, async *loadInstanceKeys() { for (const key of instanceKeys) { yield key; } }, }, ], }), {}); } (0, core_bentley_1.assert)(presentation_hierarchies_1.HierarchyNode.isGeneric(hierarchyNode)); return { // note: generic nodes aren't associated with an imodel [""]: [ { identifier: modelNode.id, data: hierarchyNode, async *loadInstanceKeys() { }, }, ], }; } function groupIModelInstanceKeys(instanceKeys) { return instanceKeys.reduce((imodelSelectables, key) => { const imodelKey = key.imodelKey ?? ""; let selectablesList = imodelSelectables[imodelKey]; if (!selectablesList) { selectablesList = []; imodelSelectables[imodelKey] = selectablesList; } selectablesList.push(key); return imodelSelectables; }, {}); } //# sourceMappingURL=UseUnifiedSelection.js.map