Skip to content

Commit 6df7ff4

Browse files
replace temp event hack with context and skipHost (#18242)
1 parent 62f4666 commit 6df7ff4

File tree

3 files changed

+13
-49
lines changed

3 files changed

+13
-49
lines changed

src/Umbraco.Web.UI.Client/src/packages/core/tree/default/default-tree.context.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,6 @@ export class UmbDefaultTreeContext<
7878
// listen for page changes on the pagination manager
7979
this.pagination.addEventListener(UmbChangeEvent.TYPE, this.#onPageChange);
8080

81-
/* TODO: revisit. This is a temp solution to notify the parent it needs to reload its children
82-
there might be a better way to do this through a tree item parent context.
83-
It does not look like there is a way to have a "dynamic" parent context that will stop when a
84-
specific parent is reached (a tree item unique that matches the parentUnique of this item) */
85-
const hostElement = this.getHostElement();
86-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
87-
// @ts-ignore
88-
hostElement.addEventListener('temp-reload-tree-item-parent', (event: CustomEvent) => {
89-
const treeRoot = this.#treeRoot.getValue();
90-
const unique = treeRoot?.unique;
91-
92-
if (event.detail.unique === unique) {
93-
event.stopPropagation();
94-
this.loadTree();
95-
}
96-
});
97-
9881
// always load the tree root because we need the root entity to reload the entire tree
9982
this.#loadTreeRoot();
10083
}

src/Umbraco.Web.UI.Client/src/packages/core/tree/tree-item/tree-item-base/tree-item-context-base.ts

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ export abstract class UmbTreeItemContextBase<
6868
#foldersOnly = new UmbBooleanState(false);
6969
readonly foldersOnly = this.#foldersOnly.asObservable();
7070

71-
treeContext?: UmbDefaultTreeContext<TreeItemType, TreeRootType>;
71+
public treeContext?: UmbDefaultTreeContext<TreeItemType, TreeRootType>;
72+
public parentTreeItemContext?: UmbTreeItemContext<TreeItemType>;
73+
7274
#sectionContext?: typeof UMB_SECTION_CONTEXT.TYPE;
7375
#sectionSidebarContext?: typeof UMB_SECTION_SIDEBAR_CONTEXT.TYPE;
7476
#actionEventContext?: typeof UMB_ACTION_EVENT_CONTEXT.TYPE;
@@ -87,24 +89,6 @@ export abstract class UmbTreeItemContextBase<
8789
// listen for page changes on the pagination manager
8890
this.pagination.addEventListener(UmbChangeEvent.TYPE, this.#onPageChange);
8991

90-
/* TODO: revisit. This is a temp solution to notify the parent it needs to reload its children
91-
there might be a better way to do this through a tree item parent context.
92-
It does not look like there is a way to have a "dynamic" parent context that will stop when a
93-
specific parent is reached (a tree item unique that matches the parentUnique of this item) */
94-
const hostElement = this.getHostElement();
95-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
96-
// @ts-ignore
97-
hostElement.addEventListener('temp-reload-tree-item-parent', (event: CustomEvent) => {
98-
const treeItem = this.getTreeItem();
99-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
100-
// @ts-ignore
101-
const unique = treeItem?.unique;
102-
if (event.detail.unique === unique) {
103-
event.stopPropagation();
104-
this.loadChildren();
105-
}
106-
});
107-
10892
window.addEventListener('navigationend', this.#debouncedCheckIsActive);
10993
}
11094

@@ -248,6 +232,10 @@ export abstract class UmbTreeItemContextBase<
248232
this.#observeFoldersOnly();
249233
});
250234

235+
this.consumeContext(UMB_TREE_ITEM_CONTEXT, (instance) => {
236+
this.parentTreeItemContext = instance;
237+
}).skipHost();
238+
251239
this.consumeContext(UMB_ACTION_EVENT_CONTEXT, (instance) => {
252240
this.#removeEventListeners();
253241
this.#actionEventContext = instance;
@@ -353,19 +341,11 @@ export abstract class UmbTreeItemContextBase<
353341
if (event.getUnique() !== this.unique) return;
354342
if (event.getEntityType() !== this.entityType) return;
355343

356-
/* TODO: revisit. This is a temp solution to notify the parent it needs to reload its children
357-
there might be a better way to do this through a tree item parent context.
358-
It does not look like there is a way to have a "dynamic" parent context that will stop when a
359-
specific parent is reached (a tree item unique that matches the parentUnique of this item) */
360-
const treeItem = this.getTreeItem();
361-
const parentUnique = treeItem?.parent.unique;
362-
363-
const customEvent = new CustomEvent('temp-reload-tree-item-parent', {
364-
detail: { unique: parentUnique },
365-
bubbles: true,
366-
composed: true,
367-
});
368-
this.getHostElement().dispatchEvent(customEvent);
344+
if (this.parentTreeItemContext) {
345+
this.parentTreeItemContext.loadChildren();
346+
} else {
347+
this.treeContext?.loadTree();
348+
}
369349
};
370350

371351
#onPageChange = (event: UmbChangeEvent) => {

src/Umbraco.Web.UI.Client/src/packages/core/tree/tree-item/tree-item-context.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface UmbTreeItemContext<TreeItemType extends UmbTreeItemModel> exten
1717
hasActions: Observable<boolean>;
1818
path: Observable<string>;
1919
pagination: UmbPaginationManager;
20+
getTreeItem(): TreeItemType | undefined;
2021
setTreeItem(treeItem: TreeItemType | undefined): void;
2122
loadChildren(): void;
2223
toggleContextMenu(): void;

0 commit comments

Comments
 (0)