All files / embed embed.ts

96.55% Statements 28/29
96.15% Branches 25/26
100% Functions 3/3
100% Lines 23/23

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          5x                                 4x     4x   4x 4x   4x           4x       4x       5x 1x                         5x     5x 5x   4x 4x   3x 3x 3x     3x   3x     5x 1x   4x    
import { DEFAULT_API_ORIGIN } from '../src/shared/public-schedule';
 
import { VaccineScheduleWidget } from './vaccine-schedule';
import type { EmbedMessage } from './protocol';
 
const ELEMENT_NAME = 'cygnus-vaccine-schedule';
 
/**
 * Script-tag variant: a custom element rendered directly into the host's DOM.
 *
 * Trade-off against the iframe variant, stated once here and in the README:
 *
 * - **This one integrates.** It inherits the host's layout, reflows with it, and costs one small
 *   request. What it cannot do is isolate JavaScript — it runs on the host's origin, with access to
 *   the host's DOM, and asks the host to trust this code.
 * - **The iframe isolates.** Separate origin, separate JS context, nothing shared. It pays for that
 *   with a second document and needing postMessage to do what layout would otherwise do for free.
 *
 * Style isolation is identical in both, because both render into a shadow root. The choice is only
 * about script isolation and trust.
 */
class CygnusVaccineScheduleElement extends HTMLElement {
  private widget: VaccineScheduleWidget | null = null;
 
  connectedCallback(): void {
    Iif (this.widget) return;
 
    const apiOrigin = this.getAttribute('api-origin') ?? DEFAULT_API_ORIGIN;
    const limitAttribute = Number.parseInt(this.getAttribute('limit') ?? '', 10);
 
    this.widget = new VaccineScheduleWidget(this, {
      apiOrigin,
      limit: Number.isNaN(limitAttribute) ? undefined : limitAttribute,
      // In-DOM there is no frame boundary to cross, so messages surface as DOM events — the idiom a
      // host page already has handlers for. Same contract, same version, different transport.
      emit: (message: EmbedMessage) => {
        this.dispatchEvent(new CustomEvent(`cygnus:${message.type}`, { detail: message, bubbles: true, composed: true }));
      },
    });
 
    void this.widget.render();
  }
}
 
if (!customElements.get(ELEMENT_NAME)) {
  customElements.define(ELEMENT_NAME, CygnusVaccineScheduleElement);
}
 
/**
 * Convenience mount for hosts that would rather drop in one script tag than write markup:
 *
 *   <script src="https://cygnus.samuelsantana.dev/embed/embed.js" data-target="#vacinas" defer></script>
 *
 * `document.currentScript` is null in a module or when the script is executed asynchronously, so the
 * lookup falls back to finding our own tag by src.
 */
function autoMount(): void {
  const script =
    (document.currentScript as HTMLScriptElement | null) ??
    document.querySelector<HTMLScriptElement>('script[src*="embed.js"][data-target]');
 
  const selector = script?.dataset.target;
  if (!selector) return;
 
  const container = document.querySelector(selector);
  if (!container || container.querySelector(ELEMENT_NAME)) return;
 
  const element = document.createElement(ELEMENT_NAME);
  if (script?.dataset.apiOrigin) element.setAttribute('api-origin', script.dataset.apiOrigin);
  if (script?.dataset.limit) element.setAttribute('limit', script.dataset.limit);
  // Read by the stylesheet alone (`:host([theme])`), so the element needs no code for it, and a host
  // that changes the attribute later restyles the widget without re-rendering it.
  if (script?.dataset.theme) element.setAttribute('theme', script.dataset.theme);
 
  container.appendChild(element);
}
 
if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', autoMount, { once: true });
} else {
  autoMount();
}