@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
119 lines • 4.08 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RelationContainer = void 0;
const string_format_1 = __importDefault(require("string-format"));
const error_messages_1 = __importDefault(require("../../error-messages"));
const guard_1 = __importDefault(require("../../guard"));
const relation_1 = require("./relation");
const relation_role_1 = require("./relation-role");
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_1.default.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((0, string_format_1.default)(error_messages_1.default.Entity.SelfReferencingRelation, this.relationName));
}
if (this.parentRelation != null) {
return this.parentRelation;
}
else {
return this.childRelation;
}
}
else if (role === relation_role_1.RelationRole.Parent) {
return this.parentRelation;
}
else if (role === relation_role_1.RelationRole.Child) {
return this.childRelation;
}
else {
throw new Error((0, string_format_1.default)(error_messages_1.default.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 === relation_role_1.RelationRole.Parent) {
return this.parentRelation != null;
}
else if (role === relation_role_1.RelationRole.Child) {
return this.childRelation != null;
}
else {
throw new Error((0, string_format_1.default)(error_messages_1.default.RelationRoleMapper.UnknownRole, role));
}
}
/**
* Sets the relation on this container.
*/
setRelation(relation) {
guard_1.default.notNullOrUndefined(relation);
if (this.relationName != relation.name) {
throw new Error(error_messages_1.default.Entity.RelationNameMustMatch);
}
if (relation_1.RelationBase.isParentRelation(relation)) {
this._parentRelation = relation;
}
else if (relation_1.RelationBase.isChildRelation(relation)) {
this._childRelation = relation;
}
else {
throw new Error((0, string_format_1.default)(error_messages_1.default.RelationRoleMapper.UnknownRole, relation.role));
}
}
}
exports.RelationContainer = RelationContainer;
//# sourceMappingURL=relation-container.js.map