UNPKG

@vime/core

Version:

Customizable, extensible, accessible and framework agnostic media player.

47 lines (45 loc) 1.45 kB
const LOAD_START_EVENT = 'vmLoadStart'; // Events that toggle state and the prop is named `is{PropName}...`. const isToggleStateEvent = new Set([ 'isFullscreenActive', 'isControlsActive', 'isTextTrackVisible', 'isPiPActive', 'isLive', 'isTouch', 'isAudio', 'isVideo', 'isAudioView', 'isVideoView', ]); // Events that are emitted without the 'Change' postfix. const hasShortenedEventName = new Set([ 'ready', 'playbackStarted', 'playbackEnded', 'playbackReady', ]); const getEventName = (prop) => { // Example: isFullscreenActive -> vmFullscreenChange if (isToggleStateEvent.has(prop)) { return `vm${prop.replace('is', '').replace('Active', '')}Change`; } // Example: playbackStarted -> vmPlaybackStarted if (hasShortenedEventName.has(prop)) { return `vm${prop.charAt(0).toUpperCase()}${prop.slice(1)}`; } // Example: currentTime -> vmCurrentTimeChange return `vm${prop.charAt(0).toUpperCase()}${prop.slice(1)}Change`; }; function firePlayerEvent(el, prop, newValue, oldValue) { const events = []; events.push(new CustomEvent(getEventName(prop), { detail: newValue })); if (prop === 'paused' && !newValue) events.push(new CustomEvent('vmPlay')); if (prop === 'seeking' && oldValue && !newValue) events.push(new CustomEvent('vmSeeked')); events.forEach(event => { el.dispatchEvent(event); }); } export { LOAD_START_EVENT as L, firePlayerEvent as f, getEventName as g };