Source: ui/text_selection.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.TextSelection');
  7. goog.require('shaka.ui.Controls');
  8. goog.require('shaka.ui.Enums');
  9. goog.require('shaka.ui.LanguageUtils');
  10. goog.require('shaka.ui.Locales');
  11. goog.require('shaka.ui.Localization');
  12. goog.require('shaka.ui.OverflowMenu');
  13. goog.require('shaka.ui.SettingsMenu');
  14. goog.require('shaka.ui.Utils');
  15. goog.require('shaka.util.Dom');
  16. goog.require('shaka.util.FakeEvent');
  17. goog.requireType('shaka.ui.Controls');
  18. /**
  19. * @extends {shaka.ui.SettingsMenu}
  20. * @final
  21. * @export
  22. */
  23. shaka.ui.TextSelection = class extends shaka.ui.SettingsMenu {
  24. /**
  25. * @param {!HTMLElement} parent
  26. * @param {!shaka.ui.Controls} controls
  27. */
  28. constructor(parent, controls) {
  29. super(parent,
  30. controls, shaka.ui.Enums.MaterialDesignIcons.CLOSED_CAPTIONS);
  31. this.button.classList.add('shaka-caption-button');
  32. this.button.classList.add('shaka-tooltip-status');
  33. this.menu.classList.add('shaka-text-languages');
  34. if (this.player && this.player.isTextTrackVisible()) {
  35. this.button.ariaPressed = 'true';
  36. } else {
  37. this.button.ariaPressed = 'false';
  38. }
  39. this.addOffOption_();
  40. this.eventManager.listen(
  41. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  42. this.updateLocalizedStrings_();
  43. // If captions/subtitles are off, this string needs localization.
  44. // TODO: is there a more efficient way of updating just the strings
  45. // we need instead of running the whole language update?
  46. this.updateTextLanguages_();
  47. });
  48. this.eventManager.listen(
  49. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  50. this.updateLocalizedStrings_();
  51. // If captions/subtitles are off, this string needs localization.
  52. // TODO: is there a more efficient way of updating just the strings
  53. // we need instead of running the whole language update?
  54. this.updateTextLanguages_();
  55. });
  56. this.eventManager.listen(this.player, 'texttrackvisibility', () => {
  57. this.onCaptionStateChange_();
  58. this.updateTextLanguages_();
  59. });
  60. this.eventManager.listen(this.player, 'textchanged', () => {
  61. this.updateTextLanguages_();
  62. });
  63. this.eventManager.listen(this.player, 'trackschanged', () => {
  64. this.onTracksChanged_();
  65. });
  66. // Initialize caption state with a fake event.
  67. this.onCaptionStateChange_();
  68. // Set up all the strings in the user's preferred language.
  69. this.updateLocalizedStrings_();
  70. this.updateTextLanguages_();
  71. this.onTracksChanged_();
  72. }
  73. /**
  74. * @private
  75. */
  76. addOffOption_() {
  77. const off = shaka.util.Dom.createButton();
  78. off.ariaSelected = 'true';
  79. this.menu.appendChild(off);
  80. off.appendChild(shaka.ui.Utils.checkmarkIcon());
  81. /** @private {!HTMLElement} */
  82. this.captionsOffSpan_ = shaka.util.Dom.createHTMLElement('span');
  83. this.captionsOffSpan_.classList.add('shaka-auto-span');
  84. off.appendChild(this.captionsOffSpan_);
  85. }
  86. /** @private */
  87. onCaptionStateChange_() {
  88. if (this.player.isTextTrackVisible()) {
  89. this.icon.textContent =
  90. shaka.ui.Enums.MaterialDesignIcons.CLOSED_CAPTIONS;
  91. this.button.ariaPressed = 'true';
  92. } else {
  93. this.icon.textContent =
  94. shaka.ui.Enums.MaterialDesignIcons.CLOSED_CAPTIONS_OFF;
  95. this.button.ariaPressed = 'false';
  96. }
  97. this.controls.dispatchEvent(
  98. new shaka.util.FakeEvent('captionselectionupdated'));
  99. }
  100. /** @private */
  101. updateTextLanguages_() {
  102. const tracks = this.player.getTextTracks();
  103. shaka.ui.LanguageUtils.updateTracks(tracks, this.menu,
  104. (track) => this.onTextTrackSelected_(track),
  105. // Don't mark current text language as chosen unless captions are
  106. // enabled
  107. this.player.isTextTrackVisible(),
  108. this.currentSelection,
  109. this.localization,
  110. this.controls.getConfig().trackLabelFormat);
  111. // Add the Off button
  112. const offButton = shaka.util.Dom.createButton();
  113. offButton.classList.add('shaka-turn-captions-off-button');
  114. this.eventManager.listen(offButton, 'click', () => {
  115. this.player.setTextTrackVisibility(false);
  116. this.updateTextLanguages_();
  117. });
  118. offButton.appendChild(this.captionsOffSpan_);
  119. this.menu.appendChild(offButton);
  120. if (!this.player.isTextTrackVisible()) {
  121. offButton.ariaSelected = 'true';
  122. offButton.appendChild(shaka.ui.Utils.checkmarkIcon());
  123. this.captionsOffSpan_.classList.add('shaka-chosen-item');
  124. this.currentSelection.textContent =
  125. this.localization.resolve(shaka.ui.Locales.Ids.OFF);
  126. }
  127. this.button.setAttribute('shaka-status', this.currentSelection.textContent);
  128. shaka.ui.Utils.focusOnTheChosenItem(this.menu);
  129. this.controls.dispatchEvent(
  130. new shaka.util.FakeEvent('captionselectionupdated'));
  131. }
  132. /**
  133. * @param {!shaka.extern.Track} track
  134. * @return {!Promise}
  135. * @private
  136. */
  137. async onTextTrackSelected_(track) {
  138. // setTextTrackVisibility should be called after selectTextTrack.
  139. // selectTextTrack sets a text stream, and setTextTrackVisiblity(true)
  140. // will set a text stream if it isn't already set. Consequently, reversing
  141. // the order of these calls makes two languages display simultaneously
  142. // if captions are turned off -> on in a different language.
  143. this.player.selectTextTrack(track);
  144. await this.player.setTextTrackVisibility(true);
  145. }
  146. /**
  147. * @private
  148. */
  149. updateLocalizedStrings_() {
  150. const LocIds = shaka.ui.Locales.Ids;
  151. this.button.ariaLabel = this.localization.resolve(LocIds.CAPTIONS);
  152. this.backButton.ariaLabel = this.localization.resolve(LocIds.BACK);
  153. this.nameSpan.textContent =
  154. this.localization.resolve(LocIds.CAPTIONS);
  155. this.backSpan.textContent =
  156. this.localization.resolve(LocIds.CAPTIONS);
  157. this.captionsOffSpan_.textContent =
  158. this.localization.resolve(LocIds.OFF);
  159. }
  160. /** @private */
  161. onTracksChanged_() {
  162. const hasText = this.player.getTextTracks().length > 0;
  163. shaka.ui.Utils.setDisplay(this.button, hasText);
  164. this.updateTextLanguages_();
  165. }
  166. };
  167. /**
  168. * @implements {shaka.extern.IUIElement.Factory}
  169. * @final
  170. */
  171. shaka.ui.TextSelection.Factory = class {
  172. /** @override */
  173. create(rootElement, controls) {
  174. return new shaka.ui.TextSelection(rootElement, controls);
  175. }
  176. };
  177. shaka.ui.OverflowMenu.registerElement(
  178. 'captions', new shaka.ui.TextSelection.Factory());
  179. shaka.ui.Controls.registerElement(
  180. 'captions', new shaka.ui.TextSelection.Factory());