UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

124 lines 5.57 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 format from "string-format"; import FlatOptionList from "../contracts/base/option-lists/flat-option-list"; import FlatOptionListValue from "../contracts/base/option-lists/flat-option-list-value"; import HierarchicalOptionList from "../contracts/base/option-lists/hierarchical-option-list"; import HierarchicalOptionListValue from "../contracts/base/option-lists/hierarchical-options-list-value"; import { OptionListType } from "../contracts/base/option-lists/option-list-type"; import ErrorMessages from "../error-messages"; import Guard from "../guard"; import OptionListResource from "../models/option-lists/option-list-resource"; import OptionListValueResource from "../models/option-lists/option-list-value-resource"; export class OptionListMapper { constructor(client) { Guard.notNullOrUndefined(client, "client"); this._client = client; } mapOptionListAsync(resource) { return __awaiter(this, void 0, void 0, function* () { if (resource === null) { return null; } const createdBy = yield this._client.linkHelper.idFromEntityAsync(resource.createdBy); const modifiedBy = yield this._client.linkHelper.idFromEntityAsync(resource.modifiedBy); const labels = resource.labels || {}; let olBase; if (resource.type === OptionListType.Flat) { const values = this.mapFlatOptionListValues(resource.values); olBase = new FlatOptionList(resource.name, labels, values); } else if (resource.type === OptionListType.Hierarchical) { const values = this.mapHierarchicalOptionListValues(resource.values); olBase = new HierarchicalOptionList(resource.name, labels, values); } else { throw Error(format(ErrorMessages.UnknownOptionListType, resource.type)); } olBase.createdBy = createdBy || undefined; olBase.createdOn = resource.createdOn; olBase.modifiedBy = modifiedBy || undefined; olBase.modifiedOn = resource.modifiedOn; olBase.isSystemOwned = resource.isSystemOwned; olBase.name = resource.name; return olBase; }); } map(ol) { if (ol === null) { return null; } const type = ol.type; const values = this.mapValues(ol.getOptionListValues()); const resource = new OptionListResource(); resource.isSystemOwned = ol.isSystemOwned; resource.name = ol.name; resource.type = type; resource.values = values; resource.labels = JSON.parse(JSON.stringify(ol.labels)); return resource; } mapFlatOptionListValues(optionListValues) { if (optionListValues === null) { return []; } const values = optionListValues.map((optionListValue) => { return this.mapFlatOptionListValue(optionListValue); }); return values; } mapFlatOptionListValue(optionListValue) { Guard.notNullOrUndefined(optionListValue); return new FlatOptionListValue(optionListValue.identifier, optionListValue.labels); } mapHierarchicalOptionListValues(optionListValues) { if (optionListValues === null || optionListValues.length === 0) { return []; } const values = optionListValues.map((optionListValue) => { return this.mapHierarchicalOptionListValue(optionListValue); }); return values; } mapHierarchicalOptionListValue(optionListValue) { Guard.notNullOrUndefined(optionListValue); let values; if (optionListValue.values && optionListValue.values.length) { values = this.mapHierarchicalOptionListValues(optionListValue.values); } else { values = []; } return new HierarchicalOptionListValue(optionListValue.identifier, optionListValue.labels, values); } mapValues(optionListValues) { if (optionListValues === null || optionListValues.length === 0) { return []; } const list = []; optionListValues.forEach((optionListValue) => { const olv = this.mapValue(optionListValue); list.push(olv); }); return list; } mapValue(optionListValue) { Guard.notNullOrUndefined(optionListValue); const value = new OptionListValueResource(); value.identifier = optionListValue.identifier; value.labels = optionListValue.labels; if (optionListValue instanceof HierarchicalOptionListValue) { const subValues = this.mapValues(optionListValue.values); value.values = subValues; } return value; } } //# sourceMappingURL=option-list-mapper.js.map