Merge branch 'develop' of github.com:sampotts/plyr into develop

This commit is contained in:
Sam Potts 2019-06-01 19:29:21 +10:00
commit 5d699d5f47

View File

@ -47,6 +47,8 @@ function getHost(config) {
return undefined; return undefined;
} }
let onYouTubeReadyCallbacks;
const youtube = { const youtube = {
setup() { setup() {
// Add embed class for responsive // Add embed class for responsive
@ -56,26 +58,32 @@ const youtube = {
if (is.object(window.YT) && is.function(window.YT.Player)) { if (is.object(window.YT) && is.function(window.YT.Player)) {
youtube.ready.call(this); youtube.ready.call(this);
} else { } else {
// Load the API
loadScript(this.config.urls.youtube.sdk).catch(error => {
this.debug.warn('YouTube API failed to load', error);
});
// Setup callback for the API if (!onYouTubeReadyCallbacks) {
// YouTube has it's own system of course... const oldYouTubeIframeAPIReady = window.onYouTubeIframeAPIReady;
window.onYouTubeReadyCallbacks = window.onYouTubeReadyCallbacks || []; // Load the API
onYouTubeReadyCallbacks = [];
// Set callback to process queue
window.onYouTubeIframeAPIReady = () => {
if (oldYouTubeIframeAPIReady && is.function(oldYouTubeIframeAPIReady)) {
oldYouTubeIframeAPIReady();
}
window.onYouTubeReadyCallbacks.forEach(callback => {
callback();
});
};
loadScript(this.config.urls.youtube.sdk).catch(error => {
this.debug.warn('YouTube API failed to load', error);
});
}
// Add to queue // Add to queue
window.onYouTubeReadyCallbacks.push(() => { onYouTubeReadyCallbacks.push(() => {
youtube.ready.call(this); youtube.ready.call(this);
}); });
// Set callback to process queue
window.onYouTubeIframeAPIReady = () => {
window.onYouTubeReadyCallbacks.forEach(callback => {
callback();
});
};
} }
}, },
@ -108,7 +116,7 @@ const youtube = {
ready() { ready() {
const player = this; const player = this;
// Ignore already setup (race condition) // Ignore already setup (race condition)
const currentId = player.media.getAttribute('id'); const currentId = player.media && player.media.getAttribute('id');
if (!is.empty(currentId) && currentId.startsWith('youtube-')) { if (!is.empty(currentId) && currentId.startsWith('youtube-')) {
return; return;
} }