Files
smartguide/webapp/src/app/resolvers/utils/topic-legend.ts
T

227 lines
5.6 KiB
TypeScript

import { topicHierarchyColor, TopicHierarchyColorInput } from './topic-colors';
export interface TopicLegendTopic extends TopicHierarchyColorInput {
subfield_id?: string | null;
subfield_name?: string | null;
subfield_description?: string | null;
field_description?: string | null;
domain_description?: string | null;
}
export interface TopicLegendItem {
id: string;
name: string;
description: string;
tooltip: string;
}
export interface TopicLegendGroup {
id: string;
name: string;
description: string;
domainName: string;
domainDescription: string;
color: string;
tooltip: string;
items: TopicLegendItem[];
}
export interface TopicLegendSubfieldSelection {
group: TopicLegendGroup;
item: TopicLegendItem;
}
export interface TopicLegendSelectionState {
fieldIds: Set<string>;
subfieldIds: Set<string>;
}
export function getTopicLegendFieldId(topic: TopicLegendTopic): string {
const fieldName = normalizeLegendValue(topic.field_name) ?? 'Ohne Fachgebiet';
return topic.field_id ?? normalizeLegendKey(fieldName);
}
export function getTopicLegendSubfieldId(topic: TopicLegendTopic): string {
const subfieldName = normalizeLegendValue(topic.subfield_name);
return (
topic.subfield_id ?? (subfieldName ? normalizeLegendKey(subfieldName) : '')
);
}
export function toggleTopicLegendSelection(
currentSelection: ReadonlySet<string>,
id: string,
): Set<string> {
const nextSelection = new Set(currentSelection);
if (nextSelection.has(id)) {
nextSelection.delete(id);
return nextSelection;
}
nextSelection.add(id);
return nextSelection;
}
export function toggleTopicLegendFieldSelection(
currentFieldIds: ReadonlySet<string>,
currentSubfieldIds: ReadonlySet<string>,
group: TopicLegendGroup,
): TopicLegendSelectionState {
const fieldIds = toggleTopicLegendSelection(currentFieldIds, group.id);
const subfieldIds = new Set(currentSubfieldIds);
if (fieldIds.has(group.id)) {
for (const item of group.items) {
subfieldIds.delete(item.id);
}
}
return { fieldIds, subfieldIds };
}
export function toggleTopicLegendSubfieldSelection(
currentFieldIds: ReadonlySet<string>,
currentSubfieldIds: ReadonlySet<string>,
selection: TopicLegendSubfieldSelection,
): TopicLegendSelectionState {
const fieldIds = new Set(currentFieldIds);
const subfieldIds = toggleTopicLegendSelection(
currentSubfieldIds,
selection.item.id,
);
if (subfieldIds.has(selection.item.id)) {
fieldIds.delete(selection.group.id);
}
return { fieldIds, subfieldIds };
}
export function buildTopicLegendGroups(
topics: readonly TopicLegendTopic[],
): TopicLegendGroup[] {
const groups = new Map<string, TopicLegendGroup>();
for (const topic of topics) {
const subfieldName = normalizeLegendValue(topic.subfield_name);
if (!subfieldName) {
continue;
}
const fieldName =
normalizeLegendValue(topic.field_name) ?? 'Ohne Fachgebiet';
const fieldDescription =
normalizeLegendValue(topic.field_description) ?? '';
const domainName = normalizeLegendValue(topic.domain_name) ?? '';
const domainDescription =
normalizeLegendValue(topic.domain_description) ?? '';
const subfieldDescription =
normalizeLegendValue(topic.subfield_description) ?? '';
const fieldKey = getTopicLegendFieldId(topic);
const subfieldKey = getTopicLegendSubfieldId(topic);
if (!groups.has(fieldKey)) {
groups.set(fieldKey, {
id: fieldKey,
name: fieldName,
description: fieldDescription,
domainName,
domainDescription,
color: topicHierarchyColor(topic),
tooltip: buildTopicLegendTooltip([
{
label: 'Fachgebiet',
value: fieldName,
description: fieldDescription,
},
{
label: 'Wissenschaftsbereich',
value: domainName,
description: domainDescription,
},
]),
items: [],
});
}
const group = groups.get(fieldKey);
if (!group || group.items.some((item) => item.id === subfieldKey)) {
continue;
}
group.items.push({
id: subfieldKey,
name: subfieldName,
description: subfieldDescription,
tooltip: buildTopicLegendTooltip([
{
label: 'Themenbereich',
value: subfieldName,
description: subfieldDescription,
},
{
label: 'Fachgebiet',
value: fieldName,
description: fieldDescription,
},
{
label: 'Wissenschaftsbereich',
value: domainName,
description: domainDescription,
},
]),
});
}
return [...groups.values()]
.map((group) => ({
...group,
items: group.items.sort((left, right) =>
left.name.localeCompare(right.name),
),
}))
.sort((left, right) => left.name.localeCompare(right.name));
}
function buildTopicLegendTooltip(
entries: Array<{ label: string; value: string; description: string }>,
): string {
const lines: string[] = [];
for (const entry of entries) {
if (!entry.value && !entry.description) {
continue;
}
if (lines.length > 0) {
lines.push('');
}
if (entry.value) {
lines.push(`${entry.label}: ${entry.value}`);
}
if (entry.description) {
lines.push('');
lines.push(`Beschreibung: ${entry.description}`);
}
}
return lines.join('\n');
}
function normalizeLegendValue(value: string | null | undefined): string | null {
const normalized = value?.trim();
return normalized || null;
}
function normalizeLegendKey(value: string): string {
return value.trim().toLowerCase();
}