@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
110 lines • 5.14 kB
JavaScript
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 { MappingUtilities } from "../../mappers/mapping-utilities";
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 { LazyLoadingManager } from "./lazy-loading-manager";
export class PropertyManager {
get count() {
return Object.keys(this._properties).length;
}
constructor(client, properties, entity) {
Guard.notNullOrUndefined(entity);
Guard.notNullOrUndefined(client);
Guard.notNullOrUndefined(properties);
this._client = client;
this._entity = entity;
this._properties = new CaseInsensitiveStringMap();
for (const property of properties) {
this._properties[property.name] = property;
}
}
getProperty(name) {
Guard.stringNotNullOrEmpty(name);
return this._properties[name] || null;
}
getProperties() {
return [...Object.values(this._properties)];
}
loadPropertyAsync(propertyName) {
return __awaiter(this, void 0, void 0, function* () {
Guard.stringNotNullOrEmpty(propertyName);
LazyLoadingManager.ensureLazyLoadingIsPossible(this._entity);
// Check if it is already loaded
let property = this._properties[propertyName];
if (property) {
return property;
}
// Load the property
property = yield this.fetchPropertyAsync(propertyName);
// Add the property
if (property != null) {
this._properties[property.name] = property;
}
return property;
});
}
loadPropertiesAsync(propertyLoadOption) {
return __awaiter(this, void 0, void 0, function* () {
Guard.notNullOrUndefined(propertyLoadOption);
LazyLoadingManager.ensureLazyLoadingIsPossible(this._entity);
if (!this.hasMissingProperties(propertyLoadOption)) {
return false;
}
const cultureLoadOption = MappingUtilities.culturesToLoadOption([...this._entity.cultures]);
const loadConfig = new EntityLoadConfiguration(cultureLoadOption, propertyLoadOption, RelationLoadOption.None);
const tempEntity = yield this._client.entities.getAsync(this._entity.id, loadConfig);
this.importMissingProperties(tempEntity);
return true;
});
}
hasMissingProperties(propertyLoadOption) {
if (propertyLoadOption == null ||
propertyLoadOption.loadOption === LoadOption.None ||
propertyLoadOption.properties == null ||
propertyLoadOption.properties.length === 0) {
return false;
}
if (propertyLoadOption.loadOption === LoadOption.All) {
// We don't have any info about the definition here. (Same logic as in the C# SDK)
return true;
}
return propertyLoadOption.properties.some(requestedPropertyName => !this._properties[requestedPropertyName]);
}
importMissingProperties(entity) {
Guard.notNullOrUndefined(entity);
for (const property of entity.properties) {
if (!this._properties[property.name]) {
this._properties[property.name] = property;
}
}
}
fetchPropertyAsync(name) {
return __awaiter(this, void 0, void 0, function* () {
// Setup load configuration
const cultureLoadOption = MappingUtilities.culturesToLoadOption([...this._entity.cultures]);
const propertyLoadOption = new PropertyLoadOption(name);
const loadConfig = new EntityLoadConfiguration(cultureLoadOption, propertyLoadOption, RelationLoadOption.None);
// Load entity
const entity = yield this._client.entities.getAsync(this._entity.id, loadConfig);
if (entity == null) {
return null;
}
// We only loaded this specific property, so we can just take the first one.
const property = entity.properties[0] || null;
return property;
});
}
}
//# sourceMappingURL=property-manager.js.map