aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/captions.js
diff options
context:
space:
mode:
authorAlbin Larsson <mail@albinlarsson.com>2018-05-27 05:08:18 +0200
committerAlbin Larsson <mail@albinlarsson.com>2018-05-28 05:44:54 +0200
commit7aad747c25b07fedf4a4fc75095c560ea3c9899c (patch)
tree26e05dde93ce7fb04355250111cfde98913d7e3d /src/js/captions.js
parente9684c20217300c15fc92a71c4c4a640c4784083 (diff)
downloadplyr-7aad747c25b07fedf4a4fc75095c560ea3c9899c.tar.lz
plyr-7aad747c25b07fedf4a4fc75095c560ea3c9899c.tar.xz
plyr-7aad747c25b07fedf4a4fc75095c560ea3c9899c.zip
Optimize captions code reused and ensure captionsenabled/captionsdisabled
will be sent on initial setup
Diffstat (limited to 'src/js/captions.js')
-rw-r--r--src/js/captions.js64
1 files changed, 20 insertions, 44 deletions
diff --git a/src/js/captions.js b/src/js/captions.js
index df717351..296888b2 100644
--- a/src/js/captions.js
+++ b/src/js/captions.js
@@ -27,17 +27,6 @@ const captions = {
this.captions.language = this.config.captions.language.toLowerCase();
}
- // Set captions enabled state if not set
- if (!utils.is.boolean(this.captions.active)) {
- const active = this.storage.get('captions');
-
- if (utils.is.boolean(active)) {
- this.captions.active = active;
- } else {
- this.captions.active = this.config.captions.active;
- }
- }
-
// Only Vimeo and HTML5 video supported at this point
if (!this.isVideo || this.isYouTube || (this.isHTML5 && !support.textTracks)) {
// Clear menu and hide
@@ -55,17 +44,6 @@ const captions = {
utils.insertAfter(this.elements.captions, this.elements.wrapper);
}
- // Set the class hook
- utils.toggleClass(this.elements.container, this.config.classNames.captions.enabled, !utils.is.empty(captions.getTracks.call(this)));
-
- // Get tracks
- const tracks = captions.getTracks.call(this);
-
- // If no caption file exists, hide container for caption text
- if (utils.is.empty(tracks)) {
- return;
- }
-
// Get browser info
const browser = utils.getBrowser();
@@ -94,14 +72,30 @@ const captions = {
});
}
+ // Try to load the value from storage
+ let active = this.storage.get('captions');
+
+ // Otherwise fall back to the default config
+ if (!utils.is.boolean(active)) {
+ ({ active } = this.config.captions);
+ }
+
+ // Set toggled state
+ this.toggleCaptions(active);
+
+ // Update available languages in list
+ captions.update.call(this);
+ },
+
+ update() {
// Set language
captions.setLanguage.call(this);
- // Enable UI
- captions.show.call(this);
+ // Toggle the class hooks
+ utils.toggleClass(this.elements.container, this.config.classNames.captions.enabled, !utils.is.empty(captions.getTracks.call(this)));
- // Set available languages in list
- if (utils.is.array(this.config.controls) && this.config.controls.includes('settings') && this.config.settings.includes('captions')) {
+ // Update available languages in list
+ if ((this.config.controls || []).includes('settings') && this.config.settings.includes('captions')) {
controls.setCaptionsMenu.call(this);
}
},
@@ -247,24 +241,6 @@ const captions = {
this.debug.warn('No captions element to render to');
}
},
-
- // Display captions container and button (for initialization)
- show() {
- // Try to load the value from storage
- let active = this.storage.get('captions');
-
- // Otherwise fall back to the default config
- if (!utils.is.boolean(active)) {
- ({ active } = this.config.captions);
- } else {
- this.captions.active = active;
- }
-
- if (active) {
- utils.toggleClass(this.elements.container, this.config.classNames.captions.active, true);
- utils.toggleState(this.elements.buttons.captions, true);
- }
- },
};
export default captions;