All files / app-play/src install-button.ts

0% Statements 0/40
0% Branches 0/22
0% Functions 0/9
0% Lines 0/37

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                                                                                                                                                                   
// The header's Install button: Chromium's install prompt where the page has one, Safari's steps otherwise
// (`resolveInstallOffer`). It shows only while the player can be installed here. Once installed, the player asks for
// persistent storage (`requestPersistentStorage`).
import { resolveInstallOffer, type InstallOffer } from './install-offer.ts';
import { bindPlayerHeader, setPlayerHeaderInstallable } from './player-header.ts';
import { requestPersistentStorage } from './storage-persistence.ts';
import { releaseFocusOnClose } from './ui/dialog-focus.ts';
 
/** Chromium's `beforeinstallprompt` event, which the DOM typings leave out. */
interface BeforeInstallPromptEvent extends Event {
  prompt(): Promise<void>;
  readonly userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }>;
}
 
/** An installed window: its own window, or the title bar folded into the header. Browser fullscreen is not one. */
const INSTALLED_QUERY =
  '(display-mode: standalone), (display-mode: window-controls-overlay), (display-mode: minimal-ui)';
 
export function wireInstallButton(): void {
  const dialog = document.querySelector<HTMLDialogElement>('#install-dialog');
  const installed = matchMedia(INSTALLED_QUERY);
  let deferred: BeforeInstallPromptEvent | undefined;
  const offer = (): InstallOffer | undefined =>
    resolveInstallOffer({
      installed: installed.matches || (navigator as Navigator & { standalone?: boolean }).standalone === true,
      hasPrompt: deferred !== undefined,
      userAgent: navigator.userAgent,
      maxTouchPoints: navigator.maxTouchPoints,
    });
  const refresh = (): void => {
    const next = offer();
    setPlayerHeaderInstallable(next === 'prompt' || (next !== undefined && dialog !== null));
  };
 
  addEventListener('beforeinstallprompt', (event) => {
    // Keep the prompt for the button instead of the browser's own mini-infobar.
    event.preventDefault();
    deferred = event as BeforeInstallPromptEvent;
    refresh();
  });
  addEventListener('appinstalled', () => {
    deferred = undefined;
    refresh();
    void requestPersistentStorage(navigator.storage);
  });
  // An installed player keeps its records and offline caches when the browser runs short of space.
  if (installed.matches || (navigator as Navigator & { standalone?: boolean }).standalone === true) {
    void requestPersistentStorage(navigator.storage);
  }
  installed.addEventListener('change', refresh);
 
  if (dialog !== null) {
    dialog.querySelector('[data-install-close]')?.addEventListener('click', () => dialog.close());
    dialog.addEventListener('click', (event) => {
      if (event.target === dialog) dialog.close();
    });
    // Keys stay in the dialog, so the select screen behind it does not take them; closed, they go back to it.
    dialog.addEventListener('keydown', (event) => event.stopPropagation());
    releaseFocusOnClose(dialog);
  }
 
  bindPlayerHeader({
    install: () => {
      const current = offer();
      if (current === 'prompt' && deferred !== undefined) {
        // A prompt shows once; the browser may fire another later, which brings the button back.
        const prompt = deferred;
        deferred = undefined;
        void prompt.prompt().finally(refresh);
        return;
      }
      if ((current === 'ios-safari' || current === 'mac-safari') && dialog !== null) {
        for (const steps of dialog.querySelectorAll<HTMLElement>('[data-install-steps]')) {
          steps.hidden = steps.dataset.installSteps !== current;
        }
        dialog.showModal();
      }
    },
  });
  refresh();
}