@itwin/presentation-common
Version:
Common pieces for iModel.js presentation packages
107 lines • 4.76 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Hierarchies
*/
import { assert } from "@itwin/core-bentley";
import { InstanceKey } from "../EC.js";
/**
* Standard node types
* @public
*/
export var StandardNodeTypes;
(function (StandardNodeTypes) {
StandardNodeTypes["ECInstancesNode"] = "ECInstancesNode";
StandardNodeTypes["ECClassGroupingNode"] = "ECClassGroupingNode";
StandardNodeTypes["ECPropertyGroupingNode"] = "ECPropertyGroupingNode";
StandardNodeTypes["DisplayLabelGroupingNode"] = "DisplayLabelGroupingNode";
})(StandardNodeTypes || (StandardNodeTypes = {}));
/** @public */
// eslint-disable-next-line @typescript-eslint/no-redeclare
export var NodeKey;
(function (NodeKey) {
/** Checks if the supplied key is an [[ECInstancesNodeKey]] */
function isInstancesNodeKey(key) {
return key.type === StandardNodeTypes.ECInstancesNode;
}
NodeKey.isInstancesNodeKey = isInstancesNodeKey;
/** Checks if the supplied key is an [[ECClassGroupingNodeKey]] */
function isClassGroupingNodeKey(key) {
return key.type === StandardNodeTypes.ECClassGroupingNode;
}
NodeKey.isClassGroupingNodeKey = isClassGroupingNodeKey;
/** Checks if the supplied key is an [[ECPropertyGroupingNodeKey]] */
function isPropertyGroupingNodeKey(key) {
return key.type === StandardNodeTypes.ECPropertyGroupingNode;
}
NodeKey.isPropertyGroupingNodeKey = isPropertyGroupingNodeKey;
/** Checks if the supplied key is a [[LabelGroupingNodeKey]] */
function isLabelGroupingNodeKey(key) {
return key.type === StandardNodeTypes.DisplayLabelGroupingNode;
}
NodeKey.isLabelGroupingNodeKey = isLabelGroupingNodeKey;
/** Checks if the supplied key is a grouping node key */
function isGroupingNodeKey(key) {
return isClassGroupingNodeKey(key) || isPropertyGroupingNodeKey(key) || isLabelGroupingNodeKey(key);
}
NodeKey.isGroupingNodeKey = isGroupingNodeKey;
/**
* Checks if two given node keys are equal, taking their versions into account.
*
* When comparing two keys of the same version, the algorithm uses [[NodeKey.pathFromRoot]] array
* which is the most accurate way of checking equality. However, when version are different,
* [[NodeKey.pathFromRoot]] array may contain different strings even though keys represent the same node.
* In that case equality is checked using other key attributes, depending on the type of the node (type,
* label, grouping class, property name, etc.).
*/
function equals(lhs, rhs) {
// types must always be equal
if (lhs.type !== rhs.type) {
return false;
}
// `pathFromRoot` lengths must always be equal
if (lhs.pathFromRoot.length !== rhs.pathFromRoot.length) {
return false;
}
// when versions are equal, compare using contents of `pathFromRoot` array
if (lhs.version === rhs.version) {
for (let i = 0; i < lhs.pathFromRoot.length; ++i) {
if (lhs.pathFromRoot[i] !== rhs.pathFromRoot[i]) {
return false;
}
}
return true;
}
// when versions aren't equal, compare using other key information, because key hashes
// of different key versions can't be compared
if (isInstancesNodeKey(lhs)) {
assert(isInstancesNodeKey(rhs));
if (lhs.instanceKeys.length !== rhs.instanceKeys.length) {
return false;
}
for (let i = 0; i < lhs.instanceKeys.length; ++i) {
if (0 !== InstanceKey.compare(lhs.instanceKeys[i], rhs.instanceKeys[i])) {
return false;
}
}
return true;
}
if (isClassGroupingNodeKey(lhs)) {
assert(isClassGroupingNodeKey(rhs));
return lhs.className === rhs.className;
}
if (isPropertyGroupingNodeKey(lhs)) {
assert(isPropertyGroupingNodeKey(rhs));
return lhs.className === rhs.className && lhs.propertyName === rhs.propertyName;
}
if (isLabelGroupingNodeKey(lhs)) {
assert(isLabelGroupingNodeKey(rhs));
return lhs.label === rhs.label;
}
return true;
}
NodeKey.equals = equals;
})(NodeKey || (NodeKey = {}));
//# sourceMappingURL=Key.js.map