395 lines
12 KiB
TypeScript
395 lines
12 KiB
TypeScript
import { Component, DestroyRef, inject } from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatCardModule } from '@angular/material/card';
|
|
import { MatChipListboxChange, MatChipsModule } from '@angular/material/chips';
|
|
import { MatDividerModule } from '@angular/material/divider';
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
import { PatentsService } from '../../api/api/patents.service';
|
|
import { PatentEntry } from '../../api/model/patentEntry';
|
|
import { IpcClassification } from '../../api';
|
|
import { OpenAlexTopicTag } from '../../api/model/openAlexTopicTag';
|
|
import { TopicChipComponent } from '../../shared/ui/topic-chip/topic-chip.component';
|
|
import { YearRangeSliderComponent } from '../../shared/ui/year-range-slider/year-range-slider.component';
|
|
import {
|
|
classificationSectionColor,
|
|
topicHierarchyColor,
|
|
} from '../../resolvers/utils/topic-colors';
|
|
import {
|
|
CardSearchComponent,
|
|
Page,
|
|
} from '../../shared/ui/card-search/card-search.component';
|
|
import { FilterCardComponent } from '../../shared/ui/filter-card/filter-card.component';
|
|
import { HELP_ICON, PATENT_ICON } from '../../shared/ui/icons';
|
|
import { PageContainerComponent } from '../../shared/ui/page-container/page-container.component';
|
|
|
|
type PatentSource = 'EPO' | 'USPTO';
|
|
|
|
type ClassificationSystem = 'IPC' | 'CPC';
|
|
|
|
interface ClassificationCard {
|
|
key: string;
|
|
symbols: string[];
|
|
system: ClassificationSystem;
|
|
levelLabel: string;
|
|
primaryDescription: string | null;
|
|
hierarchy: { label: string; value: string }[];
|
|
sectionName: string | null;
|
|
}
|
|
|
|
const DESCRIPTION_PREVIEW_LENGTH = 140;
|
|
const MIN_YEAR = 1978;
|
|
|
|
@Component({
|
|
selector: 'app-patents',
|
|
imports: [
|
|
CommonModule,
|
|
RouterModule,
|
|
MatIconModule,
|
|
MatButtonModule,
|
|
MatCardModule,
|
|
MatChipsModule,
|
|
MatDividerModule,
|
|
TopicChipComponent,
|
|
YearRangeSliderComponent,
|
|
CardSearchComponent,
|
|
FilterCardComponent,
|
|
PageContainerComponent,
|
|
],
|
|
templateUrl: './patents.component.html',
|
|
styleUrl: './patents.component.scss',
|
|
})
|
|
export class PatentsComponent {
|
|
readonly PATENT_ICON = PATENT_ICON;
|
|
readonly HELP_ICON = HELP_ICON;
|
|
|
|
private router = inject(Router);
|
|
private route = inject(ActivatedRoute);
|
|
private patentsApi = inject(PatentsService);
|
|
private destroyRef = inject(DestroyRef);
|
|
|
|
patents: PatentEntry[] = [];
|
|
total = 0;
|
|
pageIndex = 0;
|
|
pageSize = 10;
|
|
|
|
loading = true;
|
|
error = false;
|
|
|
|
selectedSources: PatentSource[] = ['EPO', 'USPTO'];
|
|
sortDirection: string = 'asc';
|
|
|
|
// Jahr-Slider
|
|
readonly minYear = MIN_YEAR;
|
|
readonly maxYear = new Date().getFullYear();
|
|
selectedStartYear = MIN_YEAR;
|
|
selectedEndYear = new Date().getFullYear();
|
|
|
|
showDetail = false;
|
|
selectedPatent: PatentEntry | null = null;
|
|
readonly expandedClassificationKeys = new Set<string>();
|
|
|
|
constructor() {
|
|
this.route.queryParams
|
|
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
.subscribe((qp) => {
|
|
this.pageIndex = parseInt(qp['page'] ?? '0', 10) || 0;
|
|
this.pageSize = parseInt(qp['size'] ?? '10', 10) || 10;
|
|
const sourcesParam = qp['sources'];
|
|
this.selectedSources = ((sourcesParam ?? 'EPO,USPTO') as string)
|
|
.split(',')
|
|
.filter((s): s is PatentSource => s === 'EPO' || s === 'USPTO');
|
|
const d = qp['sort_direction'];
|
|
this.sortDirection = d === 'asc' || d === 'desc' ? d : 'asc';
|
|
const sy = parseInt(qp['year_from'] ?? '', 10);
|
|
const ey = parseInt(qp['year_to'] ?? '', 10);
|
|
this.selectedStartYear = isNaN(sy) ? this.minYear : sy;
|
|
this.selectedEndYear = isNaN(ey) ? this.maxYear : ey;
|
|
|
|
this.loadPatents(qp['search'] ?? '');
|
|
});
|
|
}
|
|
|
|
cardSearchPage(): Page {
|
|
return {
|
|
page: this.pageIndex,
|
|
size: this.pageSize,
|
|
total: this.total,
|
|
elements: this.patents,
|
|
};
|
|
}
|
|
|
|
onYearRangeChange(): void {
|
|
this.updateQueryParams();
|
|
}
|
|
|
|
private loadPatents(searchTerm: string): void {
|
|
if (this.selectedSources.length === 0) {
|
|
this.patents = [];
|
|
this.total = 0;
|
|
this.loading = false;
|
|
this.error = false;
|
|
return;
|
|
}
|
|
|
|
this.loading = true;
|
|
this.error = false;
|
|
const source =
|
|
this.selectedSources.length === 1 ? this.selectedSources[0] : undefined;
|
|
|
|
this.patentsApi
|
|
.getAllPatents(
|
|
this.pageIndex,
|
|
this.pageSize,
|
|
searchTerm || undefined,
|
|
source,
|
|
'date',
|
|
this.sortDirection,
|
|
this.selectedStartYear,
|
|
this.selectedEndYear,
|
|
)
|
|
.subscribe({
|
|
next: (res) => {
|
|
this.patents = res.patents;
|
|
this.total = res.total;
|
|
this.loading = false;
|
|
},
|
|
error: () => {
|
|
this.error = true;
|
|
this.loading = false;
|
|
},
|
|
});
|
|
}
|
|
|
|
handleSourceChipChange(change: MatChipListboxChange): void {
|
|
const selected = change.value as PatentSource[];
|
|
// Deselecting every chip resets back to "all sources", mirroring the
|
|
// project-type filter's behavior instead of showing an empty state.
|
|
this.selectedSources = selected.length ? selected : ['EPO', 'USPTO'];
|
|
this.updateQueryParams();
|
|
}
|
|
|
|
setSortDirection(direction: string): void {
|
|
this.sortDirection = direction;
|
|
this.updateQueryParams();
|
|
}
|
|
|
|
openPatent(patent: PatentEntry): void {
|
|
this.router.navigate(['/patent', patent.patent_id]);
|
|
}
|
|
|
|
selectPatent(patent: PatentEntry): void {
|
|
this.selectedPatent = patent;
|
|
this.showDetail = true;
|
|
this.expandedClassificationKeys.clear();
|
|
}
|
|
|
|
closeDetail(): void {
|
|
this.showDetail = false;
|
|
this.selectedPatent = null;
|
|
this.expandedClassificationKeys.clear();
|
|
}
|
|
|
|
private updateQueryParams(): void {
|
|
this.router.navigate([], {
|
|
relativeTo: this.route,
|
|
queryParams: {
|
|
page: 0,
|
|
size: this.pageSize,
|
|
sources: this.selectedSources.length
|
|
? this.selectedSources.join(',')
|
|
: null,
|
|
sort_direction: this.sortDirection,
|
|
year_from:
|
|
this.selectedStartYear !== this.minYear
|
|
? this.selectedStartYear
|
|
: null,
|
|
year_to:
|
|
this.selectedEndYear !== this.maxYear ? this.selectedEndYear : null,
|
|
},
|
|
queryParamsHandling: 'merge',
|
|
replaceUrl: true,
|
|
});
|
|
}
|
|
|
|
formatDate(date: string | null): string {
|
|
if (!date) return '-';
|
|
const d = date.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3');
|
|
const parsed = new Date(d);
|
|
if (isNaN(parsed.getTime())) return date;
|
|
return new Intl.DateTimeFormat('de-DE').format(parsed);
|
|
}
|
|
|
|
getPublicationStageLabel(patent: PatentEntry): string | null {
|
|
return patent.kind ?? patent.application_type ?? null;
|
|
}
|
|
|
|
getPatentMetadataRows(patent: PatentEntry) {
|
|
const rows: { label: string; value: string; variant?: 'source' }[] = [];
|
|
rows.push({ label: 'Quelle', value: patent.source, variant: 'source' });
|
|
rows.push({ label: 'Patent-Nr.', value: patent.patent_id });
|
|
if (patent.publication_date)
|
|
rows.push({
|
|
label: 'Veröffentlichungsdatum',
|
|
value: this.formatDate(patent.publication_date),
|
|
});
|
|
if (patent.application_number)
|
|
rows.push({
|
|
label: 'Anmeldungsnummer',
|
|
value: patent.application_number,
|
|
});
|
|
if (patent.application_date)
|
|
rows.push({
|
|
label: 'Anmeldedatum',
|
|
value: this.formatDate(patent.application_date),
|
|
});
|
|
const stage = this.getPublicationStageLabel(patent);
|
|
if (stage)
|
|
rows.push({
|
|
label: patent.kind ? 'Publikationsstufe' : 'Anmeldungstyp',
|
|
value: this.formatMetadataLabel(stage),
|
|
});
|
|
if (patent.number_of_claims != null)
|
|
rows.push({ label: 'Ansprüche', value: String(patent.number_of_claims) });
|
|
if (patent.status)
|
|
rows.push({
|
|
label: 'Status',
|
|
value: this.formatMetadataLabel(patent.status),
|
|
});
|
|
return rows;
|
|
}
|
|
|
|
private formatMetadataLabel(value: string): string {
|
|
return value
|
|
.replace(/_/g, ' ')
|
|
.toLowerCase()
|
|
.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
}
|
|
|
|
getClassificationCards(
|
|
entries: IpcClassification[] | undefined,
|
|
system: ClassificationSystem,
|
|
): ClassificationCard[] {
|
|
if (!entries?.length) return [];
|
|
const merged = new Map<string, ClassificationCard>();
|
|
for (const entry of entries) {
|
|
const card = this.buildClassificationCard(entry, system);
|
|
const mergeKey =
|
|
card.primaryDescription?.toLocaleLowerCase('en') ??
|
|
card.symbols[0] ??
|
|
card.key;
|
|
const existing = merged.get(mergeKey);
|
|
if (existing) {
|
|
for (const s of card.symbols) {
|
|
if (!existing.symbols.includes(s)) existing.symbols.push(s);
|
|
}
|
|
continue;
|
|
}
|
|
merged.set(mergeKey, card);
|
|
}
|
|
return [...merged.values()];
|
|
}
|
|
|
|
toggleClassificationExpanded(key: string): void {
|
|
this.expandedClassificationKeys.has(key)
|
|
? this.expandedClassificationKeys.delete(key)
|
|
: this.expandedClassificationKeys.add(key);
|
|
}
|
|
|
|
isClassificationExpanded(key: string): boolean {
|
|
return this.expandedClassificationKeys.has(key);
|
|
}
|
|
|
|
isLongDescription(text: string | null): boolean {
|
|
return !!text && text.length > DESCRIPTION_PREVIEW_LENGTH;
|
|
}
|
|
|
|
getDescriptionPreview(text: string | null): string {
|
|
if (!text) return '';
|
|
return text.length <= DESCRIPTION_PREVIEW_LENGTH
|
|
? text
|
|
: `${text.slice(0, DESCRIPTION_PREVIEW_LENGTH).trimEnd()}…`;
|
|
}
|
|
|
|
private buildClassificationCard(
|
|
entry: IpcClassification,
|
|
system: ClassificationSystem,
|
|
): ClassificationCard {
|
|
const level = this.getClassificationLevel(entry);
|
|
const primaryDescription = this.extractDescriptionText(entry[level.field]);
|
|
return {
|
|
key: `${system}-${entry.symbol}`,
|
|
symbols: [entry.symbol],
|
|
system,
|
|
levelLabel: level.label,
|
|
primaryDescription,
|
|
hierarchy: this.buildHierarchyRows(entry, primaryDescription),
|
|
sectionName: entry.section ?? null,
|
|
};
|
|
}
|
|
|
|
topicColor(topic: OpenAlexTopicTag): string {
|
|
return topicHierarchyColor({
|
|
id: topic.topic_id,
|
|
name: topic.topic_display_name,
|
|
field_name: topic.field_name,
|
|
domain_name: topic.domain_name,
|
|
});
|
|
}
|
|
|
|
sectionColor(card: ClassificationCard): string {
|
|
return classificationSectionColor(card.sectionName ?? card.symbols[0]);
|
|
}
|
|
|
|
private getClassificationLevel(entry: IpcClassification): {
|
|
label: string;
|
|
field: keyof IpcClassification;
|
|
} {
|
|
if (entry.subgroup) return { label: 'Untergruppe', field: 'subgroup' };
|
|
if (entry.main_group) return { label: 'Hauptgruppe', field: 'main_group' };
|
|
if (entry.subclass) return { label: 'Unterklasse', field: 'subclass' };
|
|
if (entry.section) return { label: 'Sektion', field: 'section' };
|
|
return { label: 'Code', field: 'symbol' };
|
|
}
|
|
|
|
private buildHierarchyRows(
|
|
entry: IpcClassification,
|
|
primaryDescription: string | null,
|
|
) {
|
|
const rows: { label: string; value: string }[] = [];
|
|
const seen = new Set<string>();
|
|
const addRow = (label: string, raw: string | null) => {
|
|
const value = this.extractDescriptionText(raw);
|
|
if (!value) return;
|
|
const norm = value.toLocaleLowerCase('en');
|
|
if (
|
|
seen.has(norm) ||
|
|
norm === primaryDescription?.toLocaleLowerCase('en')
|
|
)
|
|
return;
|
|
seen.add(norm);
|
|
rows.push({ label, value });
|
|
};
|
|
addRow('Sektion', entry.section ?? null);
|
|
addRow('Klasse', entry.class ?? null);
|
|
addRow('Unterklasse', entry.subclass ?? null);
|
|
return rows;
|
|
}
|
|
|
|
private extractDescriptionText(
|
|
value: string | null | undefined,
|
|
): string | null {
|
|
if (!value?.trim()) return null;
|
|
const text = value.includes(' - ')
|
|
? value.split(' - ').slice(1).join(' - ').trim()
|
|
: value.trim();
|
|
if (!text) return null;
|
|
const first = text.split(/[;|]/)[0]?.trim() ?? text;
|
|
return first === first.toUpperCase()
|
|
? first.charAt(0) + first.slice(1).toLowerCase()
|
|
: first;
|
|
}
|
|
}
|