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, 'loading', () => {
  57. this.onTracksChanged_();
  58. });
  59. this.eventManager.listen(this.player, 'texttrackvisibility', () => {
  60. this.onCaptionStateChange_();
  61. this.updateTextLanguages_();
  62. });
  63. this.eventManager.listen(this.player, 'textchanged', () => {
  64. this.updateTextLanguages_();
  65. });
  66. this.eventManager.listen(this.player, 'trackschanged', () => {
  67. this.onTracksChanged_();
  68. });
  69. // Initialize caption state with a fake event.
  70. this.onCaptionStateChange_();
  71. // Set up all the strings in the user's preferred language.
  72. this.updateLocalizedStrings_();
  73. this.updateTextLanguages_();
  74. this.onTracksChanged_();
  75. }
  76. /**
  77. * @private
  78. */
  79. addOffOption_() {
  80. const off = shaka.util.Dom.createButton();
  81. off.ariaSelected = 'true';
  82. this.menu.appendChild(off);
  83. off.appendChild(shaka.ui.Utils.checkmarkIcon());
  84. /** @private {!HTMLElement} */
  85. this.captionsOffSpan_ = shaka.util.Dom.createHTMLElement('span');
  86. this.captionsOffSpan_.classList.add('shaka-auto-span');
  87. off.appendChild(this.captionsOffSpan_);
  88. }
  89. /** @private */
  90. onCaptionStateChange_() {
  91. if (this.player.isTextTrackVisible()) {
  92. this.icon.textContent =
  93. shaka.ui.Enums.MaterialDesignIcons.CLOSED_CAPTIONS;
  94. this.button.ariaPressed = 'true';
  95. } else {
  96. this.icon.textContent =
  97. shaka.ui.Enums.MaterialDesignIcons.CLOSED_CAPTIONS_OFF;
  98. this.button.ariaPressed = 'false';
  99. }
  100. this.controls.dispatchEvent(
  101. new shaka.util.FakeEvent('captionselectionupdated'));
  102. }
  103. /** @private */
  104. updateTextLanguages_() {
  105. const tracks = this.player.getTextTracks();
  106. shaka.ui.LanguageUtils.updateTextTracks(tracks, this.menu,
  107. (track) => this.onTextTrackSelected_(track),
  108. // Don't mark current text language as chosen unless captions are
  109. // enabled
  110. this.player.isTextTrackVisible(),
  111. this.currentSelection,
  112. this.localization,
  113. this.controls.getConfig().textTrackLabelFormat);
  114. // Add the Off button
  115. const offButton = shaka.util.Dom.createButton();
  116. offButton.classList.add('shaka-turn-captions-off-button');
  117. this.eventManager.listen(offButton, 'click', () => {
  118. this.player.setTextTrackVisibility(false);
  119. this.updateTextLanguages_();
  120. });
  121. offButton.appendChild(this.captionsOffSpan_);
  122. this.menu.appendChild(offButton);
  123. if (!this.player.isTextTrackVisible()) {
  124. offButton.ariaSelected = 'true';
  125. offButton.appendChild(shaka.ui.Utils.checkmarkIcon());
  126. this.captionsOffSpan_.classList.add('shaka-chosen-item');
  127. this.currentSelection.textContent =
  128. this.localization.resolve(shaka.ui.Locales.Ids.OFF);
  129. }
  130. this.button.setAttribute('shaka-status', this.currentSelection.textContent);
  131. shaka.ui.Utils.focusOnTheChosenItem(this.menu);
  132. this.controls.dispatchEvent(
  133. new shaka.util.FakeEvent('captionselectionupdated'));
  134. }
  135. /**
  136. * @param {!shaka.extern.Track} track
  137. * @return {!Promise}
  138. * @private
  139. */
  140. async onTextTrackSelected_(track) {
  141. // setTextTrackVisibility should be called after selectTextTrack.
  142. // selectTextTrack sets a text stream, and setTextTrackVisibility(true)
  143. // will set a text stream if it isn't already set. Consequently, reversing
  144. // the order of these calls makes two languages display simultaneously
  145. // if captions are turned off -> on in a different language.
  146. this.player.selectTextTrack(track);
  147. await this.player.setTextTrackVisibility(true);
  148. }
  149. /**
  150. * @private
  151. */
  152. updateLocalizedStrings_() {
  153. const LocIds = shaka.ui.Locales.Ids;
  154. this.button.ariaLabel = this.localization.resolve(LocIds.CAPTIONS);
  155. this.backButton.ariaLabel = this.localization.resolve(LocIds.BACK);
  156. this.nameSpan.textContent =
  157. this.localization.resolve(LocIds.CAPTIONS);
  158. this.backSpan.textContent =
  159. this.localization.resolve(LocIds.CAPTIONS);
  160. this.captionsOffSpan_.textContent =
  161. this.localization.resolve(LocIds.OFF);
  162. }
  163. /** @private */
  164. onTracksChanged_() {
  165. const hasText = this.player.getTextTracks().length > 0;
  166. shaka.ui.Utils.setDisplay(this.button, hasText);
  167. this.updateTextLanguages_();
  168. }
  169. };
  170. /**
  171. * @implements {shaka.extern.IUIElement.Factory}
  172. * @final
  173. */
  174. shaka.ui.TextSelection.Factory = class {
  175. /** @override */
  176. create(rootElement, controls) {
  177. return new shaka.ui.TextSelection(rootElement, controls);
  178. }
  179. };
  180. shaka.ui.OverflowMenu.registerElement(
  181. 'captions', new shaka.ui.TextSelection.Factory());
  182. shaka.ui.Controls.registerElement(
  183. 'captions', new shaka.ui.TextSelection.Factory());