Skip to content

Commit

Permalink
feat(esl-media): control mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
fshovchko committed Jan 22, 2025
1 parent 0210885 commit dd220e4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions site/src/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ESLVSizeCSSProxy,
ESLImageContainerMixin,
ESLMedia,
ESLMediaControlMixin,
ESLToggleable,
ESLPopup,
ESLPopupPlaceholder,
Expand Down Expand Up @@ -88,6 +89,8 @@ ESLRandomText.register('lorem-ipsum');
ESLImageContainerMixin.register();
ESLMedia.register();

ESLMediaControlMixin.register();

ESLToggleableDispatcher.init();
ESLToggleable.register();
ESLPopup.register();
Expand Down
65 changes: 65 additions & 0 deletions src/modules/esl-media/control/esl-media-control-mixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {ESLMixinElement} from '../../esl-mixin-element/core';
import {ESLTraversingQuery} from '../../esl-traversing-query/core/esl-traversing-query';
import {jsonAttr, listen, memoize} from '../../esl-utils/decorators';
import {ExportNs} from '../../esl-utils/environment/export-ns';

import type {ESLMedia} from '../core/esl-media';

export interface ESLMediaControlConfig {
target?: string;
action?: 'play' | 'pause' | 'stop' | 'toggle';
activeCls?: string;
}

@ExportNs('MediaControlMixin')
export class ESLMediaControlMixin extends ESLMixinElement {
public static override is = 'esl-media-contol';

protected readonly actionMap: Record<string, () => Promise<void> | null | undefined> = {
play: () => this.$target?.play(),
pause: () => this.$target?.pause(),
toggle: () => this.$target?.toggle(),
stop: () => this.$target?.stop(),
};

public static defaultConfig: ESLMediaControlConfig = {
action: 'toggle',
};

@jsonAttr({name: ESLMediaControlMixin.is})
public userConfig?: ESLMediaControlConfig;

@memoize()
protected get config(): ESLMediaControlConfig {
return Object.assign({}, ESLMediaControlMixin.defaultConfig, this.userConfig);
}

@memoize()
public get $target(): ESLMedia | null {
if (!this.config.target) return null;
return ESLTraversingQuery.first(this.config.target, this.$host) as ESLMedia | null;
}

@listen('click')
protected onClick(): void {
const {$target, config, actionMap} = this;
if (!$target || !config.action) return;

const action = actionMap[config.action];
if (action) action();
}

@listen({
event: 'esl:media:play esl:media:paused esl:media:ended',
target: (control: ESLMediaControlMixin) => control.$target
})
protected onPlay(e: Event): void {
if (this.config.activeCls) this.$$cls(this.config.activeCls, e.type === 'esl:media:play');
}
}

declare global {
export interface ESLLibrary {
MediaCOntrol: typeof ESLMediaControlMixin;
}
}
1 change: 1 addition & 0 deletions src/modules/esl-media/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export type {ESLMediaTagShape} from './core/esl-media.shape';
export * from './core/esl-media';
export * from './core/esl-media-provider';
export * from './core/esl-media-registry';
export * from './control/esl-media-control-mixin';

0 comments on commit dd220e4

Please sign in to comment.