From 8c81a30ceced8e03730403e15cdaa0821ad17420 Mon Sep 17 00:00:00 2001 From: Keith Daulton Date: Wed, 14 Sep 2022 11:10:26 -0400 Subject: [PATCH] Adds expand and collapse to tree list --- .../components/list/file-change-list-item.ts | 12 ++++++++ .../apps/shared/components/list/list-container.ts | 29 ++++++++++++++++- .../apps/shared/components/list/list-item.ts | 36 +++++++++++++++++----- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/webviews/apps/shared/components/list/file-change-list-item.ts b/src/webviews/apps/shared/components/list/file-change-list-item.ts index 7930a74..c8af9d1 100644 --- a/src/webviews/apps/shared/components/list/file-change-list-item.ts +++ b/src/webviews/apps/shared/components/list/file-change-list-item.ts @@ -18,6 +18,8 @@ const template = html` tree="${x => x.tree}" level="${x => x.level}" active="${x => x.active}" + expanded="${x => x.expanded}" + parentexpanded="${x => x.parentexpanded}" @selected="${(x, c) => x.onComparePrevious(c.event)}" > ${x => x.statusName} @@ -121,6 +123,12 @@ export class FileChangeListItem extends FASTElement { @attr({ mode: 'boolean' }) tree = false; + @attr({ mode: 'boolean' }) + expanded = true; + + @attr({ mode: 'boolean' }) + parentexpanded = true; + @attr({ converter: numberConverter }) level = 1; @@ -153,6 +161,10 @@ export class FileChangeListItem extends FASTElement { this.base?.focus(options); } + get isHidden() { + return this.base?.isHidden ?? 'false'; + } + get pathIndex() { return this.path.lastIndexOf('/'); } diff --git a/src/webviews/apps/shared/components/list/list-container.ts b/src/webviews/apps/shared/components/list/list-container.ts index 84caaa9..ae3fee2 100644 --- a/src/webviews/apps/shared/components/list/list-container.ts +++ b/src/webviews/apps/shared/components/list/list-container.ts @@ -51,7 +51,34 @@ export class ListContainer extends FASTElement { } handleSelected(e: CustomEvent) { - this.$emit('selected', e.detail); + if (!e.target || !e.detail.branch) return; + const target = e.target as ListItem; + const level = target.getAttribute('level'); + + const getLevel = (el: ListItem) => parseInt(el.getAttribute('level') ?? '0', 10); + const getParent = (el: ListItem) => { + const level = getLevel(el); + let prev = el.previousElementSibling; + while (prev) { + const prevLevel = getLevel(prev as ListItem); + if (prevLevel < level) { + return prev as ListItem; + } + prev = prev.previousElementSibling; + } + + return undefined; + }; + let nextElement = target.nextElementSibling as ListItem; + while (nextElement) { + if (nextElement.getAttribute('level') === level) { + break; + } + const parentElement = getParent(nextElement); + nextElement.setAttribute('parentexpanded', parentElement?.expanded === false ? 'false' : 'true'); + nextElement.setAttribute('expanded', e.detail.expanded ? 'true' : 'false'); + nextElement = nextElement.nextElementSibling as ListItem; + } } handleKeydown(e: KeyboardEvent) { diff --git a/src/webviews/apps/shared/components/list/list-item.ts b/src/webviews/apps/shared/components/list/list-item.ts index 654bbfe..d70adec 100644 --- a/src/webviews/apps/shared/components/list/list-item.ts +++ b/src/webviews/apps/shared/components/list/list-item.ts @@ -17,7 +17,11 @@ export interface ListItemSelectedDetail { } const template = html` -