diff options
author | Sam Potts <sam@potts.es> | 2018-04-17 22:49:28 +1000 |
---|---|---|
committer | Sam Potts <sam@potts.es> | 2018-04-17 22:49:28 +1000 |
commit | 46fe3eecff478a80636f538a9f843bb0f21f4802 (patch) | |
tree | 65dc653a626b466e02e5f4133cf10d47c08ec34f /src | |
parent | 3061a701d57ab04a3b48ebe5174e9c9bef8d6d7d (diff) | |
download | plyr-46fe3eecff478a80636f538a9f843bb0f21f4802.tar.lz plyr-46fe3eecff478a80636f538a9f843bb0f21f4802.tar.xz plyr-46fe3eecff478a80636f538a9f843bb0f21f4802.zip |
Fixed bug for captions with no srclang and labels and improved logic (fixes #875)
Diffstat (limited to 'src')
-rw-r--r-- | src/js/captions.js | 46 | ||||
-rw-r--r-- | src/js/controls.js | 72 | ||||
-rw-r--r-- | src/js/defaults.js | 1 | ||||
-rw-r--r-- | src/js/listeners.js | 1 | ||||
-rw-r--r-- | src/js/plyr.js | 19 |
5 files changed, 100 insertions, 39 deletions
diff --git a/src/js/captions.js b/src/js/captions.js index c8bc5833..45efcf40 100644 --- a/src/js/captions.js +++ b/src/js/captions.js @@ -6,6 +6,7 @@ import support from './support'; import utils from './utils'; import controls from './controls'; +import i18n from './i18n'; const captions = { // Setup captions @@ -46,6 +47,7 @@ const captions = { return; } + // Inject the container if (!utils.is.element(this.elements.captions)) { this.elements.captions = utils.createElement('div', utils.getAttributesFromSelector(this.config.selectors.captions)); @@ -148,7 +150,49 @@ const captions = { // Get the current track for the current language getCurrentTrack() { - return captions.getTracks.call(this).find(track => track.language.toLowerCase() === this.language); + const tracks = captions.getTracks.call(this); + + if (!tracks.length) { + return null; + } + + // Get track based on current language + let track = tracks.find(track => track.language.toLowerCase() === this.language); + + // Get the <track> with default attribute + if (!track) { + track = utils.getElement.call(this, 'track[default]'); + } + + // Get the first track + if (!track) { + [track] = tracks; + } + + return track; + }, + + // Get UI label for track + getLabel(track) { + let currentTrack = track; + + if (!utils.is.track(currentTrack) && support.textTracks && this.captions.active) { + currentTrack = captions.getCurrentTrack.call(this); + } + + if (utils.is.track(currentTrack)) { + if (!utils.is.empty(currentTrack.label)) { + return currentTrack.label; + } + + if (!utils.is.empty(currentTrack.language)) { + return track.language.toUpperCase(); + } + + return i18n.get('enabled', this.config); + } + + return i18n.get('disabled', this.config); }, // Display active caption if it contains text diff --git a/src/js/controls.js b/src/js/controls.js index 160c3665..c44cd13a 100644 --- a/src/js/controls.js +++ b/src/js/controls.js @@ -456,6 +456,9 @@ const controls = { const toggle = !utils.is.empty(this.options.quality) && this.options.quality.length > 1; controls.toggleTab.call(this, type, toggle); + // Check if we need to toggle the parent + controls.checkMenu.call(this); + // If we're hiding, nothing more to do if (!toggle) { return; @@ -495,13 +498,15 @@ const controls = { }; // Sort options by the config and then render options - this.options.quality.sort((a, b) => { - const sorting = this.config.quality.options; - return sorting.indexOf(a) > sorting.indexOf(b) ? 1 : -1; - }).forEach(quality => { - const label = controls.getLabel.call(this, 'quality', quality); - controls.createMenuItem.call(this, quality, list, type, label, getBadge(quality)); - }); + this.options.quality + .sort((a, b) => { + const sorting = this.config.quality.options; + return sorting.indexOf(a) > sorting.indexOf(b) ? 1 : -1; + }) + .forEach(quality => { + const label = controls.getLabel.call(this, 'quality', quality); + controls.createMenuItem.call(this, quality, list, type, label, getBadge(quality)); + }); controls.updateSetting.call(this, type, list); }, @@ -517,10 +522,11 @@ const controls = { if (utils.is.number(value)) { return `${value}p`; } + return utils.toTitleCase(value); case 'captions': - return controls.getLanguage.call(this); + return captions.getLabel.call(this); default: return null; @@ -535,7 +541,16 @@ const controls = { switch (setting) { case 'captions': - value = this.captions.active ? this.captions.language : i18n.get('disabled', this.config); + if (this.captions.active) { + if (this.options.captions.length > 2 || !this.options.captions.some(lang => lang === 'enabled')) { + value = this.captions.language; + } else { + value = 'enabled'; + } + } else { + value = ''; + } + break; default: @@ -567,16 +582,13 @@ const controls = { } // Update the label - if (!utils.is.empty(value)) { - const label = this.elements.settings.tabs[setting].querySelector(`.${this.config.classNames.menu.value}`); - label.innerHTML = controls.getLabel.call(this, setting, value); - } + const label = this.elements.settings.tabs[setting].querySelector(`.${this.config.classNames.menu.value}`); + label.innerHTML = controls.getLabel.call(this, setting, value); - // Find the radio option + // Find the radio option and check it const target = list && list.querySelector(`input[value="${value}"]`); if (utils.is.element(target)) { - // Check it target.checked = true; } }, @@ -627,21 +639,7 @@ const controls = { // Get current selected caption language // TODO: rework this to user the getter in the API? - getLanguage() { - if (!this.supported.ui) { - return null; - } - - if (support.textTracks && captions.getTracks.call(this).length && this.captions.active) { - const currentTrack = captions.getCurrentTrack.call(this); - - if (utils.is.track(currentTrack)) { - return currentTrack.label; - } - } - return i18n.get('disabled', this.config); - }, // Set a list of available captions languages setCaptionsMenu() { @@ -656,6 +654,9 @@ const controls = { // Empty the menu utils.emptyElement(list); + // Check if we need to toggle the parent + controls.checkMenu.call(this); + // If there's no captions, bail if (!toggle) { return; @@ -663,8 +664,8 @@ const controls = { // Re-map the tracks into just the data we need const tracks = captions.getTracks.call(this).map(track => ({ - language: track.language, - label: !utils.is.empty(track.label) ? track.label : track.language.toUpperCase(), + language: !utils.is.empty(track.language) ? track.language : 'enabled', + label: captions.getLabel.call(this, track), })); // Add the "Disabled" option to turn off captions @@ -680,12 +681,15 @@ const controls = { track.language, list, 'language', - track.label || track.language, - controls.createBadge.call(this, track.language.toUpperCase()), + track.label, + track.language !== 'enabled' ? controls.createBadge.call(this, track.language.toUpperCase()) : null, track.language.toLowerCase() === this.captions.language.toLowerCase(), ); }); + // Store reference + this.options.captions = tracks.map(track => track.language); + controls.updateSetting.call(this, type, list); }, @@ -1211,7 +1215,7 @@ const controls = { seektime: this.config.seekTime, speed: this.speed, quality: this.quality, - captions: controls.getLanguage.call(this), + captions: captions.getLabel.call(this), // TODO: Looping // loop: 'None', }); diff --git a/src/js/defaults.js b/src/js/defaults.js index ff4316a9..b21b546f 100644 --- a/src/js/defaults.js +++ b/src/js/defaults.js @@ -185,6 +185,7 @@ const defaults = { all: 'All', reset: 'Reset', disabled: 'Disabled', + enabled: 'Enabled', advertisement: 'Ad', }, diff --git a/src/js/listeners.js b/src/js/listeners.js index c02e3388..35ee5f2a 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -2,7 +2,6 @@ // Plyr Event Listeners // ========================================================================== -import support from './support'; import utils from './utils'; import controls from './controls'; import ui from './ui'; diff --git a/src/js/plyr.js b/src/js/plyr.js index 04a68d02..200189b3 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -97,6 +97,7 @@ class Plyr { this.options = { speed: [], quality: [], + captions: [], }; // Debugging @@ -875,17 +876,29 @@ class Plyr { return; } - // Toggle captions based on input - this.toggleCaptions(!utils.is.empty(input)); - // If empty string is passed, assume disable captions if (utils.is.empty(input)) { + this.toggleCaptions(false); return; } // Normalize const language = input.toLowerCase(); + // Check for support + if (!this.options.captions.includes(language)) { + this.debug.warn(`Unsupported language option: ${language}`); + return; + } + + // Ensure captions are enabled + this.toggleCaptions(true); + + // Enabled only + if (language === 'enabled') { + return; + } + // If nothing to change, bail if (this.language === language) { return; |