added highlighting for organization on navigation

This commit is contained in:
2026-11-17 21:28:42 +01:00
parent 6b6a5c7cff
commit d60c17abf4
2 changed files with 259 additions and 2 deletions
@@ -50,6 +50,9 @@ interface TreeLevel {
const QUERY_PARAM_PATH = 'path';
const QUERY_PARAM_LANG = 'lang';
const QUERY_PARAM_ORGANIZATION_FOCUS = 'orgFocus';
const QUERY_PARAM_ORGANIZATION_FOCUS_PATH = 'orgFocusPath';
const QUERY_PARAM_ORGANIZATION_URL = 'orgUrl';
const LEAF_ROW_HEIGHT = 22;
const MIN_TREE_HEIGHT = 200;
@@ -99,6 +102,8 @@ export class HierarchyChartComponent implements OnInit {
readonly loading = signal<boolean>(true);
readonly lang = signal<'de' | 'en'>('de');
readonly levels = signal<TreeLevel[]>([]);
/** Node highlighted after a badge navigation or an analysis click. */
readonly highlightedId = signal<string | null>(null);
private readonly chartHost =
viewChild<ElementRef<HTMLDivElement>>('chartHost');
@@ -109,6 +114,10 @@ export class HierarchyChartComponent implements OnInit {
private readonly snackBar = inject(MatSnackBar);
private readonly idMap = new Map<string, RawNode>();
/** Several hierarchy nodes can share one URL; keep every candidate. */
private readonly urlToNodeIds = new Map<string, string[]>();
/** Last `orgFocus` value + path that was applied, so unrelated query changes don't re-focus. */
private lastFocusKey: string | null = null;
universityRoot: RawNode | null = null;
/**
@@ -187,7 +196,37 @@ export class HierarchyChartComponent implements OnInit {
this.isOwnNavigation = false;
return;
}
const langParam = params.get(QUERY_PARAM_LANG);
if (langParam === 'de' || langParam === 'en') {
this.lang.set(langParam);
}
const focus = this.normalizeOrgUrl(
params.get(QUERY_PARAM_ORGANIZATION_FOCUS),
);
const focusPath = this.parseFocusPath(
params.get(QUERY_PARAM_ORGANIZATION_FOCUS_PATH),
);
const focusKey = focus
? `${focus}\u0000${focusPath.join('\u0000')}`
: null;
if (
focus &&
focusKey !== this.lastFocusKey &&
this.focusOrganization(focus, focusPath)
) {
this.lastFocusKey = focusKey;
return;
}
if (!focus) {
this.lastFocusKey = null;
}
this.restoreFromUrl(params);
this.highlightAnalyzedOrganization(
params.get(QUERY_PARAM_ORGANIZATION_URL),
);
});
this.initialized = true;
}
@@ -241,6 +280,7 @@ export class HierarchyChartComponent implements OnInit {
const copy = structuredClone(children ?? []) as RawNode[];
this.idMap.clear();
this.urlToNodeIds.clear();
this.assignIds(copy, 'root');
this.universityRoot = {
@@ -267,6 +307,16 @@ export class HierarchyChartComponent implements OnInit {
node.id = id;
if (registerInMap) {
this.idMap.set(id, node);
for (const link of [node.link_de, node.link_en]) {
const normalized = this.normalizeOrgUrl(link);
if (!normalized) continue;
const candidates = this.urlToNodeIds.get(normalized);
if (candidates) {
if (!candidates.includes(id)) candidates.push(id);
} else {
this.urlToNodeIds.set(normalized, [id]);
}
}
}
if (node.children?.length) {
this.assignIds(node.children, id, registerInMap);
@@ -301,6 +351,184 @@ export class HierarchyChartComponent implements OnInit {
this.updateQueryParams();
}
/* --------------------------- organization focus --------------------------- */
/**
* Normalizes a hierarchy organization URL the same way the backend does:
* trim, drop trailing slashes and the common `/index` suffix.
*/
private normalizeOrgUrl(value: string | null | undefined): string | null {
if (!value) return null;
const normalized = value.trim().replace(/\/+$/, '');
const withoutIndex = normalized.endsWith('/index')
? normalized.slice(0, -'/index'.length).replace(/\/+$/, '')
: normalized;
return withoutIndex || null;
}
private findNodeIdByUrl(
value: string | null | undefined,
expectedPath: string[] = [],
preferVisible = false,
): string | null {
const normalized = this.normalizeOrgUrl(value);
const candidates = normalized
? (this.urlToNodeIds.get(normalized) ?? [])
: [];
if (!candidates.length) return null;
// Hierarchy URLs are not unique (several nodes can link to the same site),
// so the index-provided name path decides which node is meant.
if (expectedPath.length) {
const pathMatch = candidates.find((id) =>
this.matchesNamePath(id, expectedPath),
);
if (pathMatch) return pathMatch;
}
// Reloads only carry `orgUrl`; prefer the candidate on the open path.
if (preferVisible) {
const visible = candidates.find((id) => this.isNodeVisible(id));
if (visible) return visible;
}
return candidates[0];
}
/** Parses the hierarchy path (`orgFocusPath`) sent by the person badge. */
private parseFocusPath(value: string | null | undefined): string[] {
if (!value) return [];
try {
const parsed: unknown = JSON.parse(value);
if (!Array.isArray(parsed)) return [];
return parsed
.filter((segment): segment is string => typeof segment === 'string')
.map((segment) => segment.trim())
.filter(Boolean);
} catch {
return [];
}
}
/** Ancestor + node names from the faculty down (synthetic `root` excluded). */
private nodeNamePath(nodeId: string): RawNode[] {
return [...this.ancestorIds(nodeId).slice(1), nodeId]
.map((id) => this.idMap.get(id))
.filter((node): node is RawNode => !!node);
}
private matchesNamePath(nodeId: string, expectedPath: string[]): boolean {
if (!expectedPath.length) return false;
const actual = this.nodeNamePath(nodeId);
if (actual.length !== expectedPath.length) return false;
return expectedPath.every((name, index) =>
this.nodeMatchesName(actual[index], name),
);
}
private nodeMatchesName(node: RawNode, expected: string): boolean {
const normalize = (value: string | null | undefined) =>
(value ?? '').replace(/\s+/g, ' ').trim().toLocaleLowerCase();
const wanted = normalize(expected);
return (
normalize(node.name_de) === wanted || normalize(node.name_en) === wanted
);
}
/** Mirrors `buildLevel`: a level shows its children and group grandchildren. */
private levelRendersNode(levelId: string, nodeId: string): boolean {
const level = this.idMap.get(levelId);
if (!level) return false;
if (level.id === nodeId) return true;
for (const child of level.children ?? []) {
if (child.id === nodeId) return true;
const isGroup = child.type === 'institutes' || child.type === 'chairs';
if (
isGroup &&
child.children?.some((grandchild) => grandchild.id === nodeId)
) {
return true;
}
}
return false;
}
private isNodeVisible(nodeId: string): boolean {
return this.levels().some((level) =>
this.levelRendersNode(level.id, nodeId),
);
}
/** Positional parent ids of a node, e.g. `root-2-0-5` -> `root`, `root-2`, `root-2-0`. */
private ancestorIds(nodeId: string): string[] {
const ids = ['root'];
const parts = nodeId.split('-');
for (let index = 2; index < parts.length; index++) {
ids.push(parts.slice(0, index).join('-'));
}
return ids;
}
/**
* Opens the levels that display the requested organization and highlights
* its node. Used by the person-page badge (`orgFocus` query param).
*/
private focusOrganization(
orgUrl: string,
expectedPath: string[] = [],
): boolean {
const nodeId = this.findNodeIdByUrl(orgUrl, expectedPath);
if (!nodeId) return false;
// Open the faculty level, not the group directly above the target: the
// faculty chart shows the organization inside the full faculty subtree.
// For deeper nodes, keep opening levels until the target is rendered.
const fullPath = [...this.ancestorIds(nodeId), nodeId];
let lastLevelIndex = fullPath.findIndex(
(id) => this.idMap.get(id)?.type === 'faculty',
);
if (lastLevelIndex < 0) {
lastLevelIndex = Math.max(0, fullPath.length - 2);
}
while (
lastLevelIndex < fullPath.length - 1 &&
!this.levelRendersNode(fullPath[lastLevelIndex], nodeId)
) {
lastLevelIndex++;
}
const levels = fullPath
.slice(0, lastLevelIndex + 1)
.map((id) => this.idMap.get(id))
.filter((node): node is RawNode => !!node)
.map((node) => this.buildLevel(node));
if (!levels.length) return false;
this.highlightedId.set(nodeId);
this.levels.set(levels);
this.updateQueryParams();
return true;
}
/** Keeps the analyzed organization highlighted after query-param navigation. */
private highlightAnalyzedOrganization(orgUrl: string | null): void {
const nodeId = this.findNodeIdByUrl(orgUrl, [], true);
if (!nodeId || nodeId === this.highlightedId()) return;
this.highlightedId.set(nodeId);
if (this.universityRoot) {
this.rebuildLevelsKeepingPath();
}
}
private setHighlightedNode(nodeId: string | null): void {
if (this.highlightedId() === nodeId) return;
this.highlightedId.set(nodeId);
if (this.universityRoot) {
this.rebuildLevelsKeepingPath();
}
}
/** Writes the current levels path + language into the URL as query params. */
private updateQueryParams(): void {
const path = this.levels()
@@ -386,6 +614,7 @@ export class HierarchyChartComponent implements OnInit {
}
private onLeafSelect(raw: RawNode): void {
this.setHighlightedNode(raw.id);
this.analyzeOrgEvent.emit({
name: raw.name_de ?? raw.name_en ?? '',
url: raw.link_de ?? raw.link_en ?? '',
@@ -394,6 +623,18 @@ export class HierarchyChartComponent implements OnInit {
/* ------------------------------ tree building ----------------------------- */
/** Extra ECharts item style that marks the focused/analyzed node. */
private highlightStyle(nodeId: string): Record<string, unknown> {
if (this.highlightedId() !== nodeId) return {};
const warning = this.chartTheme.colors().warning;
return {
borderColor: warning,
borderWidth: 3,
shadowBlur: 10,
shadowColor: warning,
};
}
private getLink(raw: RawNode): string | null | undefined {
return this.lang() === 'en'
? (raw.link_en ?? raw.link_de)
@@ -428,6 +669,7 @@ export class HierarchyChartComponent implements OnInit {
(isGroup
? this.chartTheme.colors().blueHighlight
: this.chartTheme.colors().success),
...this.highlightStyle(raw.id),
},
};
@@ -451,7 +693,15 @@ export class HierarchyChartComponent implements OnInit {
}
private buildLevel(raw: RawNode): TreeLevel {
const children = (raw.children || []).map((c) => this.toChartNode(c, true));
// Leaves inherit the icon of the group that contains them, including the
// direct children of a group that is itself the root of this level.
const parentType =
raw.type === 'institutes' || raw.type === 'chairs'
? raw.type
: undefined;
const children = (raw.children || []).map((c) =>
this.toChartNode(c, true, parentType),
);
const rootData = {
id: raw.id,
@@ -459,7 +709,10 @@ export class HierarchyChartComponent implements OnInit {
nodeType: raw.type,
hasChildren: true,
link: this.getLink(raw),
itemStyle: { color: raw['color'] || this.chartTheme.colors().primary },
itemStyle: {
color: raw['color'] || this.chartTheme.colors().primary,
...this.highlightStyle(raw.id),
},
children,
};
@@ -21,6 +21,8 @@ import { HierarchySuggestion } from '../../api/model/hierarchySuggestion';
import { PageContainerComponent } from '../../shared/ui/page-container/page-container.component';
const ORGANIZATION_URL_PARAM = 'orgUrl';
const ORGANIZATION_NAME_PARAM = 'orgName';
const ORGANIZATION_FOCUS_PARAM = 'orgFocus';
const ORGANIZATION_FOCUS_PATH_PARAM = 'orgFocusPath';
const ORGMINER_EXPANDED_PARAM = 'orgExpanded';
const ORGMINER_SELECTION_CATEGORY_PARAM = 'orgSelectionCategory';
const ORGMINER_SELECTION_INDEX_PARAM = 'orgSelectionIndex';
@@ -119,6 +121,8 @@ export class HierarchyComponent implements OnInit {
queryParams: {
[ORGANIZATION_URL_PARAM]: organizationUrl,
[ORGANIZATION_NAME_PARAM]: org.name,
[ORGANIZATION_FOCUS_PARAM]: null,
[ORGANIZATION_FOCUS_PATH_PARAM]: null,
[ORGMINER_EXPANDED_PARAM]: null,
[ORGMINER_SELECTION_CATEGORY_PARAM]: null,
[ORGMINER_SELECTION_INDEX_PARAM]: null,