UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

112 lines 3.46 kB
import format from "string-format"; import ErrorMessages from "../../error-messages"; import Guard from "../../guard"; import { RelationBase } from "./relation"; import { RelationRole } from "./relation-role"; export class RelationContainer { get parentRelation() { return this._parentRelation; } get childRelation() { return this._childRelation; } /** * Checks if the relation is self-referencing. */ get isSelfRelation() { return this.parentRelation != null && this.childRelation != null; } /** * Checks if this contains any relation. */ get isEmpty() { return this.parentRelation == null && this.childRelation == null; } constructor(relationName) { Guard.stringNotNullOrEmpty(relationName); this.relationName = relationName; } /** * Checks if this contains any relation. */ any() { return this.parentRelation != null || this.childRelation != null; } /** * Gets the relation. If role is null and the relation is self-referencing, it will throw. * Returns null if there are no loaded relations. * @param role - Relation role */ getRelation(role) { if (role == null) { if (this.isSelfRelation) { throw new Error(format(ErrorMessages.Entity.SelfReferencingRelation, this.relationName)); } if (this.parentRelation != null) { return this.parentRelation; } else { return this.childRelation; } } else if (role === RelationRole.Parent) { return this.parentRelation; } else if (role === RelationRole.Child) { return this.childRelation; } else { throw new Error(format(ErrorMessages.RelationRoleMapper.UnknownRole, role)); } } /** * Gets an array of all loaded relations. */ getRelations() { if (this.isSelfRelation) { return [this.parentRelation, this.childRelation]; } else if (this.parentRelation != null) { return [this.parentRelation]; } else if (this.childRelation != null) { return [this.childRelation]; } else { return []; } } /** * Checks if the relation with specified role is loaded. */ relationExists(role) { if (role === RelationRole.Parent) { return this.parentRelation != null; } else if (role === RelationRole.Child) { return this.childRelation != null; } else { throw new Error(format(ErrorMessages.RelationRoleMapper.UnknownRole, role)); } } /** * Sets the relation on this container. */ setRelation(relation) { Guard.notNullOrUndefined(relation); if (this.relationName != relation.name) { throw new Error(ErrorMessages.Entity.RelationNameMustMatch); } if (RelationBase.isParentRelation(relation)) { this._parentRelation = relation; } else if (RelationBase.isChildRelation(relation)) { this._childRelation = relation; } else { throw new Error(format(ErrorMessages.RelationRoleMapper.UnknownRole, relation.role)); } } } //# sourceMappingURL=relation-container.js.map