All files / player-browser/src/view/shared readme-dialog.ts

32.55% Statements 14/43
22.72% Branches 5/22
23.07% Functions 3/13
36.11% Lines 13/36

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                                                              16x 16x 16x         53x   53x       53x 53x 53x                   3x 3x 3x 3x 3x                                                    
/**
 * The select screen's README dialog, the same for every skin family: `<be-music-readme-dialog>` from
 * `@be-music/player-ui`, laid over the stage in the host's DOM layer. The session decides when it shows and with what
 * texts (`SelectFrame`'s `readText`); the dialog shows the one selected, with a tab for each of the folder's text files
 * when there are several, scrolls it for the keys, and asks to close (its close button, Escape, a click on the backdrop)
 * or for another tab, which the view turns into `readtext` and `readtext-tab` intents.
 */
 
import { defineBeMusicElements, type BeMusicReadmeDialogElement, type BeMusicUIEvent } from '@be-music/player-ui';
import { basecoatTheme } from '@be-music/player-ui/themes/basecoat';
import type { SelectFrame } from '../types.ts';
 
/** The README dialog of one select view. See the module comment. */
export class SelectReadmeDialog {
  private element: BeMusicReadmeDialogElement | undefined;
  private shown: SelectFrame['readText'];
  /** Where the dialog goes (the host's DOM layer), while the view is attached. */
  private parent: (() => HTMLElement) | undefined;
  private requestClose: () => void = () => {};
  private requestTab: (index: number) => void = () => {};
 
  /** Whether the dialog is showing a text. */
  public get open(): boolean {
    return this.shown !== undefined;
  }
 
  /**
   * Lets the dialog show in `parent` (the host's DOM layer, read only once a text shows); `requestClose` runs when the
   * player asks to close it, and `requestTab` when they choose another of its text files.
   */
  public attach(parent: () => HTMLElement, requestClose: () => void, requestTab: (index: number) => void): void {
    this.parent = parent;
    this.requestClose = requestClose;
    this.requestTab = requestTab;
  }
 
  /** Shows the selected text, from its top each time it is opened or switched to; `undefined` closes the dialog. */
  public show(readText: SelectFrame['readText']): void {
    Eif (readText === this.shown) return;
    const element = readText === undefined ? this.element : this.mount();
    Iif (element === undefined) return;
    this.shown = readText;
    element.subheading =
      readText === undefined ? '' : [readText.chart.title, readText.chart.subtitle].filter(Boolean).join(' ');
    element.tabs = readText?.files.map((file) => file.name) ?? [];
    element.selectedTab = readText?.selected ?? 0;
    element.text = readText?.files[readText.selected]?.text;
  }
 
  /** Scrolls the text by `delta` CSS pixels (positive: down), stopping at either end. */
  public scrollBy(delta: number): void {
    this.element?.scrollTextBy(delta);
  }
 
  /** Takes the dialog out of the page (closing it); it shows the session's text again on the next {@link show}. */
  public detach(): void {
    this.parent = undefined;
    this.requestClose = () => {};
    this.requestTab = () => {};
    this.shown = undefined;
    Iif (this.element !== undefined) {
      this.element.text = undefined;
      this.element.remove();
    }
  }
 
  /** The dialog element in the host's DOM layer, made on the first text shown. */
  private mount(): BeMusicReadmeDialogElement | undefined {
    const parent = this.parent?.();
    if (parent === undefined) return undefined;
    let element = this.element;
    if (element === undefined) {
      const registry = parent.ownerDocument.defaultView?.customElements;
      if (registry === undefined) return undefined;
      defineBeMusicElements(registry, { theme: basecoatTheme });
      element = parent.ownerDocument.createElement('be-music-readme-dialog');
      element.addEventListener('readme-close', () => this.requestClose());
      element.addEventListener('readme-tab', (event) =>
        this.requestTab((event as BeMusicUIEvent<'readme-tab'>).detail.index),
      );
      this.element = element;
    }
    if (element.parentElement !== parent) parent.append(element);
    return element;
  }
}