Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { UmbVariantStructureItemModel } from './types.js';
import type { UmbTreeRepository, UmbTreeRootModel } from '@umbraco-cms/backoffice/tree';
import type { UmbTreeItemModel, UmbTreeRepository, UmbTreeRootModel } from '@umbraco-cms/backoffice/tree';
import { createExtensionApiByAlias } from '@umbraco-cms/backoffice/extension-registry';
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import { UMB_VARIANT_WORKSPACE_CONTEXT } from '@umbraco-cms/backoffice/workspace';
import { UmbArrayState, UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbAncestorsEntityContext } from '@umbraco-cms/backoffice/entity';
import { UmbAncestorsEntityContext, type UmbEntityModel } from '@umbraco-cms/backoffice/entity';

interface UmbMenuVariantTreeStructureWorkspaceContextBaseArgs {
treeRepositoryAlias: string;
Expand Down Expand Up @@ -44,7 +44,7 @@ export abstract class UmbMenuVariantTreeStructureWorkspaceContextBase extends Um
}

async #requestStructure() {
const isNew = this.#workspaceContext?.getIsNew();
const isNew = this.#workspaceContext?.getIsNew() ?? false;
const uniqueObservable = isNew ? (this.#workspaceContext as any)?.parentUnique : this.#workspaceContext?.unique;
const entityTypeObservable = isNew
? (this.#workspaceContext as any)?.parentEntityType
Expand All @@ -58,7 +58,7 @@ export abstract class UmbMenuVariantTreeStructureWorkspaceContextBase extends Um
const entityType = (await this.observe(entityTypeObservable, () => {})?.asPromise()) as string;
if (!entityType) throw new Error('Entity type is not available');

// TODO: add correct tree variant item model
// TODO: introduce variant tree item model
const treeRepository = await createExtensionApiByAlias<UmbTreeRepository<any, UmbTreeRootModel>>(
this,
this.#args.treeRepositoryAlias,
Expand All @@ -79,7 +79,7 @@ export abstract class UmbMenuVariantTreeStructureWorkspaceContextBase extends Um
const { data } = await treeRepository.requestTreeItemAncestors({ treeItem: { unique, entityType } });

if (data) {
const ancestorItems = data.map((treeItem) => {
const treeItemAncestors = data.map((treeItem) => {
return {
unique: treeItem.unique,
entityType: treeItem.entityType,
Expand All @@ -93,20 +93,30 @@ export abstract class UmbMenuVariantTreeStructureWorkspaceContextBase extends Um
};
});

const ancestorEntities = data.map((treeItem) => {
return {
unique: treeItem.unique,
entityType: treeItem.entityType,
};
});

this.#ancestorContext.setAncestors(ancestorEntities);

structureItems.push(...ancestorItems);
structureItems.push(...treeItemAncestors);

const parent = structureItems[structureItems.length - 2];
this.#parent.setValue(parent);
this.#structure.setValue(structureItems);

this.#setAncestorData(data);
}
}

#setAncestorData(data: Array<UmbTreeItemModel>) {
const ancestorEntities = data
.map((treeItem) => {
const entity: UmbEntityModel = {
unique: treeItem.unique,
entityType: treeItem.entityType,
};

return entity;
})
/* If the item is not new, the current item is the last item in the array.
We filter out the current item unique to handle any case where it could show up */
.filter((item) => item.unique !== this.#workspaceContext?.getUnique());

this.#ancestorContext.setAncestors(ancestorEntities);
}
}
Loading