aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/captions.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-04-25 07:38:17 +1000
committerGitHub <noreply@github.com>2018-04-25 07:38:17 +1000
commitf13260c10aad1e7e95f1d13a31c7f362c674ddb6 (patch)
treead4e351f2d3603325e2d388bdc64df8f15cd98b8 /src/js/captions.js
parente138e6d51e3cd85d85ccbd32674f75a63a3771ef (diff)
parentf1b275aedce897b42f025afac7a0937dc5871235 (diff)
downloadplyr-f13260c10aad1e7e95f1d13a31c7f362c674ddb6.tar.lz
plyr-f13260c10aad1e7e95f1d13a31c7f362c674ddb6.tar.xz
plyr-f13260c10aad1e7e95f1d13a31c7f362c674ddb6.zip
Merge pull request #919 from sampotts/master
Merge back
Diffstat (limited to 'src/js/captions.js')
-rw-r--r--src/js/captions.js51
1 files changed, 45 insertions, 6 deletions
diff --git a/src/js/captions.js b/src/js/captions.js
index c8bc5833..c6618fda 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
@@ -206,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');