157 lines
3.9 KiB
TypeScript
157 lines
3.9 KiB
TypeScript
import { Component, Input, Output, EventEmitter, OnInit, signal } from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import { MatCheckboxModule } from '@angular/material/checkbox';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
|
import { TopicsService } from '../../api/api/topics.service';
|
|
|
|
export interface TopicItem {
|
|
id: string;
|
|
name: string;
|
|
}
|
|
|
|
export interface SubfieldNode {
|
|
name: string;
|
|
topics: TopicItem[];
|
|
}
|
|
|
|
export interface FieldNode {
|
|
name: string;
|
|
subfields: SubfieldNode[];
|
|
}
|
|
|
|
export interface DomainNode {
|
|
name: string;
|
|
fields: FieldNode[];
|
|
}
|
|
|
|
interface FlatTopic {
|
|
id: string;
|
|
name: string;
|
|
path: string;
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-topic-hierarchy',
|
|
standalone: true,
|
|
imports: [
|
|
CommonModule,
|
|
MatCheckboxModule,
|
|
MatIconModule,
|
|
MatProgressSpinnerModule
|
|
],
|
|
templateUrl: './topic-hierarchy.component.html',
|
|
styleUrl: './topic-hierarchy.component.scss'
|
|
})
|
|
export class TopicHierarchyComponent implements OnInit {
|
|
@Input() selectedTopics: Set<string> = new Set<string>();
|
|
|
|
private _searchQuery = '';
|
|
@Input()
|
|
set searchQuery(value: string) {
|
|
if (value.length < 2) {
|
|
this._searchQuery = '';
|
|
} else {
|
|
this._searchQuery = value;
|
|
}
|
|
this.filterFlatTopics();
|
|
}
|
|
get searchQuery(): string {
|
|
return this._searchQuery;
|
|
}
|
|
|
|
@Output() selectionChange = new EventEmitter<Set<string>>();
|
|
|
|
domains: DomainNode[] = [];
|
|
flatTopics: FlatTopic[] = [];
|
|
filteredFlatTopics: FlatTopic[] = [];
|
|
loading = signal(true);
|
|
|
|
// Set of names of expanded nodes (lazy content rendering)
|
|
expandedDomains = new Set<string>();
|
|
expandedFields = new Set<string>();
|
|
expandedSubfields = new Set<string>();
|
|
|
|
constructor(private topicsService: TopicsService) { }
|
|
|
|
ngOnInit(): void {
|
|
this.topicsService.getTopicHierarchy().subscribe({
|
|
next: (res: any) => {
|
|
this.domains = res.domains || [];
|
|
this.flattenTopics(this.domains);
|
|
this.loading.set(false);
|
|
},
|
|
error: (err) => {
|
|
console.error('Error fetching topic hierarchy:', err);
|
|
this.loading.set(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
flattenTopics(domains: DomainNode[]): void {
|
|
const flat: FlatTopic[] = [];
|
|
domains.forEach((domain) => {
|
|
domain.fields?.forEach((field) => {
|
|
field.subfields?.forEach((subfield) => {
|
|
subfield.topics?.forEach((topic) => {
|
|
if (topic.name && topic.id) {
|
|
flat.push({
|
|
id: topic.id,
|
|
name: topic.name,
|
|
path: `${domain.name} > ${field.name} > ${subfield.name}`
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
this.flatTopics = flat;
|
|
this.filterFlatTopics();
|
|
}
|
|
|
|
filterFlatTopics(): void {
|
|
if (!this.searchQuery) {
|
|
this.filteredFlatTopics = [];
|
|
return;
|
|
}
|
|
const query = this.searchQuery.toLowerCase();
|
|
this.filteredFlatTopics = this.flatTopics.filter((t) =>
|
|
t.name.toLowerCase().includes(query) || t.path.toLowerCase().includes(query)
|
|
);
|
|
}
|
|
|
|
toggleTopic(name: string): void {
|
|
const nextSelection = new Set(this.selectedTopics);
|
|
if (nextSelection.has(name)) {
|
|
nextSelection.delete(name);
|
|
} else {
|
|
nextSelection.add(name);
|
|
}
|
|
this.selectionChange.emit(nextSelection);
|
|
}
|
|
|
|
toggleDomain(name: string): void {
|
|
if (this.expandedDomains.has(name)) {
|
|
this.expandedDomains.delete(name);
|
|
} else {
|
|
this.expandedDomains.add(name);
|
|
}
|
|
}
|
|
|
|
toggleField(name: string): void {
|
|
if (this.expandedFields.has(name)) {
|
|
this.expandedFields.delete(name);
|
|
} else {
|
|
this.expandedFields.add(name);
|
|
}
|
|
}
|
|
|
|
toggleSubfield(name: string): void {
|
|
if (this.expandedSubfields.has(name)) {
|
|
this.expandedSubfields.delete(name);
|
|
} else {
|
|
this.expandedSubfields.add(name);
|
|
}
|
|
}
|
|
}
|