From 55f1292a4999f0a0f6bc786ddf74e3fb0fa46274 Mon Sep 17 00:00:00 2001 From: genja Date: Mon, 19 Oct 2026 16:50:05 +0200 Subject: [PATCH] hierarchy editor + add/edit organization button components added --- .../add-organization.component.html | 48 +++ .../add-organization.component.scss | 23 ++ .../add-organization.component.ts | 138 ++++++++ .../edit-organization.component.html | 33 ++ .../edit-organization.component.scss | 17 + .../edit-organization.component.ts | 76 +++++ .../hierarchy-tree-editor.component.html | 155 +++++++++ .../hierarchy-tree-editor.component.scss | 108 +++++++ .../hierarchy-tree-editor.component.ts | 300 ++++++++++++++++++ 9 files changed, 898 insertions(+) create mode 100644 webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.html create mode 100644 webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.scss create mode 100644 webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.ts create mode 100644 webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.html create mode 100644 webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.scss create mode 100644 webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.ts create mode 100644 webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.html create mode 100644 webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.scss create mode 100644 webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.ts diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.html b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.html new file mode 100644 index 00000000..3ab2d2f7 --- /dev/null +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.html @@ -0,0 +1,48 @@ + + + +
+ @if (isAggregatorTarget) { + + Name + + + + URL + + + } @else { + + Institut + Lehrstuhl + + } + +
+ + +
+
+
diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.scss b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.scss new file mode 100644 index 00000000..d0b8d7df --- /dev/null +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.scss @@ -0,0 +1,23 @@ +.aoc-panel { + display: flex; + flex-direction: column; + gap: 8px; + padding: 10px 14px; + min-width: 260px; +} + +.aoc-field { + width: 100%; +} + +.aoc-radio-group { + display: flex; + justify-content: space-around; + padding: 4px 0; +} + +.aoc-actions { + display: flex; + justify-content: flex-end; + gap: 8px; +} diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.ts b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.ts new file mode 100644 index 00000000..a64e6235 --- /dev/null +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/add-organization/add-organization.component.ts @@ -0,0 +1,138 @@ +import { + Component, + EventEmitter, + Output, + ViewChild, + input, +} from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { FormsModule } from '@angular/forms'; +import { MatMenuModule, MatMenuTrigger } from '@angular/material/menu'; +import { MatButtonModule } from '@angular/material/button'; +import { MatIconModule } from '@angular/material/icon'; +import { MatFormFieldModule } from '@angular/material/form-field'; +import { MatInputModule } from '@angular/material/input'; +import { MatRadioModule } from '@angular/material/radio'; +import { MatTooltipModule } from '@angular/material/tooltip'; +import type { RawNode } from '../../hierarchy-chart.component'; + +type AggregatorType = 'institutes' | 'chairs'; + +/** + * - If `item` is itself an aggregator ('institutes' | 'chairs'), the popover + * lets the user add a new leaf organization (name + url) into it. + * - Otherwise, the popover lets the user add a missing 'institutes' or + * 'chairs' aggregator group as a child of `item` (only if one of that + * type doesn't already exist). + */ +@Component({ + selector: 'app-add-organization', + standalone: true, + imports: [ + CommonModule, + FormsModule, + MatMenuModule, + MatButtonModule, + MatIconModule, + MatFormFieldModule, + MatInputModule, + MatRadioModule, + MatTooltipModule, + ], + templateUrl: './add-organization.component.html', + styleUrls: ['./add-organization.component.scss'], +}) +export class AddOrganizationComponent { + item = input.required(); + + @Output() addOrg = new EventEmitter(); + @Output() addAgg = new EventEmitter(); + + @ViewChild(MatMenuTrigger) private menuTrigger?: MatMenuTrigger; + + newItemName = ''; + newItemUrl = ''; + aggregatorType: AggregatorType = 'institutes'; + + get isAggregatorTarget(): boolean { + const t = this.item().type; + return t === 'institutes' || t === 'chairs'; + } + + get canAdd(): boolean { + // Mirrors Vue: :disabled="!(newItemName && newItemUrl || !isAggregator)" + if (this.isAggregatorTarget) { + return !!(this.newItemName && this.newItemUrl); + } + return true; + } + + addItem(): void { + if (this.isAggregatorTarget) { + this.addOrganizationItem(); + } else { + this.addAggregatorItem(); + } + this.closeMenu(); + } + + closeMenu(): void { + this.menuTrigger?.closeMenu(); + } + + resetFields(): void { + this.newItemName = ''; + this.newItemUrl = ''; + this.aggregatorType = 'institutes'; + } + + private addAggregatorItem(): void { + const parent = this.item(); + const existing = parent.children?.find( + (c) => c.type === this.aggregatorType, + ); + if (existing) { + // Already has an aggregator of this type -> no-op, same as Vue. + return; + } + + const aggregator: RawNode = { + id: this.generateId(parent), + name_de: + this.aggregatorType === 'institutes' ? 'Instituten' : 'Lehrstühle', + name_en: this.aggregatorType === 'institutes' ? 'Institutes' : 'Chairs', + link_de: '', + link_en: '', + type: this.aggregatorType, + color: this.aggregatorType === 'institutes' ? '#3388FF' : '#FF5733', + value: 0, + children: [], + modified: 'created', + }; + + this.addAgg.emit(aggregator); + } + + private addOrganizationItem(): void { + const parent = this.item(); + const newOrganization: RawNode = { + id: this.generateId(parent), + name_de: this.newItemName, + name_en: this.newItemName, + link_de: this.newItemUrl, + link_en: this.newItemUrl, + type: 'leaf', + color: 'green', + modified: 'created', + }; + + this.resetFields(); + this.addOrg.emit(newOrganization); + } + + private generateId(parent: RawNode): string { + const count = parent.children?.length ?? 0; + const suffix = Math.random().toString(36).slice(2, 8); + return `${parent.id}-new-${count}-${suffix}`; + } +} diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.html b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.html new file mode 100644 index 00000000..70aeb831 --- /dev/null +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.html @@ -0,0 +1,33 @@ + + + +
+ + Name + + + + URL + + + +
+ + +
+
+
diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.scss b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.scss new file mode 100644 index 00000000..5ab8ba0f --- /dev/null +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.scss @@ -0,0 +1,17 @@ +.eoc-panel { + display: flex; + flex-direction: column; + gap: 8px; + padding: 10px 14px; + min-width: 280px; +} + +.eoc-field { + width: 100%; +} + +.eoc-actions { + display: flex; + justify-content: flex-end; + gap: 8px; +} diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.ts b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.ts new file mode 100644 index 00000000..16a2aa1e --- /dev/null +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/edit-organization/edit-organization.component.ts @@ -0,0 +1,76 @@ +import { + Component, + EventEmitter, + OnInit, + Output, + ViewChild, + input, +} from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { FormsModule } from '@angular/forms'; +import { MatMenuModule, MatMenuTrigger } from '@angular/material/menu'; +import { MatButtonModule } from '@angular/material/button'; +import { MatIconModule } from '@angular/material/icon'; +import { MatFormFieldModule } from '@angular/material/form-field'; +import { MatInputModule } from '@angular/material/input'; +import { MatTooltipModule } from '@angular/material/tooltip'; +import type { RawNode } from '../../hierarchy-chart.component'; + +export interface OrganizationEditResult { + name_de: string; + link_de: string; +} + +/** Port of `util-edit-organization.vue`. */ +@Component({ + selector: 'app-edit-organization', + standalone: true, + imports: [ + CommonModule, + FormsModule, + MatMenuModule, + MatButtonModule, + MatIconModule, + MatFormFieldModule, + MatInputModule, + MatTooltipModule, + ], + templateUrl: './edit-organization.component.html', + styleUrls: ['./edit-organization.component.scss'], +}) +export class EditOrganizationComponent implements OnInit { + item = input.required(); + + @Output() editItem = new EventEmitter(); + + @ViewChild(MatMenuTrigger) private menuTrigger?: MatMenuTrigger; + + newItemName = ''; + newItemUrl = ''; + + ngOnInit(): void { + this.setPrevValues(); + } + + onMenuOpened(): void { + this.setPrevValues(); + } + + get canSave(): boolean { + return !!(this.newItemName && this.newItemUrl); + } + + save(): void { + this.editItem.emit({ name_de: this.newItemName, link_de: this.newItemUrl }); + this.menuTrigger?.closeMenu(); + } + + cancel(): void { + this.menuTrigger?.closeMenu(); + } + + private setPrevValues(): void { + this.newItemName = this.item().name_de ?? ''; + this.newItemUrl = this.item().link_de ?? ''; + } +} diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.html b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.html new file mode 100644 index 00000000..be9af585 --- /dev/null +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.html @@ -0,0 +1,155 @@ +
+ + + +
+ +
+
    + @for (node of hierarchy.children; track node.id) { + + } +
+
+ +
+ + @if (numberOfSuggestions <= 0) { +
+ +
+ } @else { + + } + +
+ + + +
  • +
    + @if (hasChild(0, node)) { + + } @else { + + } + +
    + + {{ node.name_de }} + + @if (node.link_de) { + {{ node.link_de }} + } +
    + +
    + @switch (node.modified) { + @case ('updated') { + geändert + } + @case ('created') { + neu + } + @case ('deleted') { + gelöscht + } + } +
    + +
    + @if (node.modified !== 'deleted') { + + + } + + @if ( + node.type === 'leaf' && + node.modified !== 'deleted' && + node.modified !== 'created' + ) { + + + } + + @if (node.type === 'leaf' || node.modified) { + @if (node.modified === 'deleted') { + + } @else if (node.modified === 'created') { + + } @else { + + } + } +
    +
    + + @if (hasChild(0, node) && isExpanded(node)) { +
      + @for (child of node.children; track child.id) { + + } +
    + } +
  • +
    diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.scss b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.scss new file mode 100644 index 00000000..95c1b3ec --- /dev/null +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.scss @@ -0,0 +1,108 @@ +:host { + display: block; +} + +.hte-toolbar, +.hte-actions { + display: flex; + align-items: center; + gap: 8px; + padding: 4px 16px; +} + +.hte-spacer { + flex: 1 1 auto; +} + +.hte-content { + max-height: 70vh; + overflow: auto; +} + +.hte-tree, +.hte-children { + list-style: none; + margin: 0; + padding: 0; +} + +/* indentation + vertical guide line per level */ +.hte-children { + margin-left: 20px; + padding-left: 12px; + border-left: 1px solid rgba(0, 0, 0, 0.18); +} + +.hte-node-row { + display: flex; + align-items: center; + justify-content: space-between; + gap: 4px; + min-height: 40px; + padding-right: 4px; + border-radius: 4px; + border-bottom: 1px solid rgba(0, 0, 0, 0.18); + &:hover { + opacity: 0.8; + } +} + +.hte-toggle, +.hte-toggle-spacer { + flex: 0 0 40px; + width: 40px; + height: 40px; +} + +.hte-node-label { + flex: 1 1 auto; + min-width: 0; + display: flex; + flex-direction: column; +} + +.hte-node-name { + overflow-wrap: anywhere; + + &--bold { + font-weight: 600; + } +} + +.hte-node-link { + font-size: 12px; + opacity: 0.6; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.hte-node-status { + flex: 0 0 96px; // fixed column, wide enough for "gelöscht" + display: flex; + justify-content: center; + font-size: 12px; +} + +.hte-node-actions { + flex: 0 0 132px; // 3 icon buttons; same width on every row + display: flex; + align-items: center; + justify-content: flex-end; +} +.deleted_node { + &:after { + content: 'PIDZ'; + display: block; + width: 8px; + height: 8px; + border-radius: 50%; + background-color: currentColor; + } +} +.modified_node { + background-color: rgb(255, 255, 0, 0.2); +} +.created_node { + background-color: rgba(0, 255, 0, 0.1); +} diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.ts b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.ts new file mode 100644 index 00000000..76db0b61 --- /dev/null +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-tree-editor/hierarchy-tree-editor.component.ts @@ -0,0 +1,300 @@ +import { Component, Inject } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { MatButtonModule } from '@angular/material/button'; +import { MatIconModule } from '@angular/material/icon'; +import { MatTooltipModule } from '@angular/material/tooltip'; +import { + MAT_DIALOG_DATA, + MatDialogModule, + MatDialogRef, +} from '@angular/material/dialog'; + +import { AddOrganizationComponent } from './add-organization/add-organization.component'; +import { + EditOrganizationComponent, + OrganizationEditResult, +} from './edit-organization/edit-organization.component'; +import { ChipComponent } from '../../../../shared/ui/chip/chip.component'; +import type { RawNode } from '../hierarchy-chart.component'; +import type { HierarchyEditorDialogData } from '../hierarchy-edit-dialog/hierarchy-edit-dialog.component'; +@Component({ + standalone: true, + imports: [ + CommonModule, + MatButtonModule, + MatIconModule, + MatTooltipModule, + MatDialogModule, + AddOrganizationComponent, + ChipComponent, + EditOrganizationComponent, + ], + templateUrl: './hierarchy-tree-editor.component.html', + styleUrls: ['./hierarchy-tree-editor.component.scss'], +}) +export class HierarchyTreeEditorComponent { + hierarchy!: RawNode; + + private readonly originalData: RawNode; + readonly numberOfSuggestions: number; + + readonly expanded = new Set(); + + /** The previous status of each node. */ + private readonly previousStatus = new Map< + RawNode['id'], + RawNode['modified'] + >(); + isExpanded(node: RawNode): boolean { + return this.expanded.has(node.id); + } + + toggle(node: RawNode): void { + this.expanded.has(node.id) + ? this.expanded.delete(node.id) + : this.expanded.add(node.id); + } + + readonly hasChild = (_: number, node: RawNode) => + !!(node.children && node.children.length); + + constructor( + private readonly dialogRef: MatDialogRef< + HierarchyTreeEditorComponent, + RawNode + >, + @Inject(MAT_DIALOG_DATA) data: HierarchyEditorDialogData, + ) { + this.numberOfSuggestions = data.numberOfSuggestions; + this.originalData = JSON.parse(JSON.stringify(data.tree)); + this.initHierarchy(); + } + + /* ------------------------------ init / lifecycle ------------------------------ */ + + private initHierarchy(): void { + this.hierarchy = JSON.parse(JSON.stringify(this.originalData)); + this.expanded.clear(); + } + + rollback(): void { + this.initHierarchy(); + } + + /* --------------------------------- add / edit --------------------------------- */ + + private ensureId(node: RawNode): void { + if (!node.id) { + node.id = `new-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; + } + node.children?.forEach((c) => this.ensureId(c)); + } + + addOrganization(newOrg: RawNode, target: RawNode): void { + this.ensureId(newOrg); + this.expanded.add(target.id); + if (!target.children) { + target.children = []; + } + if (target.value === undefined) { + target.value = 0; + } + target.children.unshift(newOrg); + // target.value++; + + // const targetParent = this.findParent(target.id); + // if (targetParent?.children) { + // targetParent.value = targetParent.children.reduce( + // (acc, child) => (child.children ? acc + child.children.length : acc), + // 0, + // ); + // } + } + + addAggregator(newAgg: RawNode, target: RawNode): void { + this.ensureId(newAgg); + this.expanded.add(target.id); + if (!target.children) { + target.children = []; + } + if (target.value === undefined) { + target.value = 0; + } + target.children.unshift(newAgg); + } + + editItem(result: OrganizationEditResult, target: RawNode): void { + // Update the target node if some fields have changed. If nothing has changed, do nothing (same as Vue). + if ( + result.name_de === target.name_de && + result.link_de === target.link_de + ) { + return; + } + this.previousStatus.set(target.id, target.modified); + target.name_de = result.name_de; + target.link_de = result.link_de; + target.modified = 'updated'; + } + + /* ---------------------------------- structure ---------------------------------- */ + + /** Finds the parent of the node with the given id, searching from the tree root. */ + findParent(id: string, items?: RawNode[]): RawNode | undefined { + const searchIn = items ?? [this.hierarchy]; + if (searchIn.length === 0) return undefined; + + const directParent = searchIn.find((item) => + (item.children ?? []).some((child) => child.id === id), + ); + if (directParent) return directParent; + + const allChildren = searchIn.flatMap((item) => item.children ?? []); + if (allChildren.length === 0) return undefined; + + return this.findParent(id, allChildren); + } + // For the case we want to affect the sorting of items + // findItemBefore(item: RawNode): RawNode | undefined { + // const parent = this.findParent(item.id); + // if (!parent?.children) return undefined; + // return parent.children.find( + // (_, i, all) => all[i + 1] && all[i + 1].id === item.id, + // ); + // } + + // findItemAfter(item: RawNode): RawNode | undefined { + // const parent = this.findParent(item.id); + // if (!parent?.children) return undefined; + // return parent.children.find( + // (_, i, all) => all[i - 1] && all[i - 1].id === item.id, + // ); + // } + + // in initHierarchy(): this.previousStatus.clear(); + + markAsDeleted(item: RawNode): void { + if (item.modified === 'deleted') return; + this.previousStatus.set(item.id, item.modified); + item.modified = 'deleted'; + + if (item.children) { + // nodes that only exist in the editor are dropped instead of deleted + item.children = item.children.filter((c) => c.modified !== 'created'); + item.children.forEach((c) => this.markAsDeleted(c)); + } + } + + restoreElement(item: RawNode): void { + this.restoreAncestors(item); + this.restoreOne(item); + this.restoreDescendants(item); + } + + private restoreOne(item: RawNode): void { + item.modified = this.previousStatus.get(item.id) ?? ''; + this.previousStatus.delete(item.id); + } + + private restoreAncestors(item: RawNode): void { + const parent = this.findParent(item.id); + if (parent?.modified === 'deleted') { + this.restoreAncestors(parent); + this.restoreOne(parent); + } + } + + private restoreDescendants(item: RawNode): void { + for (const c of item.children ?? []) { + if (c.modified === 'deleted') this.restoreOne(c); + this.restoreDescendants(c); + } + } + detach(item: RawNode): void { + const parent = this.findParent(item.id); + if (!parent?.children) return; + + const index = parent.children.indexOf(item); + if (index !== -1) { + parent.children.splice(index, 1); + // if (parent.value !== undefined) { + // parent.value--; + // } + } + } + // For the case we want to affect the sorting of items + // injectNextTo(item: RawNode, target: RawNode | undefined, after = true): void { + // if (!target || target === this.hierarchy) return; + + // this.detach(item); + + // const targetParent = this.findParent(target.id); + // if (!targetParent?.children) return; + + // const index = targetParent.children.indexOf(target); + // if (index !== -1) { + // targetParent.children.splice(index + (after ? 1 : 0), 0, item); + // } + // } + + // appendTo(item: RawNode, target: RawNode | undefined): void { + // if (!target) return; + + // this.detach(item); + + // if (!target.children) { + // target.children = []; + // } + // target.children.push(item); + // } + + // move(item: RawNode, direction: 'left' | 'up' | 'right' | 'down'): void { + // switch (direction) { + // case 'left': + // this.injectNextTo(item, this.findParent(item.id)); + // break; + // case 'up': + // this.injectNextTo(item, this.findItemBefore(item), false); + // break; + // case 'right': + // this.appendTo(item, this.findItemBefore(item)); + // break; + // case 'down': + // this.injectNextTo(item, this.findItemAfter(item)); + // break; + // } + // } + + /* ----------------------------------- dialog ------------------------------------ */ + + async save(): Promise { + const cleaned = this.cleanHierarchy( + JSON.parse(JSON.stringify(this.hierarchy)), + ); + console.log('[HierarchyTreeEditor] cleaned hierarchy to save:', cleaned); + this.dialogRef.close(cleaned); + } + + cancel(): void { + this.dialogRef.close(undefined); + } + + private cleanHierarchy(node: RawNode): RawNode { + const result: RawNode = { ...node }; + delete (result as any).id; + if (result.modified === '') { + delete result.modified; + } + if (result.value !== undefined) { + result.value = result.children?.length ?? 0; + } + + if (Array.isArray(result.children)) { + result.children = result.children.map((child) => + this.cleanHierarchy(child), + ); + } + + return result; + } +}