import { Component, computed, input, OnInit, TemplateRef, viewChild, inject, } from '@angular/core'; import { CurrencyPipe, DatePipe } from '@angular/common'; import { BundProjectPage, Topic } from '../../../api'; import { BundProjectSummary } from '../../../api/model/bundProjectSummary'; import { AppTopicChipComponent } from '../app-topic-chip/app-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 { toSignal } from '@angular/core/rxjs-interop'; import { ActivatedRoute, Router } from '@angular/router'; import { ChipComponent } from '../chip/chip.component'; import { MatTooltip } from '@angular/material/tooltip'; import { topicHierarchyColor } from '../../../resolvers/utils/topic-colors'; import { bundProjectQueryParamKeys } from '../../../resolvers/bund-projects.resolver'; import { SkTextDirective } from '../../../custom-directives/sk-text.directive'; import { ChipOverflowToggleComponent } from '../chip-overflow-toggle/chip-overflow-toggle.component'; import { SearchResultsStateService } from '../../../modules/keywords-search/search-results/search-results-state.service'; import { HighlightedTextComponent, HighlightTerm, } from '../highlighted-text/highlighted-text.component'; import { TableDetailRowComponent } from '../table-detail-row/table-detail-row.component'; import { INSTITUTION_ICON, OPEN_IN_NEW_ICON } from '../icons'; @Component({ selector: 'app-bund-project-table', imports: [ ChipOverflowToggleComponent, DataTableComponent, RouterModule, MatIconModule, MatButtonModule, QueryParamPaginatorComponent, MatTooltip, CurrencyPipe, DatePipe, ChipComponent, AppTopicChipComponent, SkTextDirective, HighlightedTextComponent, TableDetailRowComponent, ], templateUrl: './bund-project-table.component.html', styleUrl: './bund-project-table.component.scss', }) export class BundProjectTableComponent implements OnInit { readonly OPEN_IN_NEW_ICON = OPEN_IN_NEW_ICON; readonly INSTITUTION_ICON = INSTITUTION_ICON; private readonly activatedRoute = inject(ActivatedRoute); private readonly router = inject(Router); private readonly searchResultsStateService = inject( SearchResultsStateService, { optional: true }, ); page = input.required(); authorFilterEnabled = input(false); institutionFilterEnabled = input(false); topicFilterEnabled = input(false); elements = computed(() => this.page().elements ?? []); highlightRowEnabled = computed(() => { return this.elements()[0]?.highlight ? true : false; }); queryParamMap = toSignal(this.activatedRoute.queryParamMap, { initialValue: this.activatedRoute.snapshot.queryParamMap, }); queryParamKeys = bundProjectQueryParamKeys; total = computed(() => this.page().total ?? 0); columns: GenericColumn[] = []; titleTmp = viewChild.required>('titleTmp'); fundingTypeTmp = viewChild.required>('fundingTypeTmp'); institutionsTmp = viewChild.required>('institutionsTmp'); fundingStartTmp = viewChild.required>('fundingStartTmp'); fundingEndTmp = viewChild.required>('fundingEndTmp'); actionsTmp = viewChild.required>('actionsTmp'); fundingAmountTmp = viewChild.required>('fundingAmountTmp'); topicsTmp = viewChild.required>('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.agency); }); selectedTopicIdsQueryParams = computed(() => { return this.queryParamMap().getAll(this.queryParamKeys.topicId); }); selectedTopicMatchesQueryParams = computed(() => { return this.queryParamMap().getAll('topics'); }); selectedFundingTypesQueryParams = computed(() => { return this.queryParamMap().getAll(this.queryParamKeys.funding_type); }); ngOnInit(): void { this.columns = [ { id: 'title', header: 'Titel', type: 'accessor', accessorKey: 'topic' }, { id: 'funding_type', header: 'Förderart', type: 'accessor', accessorKey: 'funding_type', cellTemplate: this.fundingTypeTmp(), }, { id: 'funding_start', header: 'Förderbeginn', type: 'accessor', accessorKey: 'funding_start', cellTemplate: this.fundingStartTmp(), }, { id: 'funding_end', header: 'Förderende', type: 'accessor', accessorKey: 'funding_end', cellTemplate: this.fundingEndTmp(), }, { id: 'institutions', header: 'Institutionen', type: 'display', cellTemplate: this.institutionsTmp(), }, { id: 'grant_amount', header: 'Förderbetrag', type: 'accessor', accessorKey: 'grant_amount', cellTemplate: this.fundingAmountTmp(), }, { id: 'topics', header: 'Themen', type: 'display', cellTemplate: this.topicsTmp(), }, { id: 'actions', header: '', type: 'display', cellTemplate: this.actionsTmp(), }, ]; } getDetailId(funding_code: string): string | null { const encodedId = encodeURIComponent(funding_code); return encodedId; } 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)) ); } isFundingTypeActive(type: string): boolean { return this.selectedFundingTypesQueryParams().includes(type); } getFundingTypeColor(type: string | null | undefined): string { const normalized = (type ?? '').toLowerCase(); if (normalized == 'direkte projektförderung') return 'success'; if (normalized == 'indirekte projektförderung') return 'warning'; if (normalized == 'übrige indirekte projektförderung') return 'help'; return 'info'; } toggleFundingTypeFilter(event: Event, type: string): void { event.stopPropagation(); const selectedTypes = this.selectedFundingTypesQueryParams(); if (selectedTypes.includes(type)) { this.router.navigate([], { queryParams: { [this.queryParamKeys.funding_type]: selectedTypes.filter( (t) => t !== type, ), [this.queryParamKeys.page]: 0, }, queryParamsHandling: 'merge', }); } else { this.router.navigate([], { queryParams: { [this.queryParamKeys.funding_type]: [...selectedTypes, type], [this.queryParamKeys.page]: 0, }, queryParamsHandling: 'merge', }); } } isProjectPersonsExpanded(project: BundProjectSummary): boolean { if (!project.institution?.name) return false; return this.expandedProjectAuthorsId.includes(project.institution.name); } getStartDate(element: BundProjectSummary): string | Date | undefined { return (element as any).funding_start; } getEndDate(element: BundProjectSummary): string | Date | undefined { return (element as any).funding_end; } getFundingAmount(element: BundProjectSummary): number | undefined { return (element as any).grant_amount; } getProjectInstitutions(project: BundProjectSummary): any[] { return project.institution ? [project.institution] : []; } getProjectTopics(project: BundProjectSummary): Topic[] { const topics = (project as any).oa_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, ); } isInstitutionActive( institution_name: string | number | null | undefined, ): boolean { return ( !!institution_name && this.institutionFilterEnabled() && this.selectedInstitutionIdsQueryParams().includes( String(institution_name), ) ); } filterByInstitution(event: Event, institution: any): void { event.stopPropagation(); const institutionName = institution.name ?? institution.recepient; if (!institutionName || !this.institutionFilterEnabled()) return; const selectedInstitutionIds = this.selectedInstitutionIdsQueryParams(); if (selectedInstitutionIds.includes(institutionName)) { this.router.navigate([], { queryParams: { [this.queryParamKeys.agency]: selectedInstitutionIds.filter( (id) => id !== institutionName, ), [this.queryParamKeys.page]: 0, }, queryParamsHandling: 'merge', }); } else { this.router.navigate([], { queryParams: { [this.queryParamKeys.agency]: [ ...selectedInstitutionIds, institutionName, ], [this.queryParamKeys.page]: 0, }, queryParamsHandling: 'merge', }); } } getInstitutionTooltipText(institution: any): string { const name = institution.name ?? institution.recepient ?? 'Unbekannt'; 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.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(); 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: BundProjectSummary): void { if (!project.funding_code) return; if (this.expandedProjectTopicsId.includes(project.funding_code)) { this.expandedProjectTopicsId = this.expandedProjectTopicsId.filter( (id) => id !== project.funding_code, ); } else { this.expandedProjectTopicsId.push(project.funding_code); } } isProjectTopicsExpanded(project: BundProjectSummary): boolean { if (!project.funding_code) return false; return this.expandedProjectTopicsId.includes(project.funding_code); } /** * Extracts the distinct matched terms out of Elasticsearch-style * highlight fragments (terms wrapped in ...). */ extractHighlightTerms(fragments: string[] | null | undefined): string[] { const terms = new Set(); for (const fragment of fragments ?? []) { if (typeof fragment !== 'string') continue; const matches = fragment.matchAll(/(.*?)<\/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, })); } }