Source: ui/audio_language_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.AudioLanguageSelection');
  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.FakeEvent');
  16. goog.requireType('shaka.ui.Controls');
  17. /**
  18. * @extends {shaka.ui.SettingsMenu}
  19. * @final
  20. * @export
  21. */
  22. shaka.ui.AudioLanguageSelection = class extends shaka.ui.SettingsMenu {
  23. /**
  24. * @param {!HTMLElement} parent
  25. * @param {!shaka.ui.Controls} controls
  26. */
  27. constructor(parent, controls) {
  28. super(parent, controls, shaka.ui.Enums.MaterialDesignIcons.LANGUAGE);
  29. this.button.classList.add('shaka-language-button');
  30. this.button.classList.add('shaka-tooltip-status');
  31. this.menu.classList.add('shaka-audio-languages');
  32. this.eventManager.listen(
  33. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  34. this.updateLocalizedStrings_();
  35. });
  36. this.eventManager.listen(
  37. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  38. this.updateLocalizedStrings_();
  39. });
  40. this.eventManager.listen(this.player, 'loading', () => {
  41. this.onTracksChanged_();
  42. });
  43. this.eventManager.listen(this.player, 'trackschanged', () => {
  44. this.onTracksChanged_();
  45. });
  46. this.eventManager.listen(this.player, 'variantchanged', () => {
  47. this.updateAudioLanguages_();
  48. });
  49. // Set up all the strings in the user's preferred language.
  50. this.updateLocalizedStrings_();
  51. this.updateAudioLanguages_();
  52. }
  53. /** @private */
  54. updateAudioLanguages_() {
  55. const tracks = this.player.getVariantTracks();
  56. shaka.ui.LanguageUtils.updateTracks(tracks, this.menu,
  57. (track) => this.onAudioTrackSelected_(track),
  58. /* updateChosen= */ true, this.currentSelection, this.localization,
  59. this.controls.getConfig().trackLabelFormat,
  60. this.controls.getConfig().showAudioChannelCountVariants);
  61. shaka.ui.Utils.focusOnTheChosenItem(this.menu);
  62. this.controls.dispatchEvent(
  63. new shaka.util.FakeEvent('languageselectionupdated'));
  64. this.button.setAttribute('shaka-status', this.currentSelection.innerText);
  65. const numberOfItems = this.menu.getElementsByTagName('button').length;
  66. shaka.ui.Utils.setDisplay(this.button, numberOfItems > 2);
  67. }
  68. /** @private */
  69. onTracksChanged_() {
  70. const hasVariants = this.player.getVariantTracks().length > 0;
  71. shaka.ui.Utils.setDisplay(this.button, hasVariants);
  72. this.updateAudioLanguages_();
  73. }
  74. /**
  75. * @param {!shaka.extern.Track} track
  76. * @private
  77. */
  78. onAudioTrackSelected_(track) {
  79. let channelsCount = undefined;
  80. if (track.channelsCount &&
  81. this.controls.getConfig().showAudioChannelCountVariants) {
  82. channelsCount = track.channelsCount;
  83. }
  84. let codec = undefined;
  85. if (track.audioCodec) {
  86. codec = track.audioCodec;
  87. }
  88. this.player.selectAudioLanguage(track.language, track.roles[0],
  89. channelsCount, /* safeMargin= */ 0, codec);
  90. }
  91. /**
  92. * @private
  93. */
  94. updateLocalizedStrings_() {
  95. const LocIds = shaka.ui.Locales.Ids;
  96. this.backButton.ariaLabel = this.localization.resolve(LocIds.BACK);
  97. this.button.ariaLabel = this.localization.resolve(LocIds.LANGUAGE);
  98. this.nameSpan.textContent =
  99. this.localization.resolve(LocIds.LANGUAGE);
  100. this.backSpan.textContent =
  101. this.localization.resolve(LocIds.LANGUAGE);
  102. }
  103. };
  104. /**
  105. * @implements {shaka.extern.IUIElement.Factory}
  106. * @final
  107. */
  108. shaka.ui.AudioLanguageSelection.Factory = class {
  109. /** @override */
  110. create(rootElement, controls) {
  111. return new shaka.ui.AudioLanguageSelection(rootElement, controls);
  112. }
  113. };
  114. shaka.ui.OverflowMenu.registerElement(
  115. 'language', new shaka.ui.AudioLanguageSelection.Factory());
  116. shaka.ui.Controls.registerElement(
  117. 'language', new shaka.ui.AudioLanguageSelection.Factory());