UNPKG

@itwin/presentation-common

Version:

Common pieces for iModel.js presentation packages

207 lines • 10.6 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. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Core */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RelationshipPath = exports.RelatedClassInfoWithOptionalRelationship = exports.RelatedClassInfo = exports.PropertyInfo = exports.NavigationPropertyInfo = exports.InstanceKey = void 0; const core_bentley_1 = require("@itwin/core-bentley"); const presentation_shared_1 = require("@itwin/presentation-shared"); /** @public */ var InstanceKey; (function (InstanceKey) { /** Compare 2 instance keys */ function compare(lhs, rhs) { const lhsClass = (0, presentation_shared_1.parseFullClassName)(lhs.className); const rhsClass = (0, presentation_shared_1.parseFullClassName)(rhs.className); const schemaNameCompare = lhsClass.schemaName.toLowerCase().localeCompare(rhsClass.schemaName.toLowerCase()); if (schemaNameCompare !== 0) { return schemaNameCompare; } const classNameCompare = lhsClass.className.toLowerCase().localeCompare(rhsClass.className.toLowerCase()); if (classNameCompare !== 0) { return classNameCompare; } return lhs.id.toLowerCase().localeCompare(rhs.id.toLowerCase()); } InstanceKey.compare = compare; })(InstanceKey || (exports.InstanceKey = InstanceKey = {})); /** * Contains utilities for working with objects of [[NavigationPropertyInfo]] type. * @public */ var NavigationPropertyInfo; (function (NavigationPropertyInfo) { /** Serialize [[NavigationPropertyInfo]] to compressed JSON */ function toCompressedJSON(navigationPropertyInfo, classesMap) { const { id: relationshipId, ...relationshipLeftOverInfo } = navigationPropertyInfo.classInfo; const { id: targetId, ...targetLeftOverInfo } = navigationPropertyInfo.targetClassInfo; classesMap[relationshipId] = relationshipLeftOverInfo; classesMap[targetId] = targetLeftOverInfo; return { ...navigationPropertyInfo, classInfo: relationshipId, targetClassInfo: targetId, }; } NavigationPropertyInfo.toCompressedJSON = toCompressedJSON; /** Deserialize [[NavigationPropertyInfo]] from compressed JSON */ function fromCompressedJSON(compressedNavigationPropertyInfoJSON, classesMap) { return { ...compressedNavigationPropertyInfoJSON, classInfo: { id: compressedNavigationPropertyInfoJSON.classInfo, ...classesMap[compressedNavigationPropertyInfoJSON.classInfo] }, targetClassInfo: { id: compressedNavigationPropertyInfoJSON.targetClassInfo, ...classesMap[compressedNavigationPropertyInfoJSON.targetClassInfo] }, }; } NavigationPropertyInfo.fromCompressedJSON = fromCompressedJSON; })(NavigationPropertyInfo || (exports.NavigationPropertyInfo = NavigationPropertyInfo = {})); /** @public */ var PropertyInfo; (function (PropertyInfo) { /** Serialize [[PropertyInfo]] to compressed JSON */ function toCompressedJSON(propertyInfo, classesMap) { const { navigationPropertyInfo, ...leftOverPropertyInfo } = propertyInfo; const { id, ...leftOverInfo } = propertyInfo.classInfo; classesMap[id] = leftOverInfo; return { ...leftOverPropertyInfo, classInfo: propertyInfo.classInfo.id, ...(navigationPropertyInfo ? { navigationPropertyInfo: NavigationPropertyInfo.toCompressedJSON(navigationPropertyInfo, classesMap) } : undefined), }; } PropertyInfo.toCompressedJSON = toCompressedJSON; })(PropertyInfo || (exports.PropertyInfo = PropertyInfo = {})); /** @public */ var RelatedClassInfo; (function (RelatedClassInfo) { /** Serialize [[RelatedClassInfo]] to compressed JSON */ function toCompressedJSON(classInfo, classesMap) { const { id: sourceId, ...sourceLeftOverInfo } = classInfo.sourceClassInfo; const { id: targetId, ...targetLeftOverInfo } = classInfo.targetClassInfo; const { id: relationshipId, ...relationshipLeftOverInfo } = classInfo.relationshipInfo; classesMap[sourceId] = sourceLeftOverInfo; classesMap[targetId] = targetLeftOverInfo; classesMap[relationshipId] = relationshipLeftOverInfo; return { ...classInfo, sourceClassInfo: sourceId, targetClassInfo: targetId, relationshipInfo: relationshipId, }; } RelatedClassInfo.toCompressedJSON = toCompressedJSON; /** Deserialize [[RelatedClassInfo]] from compressed JSON */ function fromCompressedJSON(json, classesMap) { (0, core_bentley_1.assert)(classesMap.hasOwnProperty(json.sourceClassInfo)); (0, core_bentley_1.assert)(classesMap.hasOwnProperty(json.targetClassInfo)); (0, core_bentley_1.assert)(classesMap.hasOwnProperty(json.relationshipInfo)); return { ...json, sourceClassInfo: { id: json.sourceClassInfo, ...classesMap[json.sourceClassInfo] }, targetClassInfo: { id: json.targetClassInfo, ...classesMap[json.targetClassInfo] }, relationshipInfo: { id: json.relationshipInfo, ...classesMap[json.relationshipInfo] }, }; } RelatedClassInfo.fromCompressedJSON = fromCompressedJSON; /** Check two [[RelatedClassInfo]] or [[StrippedRelatedClassInfo]] for equality */ function equals(lhs, rhs) { return (lhs.isForwardRelationship === rhs.isForwardRelationship && getClassName(lhs, "source") === getClassName(rhs, "source") && getClassName(lhs, "target") === getClassName(rhs, "target") && getClassName(lhs, "relationship") === getClassName(rhs, "relationship")); } RelatedClassInfo.equals = equals; function isStripped(info) { const maybeStripped = info; return !!maybeStripped.relationshipName && !!maybeStripped.sourceClassName && !!maybeStripped.targetClassName; } function getClassName(info, whichClass) { switch (whichClass) { case "source": return isStripped(info) ? info.sourceClassName : info.sourceClassInfo.name; case "target": return isStripped(info) ? info.targetClassName : info.targetClassInfo.name; case "relationship": return isStripped(info) ? info.relationshipName : info.relationshipInfo.name; } } /** Strip given [[RelatedClassInfo]] to [[StrippedRelatedClassInfo]] */ function strip(full) { return { sourceClassName: full.sourceClassInfo.name, targetClassName: full.targetClassInfo.name, relationshipName: full.relationshipInfo.name, isForwardRelationship: full.isForwardRelationship, }; } RelatedClassInfo.strip = strip; })(RelatedClassInfo || (exports.RelatedClassInfo = RelatedClassInfo = {})); /** @public */ // eslint-disable-next-line @typescript-eslint/no-redeclare var RelatedClassInfoWithOptionalRelationship; (function (RelatedClassInfoWithOptionalRelationship) { /** Serialize [[RelatedClassInfoWithOptionalRelationship]] to compressed JSON */ function toCompressedJSON(classInfo, classesMap) { const { sourceClassInfo, targetClassInfo, relationshipInfo, ...otherProps } = classInfo; const { id: sourceId, ...sourceLeftOverInfo } = sourceClassInfo; const { id: targetId, ...targetLeftOverInfo } = targetClassInfo; classesMap[sourceId] = sourceLeftOverInfo; classesMap[targetId] = targetLeftOverInfo; if (relationshipInfo) { const { id: relationshipId, ...relationshipLeftOverInfo } = relationshipInfo; classesMap[relationshipId] = relationshipLeftOverInfo; } return { ...otherProps, sourceClassInfo: sourceId, targetClassInfo: targetId, ...(relationshipInfo ? { relationshipInfo: relationshipInfo.id } : undefined), }; } RelatedClassInfoWithOptionalRelationship.toCompressedJSON = toCompressedJSON; /** Deserialize [[RelatedClassInfoWithOptionalRelationship]] from compressed JSON */ function fromCompressedJSON(json, classesMap) { const { sourceClassInfo, targetClassInfo, relationshipInfo, ...otherProps } = json; (0, core_bentley_1.assert)(classesMap.hasOwnProperty(sourceClassInfo)); (0, core_bentley_1.assert)(classesMap.hasOwnProperty(targetClassInfo)); if (relationshipInfo) { (0, core_bentley_1.assert)(classesMap.hasOwnProperty(relationshipInfo)); } return { ...otherProps, sourceClassInfo: { id: sourceClassInfo, ...classesMap[sourceClassInfo] }, targetClassInfo: { id: targetClassInfo, ...classesMap[targetClassInfo] }, ...(relationshipInfo ? { relationshipInfo: { id: relationshipInfo, ...classesMap[relationshipInfo] } } : undefined), }; } RelatedClassInfoWithOptionalRelationship.fromCompressedJSON = fromCompressedJSON; })(RelatedClassInfoWithOptionalRelationship || (exports.RelatedClassInfoWithOptionalRelationship = RelatedClassInfoWithOptionalRelationship = {})); /** @public */ // eslint-disable-next-line @typescript-eslint/no-redeclare var RelationshipPath; (function (RelationshipPath) { /** Reverse direction of the given [[RelationshipPath]] */ function reverse(path) { return [...path].reverse().map((step) => ({ ...step, sourceClassInfo: step.targetClassInfo, targetClassInfo: step.sourceClassInfo, isForwardRelationship: !step.isForwardRelationship, })); } RelationshipPath.reverse = reverse; /** Check two [[RelationshipPath]] or [[StrippedRelationshipPath]] for equality */ function equals(lhs, rhs) { return lhs.length === rhs.length && lhs.every((lhsPart, i) => RelatedClassInfo.equals(lhsPart, rhs[i])); } RelationshipPath.equals = equals; /** Strip given [[RelationshipPath]] to [[StrippedRelationshipPath]] */ function strip(full) { return full.map(RelatedClassInfo.strip); } RelationshipPath.strip = strip; })(RelationshipPath || (exports.RelationshipPath = RelationshipPath = {})); //# sourceMappingURL=EC.js.map