UNPKG

@itwin/presentation-components

Version:

React components based on iTwin.js Presentation library

120 lines 4.84 kB
/*--------------------------------------------------------------------------------------------- * 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 */ import "../common/DisposePolyfill.js"; import { SimpleTreeDataProvider, } from "@itwin/components-react"; import { memoize } from "../common/Utils.js"; import { PresentationTreeDataProvider } from "./DataProvider.js"; import { createTreeNodeItem } from "./Utils.js"; /** * Rules-driven presentation tree data provider that returns filtered results. * @internal */ export class FilteredPresentationTreeDataProvider { _parentDataProvider; _filteredDataProvider; _filter; _filteredResultMatches = []; constructor(props) { const { filter, parentDataProvider } = props; this._parentDataProvider = parentDataProvider; this._filter = filter; const treeNodeItemFactory = parentDataProvider instanceof PresentationTreeDataProvider ? (node, parentId) => createTreeNodeItem(node, parentId, parentDataProvider.props) : createTreeNodeItem; const hierarchy = new Map(); this.createHierarchy(props.paths, hierarchy, treeNodeItemFactory); this._filteredDataProvider = new SimpleTreeDataProvider(hierarchy); } /* c8 ignore next - only here to meet interface's requirements, nothing to test */ [Symbol.dispose]() { } /* c8 ignore next - only here to meet interface's requirements, nothing to test */ dispose() { } get rulesetId() { return this._parentDataProvider.rulesetId; } get imodel() { return this._parentDataProvider.imodel; } get filter() { return this._filter; } get parentDataProvider() { return this._parentDataProvider; } createHierarchy(paths, hierarchy, treeNodeItemFactory, parentId) { const treeNodes = []; for (let i = 0; i < paths.length; i++) { const path = paths[i]; const node = treeNodeItemFactory(path.node, parentId); if (path.filteringData && path.filteringData.matchesCount) { this._filteredResultMatches.push({ id: node.id, matchesCount: path.filteringData.matchesCount }); } if (path.children.length !== 0) { this.createHierarchy(path.children, hierarchy, treeNodeItemFactory, node.id); node.hasChildren = true; node.autoExpand = true; } else { delete node.hasChildren; delete node.autoExpand; } treeNodes[i] = node; } hierarchy.set(parentId, treeNodes); } getActiveMatch = memoize((index) => { let activeMatch; if (index <= 0) { return undefined; } let i = 1; for (const node of this._filteredResultMatches) { if (index < i + node.matchesCount) { activeMatch = { nodeId: node.id, matchIndex: index - i, }; break; } i += node.matchesCount; } return activeMatch; }); /** Count filtering results. Including multiple possible matches within node labels */ countFilteringResults(nodePaths) { let resultCount = 0; // Loops through root level only for (const path of nodePaths) { if (path.filteringData) { resultCount += path.filteringData.matchesCount + path.filteringData.childMatchesCount; } } return resultCount; } async getNodes(parent, pageOptions) { return this._filteredDataProvider.getNodes(parent, pageOptions); } async getNodesCount(parent) { return this._filteredDataProvider.getNodesCount(parent); } async getFilteredNodePaths(filter) { return this._parentDataProvider.getFilteredNodePaths(filter); } createRequestOptions(parentKey, instanceFilter) { return this._parentDataProvider.createRequestOptions(parentKey, instanceFilter); } /** @deprecated in 4.0. Use [[isPresentationTreeNodeItem]] and [[PresentationTreeNodeItem.key]] to get [NodeKey]($presentation-common). */ /* c8 ignore start */ getNodeKey(node) { return this._parentDataProvider.getNodeKey(node); } /* c8 ignore end */ /** Check if node matches currently applied filter */ nodeMatchesFilter(node) { return this._filteredResultMatches.some((result) => result.id === node.id); } } //# sourceMappingURL=FilteredDataProvider.js.map