| /** | |
| * @license | |
| * Copyright 2021 Google LLC | |
| * SPDX-License-Identifier: Apache-2.0 | |
| */ | |
| import '../gr-tooltip-content/gr-tooltip-content'; | |
| import '../gr-icon/gr-icon'; | |
| import {css, html, LitElement, TemplateResult} from 'lit'; | |
| import {customElement, property} from 'lit/decorators.js'; | |
| import { | |
| ApprovalInfo, | |
| isDetailedLabelInfo, | |
| isQuickLabelInfo, | |
| LabelInfo, | |
| } from '../../../api/rest-api'; | |
| import { | |
| classForLabelStatus, | |
| getLabelStatus, | |
| hasOnlyAiVotes, | |
| LabelStatus, | |
| valueString, | |
| } from '../../../utils/label-util'; | |
| declare global { | |
| interface HTMLElementTagNameMap { | |
| 'gr-vote-chip': GrVoteChip; | |
| } | |
| } | |
| /** | |
| * @attr {Boolean} circle-shape - element has shape as circle | |
| */ | |
| @customElement('gr-vote-chip') | |
| export class GrVoteChip extends LitElement { | |
| @property({type: Object}) | |
| vote?: ApprovalInfo; | |
| @property({type: Object}) | |
| label?: LabelInfo; | |
| /** Show vote as a stack of same votes. */ | |
| @property({type: Boolean}) | |
| more = false; | |
| /** | |
| * If defined, vote-chip is shown with this value instead of the latest vote. | |
| * This is useful for change log. | |
| */ | |
| @property() | |
| displayValue?: string; | |
| @property({type: Boolean, attribute: 'tooltip-with-who-voted'}) | |
| tooltipWithWhoVoted = false; | |
| @property({type: Boolean, attribute: 'is-ai', reflect: true}) | |
| isAi = false; | |
| /** Include the label abbreviation inside a self-contained vote badge. */ | |
| @property({attribute: 'label-name', reflect: true}) | |
| labelName?: string; | |
| static override get styles() { | |
| return [ | |
| css` | |
| :host([circle-shape]) .vote-chip { | |
| border-radius: 50%; | |
| border: none; | |
| padding: 2px; | |
| width: var(--gr-vote-chip-width, 16px); | |
| } | |
| :host([circle-shape]) .vote-chip gr-icon.ai-icon { | |
| display: none; | |
| } | |
| .vote-chip.max { | |
| background-color: var(--vote-color-approved); | |
| padding: 2px; | |
| } | |
| .more > .vote-chip.max { | |
| padding: 1px; | |
| border: 1px solid var(--vote-outline-recommended); | |
| } | |
| .vote-chip.min { | |
| background-color: var(--vote-color-rejected); | |
| padding: 2px; | |
| } | |
| .more > .vote-chip.min { | |
| padding: 1px; | |
| border: 1px solid var(--vote-outline-disliked); | |
| } | |
| .vote-chip.positive, | |
| .chip-angle.max, | |
| .chip-angle.positive { | |
| background-color: var(--vote-color-recommended); | |
| border: 1px solid var(--vote-outline-recommended); | |
| color: var(--chip-color); | |
| } | |
| .vote-chip.negative, | |
| .chip-angle.min, | |
| .chip-angle.negative { | |
| background-color: var(--vote-color-disliked); | |
| border: 1px solid var(--vote-outline-disliked); | |
| color: var(--chip-color); | |
| } | |
| .vote-chip, | |
| .chip-angle { | |
| display: flex; | |
| width: var(--gr-vote-chip-width, 16px); | |
| height: var(--gr-vote-chip-height, 16px); | |
| font-size: var(--font-size-small); | |
| justify-content: center; | |
| padding: 1px; | |
| border-radius: var(--border-radius); | |
| line-height: var(--gr-vote-chip-width, 16px); | |
| color: var(--vote-text-color); | |
| } | |
| .vote-chip:has(.ai-icon) { | |
| width: auto; | |
| align-items: center; | |
| padding-left: 3px; | |
| padding-right: 2px; | |
| } | |
| .more > .vote-chip:has(.ai-icon) { | |
| padding-left: 2px; | |
| padding-right: 1px; | |
| } | |
| :host([label-name]) .vote-chip { | |
| width: auto; | |
| align-items: center; | |
| min-width: 16px; | |
| padding: 2px 6px; | |
| border-radius: 12px; | |
| white-space: nowrap; | |
| } | |
| .vote-chip gr-icon { | |
| width: 14px; | |
| height: 16px; | |
| font-size: 16px; | |
| } | |
| .vote-chip gr-icon.ai-icon { | |
| display: flex; | |
| align-items: center; | |
| color: inherit; | |
| width: 12px; | |
| height: 12px; | |
| --gr-icon-size: 12px; | |
| font-size: 12px; | |
| margin-left: 1px; | |
| } | |
| .more > .vote-chip { | |
| position: relative; | |
| z-index: 2; | |
| } | |
| .more > .chip-angle { | |
| position: absolute; | |
| top: 2px; | |
| left: 2px; | |
| width: 100%; | |
| height: 100%; | |
| box-sizing: border-box; | |
| z-index: 1; | |
| } | |
| .container { | |
| position: relative; | |
| display: block; | |
| width: fit-content; | |
| } | |
| /* fix for firefox only */ | |
| @supports (-moz-appearance: none) { | |
| .container.more { | |
| display: inline-block; | |
| } | |
| } | |
| `, | |
| ]; | |
| } | |
| override render() { | |
| const renderValue = this.renderValue(); | |
| if (!renderValue) return; | |
| const isAi = this.computeIsAi(); | |
| return html`<gr-tooltip-content | |
| class="container ${this.more ? 'more' : ''}" | |
| title=${this.computeTooltip(renderValue, isAi)} | |
| has-tooltip | |
| > | |
| <div class="vote-chip ${this.computeClass()}"> | |
| ${this.labelName}${renderValue}${isAi | |
| ? html`<gr-icon icon="spark" class="ai-icon"></gr-icon>` | |
| : ''} | |
| </div> | |
| ${this.more | |
| ? html`<div | |
| class="chip-angle ${this.computeClass()}" | |
| aria-hidden="true" | |
| > | |
| ${renderValue} | |
| </div>` | |
| : ''} | |
| </gr-tooltip-content>`; | |
| } | |
| private computeIsAi(): boolean { | |
| if (this.isAi) return true; | |
| return hasOnlyAiVotes(this.label, this.vote); | |
| } | |
| private renderValue() { | |
| if (this.displayValue) { | |
| return this.displayValue; | |
| } | |
| if (!this.label) { | |
| return ''; | |
| } else if (isDetailedLabelInfo(this.label)) { | |
| if (this.vote?.value) { | |
| if (this.labelName) { | |
| const status = getLabelStatus(this.label, this.vote.value); | |
| if (status === LabelStatus.APPROVED) | |
| return this.renderStatusIcon('check'); | |
| if (status === LabelStatus.REJECTED) | |
| return this.renderStatusIcon('close'); | |
| } | |
| return valueString(this.vote.value); | |
| } | |
| } else if (isQuickLabelInfo(this.label)) { | |
| if (this.label.approved) { | |
| return this.labelName ? this.renderStatusIcon('check') : html`✓`; // check mark | |
| } else if (this.label.rejected) { | |
| return this.labelName ? this.renderStatusIcon('close') : html`✗`; // x mark | |
| } else if (this.label.disliked || this.label.recommended) { | |
| return valueString(this.label.value); | |
| } | |
| } | |
| return ''; | |
| } | |
| private renderStatusIcon(icon: 'check' | 'close') { | |
| return html`<gr-icon icon=${icon}></gr-icon>`; | |
| } | |
| private computeClass() { | |
| if (!this.label) { | |
| return ''; | |
| } else if (this.displayValue) { | |
| const status = getLabelStatus(this.label, Number(this.displayValue)); | |
| return classForLabelStatus(status); | |
| } else { | |
| const status = getLabelStatus(this.label, this.vote?.value); | |
| return classForLabelStatus(status); | |
| } | |
| } | |
| private computeTooltip( | |
| renderValue: string | TemplateResult<1>, | |
| isAi: boolean | |
| ) { | |
| if (!this.label || !isDetailedLabelInfo(this.label)) { | |
| return ''; | |
| } | |
| const value = | |
| typeof renderValue === 'string' | |
| ? renderValue | |
| : this.labelName && this.vote | |
| ? valueString(this.vote.value) | |
| : ''; | |
| let voteDescription = this.label.values?.[value] ?? ''; | |
| if (isAi) { | |
| voteDescription = `${voteDescription} (AI vote)`.trim(); | |
| } | |
| if (this.tooltipWithWhoVoted && this.vote) { | |
| return `${this.vote?.name}: ${voteDescription}`; | |
| } else { | |
| return voteDescription; | |
| } | |
| } | |
| } |