Refactor media load events to add unload.

This commit is contained in:
Dermot Duffy
2022-08-07 19:02:57 -07:00
parent 08a0b10104
commit c4995a8fb3
7 changed files with 176 additions and 111 deletions
+26 -16
View File
@@ -1,15 +1,17 @@
import { MediaShowInfo } from '../types.js';
import { MediaLoadedInfo } from '../types.js';
import { dispatchFrigateCardEvent } from './basic.js';
const MEDIA_INFO_HEIGHT_CUTOFF = 50;
const MEDIA_INFO_WIDTH_CUTOFF = MEDIA_INFO_HEIGHT_CUTOFF;
/**
* Create a MediaShowInfo object.
* Create a MediaLoadedInfo object.
* @param source An event or HTMLElement that should be used as a source.
* @returns A new MediaShowInfo object or null if one could not be created.
* @returns A new MediaLoadedInfo object or null if one could not be created.
*/
export function createMediaShowInfo(source: Event | HTMLElement): MediaShowInfo | null {
export function createMediaLoadedInfo(
source: Event | HTMLElement,
): MediaLoadedInfo | null {
let target: HTMLElement | EventTarget;
if (source instanceof Event) {
target = source.composedPath()[0];
@@ -37,7 +39,7 @@ export function createMediaShowInfo(source: Event | HTMLElement): MediaShowInfo
}
/**
* Dispatch a Frigate card media show event.
* Dispatch a Frigate card media loaded event.
* @param element The element to send the event.
* @param source An event or HTMLElement that should be used as a source.
*/
@@ -45,31 +47,39 @@ export function dispatchMediaShowEvent(
element: HTMLElement,
source: Event | HTMLElement,
): void {
const mediaShowInfo = createMediaShowInfo(source);
if (mediaShowInfo) {
dispatchExistingMediaShowInfoAsEvent(element, mediaShowInfo);
const mediaLoadedInfo = createMediaLoadedInfo(source);
if (mediaLoadedInfo) {
dispatchExistingMediaLoadedInfoAsEvent(element, mediaLoadedInfo);
}
}
/**
* Dispatch a pre-existing MediaShowInfo object as an event.
* Dispatch a pre-existing MediaLoadedInfo object as an event.
* @param element The element to send the event.
* @param mediaShowInfo The MediaShowInfo object to send.
* @param MediaLoadedInfo The MediaLoadedInfo object to send.
*/
export function dispatchExistingMediaShowInfoAsEvent(
export function dispatchExistingMediaLoadedInfoAsEvent(
element: HTMLElement,
mediaShowInfo: MediaShowInfo,
MediaLoadedInfo: MediaLoadedInfo,
): void {
dispatchFrigateCardEvent<MediaShowInfo>(element, 'media-show', mediaShowInfo);
dispatchFrigateCardEvent<MediaLoadedInfo>(element, 'media:loaded', MediaLoadedInfo);
}
/**
* Determine if a MediaShowInfo object is valid/acceptable.
* @param info The MediaShowInfo object.
* Determine if a MediaLoadedInfo object is valid/acceptable.
* @param info The MediaLoadedInfo object.
* @returns True if the object is valid, false otherwise.
*/
export function isValidMediaShowInfo(info: MediaShowInfo): boolean {
export function isValidMediaLoadedInfo(info: MediaLoadedInfo): boolean {
return (
info.height >= MEDIA_INFO_HEIGHT_CUTOFF && info.width >= MEDIA_INFO_WIDTH_CUTOFF
);
}
/**
* Dispatch a media unloaded event.
* @param element The element to send the event.
*/
export function dispatchMediaUnloadedEvent(element: HTMLElement): void {
dispatchFrigateCardEvent(element, 'media:unloaded');
}