diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/content/manager/element-data-properties.manager.tempts b/src/Umbraco.Web.UI.Client/src/packages/core/content/manager/element-data-properties.manager.tempts deleted file mode 100644 index ed8f8e9b87..0000000000 --- a/src/Umbraco.Web.UI.Client/src/packages/core/content/manager/element-data-properties.manager.tempts +++ /dev/null @@ -1,160 +0,0 @@ -import type { UmbElementPropertyDataOwner } from '../property-dataset-context/element-property-data-owner.interface.js'; -import type { UmbPropertyValueData } from '@umbraco-cms/backoffice/property'; -import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; -import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api'; -import type { Observable } from '@umbraco-cms/backoffice/external/rxjs'; -import { - UmbBasicState, - classEqualMemoization, - createObservablePart, - mergeObservables, -} from '@umbraco-cms/backoffice/observable-api'; -import { UmbVariantId } from '@umbraco-cms/backoffice/variant'; -import type { UmbContentTypeModel, UmbPropertyTypeModel } from '@umbraco-cms/backoffice/content-type'; - -type UmbPropertyVariantIdMapType = Array<{ alias: string; variantId: UmbVariantId }>; - -export class UmbElementDataPropertyManager< - ContentModel extends { values: Array }, - ContentTypeModel extends UmbContentTypeModel = UmbContentTypeModel, -> extends UmbControllerBase { - #dataOwner: UmbElementPropertyDataOwner; - #variantId: UmbVariantId; - - // eslint-disable-next-line no-unused-private-class-members - #propertyVariantIdPromise?: Promise; - #propertyVariantIdPromiseResolver?: () => void; - #propertyVariantIdMap = new UmbBasicState([]); - private readonly _propertyVariantIdMap = this.#propertyVariantIdMap.asObservable(); - - constructor( - host: UmbControllerHost, - dataOwner: UmbElementPropertyDataOwner, - variantId?: UmbVariantId, - ) { - // The controller alias, is a very generic name cause we want only one of these for this controller host. - super(host); - this.#dataOwner = dataOwner; - this.#variantId = variantId ?? UmbVariantId.CreateInvariant(); - - this.observe(this.#dataOwner.structure.contentTypeProperties, (props: UmbPropertyTypeModel[]) => { - this.#propertyVariantIdPromise ??= new Promise((resolve) => { - this.#propertyVariantIdPromiseResolver = resolve as any; - }); - const map = props.map((prop) => ({ alias: prop.alias, variantId: this.#createPropertyVariantId(prop) })); - this.#propertyVariantIdMap.setValue(map); - // Resolve promise, to let the once waiting on this know. - if (this.#propertyVariantIdPromiseResolver) { - this.#propertyVariantIdPromise = undefined; - this.#propertyVariantIdPromiseResolver(); - this.#propertyVariantIdPromiseResolver = undefined; - } - }); - } - - #createPropertyVariantId(property: UmbPropertyTypeModel) { - return UmbVariantId.Create({ - culture: property.variesByCulture ? this.#variantId.culture : null, - segment: property.variesBySegment ? this.#variantId.segment : null, - }); - } - - #propertiesObservable?: Observable>; - // Should it be possible to get the properties as a list of property aliases? - get properties(): Observable> { - if (!this.#propertiesObservable) { - this.#propertiesObservable = mergeObservables( - [this._propertyVariantIdMap, this.#dataOwner.values], - this.#mergeVariantIdsAndValues, - ); - } - - return this.#propertiesObservable; - } - - #mergeVariantIdsAndValues([props, values]: [UmbPropertyVariantIdMapType, Array | undefined]) { - const r: Array = []; - if (values) { - for (const prop of props) { - const f = values.find((v) => prop.alias === v.alias && prop.variantId.compare(v)); - if (f) { - r.push(f); - } - } - } - return r; - } - - async getProperties(): Promise> { - await this.#propertyVariantIdPromise; - return this.#mergeVariantIdsAndValues([this.#propertyVariantIdMap.getValue(), this.#dataOwner.getValues()]); - } - - /** - * @function propertyVariantId - * @param {string} propertyAlias - The property alias to observe the variantId of. - * @returns {Promise | undefined>} - The observable for this properties variantId. - * @description Get an Observable for the variant id of this property. - */ - async propertyVariantId(propertyAlias: string) { - return createObservablePart( - this._propertyVariantIdMap, - (x) => x.find((v) => v.alias === propertyAlias)?.variantId, - classEqualMemoization, - ); - } - - /** - * @function propertyValueByAlias - * @param {string} propertyAlias The alias of the property - * @returns {Promise | undefined>} - An observable of the property value - * @description Get an Observable for the value of this property. - */ - async propertyValueByAlias( - propertyAlias: string, - ): Promise | undefined> { - await this.#dataOwner.isLoaded(); - await this.#propertyVariantIdPromise; - const propVariantId = this.#propertyVariantIdMap.getValue().find((x) => x.alias === propertyAlias); - if (propVariantId) { - return this.#dataOwner.propertyValueByAlias(propertyAlias, propVariantId.variantId); - } - return; - } - - async propertyValueByAliasAndVariantId( - propertyAlias: string, - propertyVariantId: UmbVariantId, - ): Promise | undefined> { - return this.#dataOwner.propertyValueByAlias(propertyAlias, propertyVariantId); - } - - /** - * @function setPropertyValueByVariant - * @param {string} propertyAlias - The alias of the property - * @param {unknown} value - value can be a promise resolving into the actual value or the raw value it self. - * @param {UmbVariantId} propertyVariantId - The variant id for the value to be set for. - * @returns {Promise} - A promise that resolves once the value has been set. - * @description Get the value of this property. - */ - setPropertyValueByVariant(propertyAlias: string, value: unknown, propertyVariantId: UmbVariantId): Promise { - return this.#dataOwner.setPropertyValue(propertyAlias, value, propertyVariantId); - } - - /** - * @function setPropertyValue - * @param {string} propertyAlias - The alias for the value to be set - * @param {PromiseLike} value - value can be a promise resolving into the actual value or the raw value it self. - * @returns {Promise} - * @description Set the value of this property. - */ - async setPropertyValue(propertyAlias: string, value: PromiseLike) { - this.#dataOwner.initiatePropertyValueChange(); - await this.#propertyVariantIdPromise; - const propVariantId = this.#propertyVariantIdMap.getValue().find((x) => x.alias === propertyAlias); - if (propVariantId) { - return this.#dataOwner.setPropertyValue(propertyAlias, await value, propVariantId.variantId); - } - this.#dataOwner.finishPropertyValueChange(); - } -}