Files
smartguide/webapp/src/app/shared/ui/app-topic-chip/app-topic-chip.component.ts
T

107 lines
3.1 KiB
TypeScript

import {
Component,
computed,
effect,
inject,
input,
output,
signal,
} from '@angular/core';
import { Topic, TopicsV2Service } from '../../../api';
import { MatChipsModule } from '@angular/material/chips';
import { ComponentTooltipDirective } from '../../../directives/component-tooltip.directive';
import { topicHierarchyColor } from '../../../resolvers/utils/topic-colors';
import { MatIconModule } from '@angular/material/icon';
import { AppTopicTooltipComponent } from '../app-topic-tooltip/app-topic-tooltip.component';
import { SkTextDirective } from '../../../custom-directives/sk-text.directive';
@Component({
host: {
'[style.--topic-color]': 'color()',
'[style.--border-width]': 'isSelected() ? "3px" : "1px"',
'[class.is-clickable]': 'isClickable()',
'[class.is-selected]': 'isSelected()',
'[class.is-mini-size]': 'isMiniSize()',
},
selector: 'app-topic-chip-2',
imports: [
MatChipsModule,
ComponentTooltipDirective,
MatIconModule,
SkTextDirective,
],
templateUrl: './app-topic-chip.component.html',
styleUrl: './app-topic-chip.component.scss',
})
export class AppTopicChipComponent {
readonly AppTopicTooltipComponent = AppTopicTooltipComponent;
private readonly topicsService = inject(TopicsV2Service);
topic = input.required<Topic>();
rawOaTopicId = computed(() => {
return this.stripOpenAlexId(this.topic()?.id);
});
topicId = input<string | undefined>(undefined);
_topic = signal<Topic | undefined>(undefined);
size = input<'normal' | 'mini'>('normal');
isMiniSize = computed(() => this.size() === 'mini');
isSelected = input<boolean>(false);
isClickable = input<boolean>(false);
handleChipClick = output<Topic>();
showIcon = input<boolean>(false);
icon = input<string>('topic');
showCount = input<boolean>(false);
count = input<number>(0);
constructor() {
effect(() => {
const topic = this.topic();
const topicId = this.topicId();
if (!topic && !topicId) {
this._topic.set(undefined);
}
if (topic) {
this._topic.set(topic);
} else if (topicId) {
this.topicsService.getTopicById(topicId).subscribe((fetchedTopic) => {
this._topic.set(fetchedTopic);
});
}
});
}
color = computed<string>(() => {
const topic = this.topic();
return topicHierarchyColor({
id: topic.id ?? undefined,
name: topic.display_name ?? undefined,
field_id: topic.field?.id ?? undefined,
field_name: topic.field?.display_name ?? undefined,
domain_id: topic.domain?.id ?? undefined,
domain_name: topic.domain?.display_name ?? undefined,
})
.replace('hsl(', '')
.replace(')', '');
});
_onChipClick(event: MouseEvent | KeyboardEvent) {
event.stopPropagation();
const topic = this.topic();
if (this.isClickable() && this.handleChipClick && topic) {
this.handleChipClick.emit(topic);
}
}
stripOpenAlexId(id: string | null | undefined): string | undefined {
if (!id) {
return undefined;
}
return id.replace('https://openalex.org/', '');
}
}