Source: ui/audio_language_selection.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.ui.AudioLanguageSelection');
  18. goog.require('shaka.ui.Enums');
  19. goog.require('shaka.ui.LanguageUtils');
  20. goog.require('shaka.ui.Locales');
  21. goog.require('shaka.ui.Localization');
  22. goog.require('shaka.ui.OverflowMenu');
  23. goog.require('shaka.ui.SettingsMenu');
  24. /**
  25. * @extends {shaka.ui.SettingsMenu}
  26. * @final
  27. * @export
  28. */
  29. shaka.ui.AudioLanguageSelection = class extends shaka.ui.SettingsMenu {
  30. /**
  31. * @param {!HTMLElement} parent
  32. * @param {!shaka.ui.Controls} controls
  33. */
  34. constructor(parent, controls) {
  35. super(parent, controls, shaka.ui.Enums.MaterialDesignIcons.LANGUAGE);
  36. this.button.classList.add('shaka-language-button');
  37. this.menu.classList.add('shaka-audio-languages');
  38. this.eventManager.listen(
  39. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  40. this.updateLocalizedStrings_();
  41. });
  42. this.eventManager.listen(
  43. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  44. this.updateLocalizedStrings_();
  45. });
  46. this.eventManager.listen(this.player, 'trackschanged', () => {
  47. this.onTracksChanged_();
  48. });
  49. this.eventManager.listen(this.player, 'variantchanged', () => {
  50. this.updateAudioLanguages_();
  51. });
  52. // Set up all the strings in the user's preferred language.
  53. this.updateLocalizedStrings_();
  54. this.updateAudioLanguages_();
  55. }
  56. /** @private */
  57. updateAudioLanguages_() {
  58. const tracks = this.player.getVariantTracks();
  59. shaka.ui.LanguageUtils.updateTracks(tracks, this.menu,
  60. (track) => this.onAudioTrackSelected_(track),
  61. /* updateChosen */ true, this.currentSelection, this.localization,
  62. this.controls.getConfig().trackLabelFormat);
  63. shaka.ui.Utils.focusOnTheChosenItem(this.menu);
  64. this.controls.dispatchEvent(
  65. new shaka.util.FakeEvent('languageselectionupdated'));
  66. }
  67. /** @private */
  68. onTracksChanged_() {
  69. const hasVariants = this.player.getVariantTracks().length > 0;
  70. shaka.ui.Utils.setDisplay(this.button, hasVariants);
  71. this.updateAudioLanguages_();
  72. }
  73. /**
  74. * @param {!shaka.extern.Track} track
  75. * @private
  76. */
  77. onAudioTrackSelected_(track) {
  78. this.player.selectAudioLanguage(track.language, track.roles[0]);
  79. }
  80. /**
  81. * @private
  82. */
  83. updateLocalizedStrings_() {
  84. const LocIds = shaka.ui.Locales.Ids;
  85. this.backButton.setAttribute(shaka.ui.Constants.ARIA_LABEL,
  86. this.localization.resolve(LocIds.BACK));
  87. this.button.setAttribute(shaka.ui.Constants.ARIA_LABEL,
  88. this.localization.resolve(LocIds.LANGUAGE));
  89. this.nameSpan.textContent =
  90. this.localization.resolve(LocIds.LANGUAGE);
  91. this.backSpan.textContent =
  92. this.localization.resolve(LocIds.LANGUAGE);
  93. }
  94. };
  95. /**
  96. * @implements {shaka.extern.IUIElement.Factory}
  97. * @final
  98. */
  99. shaka.ui.AudioLanguageSelection.Factory = class {
  100. /** @override */
  101. create(rootElement, controls) {
  102. return new shaka.ui.AudioLanguageSelection(rootElement, controls);
  103. }
  104. };
  105. shaka.ui.OverflowMenu.registerElement(
  106. 'language', new shaka.ui.AudioLanguageSelection.Factory());