import browser from '../../scripts/browser'; import { appRouter } from '../../components/appRouter'; import loading from '../../components/loading/loading'; import { setBackdropTransparency, TRANSPARENCY_LEVEL } from '../../components/backdrop/backdrop'; import Events from '../../utils/events.ts'; /* globals YT */ const errorCodes = { 2: 'YoutubeBadRequest', 5: 'YoutubePlaybackError', 100: 'YoutubeNotFound', 101: 'YoutubeDenied', 150: 'YoutubeDenied' }; function zoomIn(elem, iterations) { const keyframes = [ { transform: 'scale3d(.2, .2, .2) ', opacity: '.6', offset: 0 }, { transform: 'none', opacity: '1', offset: 1 } ]; const timing = { duration: 240, iterations: iterations }; return elem.animate(keyframes, timing); } function createMediaElement(instance, options) { return new Promise(function (resolve) { const dlg = document.querySelector('.youtubePlayerContainer'); if (!dlg) { import('./style.scss').then(() => { loading.show(); const dlg = document.createElement('div'); dlg.classList.add('youtubePlayerContainer'); if (options.fullscreen) { dlg.classList.add('onTop'); } dlg.innerHTML = '
'; const videoElement = dlg.querySelector('#player'); document.body.insertBefore(dlg, document.body.firstChild); instance.videoDialog = dlg; if (options.fullscreen) { document.body.classList.add('hide-scroll'); } if (options.fullscreen && dlg.animate && !browser.slow) { zoomIn(dlg, 1).onfinish = function () { resolve(videoElement); }; } else { resolve(videoElement); } }); } else { // we need to hide scrollbar when starting playback from page with animated background if (options.fullscreen) { document.body.classList.add('hide-scroll'); } resolve(dlg.querySelector('#player')); } }); } function onVideoResize() { const instance = this; const player = instance.currentYoutubePlayer; const dlg = instance.videoDialog; if (player && dlg) { player.setSize(dlg.offsetWidth, dlg.offsetHeight); } } function clearTimeUpdateInterval(instance) { if (instance.timeUpdateInterval) { clearInterval(instance.timeUpdateInterval); } instance.timeUpdateInterval = null; } function onEndedInternal(instance) { clearTimeUpdateInterval(instance); const resizeListener = instance.resizeListener; if (resizeListener) { window.removeEventListener('resize', resizeListener); window.removeEventListener('orientationChange', resizeListener); instance.resizeListener = null; } const stopInfo = { src: instance._currentSrc }; Events.trigger(instance, 'stopped', [stopInfo]); instance._currentSrc = null; if (instance.currentYoutubePlayer) { instance.currentYoutubePlayer.destroy(); } instance.currentYoutubePlayer = null; } // 4. The API will call this function when the video player is ready. function onPlayerReady(event) { event.target.playVideo(); } function onTimeUpdate() { Events.trigger(this, 'timeupdate'); } function onPlaying(instance, playOptions, resolve) { if (!instance.started) { instance.started = true; resolve(); clearTimeUpdateInterval(instance); instance.timeUpdateInterval = setInterval(onTimeUpdate.bind(instance), 500); if (playOptions.fullscreen) { appRouter.showVideoOsd().then(function () { instance.videoDialog.classList.remove('onTop'); }); } else { setBackdropTransparency(TRANSPARENCY_LEVEL.Backdrop); instance.videoDialog.classList.remove('onTop'); } loading.hide(); } } function setCurrentSrc(instance, elem, options) { return new Promise(function (resolve, reject) { instance._currentSrc = options.url; const params = new URLSearchParams(options.url.split('?')[1]); /* eslint-disable-line compat/compat */ // 3. This function creates an