more structure manipulation methods

This commit is contained in:
Niels Lyngsø
2023-03-29 15:51:28 +02:00
parent 55d501ac68
commit 69f46f74c9
4 changed files with 56 additions and 13 deletions

View File

@@ -10,3 +10,4 @@ export * from './object-state';
export * from './create-observable-part.function';
export * from './append-to-frozen-array.function';
export * from './partial-update-frozen-array.function';
export * from './mapping-function';

View File

@@ -4,16 +4,15 @@ import { UmbDocumentTypeRepository } from '../repository/document-type.repositor
import { UmbWorkspacePropertyStructureManager } from '../../../shared/components/workspace/workspace-context/workspace-property-structure-manager.class';
import type { DocumentTypeResponseModel } from '@umbraco-cms/backoffice/backend-api';
import { UmbControllerHostInterface } from '@umbraco-cms/backoffice/controller';
import { ObjectState } from '@umbraco-cms/backoffice/observable-api';
type EntityType = DocumentTypeResponseModel;
export class UmbWorkspaceDocumentTypeContext
extends UmbWorkspaceContext<UmbDocumentTypeRepository>
implements UmbEntityWorkspaceContextInterface<EntityType | undefined>
{
#draft = new ObjectState<EntityType | undefined>(undefined);
data = this.#draft.asObservable();
name = this.#draft.getObservablePart((data) => data?.name);
// Draft is located in structure manager
readonly data;
readonly name;
readonly structure;
@@ -21,6 +20,8 @@ export class UmbWorkspaceDocumentTypeContext
super(host, new UmbDocumentTypeRepository(host));
this.structure = new UmbWorkspacePropertyStructureManager(this.host, this.repository);
this.data = this.structure.rootDocumentType;
this.name = this.structure.rootDocumentTypeObservablePart((data) => data?.name);
}
public setPropertyValue(alias: string, value: unknown) {
@@ -28,11 +29,11 @@ export class UmbWorkspaceDocumentTypeContext
}
getData() {
return this.#draft.getValue();
return this.structure.getRootDocumentType() || {};
}
getEntityKey() {
return this.getData()?.key || '';
return this.getData().key;
}
getEntityType() {
@@ -40,12 +41,12 @@ export class UmbWorkspaceDocumentTypeContext
}
setName(name: string) {
this.#draft.update({ name });
this.structure.updateRootDocumentType({ name });
}
// TODO => manage setting icon color
setIcon(icon: string) {
this.#draft.update({ icon });
this.structure.updateRootDocumentType({ icon });
}
async createScaffold(documentTypeKey: string) {
@@ -67,11 +68,10 @@ export class UmbWorkspaceDocumentTypeContext
}
async save() {
if (!this.#draft.value) return;
this.repository.save(this.#draft.value);
this.repository.save(this.getData());
}
public destroy(): void {
this.#draft.complete();
this.structure.destroy();
}
}

View File

@@ -178,6 +178,7 @@ export class UmbDocumentWorkspaceContext
public destroy(): void {
this.#draft.complete();
this.structure.destroy();
}
}

View File

@@ -6,7 +6,7 @@ import {
ContentTypeResponseModelBaseDocumentTypePropertyTypeResponseModelDocumentTypePropertyTypeContainerResponseModel,
} from '@umbraco-cms/backoffice/backend-api';
import { UmbControllerHostInterface, UmbControllerInterface } from '@umbraco-cms/backoffice/controller';
import { ArrayState, UmbObserverController } from '@umbraco-cms/backoffice/observable-api';
import { ArrayState, UmbObserverController, MappingFunction } from '@umbraco-cms/backoffice/observable-api';
export type PropertyContainerTypes = 'Group' | 'Tab';
@@ -19,6 +19,7 @@ export class UmbWorkspacePropertyStructureManager<R extends UmbDocumentTypeRepos
#documentTypeRepository: R;
#rootDocumentTypeKey?: string;
#documentTypeObservers = new Array<UmbControllerInterface>();
#documentTypes = new ArrayState<T>([], (x) => x.key);
@@ -35,6 +36,10 @@ export class UmbWorkspacePropertyStructureManager<R extends UmbDocumentTypeRepos
*/
public async loadType(key?: string) {
this._reset();
if (!key) return {};
this.#rootDocumentTypeKey = key;
return await this._loadType(key);
}
@@ -44,7 +49,9 @@ export class UmbWorkspacePropertyStructureManager<R extends UmbDocumentTypeRepos
if (!parentKey) return {};
const { data } = await this.#documentTypeRepository.createScaffold(parentKey);
if (!data) return {};
if (!data || !data.key) return {};
this.#rootDocumentTypeKey = data.key;
await this._observeDocumentType(data);
return { data };
@@ -93,6 +100,40 @@ export class UmbWorkspacePropertyStructureManager<R extends UmbDocumentTypeRepos
/** Public methods for consuming structure: */
rootDocumentType() {
return this.#documentTypes.getObservablePart((x) => x.find((y) => y.key === this.#rootDocumentTypeKey));
}
getRootDocumentType() {
return this.#documentTypes.getValue().find((y) => y.key === this.#rootDocumentTypeKey);
}
updateRootDocumentType(entry: T) {
return this.#documentTypes.updateOne(this.#rootDocumentTypeKey, entry);
}
/*
rootDocumentTypeName() {
return this.#documentTypes.getObservablePart((docTypes) => {
const docType = docTypes.find((x) => x.key === this.#rootDocumentTypeKey);
return docType?.name ?? '';
});
}
*/
rootDocumentTypeObservablePart<PartResult>(mappingFunction: MappingFunction<T, PartResult>) {
return this.#documentTypes.getObservablePart((docTypes) => {
const docType = docTypes.find((x) => x.key === this.#rootDocumentTypeKey);
return docType ? mappingFunction(docType) : undefined;
});
}
/*
nameOfDocumentType(key: string) {
return this.#documentTypes.getObservablePart((docTypes) => {
const docType = docTypes.find((x) => x.key === key);
return docType?.name ?? '';
});
}
*/
hasPropertyStructuresOf(containerKey: string | null) {
return this.#documentTypes.getObservablePart((docTypes) => {
return (