Files
smartguide/webapp/src/app/shared/ui/dfg-project-table/dfg-project-table.component.ts
T
2026-07-24 02:02:52 +02:00

364 lines
11 KiB
TypeScript

import {
Component,
computed,
input,
OnInit,
TemplateRef,
viewChild,
inject,
} from '@angular/core';
import { DfgProjectPage, DfgProjectSummary, Topic } from '../../../api';
import { TopicChipComponent } from '../topic-chip/topic-chip.component';
import {
DataTableComponent,
GenericColumn,
} from '../data-table/data-table.component';
import { RouterModule } from '@angular/router';
import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button';
import { QueryParamPaginatorComponent } from '../query-param-paginator/query-param-paginator.component';
import { dfgProjectQueryParamKeys } from '../../../resolvers/dfg-projects.resolver';
import { toSignal } from '@angular/core/rxjs-interop';
import { ActivatedRoute, Router } from '@angular/router';
import { DfgPersonSummary } from '../../../api/model/dfgPersonSummary';
import { MatTooltip } from '@angular/material/tooltip';
import { topicHierarchyColor } from '../../../resolvers/utils/topic-colors';
@Component({
selector: 'app-dfg-project-table',
imports: [
DataTableComponent,
RouterModule,
MatIconModule,
MatButtonModule,
QueryParamPaginatorComponent,
MatTooltip,
TopicChipComponent,
],
templateUrl: './dfg-project-table.component.html',
styleUrl: './dfg-project-table.component.scss',
})
export class DfgProjectTableComponent implements OnInit {
private readonly activatedRoute = inject(ActivatedRoute);
private readonly router = inject(Router);
page = input.required<DfgProjectPage>();
authorFilterEnabled = input<boolean>(false);
institutionFilterEnabled = input<boolean>(false);
topicFilterEnabled = input<boolean>(false);
elements = computed(() => this.page().elements ?? []);
queryParamMap = toSignal(this.activatedRoute.queryParamMap, {
initialValue: this.activatedRoute.snapshot.queryParamMap,
});
queryParamKeys = dfgProjectQueryParamKeys;
total = computed(() => this.page().total ?? 0);
columns: GenericColumn<DfgProjectSummary>[] = [];
titleTmp = viewChild.required<TemplateRef<any>>('titleTmp');
authorsTmp = viewChild.required<TemplateRef<any>>('authorsTmp');
institutionsTmp = viewChild.required<TemplateRef<any>>('institutionsTmp');
fundingStartTmp = viewChild.required<TemplateRef<any>>('fundingStartTmp');
fundingEndTmp = viewChild.required<TemplateRef<any>>('fundingEndTmp');
actionsTmp = viewChild.required<TemplateRef<any>>('actionsTmp');
detailRowTmp = viewChild.required<TemplateRef<any>>('detailRowTmp');
topicsTmp = viewChild.required<TemplateRef<any>>('topicsTmp');
expandedProjectAuthorsId: string[] = [];
expandedProjectInstitutionsId: string[] = [];
expandedProjectTopicsId: string[] = [];
selectedFundingStartYearsQueryParams = computed(() => {
return this.queryParamMap()
.getAll(this.queryParamKeys.funding_start)
.map(Number);
});
selectedFundingEndYearsQueryParams = computed(() => {
return this.queryParamMap()
.getAll(this.queryParamKeys.funding_end)
.map(Number);
});
selectedAuthorIdsQueryParams = computed(() => {
return this.queryParamMap().getAll(this.queryParamKeys.authorId);
});
selectedInstitutionIdsQueryParams = computed(() => {
return this.queryParamMap().getAll(this.queryParamKeys.institutionId);
});
selectedTopicIdsQueryParams = computed(() => {
return this.queryParamMap().getAll(this.queryParamKeys.topicId);
});
ngOnInit(): void {
this.columns = [
{ id: 'title', header: 'Titel', type: 'accessor', accessorKey: 'title' },
{
id: 'funding_start',
header: 'Förderbeginn',
type: 'accessor',
accessorKey: 'funding_start',
},
{
id: 'funding_end',
header: 'Förderende',
type: 'accessor',
accessorKey: 'funding_end',
cellTemplate: this.fundingEndTmp(),
},
{
id: 'names',
header: 'Autoren',
type: 'accessor',
accessorKey: 'names',
cellTemplate: this.authorsTmp(),
},
{
id: 'institutions',
header: 'Institutionen',
type: 'display',
cellTemplate: this.institutionsTmp(),
},
{
id: 'professional_classification',
header: 'Fachliche Zuordnung',
type: 'display',
cellTemplate: this.detailRowTmp(),
},
{
id: 'topics',
header: 'Themen',
type: 'display',
cellTemplate: this.topicsTmp(),
},
{
id: 'actions',
header: '',
type: 'display',
cellTemplate: this.actionsTmp(),
},
];
}
sortNames(names: string[] | undefined): string[] {
return [...(names ?? [])].sort((a, b) => a.localeCompare(b));
}
isAuthorActive(authorId: number): boolean {
return (
!!authorId &&
this.authorFilterEnabled() &&
this.selectedAuthorIdsQueryParams().includes(String(authorId))
);
}
getProjectAuthors(project: DfgProjectSummary): DfgPersonSummary[] {
return project.persons?.filter((a): a is DfgPersonSummary => !!a) || [];
}
toggleProjectAuthorsExpansion(project: DfgProjectSummary): void {
if (!project.id) return;
if (this.expandedProjectAuthorsId.includes(project.id)) {
this.expandedProjectAuthorsId = this.expandedProjectAuthorsId.filter(
(id) => id !== project.id,
);
} else {
this.expandedProjectAuthorsId.push(project.id);
}
}
isProjectPersonsExpanded(project: DfgProjectSummary): boolean {
if (!project.id) return false;
return this.expandedProjectAuthorsId.includes(project.id);
}
getAuthorTooltipText(author: DfgPersonSummary): string {
if (this.authorFilterEnabled() && author.person_id) {
return `Nach Autor filtern: ${author.full_name}`;
}
return `Autor: ${author.full_name}`;
}
filterByAuthor(authorId: number): void {
if (!authorId || !this.authorFilterEnabled()) return;
const selectedAuthorIds = this.selectedAuthorIdsQueryParams();
if (selectedAuthorIds.includes(String(authorId))) {
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.authorId]: selectedAuthorIds.filter(
(id) => id !== String(authorId),
),
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
} else {
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.authorId]: [
...selectedAuthorIds,
String(authorId),
],
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
}
isProjectInstitutionsExpanded(project: DfgProjectSummary): boolean {
if (!project.id) return false;
return this.expandedProjectInstitutionsId.includes(project.id);
}
toggleProjectInstitutionsExpansion(project: DfgProjectSummary): void {
if (!project.id) return;
if (this.expandedProjectInstitutionsId.includes(project.id)) {
this.expandedProjectInstitutionsId =
this.expandedProjectInstitutionsId.filter((id) => id !== project.id);
} else {
this.expandedProjectInstitutionsId.push(project.id);
}
}
getProjectInstitutions(project: DfgProjectSummary): any[] {
const institutions = new Map<string, any>();
for (const institution of project.institutions ?? []) {
const id = this.getProjectInstitutionId(institution);
if (!id) continue;
institutions.set(id, institution);
}
return Array.from(institutions.values()).sort((a, b) =>
this.getProjectInstitutionName(a).localeCompare(
this.getProjectInstitutionName(b),
),
);
}
getProjectInstitutionId(institution: any): string | null {
const id = institution.id;
return id == null ? null : String(id);
}
getProjectInstitutionName(institution: any): string {
return institution.institute_name_de ?? institution.institute_name_en;
}
isInstitutionActive(
institutionId: string | number | null | undefined,
): boolean {
return (
!!institutionId &&
this.institutionFilterEnabled() &&
this.selectedInstitutionIdsQueryParams().includes(String(institutionId))
);
}
filterByInstitution(institution: any): void {
const institutionId = this.getProjectInstitutionId(institution);
if (!institutionId || !this.institutionFilterEnabled()) return;
const selectedInstitutionIds = this.selectedInstitutionIdsQueryParams();
if (selectedInstitutionIds.includes(institutionId)) {
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.institutionId]: selectedInstitutionIds.filter(
(id) => id !== institutionId,
),
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
} else {
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.institutionId]: [
...selectedInstitutionIds,
institutionId,
],
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
}
getInstitutionTooltipText(institution: any): string {
const name = this.getProjectInstitutionName(institution);
if (this.institutionFilterEnabled()) {
return `Nach Institution filtern: ${name}`;
}
return `Institution: ${name}`;
}
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.authorFilterEnabled() &&
this.selectedTopicIdsQueryParams().includes(topic.id)
);
}
filterByTopic(topic: Topic): void {
if (!topic.id || !this.topicFilterEnabled()) return;
const selectedTopicIds = this.selectedTopicIdsQueryParams();
if (selectedTopicIds.includes(topic.id)) {
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.topicId]: selectedTopicIds.filter(
(id) => id !== topic.id,
),
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
} else {
this.router.navigate([], {
queryParams: {
[this.queryParamKeys.topicId]: [...selectedTopicIds, topic.id],
[this.queryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
}
toggleProjectTopicsExpansion(project: DfgProjectSummary): void {
if (!project.id) return;
if (this.expandedProjectTopicsId.includes(project.id)) {
this.expandedProjectTopicsId = this.expandedProjectTopicsId.filter(
(id) => id !== project.id,
);
} else {
this.expandedProjectTopicsId.push(project.id);
}
}
isProjectTopicsExpanded(project: DfgProjectSummary): boolean {
if (!project.id) return false;
return this.expandedProjectTopicsId.includes(project.id);
}
}