UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

198 lines 8.67 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { CaseInsensitiveStringMap } from "../../base-types"; import Guard from "../../guard"; import { CultureLoadOption } from "../querying/culture-load-option"; import { EntityLoadConfiguration } from "../querying/entity-load-configuration"; import { LoadOption } from "../querying/load-options"; import { PropertyLoadOption } from "../querying/property-load-option"; import { RelationLoadOption } from "../querying/relation-load-option"; import { RelationSpecification } from "../querying/relation-specification"; import { LazyLoadingManager } from "./lazy-loading-manager"; import { RelationBase } from "./relation"; import { RelationContainer } from "./relation-container"; export class RelationManager { get count() { return this._count; } constructor(client, relations, entity) { Guard.notNullOrUndefined(client); Guard.notNullOrUndefined(relations); Guard.notNullOrUndefined(entity); this._client = client; this._entity = entity; this._relations = new CaseInsensitiveStringMap(); for (const relation of relations) { this.addRelation(relation); } } /** * Gets the relation for the specified name. If role is null and the relation is self-referencing, it will throw. * Returns null if there are no loaded relations. * @param relationName - The name of the relation * @param role - The role of the relation * @returns A relation or null. */ getRelation(relationName, role) { Guard.stringNotNullOrEmpty(relationName); const container = this._relations[relationName]; if (!container) { return null; } return container.getRelation(role); } /** * Gets a list of all loaded relations. */ getRelations() { const relations = []; for (const container of Object.values(this._relations)) { if (container.isSelfRelation) { Array.prototype.push.apply(relations, container.getRelations()); } else { relations.push(container.getRelation()); } } return relations; } /** * Lazy loads a relation if it is not loaded yet, adds it to the loaded relations and returns it. * If the relation does not exist, null will be returned. * @param relationName - The name of the relation * @param role - The role of the relation * @param forceLoad - Forces the relation to be loaded even though it has already been loaded before * @returns A promise resolving to the relation or null. */ loadRelationAsync(relationName_1, role_1) { return __awaiter(this, arguments, void 0, function* (relationName, role, forceLoad = false) { Guard.stringNotNullOrEmpty(relationName); LazyLoadingManager.ensureLazyLoadingIsPossible(this._entity); let relation = this.getRelation(relationName, role); if (relation != null && !forceLoad) { return relation; } relation = yield this.fetchRelationAsync(relationName, role); if (relation != null) { this.addRelation(relation); } return relation; }); } /** * Loads the specified relations. * It will only load if it is actually missing relations and does not overwrite existing relations. * @param relationLoadOption - Option specifying which relations to load * @returns True when something was loaded. */ loadRelationsAsync(relationLoadOption) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(relationLoadOption); LazyLoadingManager.ensureLazyLoadingIsPossible(this._entity); if (!this.hasMissingRelations(relationLoadOption)) { return false; } const loadConfig = new EntityLoadConfiguration(CultureLoadOption.None, PropertyLoadOption.None, relationLoadOption); const tempEntity = yield this._client.entities.getAsync(this._entity.id, loadConfig); this.importMissingRelations(tempEntity); return true; }); } /** * Checks if the entity is missing relations that are specified in the {@link IRelationLoadOption}. * @param relationLoadOption - Option specifying which relations to load * @returns Boolean indicating if something is missing. */ hasMissingRelations(relationLoadOption) { if (relationLoadOption == null || relationLoadOption.loadOption === LoadOption.None || relationLoadOption.relations == null || relationLoadOption.relations.length === 0) { return false; } if (relationLoadOption.loadOption === LoadOption.All) { // We don't have any info about the definition here. (Same logic as in the C# SDK) return true; } return relationLoadOption.relations.some(requestedRelation => !this.exists(requestedRelation.name, requestedRelation.role)); } /** * Checks if the specified relation is loaded. If role is null, any loaded role will return true. * @param relationName - The name of the relation * @param role - The role of the relation * @returns Boolean indicating if the specified relation exists. */ exists(relationName, role) { Guard.stringNotNullOrEmpty(relationName); let exists = false; const container = this._relations[relationName]; if (container) { if (role == null) { exists = container.any(); } else { exists = container.relationExists(role); } } return exists; } /** * Imports missing relations from the entity. * @param tempEntity - An entity */ importMissingRelations(tempEntity) { Guard.notNullOrUndefined(tempEntity); for (const relation of tempEntity.relations) { if (!this.exists(relation.name, relation.role)) { this.addRelation(relation); } } } /** * Fetches the relation from the server. * @param relationName - The name of the relation * @param role - The role of the relation */ fetchRelationAsync(relationName, role) { return __awaiter(this, void 0, void 0, function* () { const relationLoadOption = new RelationLoadOption([new RelationSpecification(relationName, role)]); const loadConfig = new EntityLoadConfiguration(new CultureLoadOption([...this._entity.cultures]), PropertyLoadOption.None, relationLoadOption); const tempEntity = yield this._client.entities.getAsync(this._entity.id, loadConfig); if (tempEntity == null) { return null; } const relation = tempEntity.getRelation(relationName, role); return relation; }); } /** * Adds the relation to the loaded relations and updates the count (when the relation doesn't exist yet). * @param relation - Relation to add */ addRelation(relation) { let container = this._relations[relation.name]; if (!container) { container = new RelationContainer(relation.name); this._relations[relation.name] = container; } if (container.relationExists(relation.role)) { const relationInRole = container.getRelation(relation.role); if ((RelationBase.isChildToManyParentsRelation(relationInRole) || RelationBase.isParentToManyChildrenRelation(relationInRole)) && !relationInRole.isFullyLoaded) { container.setRelation(relation); } return; } container.setRelation(relation); this._count++; } } //# sourceMappingURL=relation-manager.js.map