diff options
author | Albin Larsson <mail@albinlarsson.com> | 2018-05-15 05:16:06 +0200 |
---|---|---|
committer | Albin Larsson <mail@albinlarsson.com> | 2018-05-15 16:21:36 +0200 |
commit | 16c3a7d9e5be8ed2ffbbcee1c786b88d1cecc4cd (patch) | |
tree | 2620f3e4572e08b69c18a8adab654d39f6582585 /src/js/ui.js | |
parent | 90d5b48845661ce99a204354f93fbbbc7a19f100 (diff) | |
download | plyr-16c3a7d9e5be8ed2ffbbcee1c786b88d1cecc4cd.tar.lz plyr-16c3a7d9e5be8ed2ffbbcee1c786b88d1cecc4cd.tar.xz plyr-16c3a7d9e5be8ed2ffbbcee1c786b88d1cecc4cd.zip |
Rewrite ui.setPoster to check that images arent broken or youtube fallback images. Only show poster element when valid
Diffstat (limited to 'src/js/ui.js')
-rw-r--r-- | src/js/ui.js | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/src/js/ui.js b/src/js/ui.js index 2347b5c8..50764a86 100644 --- a/src/js/ui.js +++ b/src/js/ui.js @@ -105,8 +105,10 @@ const ui = { // Set the title ui.setTitle.call(this); - // Set the poster image - ui.setPoster.call(this); + // Assure the poster image is set, if the property was added before the UI was created + if (this.poster) { + ui.setPoster.call(this, this.poster); + } }, // Setup aria attribute for play and iframe title @@ -146,15 +148,34 @@ const ui = { } }, - // Set the poster image - setPoster() { - if (!utils.is.element(this.elements.poster) || utils.is.empty(this.poster)) { - return; + // Toggle poster + togglePoster(enable) { + utils.toggleClass(this.elements.container, this.config.classNames.posterEnabled, enable); + }, + + // Set the poster image (async) + setPoster(poster) { + // Set property regardless of validity + this.media.setAttribute('poster', poster); + + // Bail if element is missing + if (!utils.is.element(this.elements.poster)) { + return Promise.reject(); } - // Set the inline style - const posters = this.poster.split(','); - this.elements.poster.style.backgroundImage = posters.map(p => `url('${p}')`).join(','); + // Load the image, and set poster if successful + const loadPromise = utils.loadImage(poster) + .then(() => { + this.elements.poster.style.backgroundImage = `url('${poster}')`; + ui.togglePoster.call(this, true); + return poster; + }); + + // Hide the element if the poster can't be loaded (otherwise it will just be a black element covering the video) + loadPromise.catch(() => ui.togglePoster.call(this, false)); + + // Return the promise so the caller can use it as well + return loadPromise; }, // Check playing state |