remove temp files

This commit is contained in:
Niels Lyngsø
2024-10-11 20:17:20 +02:00
parent 13afb93968
commit b166b04ed0

View File

@@ -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<UmbPropertyValueData> },
ContentTypeModel extends UmbContentTypeModel = UmbContentTypeModel,
> extends UmbControllerBase {
#dataOwner: UmbElementPropertyDataOwner<ContentModel, ContentTypeModel>;
#variantId: UmbVariantId;
// eslint-disable-next-line no-unused-private-class-members
#propertyVariantIdPromise?: Promise<never>;
#propertyVariantIdPromiseResolver?: () => void;
#propertyVariantIdMap = new UmbBasicState<UmbPropertyVariantIdMapType>([]);
private readonly _propertyVariantIdMap = this.#propertyVariantIdMap.asObservable();
constructor(
host: UmbControllerHost,
dataOwner: UmbElementPropertyDataOwner<ContentModel, ContentTypeModel>,
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<Array<UmbPropertyValueData>>;
// Should it be possible to get the properties as a list of property aliases?
get properties(): Observable<Array<UmbPropertyValueData>> {
if (!this.#propertiesObservable) {
this.#propertiesObservable = mergeObservables(
[this._propertyVariantIdMap, this.#dataOwner.values],
this.#mergeVariantIdsAndValues,
);
}
return this.#propertiesObservable;
}
#mergeVariantIdsAndValues([props, values]: [UmbPropertyVariantIdMapType, Array<UmbPropertyValueData> | undefined]) {
const r: Array<UmbPropertyValueData> = [];
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<Array<UmbPropertyValueData>> {
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<Observable<UmbVariantId | undefined> | 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<Observable<ReturnType | undefined> | undefined>} - An observable of the property value
* @description Get an Observable for the value of this property.
*/
async propertyValueByAlias<ReturnType = unknown>(
propertyAlias: string,
): Promise<Observable<ReturnType | undefined> | undefined> {
await this.#dataOwner.isLoaded();
await this.#propertyVariantIdPromise;
const propVariantId = this.#propertyVariantIdMap.getValue().find((x) => x.alias === propertyAlias);
if (propVariantId) {
return this.#dataOwner.propertyValueByAlias<ReturnType>(propertyAlias, propVariantId.variantId);
}
return;
}
async propertyValueByAliasAndVariantId<ReturnType = unknown>(
propertyAlias: string,
propertyVariantId: UmbVariantId,
): Promise<Observable<ReturnType | undefined> | undefined> {
return this.#dataOwner.propertyValueByAlias<ReturnType>(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<unknown>} - 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<void> {
return this.#dataOwner.setPropertyValue(propertyAlias, value, propertyVariantId);
}
/**
* @function setPropertyValue
* @param {string} propertyAlias - The alias for the value to be set
* @param {PromiseLike<unknown>} value - value can be a promise resolving into the actual value or the raw value it self.
* @returns {Promise<void>}
* @description Set the value of this property.
*/
async setPropertyValue(propertyAlias: string, value: PromiseLike<unknown>) {
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();
}
}