early step in refactor

This commit is contained in:
Niels Lyngsø
2023-12-02 20:38:16 +01:00
parent f6caa8c565
commit 0f6f3b9cd5
4 changed files with 83 additions and 10 deletions

View File

@@ -5,6 +5,7 @@ import {
UmbInvariantableWorkspaceContextInterface,
UmbEditableWorkspaceContextBase,
UmbWorkspaceContextInterface,
UmbBasicVariantContext,
} from '@umbraco-cms/backoffice/workspace';
import {
appendToFrozenArray,
@@ -148,8 +149,26 @@ export class UmbDataTypeWorkspaceContext
return this._configDefaultData?.find((x) => x.alias === alias)?.value;
}
createVariantContext(host: UmbControllerHost): UmbDataTypeVariantContext {
return new UmbDataTypeVariantContext(host, this);
createVariantContext(host: UmbControllerHost) {
const context = new UmbBasicVariantContext(host);
this.observe(
this.properties,
(properties) => {
if (properties) {
properties.forEach(async (property) => {
this.observe(
await this.propertyValueByAlias(property.alias),
(value) => {
context.setPropertyValue(property.alias, value);
},
'observePropertyOf_' + property.alias,
);
});
}
},
'observePropertyValues',
);
return context;
}
async load(unique: string) {

View File

@@ -0,0 +1,58 @@
import type { UmbPropertyValueData } from '../types/property-value-data.type.js';
import { type UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import { UMB_VARIANT_CONTEXT, UmbVariantContext } from '@umbraco-cms/backoffice/workspace';
import { UmbArrayState, UmbStringState } from '@umbraco-cms/backoffice/observable-api';
import { UmbVariantId } from '@umbraco-cms/backoffice/variant';
export class UmbBasicVariantContext
extends UmbContextBase<typeof UMB_VARIANT_CONTEXT.TYPE>
implements UmbVariantContext
{
#name = new UmbStringState('');
name = this.#name.asObservable();
#values = new UmbArrayState<UmbPropertyValueData<unknown>>([], (x) => x.alias);
private _entityType!: string;
private _unique!: string;
getType() {
return this._entityType;
}
getUnique() {
return this._unique;
}
getName() {
return this.#name.getValue();
}
setName(name: string) {
this.#name.next(name);
}
getVariantId() {
return UmbVariantId.CreateInvariant();
}
constructor(host: UmbControllerHost) {
// The controller alias, is a very generic name cause we want only one of these for this controller host.
super(host, UMB_VARIANT_CONTEXT);
}
/**
* TODO: Write proper JSDocs here.
* Ideally do not use these methods, its better to communicate directly with the workspace, but if you do not know the property variant id, then this will figure it out for you. So good for externals to set or get values of a property.
*/
propertyValueByAlias<ReturnType = unknown>(propertyAlias: string) {
return this.#values.asObservablePart((values) => {
const valueObj = values.find((x) => x.alias === propertyAlias);
return valueObj ? (valueObj.value as ReturnType) : undefined;
});
}
/**
* TODO: Write proper JSDocs here.
* Ideally do not use these methods, its better to communicate directly with the workspace, but if you do not know the property variant id, then this will figure it out for you. So good for externals to set or get values of a property.
*/
setPropertyValue(alias: string, value: unknown) {
this.#values.appendOne({ alias, value });
}
}

View File

@@ -1,3 +1,4 @@
export * from './basic-variant-context.js';
export * from './variant-context.interface.js';
export * from './variant-context.token.js';
export * from './nameable-variant-context.interface.js';

View File

@@ -1,7 +1,5 @@
import { DocumentVariantResponseModel } from '@umbraco-cms/backoffice/backend-api';
import { type UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbBaseController } from '@umbraco-cms/backoffice/class-api';
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
import { UmbVariantId } from '@umbraco-cms/backoffice/variant';
import {
UMB_VARIANT_CONTEXT,
@@ -17,12 +15,7 @@ export class UmbInvariantWorkspaceVariantContext<
{
protected _workspace: WorkspaceType;
#currentVariant = new UmbObjectState<DocumentVariantResponseModel | undefined>(undefined);
currentVariant = this.#currentVariant.asObservable();
name = this.#currentVariant.asObservablePart((x) => x?.name);
culture = this.#currentVariant.asObservablePart((x) => x?.culture);
segment = this.#currentVariant.asObservablePart((x) => x?.segment);
name;
// default data:
@@ -47,6 +40,8 @@ export class UmbInvariantWorkspaceVariantContext<
super(host, 'variantContext');
this._workspace = workspace;
this.name = this._workspace.name;
this.provideContext(UMB_VARIANT_CONTEXT, this);
}