diff options
Diffstat (limited to 'src/js')
26 files changed, 1087 insertions, 307 deletions
diff --git a/src/js/captions.js b/src/js/captions.js index f7c24534..04da4651 100644 --- a/src/js/captions.js +++ b/src/js/captions.js @@ -133,7 +133,8 @@ const captions = { }); // Turn off native caption rendering to avoid double captions - Object.assign(track, { mode: 'hidden' }); + // eslint-disable-next-line no-param-reassign + track.mode = 'hidden'; // Add event listener for cue changes on.call(this, track, 'cuechange', () => captions.updateCues.call(this)); @@ -150,7 +151,11 @@ const captions = { toggleClass(this.elements.container, this.config.classNames.captions.enabled, !is.empty(tracks)); // Update available languages in list - if ((this.config.controls || []).includes('settings') && this.config.settings.includes('captions')) { + if ( + is.array(this.config.controls) && + this.config.controls.includes('settings') && + this.config.settings.includes('captions') + ) { controls.setCaptionsMenu.call(this); } }, diff --git a/src/js/config/defaults.js b/src/js/config/defaults.js index 6ba6d323..c299a3c9 100644 --- a/src/js/config/defaults.js +++ b/src/js/config/defaults.js @@ -61,7 +61,7 @@ const defaults = { // Sprite (for icons) loadSprite: true, iconPrefix: 'plyr', - iconUrl: 'https://cdn.plyr.io/3.5.2/plyr.svg', + iconUrl: 'https://cdn.plyr.io/3.5.10/plyr.svg', // Blank video (used to prevent errors on source change) blankVideo: 'https://cdn.plyr.io/static/blank.mp4', @@ -69,7 +69,10 @@ const defaults = { // Quality default quality: { default: 576, + // The options to display in the UI, if available for the source media options: [4320, 2880, 2160, 1440, 1080, 720, 576, 480, 360, 240], + forced: false, + onChange: null, }, // Set loops @@ -82,7 +85,8 @@ const defaults = { // Speed default and options to display speed: { selected: 1, - options: [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2], + // The options to display in the UI, if available for the source media (e.g. Vimeo and YouTube only support 0.5x-4x) + options: [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 4], }, // Keyboard shortcut settings @@ -128,6 +132,7 @@ const defaults = { // 'fast-forward', 'progress', 'current-time', + // 'duration', 'mute', 'volume', 'captions', @@ -163,6 +168,7 @@ const defaults = { frameTitle: 'Player for {title}', captions: 'Captions', settings: 'Settings', + pip: 'PIP', menuBack: 'Go back to previous menu', speed: 'Speed', normal: 'Normal', @@ -195,7 +201,7 @@ const defaults = { }, youtube: { sdk: 'https://www.youtube.com/iframe_api', - api: 'https://noembed.com/embed?url=https://www.youtube.com/watch?v={0}', // 'https://www.googleapis.com/youtube/v3/videos?id={0}&key={1}&fields=items(snippet(title),fileDetails)&part=snippet', + api: 'https://noembed.com/embed?url=https://www.youtube.com/watch?v={0}', }, googleIMA: { sdk: 'https://imasdk.googleapis.com/js/sdkloader/ima3.js', @@ -413,6 +419,11 @@ const defaults = { title: false, speed: true, transparent: false, + // These settings require a pro or premium account to work + sidedock: false, + controls: false, + // Custom settings from Plyr + referrerPolicy: null, // https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/referrerPolicy }, // YouTube plugin diff --git a/src/js/controls.js b/src/js/controls.js index 43a92140..37df497f 100644 --- a/src/js/controls.js +++ b/src/js/controls.js @@ -28,7 +28,7 @@ import { import { off, on } from './utils/events'; import i18n from './utils/i18n'; import is from './utils/is'; -import loadSprite from './utils/loadSprite'; +import loadSprite from './utils/load-sprite'; import { extend } from './utils/objects'; import { getPercentage, replaceAll, toCamelCase, toTitleCase } from './utils/strings'; import { formatTime, getHours } from './utils/time'; @@ -111,7 +111,7 @@ const controls = { setAttributes( icon, extend(attributes, { - role: 'presentation', + 'aria-hidden': 'true', focusable: 'false', }), ); @@ -139,10 +139,7 @@ const controls = { // Create hidden text label createLabel(key, attr = {}) { const text = i18n.get(key, this.config); - - const attributes = Object.assign({}, attr, { - class: [attr.class, this.config.classNames.hidden].filter(Boolean).join(' '), - }); + const attributes = { ...attr, class: [attr.class, this.config.classNames.hidden].filter(Boolean).join(' ') }; return createElement('span', attributes, text); }, @@ -402,7 +399,8 @@ const controls = { // https://bugzilla.mozilla.org/show_bug.cgi?id=1220143 bindMenuItemShortcuts(menuItem, type) { // Navigate through menus via arrow keys and space - on( + on.call( + this, menuItem, 'keydown keyup', event => { @@ -452,7 +450,7 @@ const controls = { // Enter will fire a `click` event but we still need to manage focus // So we bind to keyup which fires after and set focus here - on(menuItem, 'keyup', event => { + on.call(this, menuItem, 'keyup', event => { if (event.which !== 13) { return; } @@ -1046,7 +1044,7 @@ const controls = { }, // Set a list of available captions languages - setSpeedMenu(options) { + setSpeedMenu() { // Menu required if (!is.element(this.elements.settings.panels.speed)) { return; @@ -1055,15 +1053,8 @@ const controls = { const type = 'speed'; const list = this.elements.settings.panels.speed.querySelector('[role="menu"]'); - // Set the speed options - if (is.array(options)) { - this.options.speed = options; - } else if (this.isHTML5 || this.isVimeo) { - this.options.speed = [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]; - } - - // Set options if passed and filter based on config - this.options.speed = this.options.speed.filter(speed => this.config.speed.options.includes(speed)); + // Filter out invalid speeds + this.options.speed = this.options.speed.filter(o => o >= this.minimumSpeed && o <= this.maximumSpeed); // Toggle the pane and tab const toggle = !is.empty(this.options.speed) && this.options.speed.length > 1; @@ -1380,7 +1371,9 @@ const controls = { } // Volume range control - if (control === 'volume') { + // Ignored on iOS as it's handled globally + // https://developer.apple.com/library/safari/documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/Device-SpecificConsiderations/Device-SpecificConsiderations.html + if (control === 'volume' && !browser.isIos) { // Set the attributes const attributes = { max: 1, @@ -1463,7 +1456,7 @@ const controls = { bindMenuItemShortcuts.call(this, menuItem, type); // Show menu on click - on(menuItem, 'click', () => { + on.call(this, menuItem, 'click', () => { showMenuPanel.call(this, type, false); }); @@ -1515,7 +1508,8 @@ const controls = { ); // Go back via keyboard - on( + on.call( + this, pane, 'keydown', event => { @@ -1535,7 +1529,7 @@ const controls = { ); // Go back via button click - on(backButton, 'click', () => { + on.call(this, backButton, 'click', () => { showMenuPanel.call(this, 'home', false); }); @@ -1581,6 +1575,11 @@ const controls = { target: '_blank', }); + // Set download attribute for HTML5 only + if (this.isHTML5) { + attributes.download = ''; + } + const { download } = this.config.urls; if (!is.url(download) && this.isEmbed) { diff --git a/src/js/fullscreen.js b/src/js/fullscreen.js index c86bf877..4d3c89ac 100644 --- a/src/js/fullscreen.js +++ b/src/js/fullscreen.js @@ -4,83 +4,11 @@ // https://webkit.org/blog/7929/designing-websites-for-iphone-x/ // ========================================================================== -import { repaint } from './utils/animation'; import browser from './utils/browser'; -import { hasClass, toggleClass, trapFocus } from './utils/elements'; +import { getElements, hasClass, toggleClass } from './utils/elements'; import { on, triggerEvent } from './utils/events'; import is from './utils/is'; - -function onChange() { - if (!this.enabled) { - return; - } - - // Update toggle button - const button = this.player.elements.buttons.fullscreen; - if (is.element(button)) { - button.pressed = this.active; - } - - // Trigger an event - triggerEvent.call(this.player, this.target, this.active ? 'enterfullscreen' : 'exitfullscreen', true); - - // Trap focus in container - if (!browser.isIos) { - trapFocus.call(this.player, this.target, this.active); - } -} - -function toggleFallback(toggle = false) { - // Store or restore scroll position - if (toggle) { - this.scrollPosition = { - x: window.scrollX || 0, - y: window.scrollY || 0, - }; - } else { - window.scrollTo(this.scrollPosition.x, this.scrollPosition.y); - } - - // Toggle scroll - document.body.style.overflow = toggle ? 'hidden' : ''; - - // Toggle class hook - toggleClass(this.target, this.player.config.classNames.fullscreen.fallback, toggle); - - // Force full viewport on iPhone X+ - if (browser.isIos) { - let viewport = document.head.querySelector('meta[name="viewport"]'); - const property = 'viewport-fit=cover'; - - // Inject the viewport meta if required - if (!viewport) { - viewport = document.createElement('meta'); - viewport.setAttribute('name', 'viewport'); - } - - // Check if the property already exists - const hasProperty = is.string(viewport.content) && viewport.content.includes(property); - - if (toggle) { - this.cleanupViewport = !hasProperty; - - if (!hasProperty) { - viewport.content += `,${property}`; - } - } else if (this.cleanupViewport) { - viewport.content = viewport.content - .split(',') - .filter(part => part.trim() !== property) - .join(','); - } - - // Force a repaint as sometimes Safari doesn't want to fill the screen - setTimeout(() => repaint(this.target), 100); - } - - // Toggle button and fire events - onChange.call(this); -} +import { silencePromise } from './utils/promise'; class Fullscreen { constructor(player) { @@ -105,7 +33,7 @@ class Fullscreen { this.prefix === 'ms' ? 'MSFullscreenChange' : `${this.prefix}fullscreenchange`, () => { // TODO: Filter for target?? - onChange.call(this); + this.onChange(); }, ); @@ -119,6 +47,9 @@ class Fullscreen { this.toggle(); }); + // Tap focus when in fullscreen + on.call(this, this.player.elements.container, 'keydown', event => this.trapFocus(event)); + // Update the UI this.update(); } @@ -188,7 +119,7 @@ class Fullscreen { const element = !this.prefix ? document.fullscreenElement : document[`${this.prefix}${this.property}Element`]; - return element === this.target; + return element && element.shadowRoot ? element === this.target.getRootNode().host : element === this.target; } // Get target element @@ -198,6 +129,97 @@ class Fullscreen { : this.player.elements.container; } + onChange() { + if (!this.enabled) { + return; + } + + // Update toggle button + const button = this.player.elements.buttons.fullscreen; + if (is.element(button)) { + button.pressed = this.active; + } + + // Trigger an event + triggerEvent.call(this.player, this.target, this.active ? 'enterfullscreen' : 'exitfullscreen', true); + } + + toggleFallback(toggle = false) { + // Store or restore scroll position + if (toggle) { + this.scrollPosition = { + x: window.scrollX || 0, + y: window.scrollY || 0, + }; + } else { + window.scrollTo(this.scrollPosition.x, this.scrollPosition.y); + } + + // Toggle scroll + document.body.style.overflow = toggle ? 'hidden' : ''; + + // Toggle class hook + toggleClass(this.target, this.player.config.classNames.fullscreen.fallback, toggle); + + // Force full viewport on iPhone X+ + if (browser.isIos) { + let viewport = document.head.querySelector('meta[name="viewport"]'); + const property = 'viewport-fit=cover'; + + // Inject the viewport meta if required + if (!viewport) { + viewport = document.createElement('meta'); + viewport.setAttribute('name', 'viewport'); + } + + // Check if the property already exists + const hasProperty = is.string(viewport.content) && viewport.content.includes(property); + + if (toggle) { + this.cleanupViewport = !hasProperty; + + if (!hasProperty) { + viewport.content += `,${property}`; + } + } else if (this.cleanupViewport) { + viewport.content = viewport.content + .split(',') + .filter(part => part.trim() !== property) + .join(','); + } + } + + // Toggle button and fire events + this.onChange(); + } + + // Trap focus inside container + trapFocus(event) { + // Bail if iOS, not active, not the tab key + if (browser.isIos || !this.active || event.key !== 'Tab' || event.keyCode !== 9) { + return; + } + + // Get the current focused element + const focused = document.activeElement; + const focusable = getElements.call( + this.player, + 'a[href], button:not(:disabled), input:not(:disabled), [tabindex]', + ); + const [first] = focusable; + const last = focusable[focusable.length - 1]; + + if (focused === last && !event.shiftKey) { + // Move focus to first element that can be tabbed if Shift isn't used + first.focus(); + event.preventDefault(); + } else if (focused === first && event.shiftKey) { + // Move focus to last element that can be tabbed if Shift is used + last.focus(); + event.preventDefault(); + } + } + // Update UI update() { if (this.enabled) { @@ -230,9 +252,9 @@ class Fullscreen { if (browser.isIos && this.player.config.fullscreen.iosNative) { this.target.webkitEnterFullscreen(); } else if (!Fullscreen.native || this.forceFallback) { - toggleFallback.call(this, true); + this.toggleFallback(true); } else if (!this.prefix) { - this.target.requestFullscreen(); + this.target.requestFullscreen({ navigationUI: 'hide' }); } else if (!is.empty(this.prefix)) { this.target[`${this.prefix}Request${this.property}`](); } @@ -247,9 +269,9 @@ class Fullscreen { // iOS native fullscreen if (browser.isIos && this.player.config.fullscreen.iosNative) { this.target.webkitExitFullscreen(); - this.player.play(); + silencePromise(this.player.play()); } else if (!Fullscreen.native || this.forceFallback) { - toggleFallback.call(this, false); + this.toggleFallback(false); } else if (!this.prefix) { (document.cancelFullScreen || document.exitFullscreen).call(document); } else if (!is.empty(this.prefix)) { diff --git a/src/js/html5.js b/src/js/html5.js index e538e922..6e8c6483 100644 --- a/src/js/html5.js +++ b/src/js/html5.js @@ -6,6 +6,7 @@ import support from './support'; import { removeElement } from './utils/elements'; import { triggerEvent } from './utils/events'; import is from './utils/is'; +import { silencePromise } from './utils/promise'; import { setAspectRatio } from './utils/style'; const html5 = { @@ -30,6 +31,11 @@ const html5 = { // Get quality levels getQualityOptions() { + // Whether we're forcing all options (e.g. for streaming) + if (this.config.quality.forced) { + return this.config.quality.options; + } + // Get sizes from <source> elements return html5.getSources .call(this) @@ -37,15 +43,20 @@ const html5 = { .filter(Boolean); }, - extend() { + setup() { if (!this.isHTML5) { return; } const player = this; - // Set aspect ratio if set - setAspectRatio.call(player); + // Set speed options from config + player.options.speed = player.config.speed.options; + + // Set aspect ratio if fixed + if (!is.empty(this.config.ratio)) { + setAspectRatio.call(player); + } // Quality Object.defineProperty(player.media, 'quality', { @@ -58,36 +69,46 @@ const html5 = { return source && Number(source.getAttribute('size')); }, set(input) { - // Get sources - const sources = html5.getSources.call(player); - // Get first match for requested size - const source = sources.find(s => Number(s.getAttribute('size')) === input); - - // No matching source found - if (!source) { + if (player.quality === input) { return; } - // Get current state - const { currentTime, paused, preload, readyState } = player.media; - - // Set new source - player.media.src = source.getAttribute('src'); - - // Prevent loading if preload="none" and the current source isn't loaded (#1044) - if (preload !== 'none' || readyState) { - // Restore time - player.once('loadedmetadata', () => { - player.currentTime = currentTime; - - // Resume playing - if (!paused) { - player.play(); - } - }); - - // Load new source - player.media.load(); + // If we're using an an external handler... + if (player.config.quality.forced && is.function(player.config.quality.onChange)) { + player.config.quality.onChange(input); + } else { + // Get sources + const sources = html5.getSources.call(player); + // Get first match for requested size + const source = sources.find(s => Number(s.getAttribute('size')) === input); + + // No matching source found + if (!source) { + return; + } + + // Get current state + const { currentTime, paused, preload, readyState, playbackRate } = player.media; + + // Set new source + player.media.src = source.getAttribute('src'); + + // Prevent loading if preload="none" and the current source isn't loaded (#1044) + if (preload !== 'none' || readyState) { + // Restore time + player.once('loadedmetadata', () => { + player.speed = playbackRate; + player.currentTime = currentTime; + + // Resume playing + if (!paused) { + silencePromise(player.play()); + } + }); + + // Load new source + player.media.load(); + } } // Trigger change event diff --git a/src/js/listeners.js b/src/js/listeners.js index f5c9cda8..cd468083 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -6,9 +6,10 @@ import controls from './controls'; import ui from './ui'; import { repaint } from './utils/animation'; import browser from './utils/browser'; -import { getElement, getElements, matches, toggleClass, toggleHidden } from './utils/elements'; +import { getElement, getElements, matches, toggleClass } from './utils/elements'; import { off, on, once, toggleListener, triggerEvent } from './utils/events'; import is from './utils/is'; +import { silencePromise } from './utils/promise'; import { getAspectRatio, setAspectRatio } from './utils/style'; class Listeners { @@ -99,7 +100,7 @@ class Listeners { case 75: // Space and K key if (!repeat) { - player.togglePlay(); + silencePromise(player.togglePlay()); } break; @@ -301,14 +302,6 @@ class Listeners { }, ); - // Force edge to repaint on exit fullscreen - // TODO: Fix weird bug where Edge doesn't re-draw when exiting fullscreen - /* if (browser.isEdge) { - on.call(player, elements.container, 'exitfullscreen', () => { - setTimeout(() => repaint(elements.container), 100); - }); - } */ - // Set a gutter for Vimeo const setGutter = (ratio, padding, toggle) => { if (!player.isVimeo) { @@ -344,8 +337,13 @@ class Listeners { on.call(player, elements.container, 'enterfullscreen exitfullscreen', event => { const { target, usingNative } = player.fullscreen; - // Ignore for iOS native - if (!player.isEmbed || target !== elements.container) { + // Ignore events not from target + if (target !== elements.container) { + return; + } + + // If it's not an embed and no ratio specified + if (!player.isEmbed && is.empty(player.config.ratio)) { return; } @@ -380,19 +378,15 @@ class Listeners { controls.durationUpdate.call(player, event), ); - // Check for audio tracks on load - // We can't use `loadedmetadata` as it doesn't seem to have audio tracks at that point - on.call(player, player.media, 'canplay loadeddata', () => { - toggleHidden(elements.volume, !player.hasAudio); - toggleHidden(elements.buttons.mute, !player.hasAudio); - }); - // Handle the media finishing on.call(player, player.media, 'ended', () => { // Show poster on end if (player.isHTML5 && player.isVideo && player.config.resetOnEnd) { // Restart player.restart(); + + // Call pause otherwise IE11 will start playing the video again + player.pause(); } }); @@ -438,9 +432,21 @@ class Listeners { if (player.ended) { this.proxy(event, player.restart, 'restart'); - this.proxy(event, player.play, 'play'); + this.proxy( + event, + () => { + silencePromise(player.play()); + }, + 'play', + ); } else { - this.proxy(event, player.togglePlay, 'play'); + this.proxy( + event, + () => { + silencePromise(player.togglePlay()); + }, + 'play', + ); } }); } @@ -516,7 +522,7 @@ class Listeners { } // Only call default handler if not prevented in custom handler - if (returned && is.function(defaultHandler)) { + if (returned !== false && is.function(defaultHandler)) { defaultHandler.call(player, event); } } @@ -546,7 +552,14 @@ class Listeners { // Play/pause toggle if (elements.buttons.play) { Array.from(elements.buttons.play).forEach(button => { - this.bind(button, 'click', player.togglePlay, 'play'); + this.bind( + button, + 'click', + () => { + silencePromise(player.togglePlay()); + }, + 'play', + ); }); } @@ -606,12 +619,19 @@ class Listeners { this.bind(elements.buttons.airplay, 'click', player.airplay, 'airplay'); // Settings menu - click toggle - this.bind(elements.buttons.settings, 'click', event => { - // Prevent the document click listener closing the menu - event.stopPropagation(); + this.bind( + elements.buttons.settings, + 'click', + event => { + // Prevent the document click listener closing the menu + event.stopPropagation(); + event.preventDefault(); - controls.toggleMenu.call(player, event); - }); + controls.toggleMenu.call(player, event); + }, + null, + false, + ); // Can't be passive as we're preventing default // Settings menu - keyboard toggle // We have to bind to keyup otherwise Firefox triggers a click when a keydown event handler shifts focus @@ -666,7 +686,7 @@ class Listeners { const code = event.keyCode ? event.keyCode : event.which; const attribute = 'play-on-seeked'; - if (is.keyboardEvent(event) && (code !== 39 && code !== 37)) { + if (is.keyboardEvent(event) && code !== 39 && code !== 37) { return; } @@ -681,7 +701,7 @@ class Listeners { // If we're done seeking and it was playing, resume playback if (play && done) { seek.removeAttribute(attribute); - player.play(); + silencePromise(player.play()); } else if (!done && player.playing) { seek.setAttribute(attribute, ''); player.pause(); @@ -732,7 +752,7 @@ class Listeners { }); // Hide thumbnail preview - on mouse click, mouse leave, and video play/seek. All four are required, e.g., for buffering - this.bind(elements.progress, 'mouseleave click', () => { + this.bind(elements.progress, 'mouseleave touchend click', () => { const { previewThumbnails } = player; if (previewThumbnails && previewThumbnails.loaded) { diff --git a/src/js/media.js b/src/js/media.js index eb37d441..8c08456d 100644 --- a/src/js/media.js +++ b/src/js/media.js @@ -39,15 +39,17 @@ const media = { wrap(this.media, this.elements.wrapper); // Faux poster container - this.elements.poster = createElement('div', { - class: this.config.classNames.poster, - }); + if (this.isEmbed) { + this.elements.poster = createElement('div', { + class: this.config.classNames.poster, + }); - this.elements.wrapper.appendChild(this.elements.poster); + this.elements.wrapper.appendChild(this.elements.poster); + } } if (this.isHTML5) { - html5.extend.call(this); + html5.setup.call(this); } else if (this.isYouTube) { youtube.setup.call(this); } else if (this.isVimeo) { diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js index e6fab967..9f1088fa 100644 --- a/src/js/plugins/ads.js +++ b/src/js/plugins/ads.js @@ -10,7 +10,8 @@ import { createElement } from '../utils/elements'; import { triggerEvent } from '../utils/events'; import i18n from '../utils/i18n'; import is from '../utils/is'; -import loadScript from '../utils/loadScript'; +import loadScript from '../utils/load-script'; +import { silencePromise } from '../utils/promise'; import { formatTime } from '../utils/time'; import { buildUrlParams } from '../utils/urls'; @@ -136,7 +137,7 @@ class Ads { cb: Date.now(), AV_WIDTH: 640, AV_HEIGHT: 480, - AV_CDIM2: this.publisherId, + AV_CDIM2: config.publisherId, }; const base = 'https://go.aniview.com/api/adserver6/vast/'; @@ -172,6 +173,17 @@ class Ads { // We assume the adContainer is the video container of the plyr element that will house the ads this.elements.displayContainer = new google.ima.AdDisplayContainer(this.elements.container, this.player.media); + // Create ads loader + this.loader = new google.ima.AdsLoader(this.elements.displayContainer); + + // Listen and respond to ads loaded and error events + this.loader.addEventListener( + google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, + event => this.onAdsManagerLoaded(event), + false, + ); + this.loader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, error => this.onAdError(error), false); + // Request video ads to be pre-loaded this.requestAds(); } @@ -183,17 +195,6 @@ class Ads { const { container } = this.player.elements; try { - // Create ads loader - this.loader = new google.ima.AdsLoader(this.elements.displayContainer); - - // Listen and respond to ads loaded and error events - this.loader.addEventListener( - google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, - event => this.onAdsManagerLoaded(event), - false, - ); - this.loader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, error => this.onAdError(error), false); - // Request video ads const request = new google.ima.AdsRequest(); request.adTagUrl = this.tagUrl; @@ -369,7 +370,12 @@ class Ads { // TODO: So there is still this thing where a video should only be allowed to start // playing when the IMA SDK is ready or has failed - this.loadAds(); + if (this.player.ended) { + this.loadAds(); + } else { + // The SDK won't allow new ads to be called without receiving a contentComplete() + this.loader.contentComplete(); + } break; @@ -510,7 +516,7 @@ class Ads { this.playing = false; // Play video - this.player.media.play(); + silencePromise(this.player.media.play()); } /** @@ -563,6 +569,8 @@ class Ads { this.on('loaded', resolve); this.player.debug.log(this.manager); }); + // Now that the manager has been destroyed set it to also be un-initialized + this.initialized = false; // Now request some new advertisements this.requestAds(); diff --git a/src/js/plugins/previewThumbnails.js b/src/js/plugins/preview-thumbnails.js index 2d34fe46..290ce949 100644 --- a/src/js/plugins/previewThumbnails.js +++ b/src/js/plugins/preview-thumbnails.js @@ -63,6 +63,20 @@ const parseVtt = vttDataString => { * - This implementation uses multiple separate img elements. Other implementations use background-image on one element. This would be nice and simple, but Firefox and Safari have flickering issues with replacing backgrounds of larger images. It seems that YouTube perhaps only avoids this because they don't have the option for high-res previews (even the fullscreen ones, when mousedown/seeking). Images appear over the top of each other, and previous ones are discarded once the new ones have been rendered */ +const fitRatio = (ratio, outer) => { + const targetRatio = outer.width / outer.height; + const result = {}; + if (ratio > targetRatio) { + result.width = outer.width; + result.height = (1 / ratio) * outer.width; + } else { + result.height = outer.height; + result.width = ratio * outer.height; + } + + return result; +}; + class PreviewThumbnails { /** * PreviewThumbnails constructor. @@ -90,7 +104,7 @@ class PreviewThumbnails { } load() { - // Togglethe regular seek tooltip + // Toggle the regular seek tooltip if (this.player.elements.display.seekTooltip) { this.player.elements.display.seekTooltip.hidden = this.enabled; } @@ -100,6 +114,10 @@ class PreviewThumbnails { } this.getThumbnails().then(() => { + if (!this.enabled) { + return; + } + // Render DOM elements this.render(); @@ -119,19 +137,32 @@ class PreviewThumbnails { throw new Error('Missing previewThumbnails.src config attribute'); } - // If string, convert into single-element list - const urls = is.string(src) ? [src] : src; - // Loop through each src URL. Download and process the VTT file, storing the resulting data in this.thumbnails - const promises = urls.map(u => this.getThumbnail(u)); - - Promise.all(promises).then(() => { + // Resolve promise + const sortAndResolve = () => { // Sort smallest to biggest (e.g., [120p, 480p, 1080p]) this.thumbnails.sort((x, y) => x.height - y.height); this.player.debug.log('Preview thumbnails', this.thumbnails); resolve(); - }); + }; + + // Via callback() + if (is.function(src)) { + src(thumbnails => { + this.thumbnails = thumbnails; + sortAndResolve(); + }); + } + // VTT urls + else { + // If string, convert into single-element list + const urls = is.string(src) ? [src] : src; + // Loop through each src URL. Download and process the VTT file, storing the resulting data in this.thumbnails + const promises = urls.map(u => this.getThumbnail(u)); + // Resolve + Promise.all(promises).then(sortAndResolve); + } }); } @@ -221,8 +252,8 @@ class PreviewThumbnails { } startScrubbing(event) { - // Only act on left mouse button (0), or touch device (event.button is false) - if (event.button === false || event.button === 0) { + // Only act on left mouse button (0), or touch device (event.button does not exist or is false) + if (is.nullOrUndefined(event.button) || event.button === false || event.button === 0) { this.mouseDown = true; // Wait until media has a duration @@ -310,6 +341,15 @@ class PreviewThumbnails { this.player.elements.wrapper.appendChild(this.elements.scrubbing.container); } + destroy() { + if (this.elements.thumb.container) { + this.elements.thumb.container.remove(); + } + if (this.elements.scrubbing.container) { + this.elements.scrubbing.container.remove(); + } + } + showImageAtCurrentTime() { if (this.mouseDown) { this.setScrubbingContainerSize(); @@ -536,8 +576,16 @@ class PreviewThumbnails { get thumbContainerHeight() { if (this.mouseDown) { - // Can't use media.clientHeight - HTML5 video goes big and does black bars above and below - return Math.floor(this.player.media.clientWidth / this.thumbAspectRatio); + const { height } = fitRatio(this.thumbAspectRatio, { + width: this.player.media.clientWidth, + height: this.player.media.clientHeight, + }); + return height; + } + + // If css is used this needs to return the css height for sprites to work (see setImageSizeAndOffset) + if (this.sizeSpecifiedInCSS) { + return this.elements.thumb.imageContainer.clientHeight; } return Math.floor(this.player.media.clientWidth / this.thumbAspectRatio / 4); @@ -580,7 +628,7 @@ class PreviewThumbnails { } determineContainerAutoSizing() { - if (this.elements.thumb.imageContainer.clientHeight > 20) { + if (this.elements.thumb.imageContainer.clientHeight > 20 || this.elements.thumb.imageContainer.clientWidth > 20) { // This will prevent auto sizing in this.setThumbContainerSizeAndPos() this.sizeSpecifiedInCSS = true; } @@ -592,6 +640,12 @@ class PreviewThumbnails { const thumbWidth = Math.floor(this.thumbContainerHeight * this.thumbAspectRatio); this.elements.thumb.imageContainer.style.height = `${this.thumbContainerHeight}px`; this.elements.thumb.imageContainer.style.width = `${thumbWidth}px`; + } else if (this.elements.thumb.imageContainer.clientHeight > 20 && this.elements.thumb.imageContainer.clientWidth < 20) { + const thumbWidth = Math.floor(this.elements.thumb.imageContainer.clientHeight * this.thumbAspectRatio); + this.elements.thumb.imageContainer.style.width = `${thumbWidth}px`; + } else if (this.elements.thumb.imageContainer.clientHeight < 20 && this.elements.thumb.imageContainer.clientWidth > 20) { + const thumbHeight = Math.floor(this.elements.thumb.imageContainer.clientWidth / this.thumbAspectRatio); + this.elements.thumb.imageContainer.style.height = `${thumbHeight}px`; } this.setThumbContainerPos(); @@ -620,9 +674,12 @@ class PreviewThumbnails { // Can't use 100% width, in case the video is a different aspect ratio to the video container setScrubbingContainerSize() { - this.elements.scrubbing.container.style.width = `${this.player.media.clientWidth}px`; - // Can't use media.clientHeight - html5 video goes big and does black bars above and below - this.elements.scrubbing.container.style.height = `${this.player.media.clientWidth / this.thumbAspectRatio}px`; + const { width, height } = fitRatio(this.thumbAspectRatio, { + width: this.player.media.clientWidth, + height: this.player.media.clientHeight, + }); + this.elements.scrubbing.container.style.width = `${width}px`; + this.elements.scrubbing.container.style.height = `${height}px`; } // Sprites need to be offset to the correct location @@ -635,9 +692,9 @@ class PreviewThumbnails { const multiplier = this.thumbContainerHeight / frame.h; // eslint-disable-next-line no-param-reassign - previewImage.style.height = `${Math.floor(previewImage.naturalHeight * multiplier)}px`; + previewImage.style.height = `${previewImage.naturalHeight * multiplier}px`; // eslint-disable-next-line no-param-reassign - previewImage.style.width = `${Math.floor(previewImage.naturalWidth * multiplier)}px`; + previewImage.style.width = `${previewImage.naturalWidth * multiplier}px`; // eslint-disable-next-line no-param-reassign previewImage.style.left = `-${frame.x * multiplier}px`; // eslint-disable-next-line no-param-reassign diff --git a/src/js/plugins/vimeo.js b/src/js/plugins/vimeo.js index bef48708..010cf5f7 100644 --- a/src/js/plugins/vimeo.js +++ b/src/js/plugins/vimeo.js @@ -9,7 +9,7 @@ import { createElement, replaceElement, toggleClass } from '../utils/elements'; import { triggerEvent } from '../utils/events'; import fetch from '../utils/fetch'; import is from '../utils/is'; -import loadScript from '../utils/loadScript'; +import loadScript from '../utils/load-script'; import { extend } from '../utils/objects'; import { format, stripHTML } from '../utils/strings'; import { setAspectRatio } from '../utils/style'; @@ -42,23 +42,28 @@ function assurePlaybackState(play) { const vimeo = { setup() { + const player = this; + // Add embed class for responsive - toggleClass(this.elements.wrapper, this.config.classNames.embed, true); + toggleClass(player.elements.wrapper, player.config.classNames.embed, true); + + // Set speed options from config + player.options.speed = player.config.speed.options; // Set intial ratio - setAspectRatio.call(this); + setAspectRatio.call(player); // Load the SDK if not already if (!is.object(window.Vimeo)) { - loadScript(this.config.urls.vimeo.sdk) + loadScript(player.config.urls.vimeo.sdk) .then(() => { - vimeo.ready.call(this); + vimeo.ready.call(player); }) .catch(error => { - this.debug.warn('Vimeo SDK (player.js) failed to load', error); + player.debug.warn('Vimeo SDK (player.js) failed to load', error); }); } else { - vimeo.ready.call(this); + vimeo.ready.call(player); } }, @@ -99,6 +104,11 @@ const vimeo = { iframe.setAttribute('allowtransparency', ''); iframe.setAttribute('allow', 'autoplay'); + // Set the referrer policy if required + if (!is.empty(config.referrerPolicy)) { + iframe.setAttribute('referrerPolicy', config.referrerPolicy); + } + // Get poster, if already set const { poster } = player; // Inject the package @@ -191,18 +201,13 @@ const vimeo = { return speed; }, set(input) { - player.embed - .setPlaybackRate(input) - .then(() => { - speed = input; - triggerEvent.call(player, player.media, 'ratechange'); - }) - .catch(error => { - // Hide menu item (and menu if empty) - if (error.name === 'Error') { - controls.setSpeedMenu.call(player, []); - } - }); + player.embed.setPlaybackRate(input).then(() => { + speed = input; + triggerEvent.call(player, player.media, 'ratechange'); + }).catch(() => { + // Cannot set Playback Rate, Video is probably not on Pro account + player.options.speed = [1]; + }); }, }); @@ -335,6 +340,14 @@ const vimeo = { } }); + player.embed.on('bufferstart', () => { + triggerEvent.call(player, player.media, 'waiting'); + }); + + player.embed.on('bufferend', () => { + triggerEvent.call(player, player.media, 'playing'); + }); + player.embed.on('play', () => { assurePlaybackState.call(player, true); triggerEvent.call(player, player.media, 'playing'); diff --git a/src/js/plugins/youtube.js b/src/js/plugins/youtube.js index 34c5de7e..8c65b1dc 100644 --- a/src/js/plugins/youtube.js +++ b/src/js/plugins/youtube.js @@ -7,8 +7,8 @@ import { createElement, replaceElement, toggleClass } from '../utils/elements'; import { triggerEvent } from '../utils/events'; import fetch from '../utils/fetch'; import is from '../utils/is'; -import loadImage from '../utils/loadImage'; -import loadScript from '../utils/loadScript'; +import loadImage from '../utils/load-image'; +import loadScript from '../utils/load-script'; import { extend } from '../utils/objects'; import { format, generateId } from '../utils/strings'; import { setAspectRatio } from '../utils/style'; @@ -297,7 +297,9 @@ const youtube = { }); // Get available speeds - player.options.speed = instance.getAvailablePlaybackRates(); + const speeds = instance.getAvailablePlaybackRates(); + // Filter based on config + player.options.speed = speeds.filter(s => player.config.speed.options.includes(s)); // Set the tabindex to avoid focus entering iframe if (player.supported.ui) { @@ -416,6 +418,12 @@ const youtube = { break; + case 3: + // Trigger waiting event to add loading classes to container as the video buffers. + triggerEvent.call(player, player.media, 'waiting'); + + break; + default: break; } diff --git a/src/js/plyr.d.ts b/src/js/plyr.d.ts new file mode 100644 index 00000000..d68935b2 --- /dev/null +++ b/src/js/plyr.d.ts @@ -0,0 +1,620 @@ +// Type definitions for plyr 3.5 +// Project: https://plyr.io +// Definitions by: ondratra <https://github.com/ondratra> +// TypeScript Version: 3.0 + +export = Plyr; +export as namespace Plyr; + +declare class Plyr { + /** + * Setup a new instance + */ + static setup(targets: NodeList | HTMLElement | HTMLElement[] | string, options?: Plyr.Options): Plyr[]; + + /** + * Check for support + * @param mediaType + * @param provider + * @param playsInline Whether the player has the playsinline attribute (only applicable to iOS 10+) + */ + static supported(mediaType?: Plyr.MediaType, provider?: Plyr.Provider, playsInline?: boolean): Plyr.Support; + + constructor(targets: NodeList | HTMLElement | HTMLElement[] | string, options?: Plyr.Options); + + /** + * Indicates if the current player is HTML5. + */ + readonly isHTML5: boolean; + + /** + * Indicates if the current player is an embedded player. + */ + readonly isEmbed: boolean; + + /** + * Indicates if the current player is playing. + */ + readonly playing: boolean; + + /** + * Indicates if the current player is paused. + */ + readonly paused: boolean; + + /** + * Indicates if the current player is stopped. + */ + readonly stopped: boolean; + + /** + * Indicates if the current player has finished playback. + */ + readonly ended: boolean; + + /** + * Returns a float between 0 and 1 indicating how much of the media is buffered + */ + readonly buffered: number; + + /** + * Gets or sets the currentTime for the player. The setter accepts a float in seconds. + */ + currentTime: number; + + /** + * Indicates if the current player is seeking. + */ + readonly seeking: boolean; + + /** + * Returns the duration for the current media. + */ + readonly duration: number; + + /** + * Gets or sets the volume for the player. The setter accepts a float between 0 and 1. + */ + volume: number; + + /** + * Gets or sets the muted state of the player. The setter accepts a boolean. + */ + muted: boolean; + + /** + * Indicates if the current media has an audio track. + */ + readonly hasAudio: boolean; + + /** + * Gets or sets the speed for the player. The setter accepts a value in the options specified in your config. Generally the minimum should be 0.5. + */ + speed: number; + + /** + * Gets or sets the quality for the player. The setter accepts a value from the options specified in your config. + */ + quality: number; + + /** + * Gets or sets the current loop state of the player. + */ + loop: boolean; + + /** + * Gets or sets the current source for the player. + */ + source: Plyr.SourceInfo; + + /** + * Gets or sets the current poster image URL for the player. + */ + poster: string; + + /** + * Gets or sets the autoplay state of the player. + */ + autoplay: boolean; + + /** + * Gets or sets the caption track by index. 1 means the track is missing or captions is not active + */ + currentTrack: number; + + /** + * Gets or sets the preferred captions language for the player. The setter accepts an ISO twoletter language code. Support for the languages is dependent on the captions you include. + * If your captions don't have any language data, or if you have multiple tracks with the same language, you may want to use currentTrack instead. + */ + language: string; + + /** + * Gets or sets the picture-in-picture state of the player. This currently only supported on Safari 10+ on MacOS Sierra+ and iOS 10+. + */ + pip: boolean; + + /** + * Gets or sets the aspect ratio for embedded players. + */ + ratio?: string; + + /** + * Returns the current video Provider + */ + readonly provider: 'html5' | 'vimeo' | 'youtube'; + + /** + * Returns the native API for Vimeo or Youtube players + */ + readonly embed?: any; + + readonly fullscreen: Plyr.FullscreenControl; + + /** + * Start playback. + * For HTML5 players, play() will return a Promise in some browsers - WebKit and Mozilla according to MDN at time of writing. + */ + play(): Promise<void> | void; + + /** + * Pause playback. + */ + pause(): void; + + /** + * Toggle playback, if no parameters are passed, it will toggle based on current status. + */ + togglePlay(toggle?: boolean): boolean; + + /** + * Stop playback and reset to start. + */ + stop(): void; + + /** + * Restart playback. + */ + restart(): void; + + /** + * Rewind playback by the specified seek time. If no parameter is passed, the default seek time will be used. + */ + rewind(seekTime?: number): void; + + /** + * Fast forward by the specified seek time. If no parameter is passed, the default seek time will be used. + */ + forward(seekTime?: number): void; + + /** + * Increase volume by the specified step. If no parameter is passed, the default step will be used. + */ + increaseVolume(step?: number): void; + + /** + * Increase volume by the specified step. If no parameter is passed, the default step will be used. + */ + decreaseVolume(step?: number): void; + + /** + * Toggle captions display. If no parameter is passed, it will toggle based on current status. + */ + toggleCaptions(toggle?: boolean): void; + + /** + * Trigger the airplay dialog on supported devices. + */ + airplay(): void; + + /** + * Toggle the controls (video only). Takes optional truthy value to force it on/off. + */ + toggleControls(toggle: boolean): void; + + /** + * Add an event listener for the specified event. + */ + on( + event: Plyr.StandardEvent | Plyr.Html5Event | Plyr.YoutubeEvent, + callback: (this: this, event: Plyr.PlyrEvent) => void, + ): void; + + /** + * Add an event listener for the specified event once. + */ + once( + event: Plyr.StandardEvent | Plyr.Html5Event | Plyr.YoutubeEvent, + callback: (this: this, event: Plyr.PlyrEvent) => void, + ): void; + + /** + * Remove an event listener for the specified event. + */ + off( + event: Plyr.StandardEvent | Plyr.Html5Event | Plyr.YoutubeEvent, + callback: (this: this, event: Plyr.PlyrEvent) => void, + ): void; + + /** + * Check support for a mime type. + */ + supports(type: string): boolean; + + /** + * Destroy lib instance + */ + destroy(): void; +} + +declare namespace Plyr { + type MediaType = 'audio' | 'video'; + type Provider = 'html5' | 'youtube' | 'vimeo'; + type StandardEvent = + | 'progress' + | 'playing' + | 'play' + | 'pause' + | 'timeupdate' + | 'volumechange' + | 'seeking' + | 'seeked' + | 'ratechange' + | 'ended' + | 'enterfullscreen' + | 'exitfullscreen' + | 'captionsenabled' + | 'captionsdisabled' + | 'languagechange' + | 'controlshidden' + | 'controlsshown' + | 'ready'; + type Html5Event = + | 'loadstart' + | 'loadeddata' + | 'loadedmetadata' + | 'canplay' + | 'canplaythrough' + | 'stalled' + | 'waiting' + | 'emptied' + | 'cuechange' + | 'error'; + type YoutubeEvent = 'statechange' | 'qualitychange' | 'qualityrequested'; + + interface FullscreenControl { + /** + * Indicates if the current player is in fullscreen mode. + */ + readonly active: boolean; + + /** + * Indicates if the current player has fullscreen enabled. + */ + readonly enabled: boolean; + + /** + * Enter fullscreen. If fullscreen is not supported, a fallback ""full window/viewport"" is used instead. + */ + enter(): void; + + /** + * Exit fullscreen. + */ + exit(): void; + + /** + * Toggle fullscreen. + */ + toggle(): void; + } + + interface Options { + /** + * Completely disable Plyr. This would allow you to do a User Agent check or similar to programmatically enable or disable Plyr for a certain UA. Example below. + */ + enabled?: boolean; + + /** + * Display debugging information in the console + */ + debug?: boolean; + + /** + * If a function is passed, it is assumed your method will return either an element or HTML string for the controls. Three arguments will be passed to your function; + * id (the unique id for the player), seektime (the seektime step in seconds), and title (the media title). See controls.md for more info on how the html needs to be structured. + * Defaults to ['play-large', 'play', 'progress', 'current-time', 'mute', 'volume', 'captions', 'settings', 'pip', 'airplay', 'fullscreen'] + */ + controls?: string[] | ((id: string, seektime: number, title: string) => unknown) | Element; + + /** + * If you're using the default controls are used then you can specify which settings to show in the menu + * Defaults to ['captions', 'quality', 'speed', 'loop'] + */ + settings?: string[]; + + /** + * Used for internationalization (i18n) of the text within the UI. + */ + i18n?: any; + + /** + * Load the SVG sprite specified as the iconUrl option (if a URL). If false, it is assumed you are handling sprite loading yourself. + */ + loadSprite?: boolean; + + /** + * Specify a URL or path to the SVG sprite. See the SVG section for more info. + */ + iconUrl?: string; + + /** + * Specify the id prefix for the icons used in the default controls (e.g. plyr-play would be plyr). + * This is to prevent clashes if you're using your own SVG sprite but with the default controls. + * Most people can ignore this option. + */ + iconPrefix?: string; + + /** + * Specify a URL or path to a blank video file used to properly cancel network requests. + */ + blankUrl?: string; + + /** + * Autoplay the media on load. This is generally advised against on UX grounds. It is also disabled by default in some browsers. + * If the autoplay attribute is present on a <video> or <audio> element, this will be automatically set to true. + */ + autoplay?: boolean; + + /** + * Only allow one player playing at once. + */ + autopause?: boolean; + + /** + * The time, in seconds, to seek when a user hits fast forward or rewind. + */ + seekTime?: number; + + /** + * A number, between 0 and 1, representing the initial volume of the player. + */ + volume?: number; + + /** + * Whether to start playback muted. If the muted attribute is present on a <video> or <audio> element, this will be automatically set to true. + */ + muted?: boolean; + + /** + * Click (or tap) of the video container will toggle play/pause. + */ + clickToPlay?: boolean; + + /** + * Disable right click menu on video to help as very primitive obfuscation to prevent downloads of content. + */ + disableContextMenu?: boolean; + + /** + * Hide video controls automatically after 2s of no mouse or focus movement, on control element blur (tab out), on playback start or entering fullscreen. + * As soon as the mouse is moved, a control element is focused or playback is paused, the controls reappear instantly. + */ + hideControls?: boolean; + + /** + * Reset the playback to the start once playback is complete. + */ + resetOnEnd?: boolean; + + /** + * Enable keyboard shortcuts for focused players only or globally + */ + keyboard?: KeyboardOptions; + + /** + * controls: Display control labels as tooltips on :hover & :focus (by default, the labels are screen reader only). + * seek: Display a seek tooltip to indicate on click where the media would seek to. + */ + tooltips?: TooltipOptions; + + /** + * Specify a custom duration for media. + */ + duration?: number; + + /** + * Displays the duration of the media on the metadataloaded event (on startup) in the current time display. + * This will only work if the preload attribute is not set to none (or is not set at all) and you choose not to display the duration (see controls option). + */ + displayDuration?: boolean; + + /** + * Display the current time as a countdown rather than an incremental counter. + */ + invertTime?: boolean; + + /** + * Allow users to click to toggle the above. + */ + toggleInvert?: boolean; + + /** + * Allows binding of event listeners to the controls before the default handlers. See the defaults.js for available listeners. + * If your handler prevents default on the event (event.preventDefault()), the default handler will not fire. + */ + listeners?: { [key: string]: (error: PlyrEvent) => void }; + + /** + * active: Toggles if captions should be active by default. language: Sets the default language to load (if available). 'auto' uses the browser language. + * update: Listen to changes to tracks and update menu. This is needed for some streaming libraries, but can result in unselectable language options). + */ + captions?: CaptionOptions; + + /** + * enabled: Toggles whether fullscreen should be enabled. fallback: Allow fallback to a full-window solution. + * iosNative: whether to use native iOS fullscreen when entering fullscreen (no custom controls) + */ + fullscreen?: FullScreenOptions; + + /** + * The aspect ratio you want to use for embedded players. + */ + ratio?: string; + + /** + * enabled: Allow use of local storage to store user settings. key: The key name to use. + */ + storage?: StorageOptions; + + /** + * selected: The default speed for playback. options: The speed options to display in the UI. YouTube and Vimeo will ignore any options outside of the 0.5-2 range, so options outside of this range will be hidden automatically. + */ + speed?: SpeedOptions; + + /** + * Currently only supported by YouTube. default is the default quality level, determined by YouTube. options are the options to display. + */ + quality?: QualityOptions; + + /** + * active: Whether to loop the current video. If the loop attribute is present on a <video> or <audio> element, + * this will be automatically set to true This is an object to support future functionality. + */ + loop?: LoopOptions; + + /** + * enabled: Whether to enable vi.ai ads. publisherId: Your unique vi.ai publisher ID. + */ + ads?: AdOptions; + + /** + * Vimeo Player Options. + */ + vimeo?: object; + + /** + * Youtube Player Options. + */ + youtube?: object; + } + + interface QualityOptions { + default: number; + options: number[]; + } + + interface LoopOptions { + active: boolean; + } + + interface AdOptions { + enabled: boolean; + publisherId: string; + } + + interface SpeedOptions { + selected: number; + options: number[]; + } + + interface KeyboardOptions { + focused?: boolean; + global?: boolean; + } + + interface TooltipOptions { + controls?: boolean; + seek?: boolean; + } + + interface FullScreenOptions { + enabled?: boolean; + fallback?: boolean; + allowAudio?: boolean; + iosNative?: boolean; + } + + interface CaptionOptions { + active?: boolean; + language?: string; + update?: boolean; + } + + interface StorageOptions { + enabled?: boolean; + key?: string; + } + + interface SourceInfo { + /** + * Note: YouTube and Vimeo are currently not supported as audio sources. + */ + type: MediaType; + + /** + * Title of the new media. Used for the aria-label attribute on the play button, and outer container. YouTube and Vimeo are populated automatically. + */ + title?: string; + + /** + * This is an array of sources. For HTML5 media, the properties of this object are mapped directly to HTML attributes so more can be added to the object if required. + */ + sources: Source[]; + + /** + * The URL for the poster image (HTML5 video only). + */ + poster?: string; + + /** + * An array of track objects. Each element in the array is mapped directly to a track element and any keys mapped directly to HTML attributes so as in the example above, + * it will render as <track kind="captions" label="English" srclang="en" src="https://cdn.selz.com/plyr/1.0/example_captions_en.vtt" default> and similar for the French version. + * Booleans are converted to HTML5 value-less attributes. + */ + tracks?: Track[]; + } + + interface Source { + /** + * The URL of the media file (or YouTube/Vimeo URL). + */ + src: string; + /** + * The MIME type of the media file (if HTML5). + */ + type?: string; + provider?: Provider; + size?: number; + } + + type TrackKind = 'subtitles' | 'captions' | 'descriptions' | 'chapters' | 'metadata'; + interface Track { + /** + * Indicates how the text track is meant to be used + */ + kind: TrackKind; + /** + * Indicates a user-readable title for the track + */ + label: string; + /** + * The language of the track text data. It must be a valid BCP 47 language tag. If the kind attribute is set to subtitles, then srclang must be defined. + */ + srcLang?: string; + /** + * The URL of the track (.vtt file). + */ + src: string; + + default?: boolean; + } + + interface PlyrEvent extends CustomEvent { + readonly detail: { readonly plyr: Plyr }; + } + + interface Support { + api: boolean; + ui: boolean; + } +} diff --git a/src/js/plyr.js b/src/js/plyr.js index 83897cab..00b95a5f 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr -// plyr.js v3.5.3 +// plyr.js v3.5.10 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== @@ -15,7 +15,7 @@ import Fullscreen from './fullscreen'; import Listeners from './listeners'; import media from './media'; import Ads from './plugins/ads'; -import PreviewThumbnails from './plugins/previewThumbnails'; +import PreviewThumbnails from './plugins/preview-thumbnails'; import source from './source'; import Storage from './storage'; import support from './support'; @@ -24,9 +24,10 @@ import { closest } from './utils/arrays'; import { createElement, hasClass, removeElement, replaceElement, toggleClass, wrap } from './utils/elements'; import { off, on, once, triggerEvent, unbindListeners } from './utils/events'; import is from './utils/is'; -import loadSprite from './utils/loadSprite'; +import loadSprite from './utils/load-sprite'; import { clamp } from './utils/numbers'; import { cloneDeep, extend } from './utils/objects'; +import { silencePromise } from './utils/promise'; import { getAspectRatio, reduceAspectRatio, setAspectRatio, validateRatio } from './utils/style'; import { parseUrl } from './utils/urls'; @@ -303,7 +304,7 @@ class Plyr { // Autoplay if required if (this.isHTML5 && this.config.autoplay) { - setTimeout(() => this.play(), 10); + setTimeout(() => silencePromise(this.play()), 10); } // Seek time will be recorded (in listeners.js) so we can prevent hiding controls for a few seconds after seek @@ -356,7 +357,7 @@ class Plyr { // Intecept play with ads if (this.ads && this.ads.enabled) { - this.ads.managerPromise.then(() => this.ads.play()).catch(() => this.media.play()); + this.ads.managerPromise.then(() => this.ads.play()).catch(() => silencePromise(this.media.play())); } // Return the promise (for HTML5) @@ -368,10 +369,10 @@ class Plyr { */ pause() { if (!this.playing || !is.function(this.media.pause)) { - return; + return null; } - this.media.pause(); + return this.media.pause(); } /** @@ -411,10 +412,10 @@ class Plyr { const toggle = is.boolean(input) ? input : !this.playing; if (toggle) { - this.play(); - } else { - this.pause(); + return this.play(); } + + return this.pause(); } /** @@ -441,7 +442,7 @@ class Plyr { * @param {Number} seekTime - how far to rewind in seconds. Defaults to the config.seekTime */ rewind(seekTime) { - this.currentTime = this.currentTime - (is.number(seekTime) ? seekTime : this.config.seekTime); + this.currentTime -= is.number(seekTime) ? seekTime : this.config.seekTime; } /** @@ -449,7 +450,7 @@ class Plyr { * @param {Number} seekTime - how far to fast forward in seconds. Defaults to the config.seekTime */ forward(seekTime) { - this.currentTime = this.currentTime + (is.number(seekTime) ? seekTime : this.config.seekTime); + this.currentTime += is.number(seekTime) ? seekTime : this.config.seekTime; } /** diff --git a/src/js/plyr.polyfilled.js b/src/js/plyr.polyfilled.js index 0f1e7e25..ca8c3290 100644 --- a/src/js/plyr.polyfilled.js +++ b/src/js/plyr.polyfilled.js @@ -1,6 +1,6 @@ // ========================================================================== // Plyr Polyfilled Build -// plyr.js v3.5.3 +// plyr.js v3.5.10 // https://github.com/sampotts/plyr // License: The MIT License (MIT) // ========================================================================== diff --git a/src/js/source.js b/src/js/source.js index 0173cc9e..cb806746 100644 --- a/src/js/source.js +++ b/src/js/source.js @@ -5,6 +5,7 @@ import { providers } from './config/types'; import html5 from './html5'; import media from './media'; +import PreviewThumbnails from './plugins/preview-thumbnails'; import support from './support'; import ui from './ui'; import { createElement, insertElement, removeElement } from './utils/elements'; @@ -130,9 +131,20 @@ const source = { this.media.load(); } - // Reload thumbnails - if (this.previewThumbnails) { - this.previewThumbnails.load(); + // Update previewThumbnails config & reload plugin + if (!is.empty(input.previewThumbnails)) { + Object.assign(this.config.previewThumbnails, input.previewThumbnails); + + // Cleanup previewThumbnails plugin if it was loaded + if (this.previewThumbnails && this.previewThumbnails.loaded) { + this.previewThumbnails.destroy(); + this.previewThumbnails = null; + } + + // Create new instance if it is still enabled + if (this.config.previewThumbnails.enabled) { + this.previewThumbnails = new PreviewThumbnails(this); + } } // Update the fullscreen support diff --git a/src/js/ui.js b/src/js/ui.js index df52eb64..32db6ae7 100644 --- a/src/js/ui.js +++ b/src/js/ui.js @@ -10,7 +10,7 @@ import { getElement, toggleClass } from './utils/elements'; import { ready, triggerEvent } from './utils/events'; import i18n from './utils/i18n'; import is from './utils/is'; -import loadImage from './utils/loadImage'; +import loadImage from './utils/load-image'; const ui = { addStyleHook() { @@ -172,6 +172,11 @@ const ui = { // Set property synchronously to respect the call order this.media.setAttribute('poster', poster); + // HTML5 uses native poster attribute + if (this.isHTML5) { + return Promise.resolve(poster); + } + // Wait until ui is ready return ( ready @@ -198,7 +203,9 @@ const ui = { // Reset backgroundSize as well (since it can be set to "cover" for padded thumbnails for youtube) backgroundSize: '', }); + ui.togglePoster.call(this, true); + return poster; }) ); @@ -214,6 +221,7 @@ const ui = { // Set state Array.from(this.elements.buttons.play || []).forEach(target => { Object.assign(target, { pressed: this.playing }); + target.setAttribute('aria-label', i18n.get(this.playing ? 'pause' : 'play', this.config)); }); // Only update controls on non timeupdate events diff --git a/src/js/utils/animation.js b/src/js/utils/animation.js index 6b950b61..3f721b5a 100644 --- a/src/js/utils/animation.js +++ b/src/js/utils/animation.js @@ -2,7 +2,6 @@ // Animation utils // ========================================================================== -import { toggleHidden } from './elements'; import is from './is'; export const transitionEndEvent = (() => { @@ -21,14 +20,19 @@ export const transitionEndEvent = (() => { })(); // Force repaint of element -export function repaint(element) { +export function repaint(element, delay) { setTimeout(() => { try { - toggleHidden(element, true); - element.offsetHeight; // eslint-disable-line - toggleHidden(element, false); + // eslint-disable-next-line no-param-reassign + element.hidden = true; + + // eslint-disable-next-line no-unused-expressions + element.offsetHeight; + + // eslint-disable-next-line no-param-reassign + element.hidden = false; } catch (e) { // Do nothing } - }, 0); + }, delay); } diff --git a/src/js/utils/elements.js b/src/js/utils/elements.js index 98b44f13..bdf18bfd 100644 --- a/src/js/utils/elements.js +++ b/src/js/utils/elements.js @@ -2,7 +2,6 @@ // Element utils // ========================================================================== -import { toggleListener } from './events'; import is from './is'; import { extend } from './objects'; @@ -192,11 +191,8 @@ export function toggleHidden(element, hidden) { hide = !element.hidden; } - if (hide) { - element.setAttribute('hidden', ''); - } else { - element.removeAttribute('hidden'); - } + // eslint-disable-next-line no-param-reassign + element.hidden = hide; } // Mirror Element.classList.toggle, with IE compatibility for "force" argument @@ -225,7 +221,7 @@ export function hasClass(element, className) { // Element matches selector export function matches(element, selector) { - const prototype = { Element }; + const {prototype} = Element; function match() { return Array.from(document.querySelectorAll(selector)).includes(this); @@ -251,39 +247,6 @@ export function getElement(selector) { return this.elements.container.querySelector(selector); } -// Trap focus inside container -export function trapFocus(element = null, toggle = false) { - if (!is.element(element)) { - return; - } - - const focusable = getElements.call(this, 'button:not(:disabled), input:not(:disabled), [tabindex]'); - const first = focusable[0]; - const last = focusable[focusable.length - 1]; - - const trap = event => { - // Bail if not tab key or not fullscreen - if (event.key !== 'Tab' || event.keyCode !== 9) { - return; - } - - // Get the current focused element - const focused = document.activeElement; - - if (focused === last && !event.shiftKey) { - // Move focus to first element that can be tabbed if Shift isn't used - first.focus(); - event.preventDefault(); - } else if (focused === first && event.shiftKey) { - // Move focus to last element that can be tabbed if Shift is used - last.focus(); - event.preventDefault(); - } - }; - - toggleListener.call(this, this.elements.container, 'keydown', trap, toggle, false); -} - // Set focus and tab focus class export function setFocus(element = null, tabFocus = false) { if (!is.element(element)) { diff --git a/src/js/utils/events.js b/src/js/utils/events.js index 87c35d26..31571b2d 100644 --- a/src/js/utils/events.js +++ b/src/js/utils/events.js @@ -90,9 +90,7 @@ export function triggerEvent(element, type = '', bubbles = false, detail = {}) { // Create and dispatch the event const event = new CustomEvent(type, { bubbles, - detail: Object.assign({}, detail, { - plyr: this, - }), + detail: { ...detail, plyr: this,}, }); // Dispatch the event diff --git a/src/js/utils/is.js b/src/js/utils/is.js index b005cd31..24f176cc 100644 --- a/src/js/utils/is.js +++ b/src/js/utils/is.js @@ -19,7 +19,7 @@ const isEvent = input => instanceOf(input, Event); const isKeyboardEvent = input => instanceOf(input, KeyboardEvent); const isCue = input => instanceOf(input, window.TextTrackCue) || instanceOf(input, window.VTTCue); const isTrack = input => instanceOf(input, TextTrack) || (!isNullOrUndefined(input) && isString(input.kind)); -const isPromise = input => instanceOf(input, Promise); +const isPromise = input => instanceOf(input, Promise) && isFunction(input.then); const isEmpty = input => isNullOrUndefined(input) || diff --git a/src/js/utils/loadImage.js b/src/js/utils/load-image.js index 8acd2496..8acd2496 100644 --- a/src/js/utils/loadImage.js +++ b/src/js/utils/load-image.js diff --git a/src/js/utils/loadScript.js b/src/js/utils/load-script.js index 81ae36f4..81ae36f4 100644 --- a/src/js/utils/loadScript.js +++ b/src/js/utils/load-script.js diff --git a/src/js/utils/loadSprite.js b/src/js/utils/load-sprite.js index fe4add00..fe4add00 100644 --- a/src/js/utils/loadSprite.js +++ b/src/js/utils/load-sprite.js diff --git a/src/js/utils/promise.js b/src/js/utils/promise.js new file mode 100644 index 00000000..f45b46ab --- /dev/null +++ b/src/js/utils/promise.js @@ -0,0 +1,14 @@ +import is from './is'; +/** + * Silence a Promise-like object. + * This is useful for avoiding non-harmful, but potentially confusing "uncaught + * play promise" rejection error messages. + * @param {Object} value An object that may or may not be `Promise`-like. + */ +export function silencePromise(value) { + if (is.promise(value)) { + value.then(null, () => {}); + } +} + +export default { silencePromise }; diff --git a/src/js/utils/style.js b/src/js/utils/style.js index 6f3069c9..17a033fe 100644 --- a/src/js/utils/style.js +++ b/src/js/utils/style.js @@ -27,15 +27,8 @@ export function reduceAspectRatio(ratio) { } export function getAspectRatio(input) { - const parse = ratio => { - if (!validateRatio(ratio)) { - return null; - } - - return ratio.split(':').map(Number); - }; - - // Provided ratio + const parse = ratio => (validateRatio(ratio) ? ratio.split(':').map(Number) : null); + // Try provided ratio let ratio = parse(input); // Get from config @@ -63,11 +56,12 @@ export function setAspectRatio(input) { return {}; } + const { wrapper } = this.elements; const ratio = getAspectRatio.call(this, input); const [w, h] = is.array(ratio) ? ratio : [0, 0]; const padding = (100 / w) * h; - this.elements.wrapper.style.paddingBottom = `${padding}%`; + wrapper.style.paddingBottom = `${padding}%`; // For Vimeo we have an extra <div> to hide the standard controls and UI if (this.isVimeo && this.supported.ui) { @@ -75,7 +69,7 @@ export function setAspectRatio(input) { const offset = (height - padding) / (height / 50); this.media.style.transform = `translateY(-${offset}%)`; } else if (this.isHTML5) { - this.elements.wrapper.classList.toggle(this.config.classNames.videoFixedRatio, ratio !== null); + wrapper.classList.toggle(this.config.classNames.videoFixedRatio, ratio !== null); } return { padding, ratio }; diff --git a/src/js/utils/time.js b/src/js/utils/time.js index ffca88b2..17228de5 100644 --- a/src/js/utils/time.js +++ b/src/js/utils/time.js @@ -13,7 +13,7 @@ export const getSeconds = value => Math.trunc(value % 60, 10); export function formatTime(time = 0, displayHours = false, inverted = false) { // Bail if the value isn't a number if (!is.number(time)) { - return formatTime(null, displayHours, inverted); + return formatTime(undefined, displayHours, inverted); } // Format time component to add leading zero |