hierarchy editor + add/edit organization button components added

This commit is contained in:
2026-10-19 16:50:05 +02:00
parent 153c50dab9
commit 55f1292a49
9 changed files with 898 additions and 0 deletions
@@ -0,0 +1,48 @@
<button
mat-icon-button
[matMenuTriggerFor]="menu"
matTooltip="Hinzufügen"
(click)="resetFields()">
<mat-icon color="primary">add_circle</mat-icon>
</button>
<mat-menu #menu="matMenu">
<div class="aoc-panel" (click)="$event.stopPropagation()">
@if (isAggregatorTarget) {
<mat-form-field appearance="outline" class="aoc-field">
<mat-label>Name</mat-label>
<input
matInput
[(ngModel)]="newItemName"
(keydown)="$event.stopPropagation()" />
</mat-form-field>
<mat-form-field appearance="outline" class="aoc-field">
<mat-label>URL</mat-label>
<input
matInput
[(ngModel)]="newItemUrl"
(keydown)="$event.stopPropagation()" />
</mat-form-field>
} @else {
<mat-radio-group [(ngModel)]="aggregatorType" class="aoc-radio-group">
<mat-radio-button value="institutes" color="primary"
>Institut</mat-radio-button
>
<mat-radio-button value="chairs" color="primary"
>Lehrstuhl</mat-radio-button
>
</mat-radio-group>
}
<div class="aoc-actions">
<button
mat-stroked-button
color="primary"
[disabled]="!canAdd"
(click)="addItem()">
Hinzufügen
</button>
<button mat-stroked-button (click)="closeMenu()">Abbrechen</button>
</div>
</div>
</mat-menu>
@@ -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;
}
@@ -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<RawNode>();
@Output() addOrg = new EventEmitter<RawNode>();
@Output() addAgg = new EventEmitter<RawNode>();
@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}`;
}
}
@@ -0,0 +1,33 @@
<button mat-icon-button [matMenuTriggerFor]="menu" matTooltip="Bearbeiten">
<mat-icon>edit</mat-icon>
</button>
<mat-menu #menu="matMenu" (opened)="onMenuOpened()">
<div class="eoc-panel" (click)="$event.stopPropagation()">
<mat-form-field appearance="outline" class="eoc-field">
<mat-label>Name</mat-label>
<input
matInput
[(ngModel)]="newItemName"
(keydown)="$event.stopPropagation()" />
</mat-form-field>
<mat-form-field appearance="outline" class="eoc-field">
<mat-label>URL</mat-label>
<input
matInput
[(ngModel)]="newItemUrl"
(keydown)="$event.stopPropagation()" />
</mat-form-field>
<div class="eoc-actions">
<button
mat-stroked-button
color="primary"
[disabled]="!canSave"
(click)="save()">
Speichern
</button>
<button mat-stroked-button (click)="cancel()">Abbrechen</button>
</div>
</div>
</mat-menu>
@@ -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;
}
@@ -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<RawNode>();
@Output() editItem = new EventEmitter<OrganizationEditResult>();
@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 ?? '';
}
}
@@ -0,0 +1,155 @@
<div class="hte-toolbar">
<button mat-icon-button (click)="rollback()" matTooltip="Zurücksetzen">
<mat-icon>refresh</mat-icon>
</button>
<span class="hte-spacer"></span>
<button mat-icon-button (click)="cancel()" matTooltip="Schließen">
<mat-icon color="warn">close</mat-icon>
</button>
</div>
<div mat-dialog-content class="hte-content">
<ul class="hte-tree" role="tree">
@for (node of hierarchy.children; track node.id) {
<ng-container
*ngTemplateOutlet="
nodeTpl;
context: { $implicit: node }
"></ng-container>
}
</ul>
</div>
<div mat-dialog-actions class="hte-actions">
<span class="hte-spacer"></span>
@if (numberOfSuggestions <= 0) {
<div
[matTooltip]="
'Sie haben keine Vorschläge mehr übrig, um Änderungen zu speichern.'
">
<button [disabled]="true" mat-stroked-button color="primary">
Speichern
</button>
</div>
} @else {
<button
mat-stroked-button
color="primary"
(click)="save()"
[matTooltip]="
'Sie haben noch ' +
numberOfSuggestions +
' Vorschläge übrig, um Änderungen zu speichern.'
">
Speichern
</button>
}
<button mat-stroked-button (click)="cancel()">Abbrechen</button>
</div>
<!-- Recursive node template -->
<ng-template #nodeTpl let-node>
<li
class="hte-node"
role="treeitem"
[attr.aria-expanded]="hasChild(0, node) ? isExpanded(node) : null">
<div class="hte-node-row" [class.hte-node-row--branch]="hasChild(0, node)">
@if (hasChild(0, node)) {
<button
mat-icon-button
class="hte-toggle"
(click)="toggle(node)"
[attr.aria-label]="'toggle ' + node.name_de">
<mat-icon>{{
isExpanded(node) ? 'expand_more' : 'chevron_right'
}}</mat-icon>
</button>
} @else {
<span class="hte-toggle-spacer"></span>
}
<div class="hte-node-label">
<span
class="hte-node-name"
[class.hte-node-name--bold]="hasChild(0, node)">
{{ node.name_de }}
</span>
@if (node.link_de) {
<span class="hte-node-link">{{ node.link_de }}</span>
}
</div>
<div class="hte-node-status">
@switch (node.modified) {
@case ('updated') {
<app-chip [color]="'info'"><span>geändert</span></app-chip>
}
@case ('created') {
<app-chip [color]="'success'"><span>neu</span></app-chip>
}
@case ('deleted') {
<app-chip [color]="'danger'"><span>gelöscht</span></app-chip>
}
}
</div>
<div class="hte-node-actions">
@if (node.modified !== 'deleted') {
<app-add-organization
[item]="node"
(addOrg)="addOrganization($event, node)"
(addAgg)="addAggregator($event, node)">
</app-add-organization>
}
@if (
node.type === 'leaf' &&
node.modified !== 'deleted' &&
node.modified !== 'created'
) {
<app-edit-organization
[item]="node"
(editItem)="editItem($event, node)">
</app-edit-organization>
}
@if (node.type === 'leaf' || node.modified) {
@if (node.modified === 'deleted') {
<button
mat-icon-button
(click)="restoreElement(node)"
matTooltip="Wiederherstellen">
<mat-icon color="primary">restore</mat-icon>
</button>
} @else if (node.modified === 'created') {
<button
mat-icon-button
(click)="detach(node)"
matTooltip="Entfernen">
<mat-icon color="warn">close</mat-icon>
</button>
} @else {
<button
mat-icon-button
(click)="markAsDeleted(node)"
matTooltip="Löschen">
<mat-icon color="warn">delete</mat-icon>
</button>
}
}
</div>
</div>
@if (hasChild(0, node) && isExpanded(node)) {
<ul class="hte-children" role="group">
@for (child of node.children; track child.id) {
<ng-container
*ngTemplateOutlet="
nodeTpl;
context: { $implicit: child }
"></ng-container>
}
</ul>
}
</li>
</ng-template>
@@ -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);
}
@@ -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<RawNode['id']>();
/** 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<void> {
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;
}
}