@itwin/presentation-components
Version:
React components based on iTwin.js Presentation library
124 lines • 5.12 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 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilteredPresentationTreeDataProvider = void 0;
require("../common/DisposePolyfill.js");
const components_react_1 = require("@itwin/components-react");
const Utils_js_1 = require("../common/Utils.js");
const DataProvider_js_1 = require("./DataProvider.js");
const Utils_js_2 = require("./Utils.js");
/**
* Rules-driven presentation tree data provider that returns filtered results.
* @internal
*/
class FilteredPresentationTreeDataProvider {
_parentDataProvider;
_filteredDataProvider;
_filter;
_filteredResultMatches = [];
constructor(props) {
const { filter, parentDataProvider } = props;
this._parentDataProvider = parentDataProvider;
this._filter = filter;
const treeNodeItemFactory = parentDataProvider instanceof DataProvider_js_1.PresentationTreeDataProvider
? (node, parentId) => (0, Utils_js_2.createTreeNodeItem)(node, parentId, parentDataProvider.props)
: Utils_js_2.createTreeNodeItem;
const hierarchy = new Map();
this.createHierarchy(props.paths, hierarchy, treeNodeItemFactory);
this._filteredDataProvider = new components_react_1.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 = (0, Utils_js_1.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);
}
}
exports.FilteredPresentationTreeDataProvider = FilteredPresentationTreeDataProvider;
//# sourceMappingURL=FilteredDataProvider.js.map