Files
smartguide/webapp/src/app/shared/ui/eu-project-table/eu-project-table.component.ts
T

566 lines
17 KiB
TypeScript

import {
Component,
computed,
input,
TemplateRef,
viewChild,
inject,
} from '@angular/core';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { toSignal } from '@angular/core/rxjs-interop';
import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button';
import { MatTooltip } from '@angular/material/tooltip';
import { CurrencyPipe, DatePipe } from '@angular/common';
import { SkTextDirective } from '../../../custom-directives/sk-text.directive';
import {
DataTableComponent,
GenericColumn,
} from '../data-table/data-table.component';
import { QueryParamPaginatorComponent } from '../query-param-paginator/query-param-paginator.component';
import { AppTopicChipComponent } from '../app-topic-chip/app-topic-chip.component';
import { EuCordisProjectSearchPage } from '../../../api/model/euCordisProjectSearchPage';
import { EuCordisProjectSummary } from '../../../api/model/euCordisProjectSummary';
import { EuCordisOrganizationSummary } from '../../../api/model/euCordisOrganizationSummary';
import { Author, Topic } from '../../../api';
import { euProjectQueryParamKeys } from '../../../resolvers/eu-cordis-projects.resolver';
import { topicHierarchyColor } from '../../../resolvers/utils/topic-colors';
import { ChipComponent } from '../chip/chip.component';
import { AppAuthorChipComponent } from '../app-author-chip/app-author-chip.component';
import { ChipOverflowToggleComponent } from '../chip-overflow-toggle/chip-overflow-toggle.component';
import {
HighlightedTextComponent,
HighlightTerm,
} from '../highlighted-text/highlighted-text.component';
import { MatDividerModule } from '@angular/material/divider';
import { SearchResultsStateService } from '../../../modules/keywords-search/search-results/search-results-state.service';
import { TableDetailRowComponent } from '../table-detail-row/table-detail-row.component';
import { APARTMENT_ICON, OPEN_IN_NEW_ICON } from '../icons';
@Component({
selector: 'app-eu-project-table',
imports: [
ChipOverflowToggleComponent,
DataTableComponent,
RouterModule,
MatIconModule,
MatButtonModule,
MatTooltip,
DatePipe,
CurrencyPipe,
QueryParamPaginatorComponent,
AppTopicChipComponent,
ChipComponent,
AppAuthorChipComponent,
SkTextDirective,
MatDividerModule,
HighlightedTextComponent,
TableDetailRowComponent,
],
templateUrl: './eu-project-table.component.html',
styleUrl: './eu-project-table.component.scss',
})
export class EuProjectTableComponent {
readonly APARTMENT_ICON = APARTMENT_ICON;
readonly OPEN_IN_NEW_ICON = OPEN_IN_NEW_ICON;
private readonly activatedRoute = inject(ActivatedRoute);
private readonly router = inject(Router);
private readonly searchResultsStateService = inject(
SearchResultsStateService,
{ optional: true },
);
queryParamKeys = euProjectQueryParamKeys;
page = input<EuCordisProjectSearchPage | undefined>();
authorFilterEnabled = input<boolean>(false);
institutionFilterEnabled = input<boolean>(false);
topicFilterEnabled = input<boolean>(false);
projects = computed<EuCordisProjectSummary[]>(() => {
return this.page()?.elements ?? [];
});
total = computed(() => {
return this.page()?.total ?? 0;
});
highlightRowEnabled = computed(() => {
return this.projects()[0]?.highlight ? true : false;
});
queryParamMap = toSignal(this.activatedRoute.queryParamMap, {
initialValue: this.activatedRoute.snapshot.queryParamMap,
});
selectedAuthorIdsQueryParams = computed(() => {
return this.queryParamMap().getAll(this.queryParamKeys.authorId);
});
selectedOrganizationIdsQueryParams = computed(() => {
return this.queryParamMap().getAll(this.queryParamKeys.institutionId);
});
selectedTopicIdsQueryParams = computed(() => {
return this.queryParamMap().getAll(this.queryParamKeys.topicId);
});
selectedTopicMatchesQueryParams = computed(() => {
return this.queryParamMap().getAll('topics');
});
selectedTypesQueryParams = computed(() => {
return this.queryParamMap().getAll(this.queryParamKeys.projectType);
});
keywordTopicMap = computed(() => {
return (
this.searchResultsStateService?.keywordTopicMap() ??
new Map<string, Topic>()
);
});
projectTypeTmp = viewChild.required<TemplateRef<unknown>>('projectTypeTmp');
fundingStartTmp = viewChild.required<TemplateRef<unknown>>('fundingStartTmp');
fundingEndTmp = viewChild.required<TemplateRef<unknown>>('fundingEndTmp');
fundingAmountTmp =
viewChild.required<TemplateRef<unknown>>('fundingAmountTmp');
authorsTmp = viewChild.required<TemplateRef<unknown>>('authorsTmp');
organizationsTmp =
viewChild.required<TemplateRef<unknown>>('organizationsTmp');
topicsTmp = viewChild.required<TemplateRef<unknown>>('topicsTmp');
actionsTmp = viewChild.required<TemplateRef<unknown>>('actionsTmp');
expandedProjectAuthorsIds: string[] = [];
expandedProjectOrganizationsIds: string[] = [];
expandedProjectTopicsIds: string[] = [];
columns = computed<GenericColumn<EuCordisProjectSummary>[]>(() => [
{
id: 'title',
header: 'Titel',
type: 'accessor',
accessorKey: 'title',
},
{
id: 'projectType',
header: 'Typ',
type: 'accessor',
accessorKey: 'project_type',
cellTemplate: this.projectTypeTmp(),
},
{
id: 'startDate',
header: 'Förderbeginn',
type: 'accessor',
accessorKey: 'start_date',
cellTemplate: this.fundingStartTmp(),
},
{
id: 'endDate',
header: 'Förderende',
type: 'accessor',
accessorKey: 'end_date',
cellTemplate: this.fundingEndTmp(),
},
{
id: 'ecMaxContribution',
header: 'Förderbetrag (EU)',
type: 'accessor',
accessorKey: 'ec_max_contribution',
cellTemplate: this.fundingAmountTmp(),
},
{
id: 'authors',
header: 'Autoren',
type: 'display',
cellTemplate: this.authorsTmp(),
},
{
id: 'organizations',
header: 'Organisationen',
type: 'display',
cellTemplate: this.organizationsTmp(),
},
{
id: 'topics',
header: 'Themen',
type: 'display',
cellTemplate: this.topicsTmp(),
},
{
id: 'actions',
header: '',
type: 'display',
cellTemplate: this.actionsTmp(),
},
]);
getProjectTypeColor(type: string | null | undefined): string {
const normalized = (type ?? '').toLowerCase();
if (normalized == 'fp7') return 'success';
if (normalized == 'h2020') return 'help';
return 'info';
}
isTypeActive(type: string): boolean {
return this.selectedTypesQueryParams().includes(type);
}
toggleTypeFilter(type: string): void {
const selectedTypes = this.selectedTypesQueryParams();
if (selectedTypes.includes(type)) {
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.projectType]: selectedTypes.filter(
(t) => t !== type,
),
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
} else {
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.projectType]: [...selectedTypes, type],
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
}
getProjectTypeLabel(element: EuCordisProjectSummary): string {
const value = (element as any).project_type ?? (element as any).projectType;
switch (value) {
case 'fp7':
return 'FP7';
case 'h2020':
return 'Horizon 2020';
case 'heu':
return 'Horizon Europe';
default:
return value ?? '';
}
}
getStartDate(element: EuCordisProjectSummary): string | Date | undefined {
return (element as any).start_date ?? (element as any).startDate;
}
getEndDate(element: EuCordisProjectSummary): string | Date | undefined {
return (element as any).end_date ?? (element as any).endDate;
}
getFundingAmount(element: EuCordisProjectSummary): number | undefined {
return (
(element as any).ec_max_contribution ?? (element as any).ecMaxContribution
);
}
private getProjectId(project: EuCordisProjectSummary): string {
return String((project as any).id ?? '');
}
// -------------------------
// Authors
// -------------------------
getProjectAuthors(project: EuCordisProjectSummary): Author[] {
const selectedAuthorIds = this.selectedAuthorIdsQueryParams();
const authors = project.authors?.elements ?? [];
return authors.sort((a, b) => {
const aActive = selectedAuthorIds.includes(String(a.id));
const bActive = selectedAuthorIds.includes(String(b.id));
if (aActive && !bActive) return -1;
if (!aActive && bActive) return 1;
return 0;
});
}
getAuthorTrackId(author: any, index: number): string {
return String(
this.getAuthorId(author) ?? this.getAuthorName(author) ?? index,
);
}
getAuthorId(author: any): string | null {
const id = author.id;
return id === null ? null : String(id);
}
getAuthorName(author: any): string {
return (
author?.display_name ??
author?.displayName ??
author?.full_name ??
author?.fullName ??
author?.name ??
'-'
);
}
isAuthorActive(authorId: string | null): boolean {
return (
!!authorId &&
this.authorFilterEnabled() &&
this.selectedAuthorIdsQueryParams().includes(String(authorId))
);
}
getAuthorTooltipText(author: any): string {
const name = this.getAuthorName(author);
const id = this.getAuthorId(author);
if (!id) {
return `Autor: ${name}`;
}
if (this.authorFilterEnabled()) {
return `Nach Autor filtern: ${name}`;
}
return `Autor: ${name}`;
}
filterByAuthor(author: any): void {
const authorId = this.getAuthorId(author);
if (!authorId || !this.authorFilterEnabled()) return;
const selectedAuthorIds = this.selectedAuthorIdsQueryParams();
const nextIds = selectedAuthorIds.includes(authorId)
? selectedAuthorIds.filter((id) => id !== authorId)
: [...selectedAuthorIds, authorId];
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.authorId]: nextIds,
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
isProjectAuthorsExpanded(project: EuCordisProjectSummary): boolean {
const projectId = this.getProjectId(project);
return this.expandedProjectAuthorsIds.includes(projectId);
}
toggleProjectAuthorsExpansion(project: EuCordisProjectSummary): void {
const projectId = this.getProjectId(project);
if (!projectId) return;
this.expandedProjectAuthorsIds = this.expandedProjectAuthorsIds.includes(
projectId,
)
? this.expandedProjectAuthorsIds.filter((id) => id !== projectId)
: [...this.expandedProjectAuthorsIds, projectId];
}
// -------------------------
// Organizations
// -------------------------
getProjectOrganizations(
project: EuCordisProjectSummary,
): EuCordisOrganizationSummary[] {
const organizations =
(project as any).organizations ?? (project as any).organisations ?? [];
if (!Array.isArray(organizations)) {
return [];
}
const selectedOrganizationIds = this.selectedOrganizationIdsQueryParams();
const byId = new Map<string, EuCordisOrganizationSummary>();
for (const organization of organizations) {
const id = this.getOrganizationId(organization);
if (!id) continue;
byId.set(id, organization);
}
return Array.from(byId.values()).sort((a, b) =>
selectedOrganizationIds.includes(this.getOrganizationId(a) ?? '')
? -1
: selectedOrganizationIds.includes(this.getOrganizationId(b) ?? '')
? 1
: 0,
);
}
getOrganizationId(
organization: EuCordisOrganizationSummary | any,
): string | null {
const id = organization?.organisation_id ?? organization?.id;
return id == null ? null : String(id);
}
getOrganizationName(organization: EuCordisOrganizationSummary | any): string {
return (
organization?.name ??
organization?.short_name ??
organization?.organisation_id ??
'-'
);
}
isOrganizationActive(organizationId: string | null): boolean {
return (
!!organizationId &&
this.institutionFilterEnabled() &&
this.selectedOrganizationIdsQueryParams().includes(String(organizationId))
);
}
getOrganizationTooltipText(
organization: EuCordisOrganizationSummary | any,
): string {
const name = this.getOrganizationName(organization);
if (this.institutionFilterEnabled()) {
return `Nach Organisation filtern: ${name}`;
}
return `Organisation: ${name}`;
}
filterByOrganization(organization: EuCordisOrganizationSummary | any): void {
const organizationId = this.getOrganizationId(organization);
if (!organizationId || !this.institutionFilterEnabled()) return;
const selectedOrganizationIds = this.selectedOrganizationIdsQueryParams();
const nextIds = selectedOrganizationIds.includes(organizationId)
? selectedOrganizationIds.filter((id) => id !== organizationId)
: [...selectedOrganizationIds, organizationId];
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.institutionId]: nextIds,
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
isProjectOrganizationsExpanded(project: EuCordisProjectSummary): boolean {
const projectId = this.getProjectId(project);
return this.expandedProjectOrganizationsIds.includes(projectId);
}
toggleProjectOrganizationsExpansion(project: EuCordisProjectSummary): void {
const projectId = this.getProjectId(project);
if (!projectId) return;
this.expandedProjectOrganizationsIds =
this.expandedProjectOrganizationsIds.includes(projectId)
? this.expandedProjectOrganizationsIds.filter((id) => id !== projectId)
: [...this.expandedProjectOrganizationsIds, projectId];
}
// -------------------------
// Topics
// -------------------------
getProjectTopics(project: EuCordisProjectSummary): Topic[] {
const topics = (project as any).topics ?? [];
const activeTopics = topics.filter((topic: Topic) =>
this.isTopicActive(topic),
);
return topics.sort((a: Topic, b: Topic) =>
activeTopics.includes(a) && !activeTopics.includes(b)
? -1
: !activeTopics.includes(a) && activeTopics.includes(b)
? 1
: 0,
);
}
getWorkTopicColor(topic: Topic): string {
return topicHierarchyColor({
id: topic.id,
name: topic.display_name,
field_id: topic.field?.id,
field_name: topic.field?.display_name,
domain_id: topic.domain?.id,
domain_name: topic.domain?.display_name,
});
}
isTopicActive(topic: Topic): boolean {
return (
!!topic.id &&
this.topicFilterEnabled() &&
(this.selectedTopicIdsQueryParams().includes(topic.id) ||
this.selectedTopicMatchesQueryParams().includes(topic.id))
);
}
filterByTopic(topic: Topic): void {
if (!topic.id || !this.topicFilterEnabled()) return;
const selectedTopicIds = this.selectedTopicIdsQueryParams();
const nextIds = selectedTopicIds.includes(topic.id)
? selectedTopicIds.filter((id) => id !== topic.id)
: [...selectedTopicIds, topic.id];
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.topicId]: nextIds,
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
isProjectTopicsExpanded(project: EuCordisProjectSummary): boolean {
const projectId = this.getProjectId(project);
return this.expandedProjectTopicsIds.includes(projectId);
}
toggleProjectTopicsExpansion(project: EuCordisProjectSummary): void {
const projectId = this.getProjectId(project);
if (!projectId) return;
this.expandedProjectTopicsIds = this.expandedProjectTopicsIds.includes(
projectId,
)
? this.expandedProjectTopicsIds.filter((id) => id !== projectId)
: [...this.expandedProjectTopicsIds, projectId];
}
// Highlighted Text
/**
* Extracts the distinct matched terms out of Elasticsearch-style
* highlight fragments (terms wrapped in <em>...</em>).
*/
extractHighlightTerms(fragments: string[] | null | undefined): string[] {
const terms = new Set<string>();
for (const fragment of fragments ?? []) {
if (typeof fragment !== 'string') continue;
const matches = fragment.matchAll(/<em>(.*?)<\/em>/gi);
for (const match of matches) {
const term = match[1]?.trim();
if (term) {
terms.add(term);
}
}
}
return Array.from(terms);
}
getHighlightTerms(terms: string[] | null | undefined): HighlightTerm[] {
return (terms ?? []).map((term) => ({
term,
topic: this.searchResultsStateService?.resolveTopicForTerm(term) ?? null,
}));
}
}