import { MediaStateReceiverAttributes } from './constants.js'; import MediaController from './media-controller.js'; import { getOrInsertCSSRule, namedNodeMapToObject } from './utils/element-utils.js'; import { globalThis } from './utils/server-safe-globals.js'; // Todo: Use data locals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString function getTemplateHTML(_attrs: Record, _props: Record = {}) { return /*html*/ ` ${this.getSlotTemplateHTML(_attrs, _props)} `; } function getSlotTemplateHTML(_attrs: Record, _props: Record) { return /*html*/ ` `; } /** * @slot - Default slotted elements. * * @attr {string} mediacontroller - The element `id` of the media controller to connect to (if not nested within). * * @cssproperty --media-primary-color - Default color of text. * @cssproperty --media-secondary-color - Default color of background. * @cssproperty --media-text-color - `color` of text. * * @cssproperty --media-control-display - `display` property of control. * @cssproperty --media-control-background - `background` of control. * @cssproperty --media-control-padding - `padding` of control. * @cssproperty --media-control-height - `line-height` of control. * * @cssproperty --media-font - `font` shorthand property. * @cssproperty --media-font-weight - `font-weight` property. * @cssproperty --media-font-family - `font-family` property. * @cssproperty --media-font-size - `font-size` property. * @cssproperty --media-text-content-height - `line-height` of text. * @cssproperty --media-text-background - `background` of text display. */ class MediaTextDisplay extends globalThis.HTMLElement { static shadowRootOptions = { mode: 'open' as ShadowRootMode }; static getTemplateHTML = getTemplateHTML; static getSlotTemplateHTML = getSlotTemplateHTML; #mediaController: MediaController | null; static get observedAttributes(): string[] { return [MediaStateReceiverAttributes.MEDIA_CONTROLLER]; } constructor() { super(); if (!this.shadowRoot) { // Set up the Shadow DOM if not using Declarative Shadow DOM. this.attachShadow((this.constructor as typeof MediaTextDisplay).shadowRootOptions); const attrs = namedNodeMapToObject(this.attributes); this.shadowRoot.innerHTML = (this.constructor as typeof MediaTextDisplay).getTemplateHTML(attrs); } } attributeChangedCallback( attrName: string, oldValue: string | null, newValue: string | null ): void { if (attrName === MediaStateReceiverAttributes.MEDIA_CONTROLLER) { if (oldValue) { this.#mediaController?.unassociateElement?.(this); this.#mediaController = null; } if (newValue && this.isConnected) { // @ts-ignore this.#mediaController = this.getRootNode()?.getElementById(newValue); this.#mediaController?.associateElement?.(this); } } } connectedCallback(): void { const { style } = getOrInsertCSSRule(this.shadowRoot, ':host'); style.setProperty( 'display', `var(--media-control-display, var(--${this.localName}-display, inline-flex))` ); const mediaControllerId = this.getAttribute( MediaStateReceiverAttributes.MEDIA_CONTROLLER ); if (mediaControllerId) { // @ts-ignore this.#mediaController = (this.getRootNode() as Document)?.getElementById( mediaControllerId ); this.#mediaController?.associateElement?.(this); } } disconnectedCallback(): void { // Use cached mediaController, getRootNode() doesn't work if disconnected. this.#mediaController?.unassociateElement?.(this); this.#mediaController = null; } } if (!globalThis.customElements.get('media-text-display')) { globalThis.customElements.define('media-text-display', MediaTextDisplay); } export { MediaTextDisplay }; export default MediaTextDisplay;