From 46fe3eecff478a80636f538a9f843bb0f21f4802 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Tue, 17 Apr 2018 22:49:28 +1000 Subject: Fixed bug for captions with no srclang and labels and improved logic (fixes #875) --- src/js/captions.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'src/js/captions.js') 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 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 -- cgit v1.2.3 From 6d79b8cd4cb3b256ee5616ffc196c87cf74bf6f8 Mon Sep 17 00:00:00 2001 From: Albin Larsson Date: Tue, 17 Apr 2018 18:56:58 +0200 Subject: Don't require captions toggle button to be enabled in order to show captions --- src/js/captions.js | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/js/captions.js') diff --git a/src/js/captions.js b/src/js/captions.js index 45efcf40..c6618fda 100644 --- a/src/js/captions.js +++ b/src/js/captions.js @@ -250,11 +250,6 @@ const captions = { // Display captions container and button (for initialization) show() { - // If there's no caption toggle, bail - if (!utils.is.element(this.elements.buttons.captions)) { - return; - } - // Try to load the value from storage let active = this.storage.get('captions'); -- cgit v1.2.3