Source: ui/mute_button.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.MuteButton');
  7. goog.require('shaka.ads.Utils');
  8. goog.require('shaka.ui.ContextMenu');
  9. goog.require('shaka.ui.Controls');
  10. goog.require('shaka.ui.Element');
  11. goog.require('shaka.ui.Enums');
  12. goog.require('shaka.ui.Locales');
  13. goog.require('shaka.ui.Localization');
  14. goog.require('shaka.ui.OverflowMenu');
  15. goog.require('shaka.util.Dom');
  16. /**
  17. * @extends {shaka.ui.Element}
  18. * @final
  19. * @export
  20. */
  21. shaka.ui.MuteButton = class extends shaka.ui.Element {
  22. /**
  23. * @param {!HTMLElement} parent
  24. * @param {!shaka.ui.Controls} controls
  25. */
  26. constructor(parent, controls) {
  27. super(parent, controls);
  28. const LocIds = shaka.ui.Locales.Ids;
  29. /** @private {!HTMLButtonElement} */
  30. this.button_ = shaka.util.Dom.createButton();
  31. this.button_.classList.add('shaka-mute-button');
  32. this.button_.classList.add('shaka-tooltip');
  33. /** @private {!HTMLElement} */
  34. this.icon_ = shaka.util.Dom.createHTMLElement('i');
  35. this.icon_.classList.add('material-icons-round');
  36. this.icon_.textContent = shaka.ui.Enums.MaterialDesignIcons.MUTE;
  37. this.button_.appendChild(this.icon_);
  38. const label = shaka.util.Dom.createHTMLElement('label');
  39. label.classList.add('shaka-overflow-button-label');
  40. label.classList.add('shaka-overflow-menu-only');
  41. this.nameSpan_ = shaka.util.Dom.createHTMLElement('span');
  42. this.nameSpan_.textContent = this.localization.resolve(LocIds.MUTE);
  43. label.appendChild(this.nameSpan_);
  44. /** @private {!HTMLElement} */
  45. this.currentState_ = shaka.util.Dom.createHTMLElement('span');
  46. this.currentState_.classList.add('shaka-current-selection-span');
  47. label.appendChild(this.currentState_);
  48. this.button_.appendChild(label);
  49. this.parent.appendChild(this.button_);
  50. this.updateLocalizedStrings_();
  51. this.updateIcon_();
  52. this.eventManager.listen(
  53. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  54. this.updateLocalizedStrings_();
  55. });
  56. this.eventManager.listen(
  57. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  58. this.updateLocalizedStrings_();
  59. });
  60. this.eventManager.listen(this.button_, 'click', () => {
  61. if (this.ad && this.ad.isLinear()) {
  62. this.ad.setMuted(!this.ad.isMuted());
  63. } else {
  64. this.video.muted = !this.video.muted;
  65. }
  66. });
  67. this.eventManager.listen(this.video, 'volumechange', () => {
  68. this.updateLocalizedStrings_();
  69. this.updateIcon_();
  70. });
  71. this.eventManager.listen(this.adManager,
  72. shaka.ads.Utils.AD_VOLUME_CHANGED, () => {
  73. this.updateLocalizedStrings_();
  74. this.updateIcon_();
  75. });
  76. this.eventManager.listen(this.adManager,
  77. shaka.ads.Utils.AD_MUTED, () => {
  78. this.updateLocalizedStrings_();
  79. this.updateIcon_();
  80. });
  81. this.eventManager.listen(this.adManager,
  82. shaka.ads.Utils.AD_STOPPED, () => {
  83. // The base class also listens for this event and sets this.ad
  84. // to null. This is a safeguard in case of a race condition as
  85. // the label and icon code depends on this.ad being correctly
  86. // updated at the time it runs.
  87. this.ad = null;
  88. this.updateLocalizedStrings_();
  89. this.updateIcon_();
  90. });
  91. }
  92. /**
  93. * @private
  94. */
  95. updateLocalizedStrings_() {
  96. const LocIds = shaka.ui.Locales.Ids;
  97. let label;
  98. if (this.ad) {
  99. label = this.ad.isMuted() ? LocIds.UNMUTE : LocIds.MUTE;
  100. } else {
  101. label = this.video.muted ? LocIds.UNMUTE : LocIds.MUTE;
  102. }
  103. this.button_.ariaLabel = this.localization.resolve(label);
  104. this.nameSpan_.textContent = this.localization.resolve(label);
  105. }
  106. /**
  107. * @private
  108. */
  109. updateIcon_() {
  110. const Icons = shaka.ui.Enums.MaterialDesignIcons;
  111. let icon;
  112. if (this.ad) {
  113. icon = this.ad.isMuted() ? Icons.UNMUTE : Icons.MUTE;
  114. } else {
  115. icon = this.video.muted ? Icons.UNMUTE : Icons.MUTE;
  116. }
  117. this.icon_.textContent = icon;
  118. }
  119. };
  120. /**
  121. * @implements {shaka.extern.IUIElement.Factory}
  122. * @final
  123. */
  124. shaka.ui.MuteButton.Factory = class {
  125. /** @override */
  126. create(rootElement, controls) {
  127. return new shaka.ui.MuteButton(rootElement, controls);
  128. }
  129. };
  130. shaka.ui.OverflowMenu.registerElement(
  131. 'mute', new shaka.ui.MuteButton.Factory());
  132. shaka.ui.Controls.registerElement(
  133. 'mute', new shaka.ui.MuteButton.Factory());
  134. shaka.ui.ContextMenu.registerElement(
  135. 'mute', new shaka.ui.MuteButton.Factory());