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

204 lines
5.1 KiB
TypeScript

import {
Component,
EventEmitter,
HostBinding,
Input,
Output,
} from '@angular/core';
import { MatIconModule } from '@angular/material/icon';
import { MatTooltipModule } from '@angular/material/tooltip';
import { buildTopicHierarchyTooltip } from '../../../resolvers/utils/topic-tooltip';
export const TOPIC_CHIP_PALETTE = [
'purple',
'blue',
'green',
'orange',
'pink',
] as const;
export type TopicChipPaletteColor = (typeof TOPIC_CHIP_PALETTE)[number];
export type TopicChipColor = TopicChipPaletteColor | string;
export type TopicChipSize = 'normal' | 'mini';
export type TopicChipTooltipTarget = 'label' | 'button';
interface TopicChipColorTokens {
border: string;
background: string;
activeBackground: string;
color: string;
}
const TOPIC_CHIP_COLORS: Record<TopicChipPaletteColor, TopicChipColorTokens> = {
purple: {
border: '#d8cafe',
background: '#f6f0ff',
activeBackground: '#d9d1f5',
color: '#7255d9',
},
blue: {
border: '#bdd4ff',
background: '#f0f6ff',
activeBackground: '#bdd5f2',
color: '#0b63ce',
},
green: {
border: '#bdebcf',
background: '#effaf3',
activeBackground: '#c4decf',
color: '#24834f',
},
orange: {
border: '#ffd796',
background: '#fff7e8',
activeBackground: '#f0daba',
color: '#c67500',
},
pink: {
border: '#ffc7d7',
background: '#fff1f5',
activeBackground: '#f2c7d4',
color: '#d12f61',
},
};
export const topicChipColorTokens = (
color: TopicChipPaletteColor,
): Readonly<TopicChipColorTokens> => TOPIC_CHIP_COLORS[color];
@Component({
selector: 'app-topic-chip',
imports: [MatIconModule, MatTooltipModule],
templateUrl: './topic-chip.component.html',
styleUrl: './topic-chip.component.scss',
standalone: true,
})
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';
@Output() chipClick = new EventEmitter<void>();
@HostBinding('class.topic-chip--normal')
get normalSize(): boolean {
return this.size === 'normal';
}
@HostBinding('class.topic-chip--mini')
get miniSize(): boolean {
return this.size === 'mini';
}
@HostBinding('class.topic-chip--active')
get activeState(): boolean {
return this.active;
}
@HostBinding('style.--topic-border')
get topicBorder(): string {
return (
this.colorTokens?.border ??
`color-mix(in srgb, ${this.topicColor} 72%, #ffffff)`
);
}
@HostBinding('style.--topic-bg')
get topicBackground(): string {
return (
this.colorTokens?.background ??
`color-mix(in srgb, ${this.topicColor} 17%, #ffffff)`
);
}
@HostBinding('style.--topic-active-bg')
get topicActiveBackground(): string {
return (
this.colorTokens?.activeBackground ??
`color-mix(in srgb, ${this.topicColor} 27%, #ffffff)`
);
}
@HostBinding('style.--topic-color')
get topicColor(): string {
return this.colorTokens?.color ?? this.color;
}
@HostBinding('style.--topic-text')
get topicText(): string {
return this.colorTokens?.color
? this.colorTokens.color
: `color-mix(in srgb, ${this.topicColor} 38%, #0f172a)`;
}
@HostBinding('style.--topic-count-bg')
get topicCountBackground(): string {
return this.colorTokens?.background ?? '#ffffff';
}
@HostBinding('style.--topic-count-text')
get topicCountText(): string {
return this.colorTokens?.color
? this.colorTokens.color
: `color-mix(in srgb, ${this.topicColor} 48%, #0f172a)`;
}
get hasCount(): boolean {
return (
this.count !== undefined && this.count !== null && `${this.count}` !== ''
);
}
get labelTooltip(): string {
return this.descriptionTooltipTarget === 'label' ? this.topicTooltip : '';
}
get buttonTooltip(): string {
return this.descriptionTooltipTarget === 'button' ? this.topicTooltip : '';
}
get normalizedCountTooltip(): string {
return (this.countTooltip ?? '').trim();
}
get countIconName(): string {
return (this.countIcon ?? '').trim();
}
handleClick(): void {
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];
}
}