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

430 lines
12 KiB
TypeScript

import {
Component,
computed,
inject,
input,
OnInit,
TemplateRef,
viewChild,
} from '@angular/core';
import {
DataTableComponent,
GenericColumn,
} from '../data-table/data-table.component';
import {
DehydratedAuthor,
InseWorksV2ModelWorkPage,
Institution,
Work,
WorkTopic,
} from '../../../api';
import { TopicChipComponent } from '../topic-chip/topic-chip.component';
import { MatIconModule } from '@angular/material/icon';
import { toSignal } from '@angular/core/rxjs-interop';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { ChipComponent } from '../chip/chip.component';
import { WorkTypePipe } from '../../pipes/work-type.pipe';
import { LanguageCodePipe } from '../../pipes/language-code.pipe';
import { MatTooltipModule } from '@angular/material/tooltip';
import { topicHierarchyColor } from '../../../resolvers/utils/topic-colors';
import { MatButtonModule } from '@angular/material/button';
import { workQueryParamKeys } from '../../../resolvers/work-page.resolver';
@Component({
selector: 'app-publication-table',
imports: [
MatIconModule,
MatTooltipModule,
MatButtonModule,
RouterModule,
DataTableComponent,
TopicChipComponent,
ChipComponent,
WorkTypePipe,
LanguageCodePipe,
],
templateUrl: './publication-table.component.html',
styleUrl: './publication-table.component.scss',
})
export class PublicationTableComponent implements OnInit {
page = input.required<InseWorksV2ModelWorkPage>();
authorFilterEnabled = input<boolean>(false);
institutionFilterEnabled = input<boolean>(false);
elements = computed(() => this.page().elements ?? []);
columns: GenericColumn<Work>[] = [];
titleTmp = viewChild.required<TemplateRef<any>>('titleTmp');
typeTmp = viewChild.required<TemplateRef<any>>('typeTmp');
yearTmp = viewChild.required<TemplateRef<any>>('yearTmp');
languageTmp = viewChild.required<TemplateRef<any>>('languageTmp');
authorsTmp = viewChild.required<TemplateRef<any>>('authorsTmp');
institutionsTmp = viewChild.required<TemplateRef<any>>('institutionsTmp');
topicsTmp = viewChild.required<TemplateRef<any>>('topicsTmp');
citationsTmp = viewChild.required<TemplateRef<any>>('citationsTmp');
actionsTmp = viewChild.required<TemplateRef<any>>('actionsTmp');
private readonly activatedRoute = inject(ActivatedRoute);
private readonly router = inject(Router);
queryParamMap = toSignal(this.activatedRoute.queryParamMap, {
initialValue: this.activatedRoute.snapshot.queryParamMap,
});
selectedYearsQueryParams = computed(() => {
return this.queryParamMap().getAll(workQueryParamKeys.years).map(Number);
});
selectedTypesQueryParams = computed(() => {
return this.queryParamMap().getAll(workQueryParamKeys.types);
});
selectedLanguagesQueryParams = computed(() => {
return this.queryParamMap().getAll(workQueryParamKeys.language);
});
selectedAuthorIdsQueryParams = computed(() => {
return this.queryParamMap().getAll(workQueryParamKeys.authorId);
});
selectedInstitutionIdsQueryParams = computed(() => {
return this.queryParamMap().getAll(workQueryParamKeys.institutionId);
});
selectedTopicIdsQueryParams = computed(() => {
return this.queryParamMap().getAll(workQueryParamKeys.topicId);
});
ngOnInit(): void {
this.columns = [
{
id: 'title',
header: 'Titel',
type: 'accessor',
accessorKey: 'title',
cellTemplate: this.titleTmp(),
},
{
id: 'type',
header: 'Typ',
type: 'accessor',
accessorKey: 'type',
cellTemplate: this.typeTmp(),
},
{
id: 'publication_year',
header: 'Jahr',
type: 'accessor',
accessorKey: 'publication_year',
cellTemplate: this.yearTmp(),
},
{
id: 'language',
header: 'Sprache',
type: 'accessor',
accessorKey: 'language',
cellTemplate: this.languageTmp(),
},
{
id: 'authors',
header: 'Autoren',
type: 'display',
cellTemplate: this.authorsTmp(),
},
{
id: 'institutions',
header: 'Institutionen',
type: 'display',
cellTemplate: this.institutionsTmp(),
},
{
id: 'topics',
header: 'Themen',
type: 'display',
cellTemplate: this.topicsTmp(),
},
{
id: 'cited_by_count',
header: 'Zitiert von',
type: 'accessor',
accessorKey: 'cited_by_count',
cellTemplate: this.citationsTmp(),
},
{
id: 'actions',
header: 'Aktionen',
type: 'display',
cellTemplate: this.actionsTmp(),
},
];
}
isTypeActive(type: string): boolean {
return this.selectedTypesQueryParams().includes(type);
}
isAuthorActive(authorId: string): boolean {
return (
!!authorId &&
this.authorFilterEnabled() &&
this.selectedAuthorIdsQueryParams().includes(authorId)
);
}
toggleTypeFilter(type: string): void {
const selectedTopics = this.selectedTypesQueryParams();
if (selectedTopics.includes(type)) {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.types]: selectedTopics.filter((t) => t !== type),
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
} else {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.types]: [...selectedTopics, type],
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
}
typeColor(type: string | null | undefined): string {
const normalized = (type ?? '').toLowerCase();
if (normalized.includes('conference')) return 'success';
if (normalized.includes('book')) return 'help';
return 'info';
}
isYearActive(year: number): boolean {
return this.selectedYearsQueryParams().includes(year);
}
toggleYearFilter(year: number): void {
const selectedYears = this.selectedYearsQueryParams();
if (selectedYears.includes(year)) {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.years]: selectedYears.filter((y) => y !== year),
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
} else {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.years]: [...selectedYears, year],
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
}
isLanguageActive(language: string): boolean {
return this.selectedLanguagesQueryParams().includes(language);
}
toggleLanguageFilter(language: string): void {
const selectedLanguages = this.selectedLanguagesQueryParams();
if (selectedLanguages.includes(language)) {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.language]: selectedLanguages.filter(
(l) => l !== language,
),
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
} else {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.language]: [...selectedLanguages, language],
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
}
expandedWorkInstitutionsId: string[] = [];
expandedWorkAuthorsId: string[] = [];
isWorkInstitutionsExpanded(work: Work): boolean {
if (!work.id) return false;
return this.expandedWorkInstitutionsId.includes(work.id);
}
toggleWorkInstitutionsExpansion(work: Work): void {
if (!work.id) return;
if (this.expandedWorkInstitutionsId.includes(work.id)) {
this.expandedWorkInstitutionsId = this.expandedWorkInstitutionsId.filter(
(id) => id !== work.id,
);
} else {
this.expandedWorkInstitutionsId.push(work.id);
}
}
isWorkAuthorsExpanded(work: Work): boolean {
if (!work.id) return false;
return this.expandedWorkAuthorsId.includes(work.id);
}
toggleWorkAuthorsExpansion(work: Work): void {
if (!work.id) return;
if (this.expandedWorkAuthorsId.includes(work.id)) {
this.expandedWorkAuthorsId = this.expandedWorkAuthorsId.filter(
(id) => id !== work.id,
);
} else {
this.expandedWorkAuthorsId.push(work.id);
}
}
isInstitutionActive(id: string): boolean {
return !!id && this.selectedInstitutionIdsQueryParams().includes(id);
}
filterByInstitution(institution: Institution): void {
if (!institution.id || !this.institutionFilterEnabled()) return;
const selectedInstitutionIds = this.selectedInstitutionIdsQueryParams();
if (selectedInstitutionIds.includes(institution.id)) {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.institutionId]: selectedInstitutionIds.filter(
(id) => id !== institution.id,
),
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
} else {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.institutionId]: [
...selectedInstitutionIds,
institution.id,
],
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
}
filterByAuthor(authorId: string): void {
if (!authorId || !this.authorFilterEnabled()) return;
const selectedAuthorIds = this.selectedAuthorIdsQueryParams();
if (selectedAuthorIds.includes(authorId)) {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.authorId]: selectedAuthorIds.filter(
(id) => id !== authorId,
),
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
} else {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.authorId]: [...selectedAuthorIds, authorId],
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
}
getWorkTopicColor(topic: WorkTopic): 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: WorkTopic): boolean {
return !!topic.id && this.selectedTopicIdsQueryParams().includes(topic.id);
}
filterByTopic(topic: WorkTopic): void {
if (!topic.id) return;
const selectedTopicIds = this.selectedTopicIdsQueryParams();
if (selectedTopicIds.includes(topic.id)) {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.topicId]: selectedTopicIds.filter(
(id) => id !== topic.id,
),
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
} else {
this.router.navigate([], {
queryParams: {
[workQueryParamKeys.topicId]: [...selectedTopicIds, topic.id],
[workQueryParamKeys.page]: 0,
},
queryParamsHandling: 'merge',
});
}
}
extractRawId(id: string | null | undefined): string | null {
if (!id) return null;
return id.split('/').filter(Boolean).pop() ?? null;
}
getWorkInstitutions(work: Work): Institution[] {
const institutions = new Map<string, Institution>();
for (const authorship of work.authorships ?? []) {
if (!authorship.institutions) continue;
for (const institution of authorship.institutions) {
if (!institution.id) continue;
institutions.set(institution.id, institution);
}
}
return Array.from(institutions.values());
}
getWorkAuthors(work: Work): DehydratedAuthor[] {
return (
work.authorships
?.map((a) => a.author)
.filter((a): a is DehydratedAuthor => !!a) || []
);
}
getAuthorTooltipText(author: DehydratedAuthor): string {
if (this.authorFilterEnabled()) {
return `Nach Autor filtern: ${author.display_name}`;
}
return `Autor: ${author.display_name}`;
}
getInstitutionTooltipText(institution: Institution): string {
if (this.institutionFilterEnabled()) {
return `Nach Institution filtern: ${institution.display_name}`;
}
return `Institution: ${institution.display_name}`;
}
}