From 6d9579d3e65c7dc86804289be297bf508e8f0b1d Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 15 Jun 2022 20:06:01 +0200 Subject: [PATCH] web/elements: rework treeview parents Signed-off-by: Jens Langhammer --- web/src/elements/TreeView.ts | 54 +++++++++++++++-------------- web/src/pages/users/UserListPage.ts | 4 +-- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/web/src/elements/TreeView.ts b/web/src/elements/TreeView.ts index c1c7e2f89..63d04b5ec 100644 --- a/web/src/elements/TreeView.ts +++ b/web/src/elements/TreeView.ts @@ -11,7 +11,7 @@ import { EVENT_REFRESH } from "../constants"; import { setURLParams } from "./router/RouteMatch"; export interface TreeViewItem { - id: string; + id?: string; label: string; childItems: TreeViewItem[]; parent?: TreeViewItem; @@ -30,7 +30,7 @@ export class TreeViewNode extends LitElement { host?: TreeView; @property() - path = ""; + activePath = ""; @property() separator = ""; @@ -43,7 +43,9 @@ export class TreeViewNode extends LitElement { const pathItems = []; let item = this.item; while (item) { - pathItems.push(item.id); + if (item.id) { + pathItems.push(item.id); + } item = item.parent; } return pathItems.reverse().join(this.separator); @@ -54,14 +56,14 @@ export class TreeViewNode extends LitElement { } firstUpdated(): void { - const pathSegments = this.path.split(this.separator); + const pathSegments = this.activePath.split(this.separator); const level = this.item?.level || 0; // Ignore the last item as that shouldn't be expanded pathSegments.pop(); if (pathSegments[level] == this.item?.id) { this.open = true; } - if (this.path === this.fullPath && this.host !== undefined) { + if (this.activePath === this.fullPath && this.host !== undefined) { this.host.activeNode = this; } } @@ -122,7 +124,7 @@ export class TreeViewNode extends LitElement { ${this.item?.childItems.map((item) => { return html``; @@ -143,61 +145,61 @@ export class TreeView extends LitElement { items: string[] = []; @property() - path = ""; + activePath = ""; @state() activeNode?: TreeViewNode; separator = "/"; - createNode(path: string[], tree: TreeViewItem[], level: number): TreeViewItem { + createNode(path: string[], parentItem: TreeViewItem, level: number): TreeViewItem { const id = path.shift(); - const idx = tree.findIndex((e: TreeViewItem) => { + const idx = parentItem.childItems.findIndex((e: TreeViewItem) => { return e.id == id; }); if (idx < 0) { const item: TreeViewItem = { - id: id || "", + id: id, label: id || "", childItems: [], level: level, + parent: parentItem, }; - tree.push(item); + parentItem.childItems.push(item); if (path.length !== 0) { - const child = this.createNode(path, tree[tree.length - 1].childItems, level + 1); + const child = this.createNode(path, item, level + 1); child.parent = item; } return item; } else { - const child = this.createNode(path, tree[idx].childItems, level + 1); - child.parent = tree[idx]; + const child = this.createNode(path, parentItem.childItems[idx], level + 1); return child; } } - parse(data: string[]): TreeViewItem[] { - const tree: TreeViewItem[] = []; + parse(data: string[]): TreeViewItem { + const rootItem: TreeViewItem = { + id: undefined, + label: t`Root`, + childItems: [], + level: -1, + }; for (let i = 0; i < data.length; i++) { const path: string = data[i]; const split: string[] = path.split(this.separator); - this.createNode(split, tree, 0); + this.createNode(split, rootItem, 0); } - return tree; + return rootItem; } render(): TemplateResult { - const result = this.parse(this.items); + const rootItem = this.parse(this.items); return html`
    { order = "last_login"; @property() - path = getURLParam("path", "/"); + activePath = getURLParam("path", "/"); static get styles(): CSSResult[] { return super.styles.concat(PFDescriptionList, PFCard, PFAlert, AKGlobal); @@ -332,7 +332,7 @@ export class UserListPage extends TablePage { .then((paths) => { return html``; }), )}