diff --git a/webapp/src/app/shared/ui/topic-chip/topic-chip.component.ts b/webapp/src/app/shared/ui/topic-chip/topic-chip.component.ts index 9e01588f..079d0990 100644 --- a/webapp/src/app/shared/ui/topic-chip/topic-chip.component.ts +++ b/webapp/src/app/shared/ui/topic-chip/topic-chip.component.ts @@ -1,7 +1,9 @@ -import { Component, EventEmitter, Input, Output } from '@angular/core'; +import { Component, computed, input, output } from '@angular/core'; import { MatIconModule } from '@angular/material/icon'; import { MatTooltipModule } from '@angular/material/tooltip'; -import { buildTopicHierarchyTooltip } from '../../../resolvers/utils/topic-tooltip'; +import { Topic } from '../../../api'; +import { ComponentTooltipDirective } from '../../../directives/component-tooltip.directive'; +import { AppTopicTooltipComponent } from '../app-topic-tooltip/app-topic-tooltip.component'; export const TOPIC_CHIP_PALETTE = [ 'purple', @@ -65,138 +67,136 @@ export const topicChipColorTokens = ( @Component({ selector: 'app-topic-chip', - imports: [MatIconModule, MatTooltipModule], + imports: [MatIconModule, MatTooltipModule, ComponentTooltipDirective], templateUrl: './topic-chip.component.html', styleUrl: './topic-chip.component.scss', standalone: true, host: { - '[class.topic-chip--normal]': 'normalSize', - '[class.topic-chip--mini]': 'miniSize', - '[class.topic-chip--active]': 'activeState', - '[style.--topic-border]': 'topicBorder', - '[style.--topic-bg]': 'topicBackground', - '[style.--topic-active-bg]': 'topicActiveBackground', - '[style.--topic-color]': 'topicColor', - '[style.--topic-text]': 'topicText', - '[style.--topic-count-bg]': 'topicCountBackground', - '[style.--topic-count-text]': 'topicCountText', + '[class.topic-chip--normal]': 'normalSize()', + '[class.topic-chip--mini]': 'miniSize()', + '[class.topic-chip--active]': 'activeState()', + '[style.--topic-border]': 'topicBorder()', + '[style.--topic-bg]': 'topicBackground()', + '[style.--topic-active-bg]': 'topicActiveBackground()', + '[style.--topic-color]': 'topicColor()', + '[style.--topic-text]': 'topicText()', + '[style.--topic-count-bg]': 'topicCountBackground()', + '[style.--topic-count-text]': 'topicCountText()', }, }) export class TopicChipComponent { - @Input() label = ''; - @Input() count: number | string | null | undefined; - @Input() description: string | null | undefined = ''; - @Input() subfieldName: string | null | undefined = ''; - @Input() fieldName: string | null | undefined = ''; - @Input() domainName: string | null | undefined = ''; - @Input() countTooltip: string | null | undefined = ''; - @Input() countIcon: string | null | undefined; - @Input() color: TopicChipColor = 'purple'; - @Input() size: TopicChipSize = 'normal'; - @Input() disabled = false; - @Input() active = false; - @Input() descriptionTooltipTarget: TopicChipTooltipTarget = 'label'; + readonly AppTopicTooltipComponent = AppTopicTooltipComponent; - @Output() chipClick = new EventEmitter(); + label = input(''); + count = input(undefined); + description = input(''); + subfieldName = input(''); + fieldName = input(''); + domainName = input(''); + countTooltip = input(''); + countIcon = input(undefined); + color = input('purple'); + size = input('normal'); + disabled = input(false); + active = input(false); + descriptionTooltipTarget = input('label'); + topicId = input(undefined); + topic = input(undefined); - get normalSize(): boolean { - return this.size === 'normal'; - } + chipClick = output(); - get miniSize(): boolean { - return this.size === 'mini'; - } + normalSize = computed(() => this.size() === 'normal'); + miniSize = computed(() => this.size() === 'mini'); + activeState = computed(() => this.active()); - get activeState(): boolean { - return this.active; - } - - get topicBorder(): string { + topicBorder = computed(() => { return ( - this.colorTokens?.border ?? - `color-mix(in srgb, ${this.topicColor} 72%, var(--sg-color-mix-base))` + this.colorTokens()?.border ?? + `color-mix(in srgb, ${this.topicColor()} 72%, var(--sg-color-mix-base))` ); - } + }); - get topicBackground(): string { + topicBackground = computed(() => { return ( - this.colorTokens?.background ?? - `color-mix(in srgb, ${this.topicColor} 17%, var(--sg-color-mix-base))` + this.colorTokens()?.background ?? + `color-mix(in srgb, ${this.topicColor()} 17%, var(--sg-color-mix-base))` ); - } + }); - get topicActiveBackground(): string { + topicActiveBackground = computed(() => { return ( - this.colorTokens?.activeBackground ?? - `color-mix(in srgb, ${this.topicColor} 27%, var(--sg-color-mix-base))` + this.colorTokens()?.activeBackground ?? + `color-mix(in srgb, ${this.topicColor()} 27%, var(--sg-color-mix-base))` ); - } + }); - get topicColor(): string { - return this.colorTokens?.color ?? this.color; - } + topicColor = computed(() => { + return this.colorTokens()?.color ?? this.color(); + }); - get topicText(): string { - return this.colorTokens?.color - ? this.colorTokens.color - : `color-mix(in srgb, ${this.topicColor} 45%, var(--sg-text))`; - } + topicText = computed(() => { + return this.colorTokens()?.color + ? this.colorTokens()!.color + : `color-mix(in srgb, ${this.topicColor()} 45%, var(--sg-text))`; + }); - get topicCountBackground(): string { - return this.colorTokens?.background ?? 'var(--sg-surface)'; - } + topicCountBackground = computed(() => { + return this.colorTokens()?.background ?? 'var(--sg-surface)'; + }); - get topicCountText(): string { - return this.colorTokens?.color - ? this.colorTokens.color - : `color-mix(in srgb, ${this.topicColor} 48%, var(--sg-text))`; - } + topicCountText = computed(() => { + return this.colorTokens()?.color + ? this.colorTokens()!.color + : `color-mix(in srgb, ${this.topicColor()} 48%, var(--sg-text))`; + }); - get hasCount(): boolean { - return ( - this.count !== undefined && this.count !== null && `${this.count}` !== '' - ); - } + hasCount = computed(() => { + const c = this.count(); + return c !== undefined && c !== null && `${c}` !== ''; + }); - get labelTooltip(): string { - return this.descriptionTooltipTarget === 'label' ? this.topicTooltip : ''; - } + normalizedCountTooltip = computed(() => { + return (this.countTooltip() ?? '').trim(); + }); - get buttonTooltip(): string { - return this.descriptionTooltipTarget === 'button' ? this.topicTooltip : ''; - } + countIconName = computed(() => { + return (this.countIcon() ?? '').trim(); + }); - get normalizedCountTooltip(): string { - return (this.countTooltip ?? '').trim(); - } - - get countIconName(): string { - return (this.countIcon ?? '').trim(); - } + tooltipInputs = computed(() => { + const t = this.topic(); + const id = this.topicId(); + if (t) { + return { topic: t }; + } + if (id) { + return { topicId: id }; + } + const fallbackTopic: Topic = { + display_name: this.label(), + description: this.description() ?? undefined, + subfield: this.subfieldName() + ? { display_name: this.subfieldName() ?? undefined } + : undefined, + field: this.fieldName() + ? { display_name: this.fieldName() ?? undefined } + : undefined, + domain: this.domainName() + ? { display_name: this.domainName() ?? undefined } + : undefined, + }; + return { topic: fallbackTopic }; + }); handleClick(): void { - if (this.disabled) { + if (this.disabled()) { return; } this.chipClick.emit(); } - private get normalizedDescription(): string { - return (this.description ?? '').trim(); - } - - private get topicTooltip(): string { - return buildTopicHierarchyTooltip({ - topicName: this.label, - description: this.description, - subfieldName: this.subfieldName, - fieldName: this.fieldName, - domainName: this.domainName, - }); - } - - private get colorTokens(): TopicChipColorTokens | undefined { - return TOPIC_CHIP_COLORS[this.color as TopicChipPaletteColor]; - } + private colorTokens = computed(() => { + return TOPIC_CHIP_COLORS[this.color() as TopicChipPaletteColor]; + }); }