UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

116 lines 4.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CultureSensitiveProperty = void 0; const dirty_value_calculator_1 = __importDefault(require("../../dirty-value-calculator")); const guard_1 = __importDefault(require("../../guard")); const property_1 = require("./property"); class CultureSensitiveProperty extends property_1.PropertyBase { get isDirty() { if (!this.isTracking) { return false; } for (const cultureString in this._values) { const culture = cultureString; const value = this._values[culture]; const isDirty = this._dirtyValueCalculators[culture].isDirty(value); if (isDirty) { return true; } } return false; } constructor(name, typeInfo, loadedCultures, values, hasDataSource = false) { super(name, typeInfo, hasDataSource); this._dirtyValueCalculators = {}; guard_1.default.arrayNotEmpty(loadedCultures); loadedCultures.forEach(culture => guard_1.default.notInvariantCulture(culture)); this.isMultiLanguage = true; this._loadedCultures = [...new Set(loadedCultures)]; this._values = this._loadedCultures.reduce((defaultValues, culture) => { defaultValues[culture] = null; return defaultValues; }, {}); if (values != null) { Object.keys(values).forEach((culture) => { this._values[culture] = values[culture]; //? Deep copy? }); } } getTypeName() { return this.constructor.name; } getValue(culture) { this.cultureMustBeLoaded(culture); if (this._values[culture] != null) { return this._values[culture]; //? Add 'casting' wrapper } return null; //? Allow undefined? } getValues(cultures) { if (cultures !== undefined) { guard_1.default.arrayNotEmpty(cultures); this.culturesMustBeLoaded(cultures); return cultures.reduce((values, culture) => { values[culture] = this._values[culture]; //? Add 'casting' wrapper? return values; }, {}); } else { return this._loadedCultures.reduce((values, culture) => { values[culture] = this._values[culture]; //? Add 'casting' wrapper? return values; }, {}); } } getCultures() { return Object.keys(this._values); } setValue(culture, value) { guard_1.default.notInvariantCulture(culture); this.cultureMustBeLoaded(culture); this._values[culture] = value; } startTracking() { if (this.isTracking) { return; } this.isTracking = true; for (const culture of this._loadedCultures) { const dirtyValueCalculator = new dirty_value_calculator_1.default(); dirtyValueCalculator.setOriginalValue(this._values[culture]); this._dirtyValueCalculators[culture] = dirtyValueCalculator; } } markClean() { if (!this.isTracking) { return; } for (const culture of this._loadedCultures) { this._dirtyValueCalculators[culture].setOriginalValue(this._values[culture]); } } getDirtyCultures() { if (!this.isTracking) { throw new Error("Can't get dirty cultures when not tracking changes."); } return this._loadedCultures.reduce((dirtyCultures, culture) => { if (this._dirtyValueCalculators[culture].isDirty(this._values[culture])) { dirtyCultures.push(culture); } return dirtyCultures; }, []); } cultureMustBeLoaded(culture) { if (!this._loadedCultures.includes(culture)) { throw new Error(`Culture ${culture} was not loaded.`); } } culturesMustBeLoaded(cultures) { cultures.forEach(culture => this.cultureMustBeLoaded(culture)); } } exports.CultureSensitiveProperty = CultureSensitiveProperty; //# sourceMappingURL=culture-sensitive-property.js.map