@nent/core
Version:
4 lines • 3.37 kB
JavaScript
/*!
* NENT 2022
*/
import{r as i,d as t,h as s,a as e,H as h}from"./index-916ca544.js";import{E as a,e as n,a as o}from"./index-f7016b94.js";import{f as r,w as d}from"./logging-5a93c8af.js";import{V as c,a as l,b as v}from"./interfaces-32699e3c.js";import{T as u}from"./interfaces-aed4a5ac.js";import{g as m}from"./time-d244c045.js";class p{constructor(i,t,s,e){this.childVideo=i,this.eventBus=t,this.actionBus=s,this.debug=e,this.disposeHandle=this.actionBus.on(c,(async i=>{r(this.debug,`n-video actions: event received ${i.topic}:${i.command}`),await this.commandReceived(i.command,i.data)}))}async commandReceived(i,t){switch(i){case l.Play:await this.play();break;case l.Pause:this.pause();break;case l.Resume:await this.resume();break;case l.Mute:this.mute(t.value)}}mute(i){this.childVideo&&(this.childVideo.muted=i,this.eventBus.emit(i?v.Muted:v.Unmuted))}async play(){var i,t;await(null===(t=null===(i=this.childVideo)||void 0===i?void 0:i.play)||void 0===t?void 0:t.call(this)),this.eventBus.emit(v.Played)}pause(){var i,t;null===(t=null===(i=this.childVideo)||void 0===i?void 0:i.pause)||void 0===t||t.call(this),this.eventBus.emit(v.Paused)}async resume(){var i,t;await(null===(t=null===(i=this.childVideo)||void 0===i?void 0:i.play)||void 0===t?void 0:t.call(this)),this.eventBus.emit(v.Resumed)}destroy(){var i;null===(i=this.disposeHandle)||void 0===i||i.call(this)}}class f extends a{constructor(i,t="timeupdate",s="currentTime",e="duration",h="ended",a=!1){super(),this.video=i,this.timeEvent=t,this.timeProperty=s,this.durationProperty=e,this.endEvent=h,this.debug=a,this.durationSeconds=0,null!=i?(this.durationSeconds=Number(i[this.durationProperty]||0),r(this.debug,`n-video-timer: creating video timer with duration ${this.durationSeconds}`),i.addEventListener(this.timeEvent,(()=>{const t=Number(i[this.timeProperty]||0);this.currentTime=m(0,1e3*t,1e3*this.durationSeconds),this.emit(u.OnInterval,this.currentTime)})),i.addEventListener(this.endEvent,(()=>{this.emit(u.OnEnd)})),this.currentTime=m(0,0,1e3*this.durationSeconds)):d("n-video-timer: a media element is required")}begin(){var i;try{null===(i=this.video.play)||void 0===i||i.call(this.video)}catch(i){}}stop(){var i;null===(i=this.video.pause)||void 0===i||i.call(this.video)}destroy(){this.stop(),this.removeAllListeners()}}const g=class{constructor(s){i(this,s),this.ready=t(this,"ready",7),this.targetElement="video",this.timeEvent="timeupdate",this.readyEvent="ready",this.timeProperty="currentTime",this.durationProperty="duration",this.endEvent="ended",this.debug=!1}get childVideo(){return this.el.querySelector(this.targetElement)}async componentWillLoad(){r(this.debug,"n-video: loading");const i=this.childVideo;null==i?d("n-video: no child video element was found"):(r(this.debug,`n-video: listening to ${this.targetElement} for ${this.readyEvent}`),i.addEventListener(this.readyEvent,(async()=>{r(this.debug,"n-video: creating timer"),this.timer=new f(i,this.timeEvent,this.timeProperty,this.durationProperty,this.endEvent,this.debug),this.ready.emit(!0)})),r(this.debug,"n-video: creating listener"),this.listener=new p(i,n,o,this.debug)),r(this.debug,"n-video: loaded")}render(){return s(h,null)}disconnectedCallback(){var i,t;null===(i=this.listener)||void 0===i||i.destroy(),null===(t=this.timer)||void 0===t||t.destroy()}get el(){return e(this)}};export{g as n_video}