All files / player-ui/src/components chart-list.ts

5.88% Statements 2/34
0% Branches 0/44
0% Functions 0/15
7.69% Lines 2/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151                            48x         48x                                                                                                                                                                                                                                                                      
import {
  html,
  css,
  type CSSResultGroup,
  type PropertyDeclarations,
  type PropertyValues,
  type TemplateResult,
} from 'lit';
import { repeat } from 'lit/directives/repeat.js';
import { browseEntryBadge, browseEntryCount, browseEntryTitle, type LibraryEntry } from '@be-music/player-skin-sdk';
import { BeMusicElement } from '../base.ts';
import { structureStyles } from '../styles/structure.ts';
 
export class BeMusicChartListElement extends BeMusicElement {
  static override properties: PropertyDeclarations = {
    entries: { attribute: false },
    selectedIndex: { type: Number },
    disabled: { type: Boolean },
  };
  static override styles: CSSResultGroup = [
    structureStyles,
    css`
      :host {
        display: flex;
        flex-direction: column;
        min-height: 0;
      }
      ul {
        list-style: none;
        margin: 0;
        padding: 0;
        flex: 1 1 auto;
        min-height: 0;
        max-height: var(--be-music-list-height, 55vh);
        overflow: auto;
        overscroll-behavior: contain;
      }
      li + li {
        margin-top: 2px;
      }
      /* Basecoat's .btn is one line high; a row is two lines, the title over the artist. */
      button.btn {
        display: grid;
        grid-template-columns: var(--be-music-row-kind-width, 3.5ch) minmax(0, 1fr);
        align-items: center;
        justify-content: stretch;
        gap: var(--be-music-gap, 8px);
        width: 100%;
        height: auto;
        min-height: 2.75rem;
        padding: 6px var(--be-music-gap, 8px);
        text-align: left;
        white-space: normal;
      }
      .text {
        display: grid;
        min-width: 0;
      }
      strong {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .text .muted {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .kind {
        font-size: 0.85em;
        font-variant-numeric: tabular-nums;
        text-align: right;
      }
      .empty-list {
        padding: var(--be-music-gap, 8px);
      }
    `,
  ];
  declare entries: readonly LibraryEntry[] | undefined;
  declare selectedIndex: number;
  declare disabled: boolean;
  private restoreListFocus = false;
  protected override willUpdate(changed: PropertyValues): void {
    this.restoreListFocus =
      (changed.has('selectedIndex') || changed.has('entries')) &&
      (this.renderRoot as ShadowRoot).activeElement?.tagName === 'BUTTON';
  }
  protected override updated(changed: PropertyValues): void {
    if (!changed.has('selectedIndex') && !changed.has('entries')) return;
    const current = this.renderRoot.querySelector<HTMLButtonElement>('[aria-current=true]');
    current?.scrollIntoView({ block: 'nearest' });
    if (this.restoreListFocus) current?.focus({ preventScroll: true });
  }
  private select(index: number, activate = false): void {
    this.emit('selection-change', { index });
    if (activate) this.emit('activate-request', { index });
  }
  protected override render(): TemplateResult {
    const entries = this.entries ?? [];
    return html`<ul class="rows" part="list" aria-label="Charts and folders">
        ${repeat(
          entries,
          (e) =>
            e.kind === 'chart'
              ? `chart:${e.chart.id}`
              : e.kind === 'course'
                ? `course:${e.course.id}`
                : `folder:${e.folder.label}`,
          (entry, index) => html`
            <li>
              <button
                class="btn"
                data-variant="ghost"
                part="row"
                aria-current=${String(index === this.selectedIndex)}
                .tabIndex=${index === this.selectedIndex ? 0 : -1}
                ?disabled=${this.disabled}
                @focus=${() => this.emit('selection-change', { index })}
                @click=${() => this.select(index, entry.kind === 'folder')}
                @dblclick=${() => {
                  if (entry.kind !== 'folder') this.select(index, true);
                }}
                @keydown=${(event: KeyboardEvent) => {
                  if (event.key === 'Enter' && !event.isComposing && !event.repeat) {
                    event.preventDefault();
                    event.stopPropagation();
                    this.select(index, true);
                  } else if ((event.key === 'ArrowDown' || event.key === 'ArrowUp') && !event.isComposing) {
                    event.preventDefault();
                    event.stopPropagation();
                    const count = entries.length;
                    if (count) this.select((index + (event.key === 'ArrowDown' ? 1 : -1) + count) % count);
                  }
                }}
              >
                <span class="kind" part="row-kind">${browseEntryBadge(entry)}</span
                ><span class="text"
                  ><strong>${browseEntryTitle(entry)}</strong
                  ><span class="muted"
                    >${entry.kind === 'chart' ? (entry.chart.artist ?? '') : (browseEntryCount(entry) ?? '')}</span
                  ></span
                >
              </button>
            </li>
          `,
        )}
      </ul>
      ${entries.length === 0 ? html`<p class="muted empty-list">No matching charts.</p>` : ''}`;
  }
}