@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
117 lines • 5.79 kB
JavaScript
"use strict";
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PropertyManager = void 0;
const base_types_1 = require("../../base-types");
const guard_1 = __importDefault(require("../../guard"));
const mapping_utilities_1 = require("../../mappers/mapping-utilities");
const entity_load_configuration_1 = require("../querying/entity-load-configuration");
const load_options_1 = require("../querying/load-options");
const property_load_option_1 = require("../querying/property-load-option");
const relation_load_option_1 = require("../querying/relation-load-option");
const lazy_loading_manager_1 = require("./lazy-loading-manager");
class PropertyManager {
get count() {
return Object.keys(this._properties).length;
}
constructor(client, properties, entity) {
guard_1.default.notNullOrUndefined(entity);
guard_1.default.notNullOrUndefined(client);
guard_1.default.notNullOrUndefined(properties);
this._client = client;
this._entity = entity;
this._properties = new base_types_1.CaseInsensitiveStringMap();
for (const property of properties) {
this._properties[property.name] = property;
}
}
getProperty(name) {
guard_1.default.stringNotNullOrEmpty(name);
return this._properties[name] || null;
}
getProperties() {
return [...Object.values(this._properties)];
}
loadPropertyAsync(propertyName) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.stringNotNullOrEmpty(propertyName);
lazy_loading_manager_1.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_1.default.notNullOrUndefined(propertyLoadOption);
lazy_loading_manager_1.LazyLoadingManager.ensureLazyLoadingIsPossible(this._entity);
if (!this.hasMissingProperties(propertyLoadOption)) {
return false;
}
const cultureLoadOption = mapping_utilities_1.MappingUtilities.culturesToLoadOption([...this._entity.cultures]);
const loadConfig = new entity_load_configuration_1.EntityLoadConfiguration(cultureLoadOption, propertyLoadOption, relation_load_option_1.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 === load_options_1.LoadOption.None ||
propertyLoadOption.properties == null ||
propertyLoadOption.properties.length === 0) {
return false;
}
if (propertyLoadOption.loadOption === load_options_1.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_1.default.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 = mapping_utilities_1.MappingUtilities.culturesToLoadOption([...this._entity.cultures]);
const propertyLoadOption = new property_load_option_1.PropertyLoadOption(name);
const loadConfig = new entity_load_configuration_1.EntityLoadConfiguration(cultureLoadOption, propertyLoadOption, relation_load_option_1.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;
});
}
}
exports.PropertyManager = PropertyManager;
//# sourceMappingURL=property-manager.js.map