import { globalThis } from '../utils/server-safe-globals.js'; import { MediaUIAttributes, MediaUIEvents } from '../constants.js'; import { getMediaController } from '../utils/element-utils.js'; import { MediaChromeMenu, createMenuItem, createIndicator, } from './media-chrome-menu.js'; import { parseTextTracksStr, stringifyTextTrackList, formatTextTrackObj, } from '../utils/captions.js'; import { TextTrackLike } from '../utils/TextTrackLike.js'; import { t } from '../utils/i18n.js'; const ccIcon = /*html*/ ` `; function getTemplateHTML(_attrs: Record) { return /*html*/ ` ${MediaChromeMenu.getTemplateHTML(_attrs)} `; } /** * @extends {MediaChromeMenu} * * @slot - Default slotted elements. * @slot header - An element shown at the top of the menu. * @slot checked-indicator - An icon element indicating a checked menu-item. * @slot captions-indicator - An icon element indicating an item with closed captions. * * @attr {string} mediasubtitleslist - (read-only) A list of all subtitles and captions. * @attr {boolean} mediasubtitlesshowing - (read-only) A list of the showing subtitles and captions. */ class MediaCaptionsMenu extends MediaChromeMenu { static getTemplateHTML = getTemplateHTML; static get observedAttributes(): string[] { return [ ...super.observedAttributes, MediaUIAttributes.MEDIA_SUBTITLES_LIST, MediaUIAttributes.MEDIA_SUBTITLES_SHOWING, ]; } #prevState: string | undefined; attributeChangedCallback( attrName: string, oldValue: string | null, newValue: string | null ): void { super.attributeChangedCallback(attrName, oldValue, newValue); if ( attrName === MediaUIAttributes.MEDIA_SUBTITLES_LIST && oldValue !== newValue ) { this.#render(); } else if ( attrName === MediaUIAttributes.MEDIA_SUBTITLES_SHOWING && oldValue !== newValue ) { this.value = newValue || ""; this.#render(); } } connectedCallback(): void { super.connectedCallback(); this.addEventListener('change', this.#onChange); } disconnectedCallback(): void { super.disconnectedCallback(); this.removeEventListener('change', this.#onChange); } /** * Returns the anchor element when it is a floating menu. */ get anchorElement(): HTMLElement { if (this.anchor !== 'auto') return super.anchorElement; return getMediaController(this).querySelector('media-captions-menu-button'); } /** * @type {Array} An array of TextTrack-like objects. * Objects must have the properties: kind, language, and label. */ get mediaSubtitlesList() { return getSubtitlesListAttr(this, MediaUIAttributes.MEDIA_SUBTITLES_LIST); } set mediaSubtitlesList(list: TextTrackLike[]) { setSubtitlesListAttr(this, MediaUIAttributes.MEDIA_SUBTITLES_LIST, list); } /** * An array of TextTrack-like objects. * Objects must have the properties: kind, language, and label. */ get mediaSubtitlesShowing(): TextTrackLike[] { return getSubtitlesListAttr( this, MediaUIAttributes.MEDIA_SUBTITLES_SHOWING ); } set mediaSubtitlesShowing(list: TextTrackLike[]) { setSubtitlesListAttr(this, MediaUIAttributes.MEDIA_SUBTITLES_SHOWING, list); } #render(): void { const hasListChanged = this.#prevState !== JSON.stringify(this.mediaSubtitlesList); const hasShowingChanged = this.value !== this.getAttribute(MediaUIAttributes.MEDIA_SUBTITLES_SHOWING); if (!hasListChanged && !hasShowingChanged) return; this.#prevState = JSON.stringify(this.mediaSubtitlesList); this.defaultSlot.textContent = ''; const isOff = !this.value; const item = createMenuItem({ type: 'radio', text: this.formatMenuItemText(t('Off')), value: 'off', checked: isOff, }); item.prepend(createIndicator(this, 'checked-indicator')); this.defaultSlot.append(item); const subtitlesList = this.mediaSubtitlesList; for (const subs of subtitlesList) { const item = createMenuItem({ type: 'radio', text: this.formatMenuItemText(subs.label, subs), value: formatTextTrackObj(subs), checked: this.value == formatTextTrackObj(subs), }); item.prepend(createIndicator(this, 'checked-indicator')); // add CC icon for captions const type = subs.kind ?? 'subs'; if ((type as string) === 'captions') { item.append(createIndicator(this, 'captions-indicator')); } this.defaultSlot.append(item); } } #onChange(): void { const showingSubs = this.mediaSubtitlesShowing; const showingSubsStr = this.getAttribute( MediaUIAttributes.MEDIA_SUBTITLES_SHOWING ); // Don't make request if this was the result of a media state change (CJP) const localStateChange = this.value !== showingSubsStr; if (showingSubs?.length && localStateChange) { // turn off currently selected tracks this.dispatchEvent( new globalThis.CustomEvent( MediaUIEvents.MEDIA_DISABLE_SUBTITLES_REQUEST, { composed: true, bubbles: true, detail: showingSubs, } ) ); } // Don't make request if this was the result of a media state change (CJP) if (!this.value || !localStateChange) return; const event = new globalThis.CustomEvent( MediaUIEvents.MEDIA_SHOW_SUBTITLES_REQUEST, { composed: true, bubbles: true, detail: this.value, } ); this.dispatchEvent(event); } } /** * @param el - Should be HTMLElement but issues with globalThis shim * @param attrName * @returns An array of TextTrack-like objects. */ const getSubtitlesListAttr = ( el: HTMLElement, attrName: string ): TextTrackLike[] => { const attrVal = el.getAttribute(attrName); return attrVal ? parseTextTracksStr(attrVal) : []; }; /** * * @param el - Should be HTMLElement but issues with globalThis shim * @param attrName * @param {Array} list An array of TextTrack-like objects */ const setSubtitlesListAttr = ( el: HTMLElement, attrName: string, list: TextTrackLike[] ): void => { // null, undefined, and empty arrays are treated as "no value" here if (!list?.length) { el.removeAttribute(attrName); return; } // don't set if the new value is the same as existing const newValStr = stringifyTextTrackList(list); const oldVal = el.getAttribute(attrName); if (oldVal === newValStr) return; el.setAttribute(attrName, newValStr); }; if (!globalThis.customElements.get('media-captions-menu')) { globalThis.customElements.define('media-captions-menu', MediaCaptionsMenu); } export { MediaCaptionsMenu }; export default MediaCaptionsMenu;