Source: ui/mute_button.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.MuteButton');
  18. goog.require('shaka.ui.Element');
  19. goog.require('shaka.ui.Enums');
  20. goog.require('shaka.ui.Locales');
  21. goog.require('shaka.ui.Localization');
  22. goog.require('shaka.util.Dom');
  23. /**
  24. * @extends {shaka.ui.Element}
  25. * @final
  26. * @export
  27. */
  28. shaka.ui.MuteButton = class extends shaka.ui.Element {
  29. /**
  30. * @param {!HTMLElement} parent
  31. * @param {!shaka.ui.Controls} controls
  32. */
  33. constructor(parent, controls) {
  34. super(parent, controls);
  35. /** @private {!HTMLElement} */
  36. this.button_ = shaka.util.Dom.createHTMLElement('button');
  37. this.button_.classList.add('shaka-mute-button');
  38. this.button_.classList.add('material-icons');
  39. this.button_.textContent = shaka.ui.Enums.MaterialDesignIcons.MUTE;
  40. this.parent.appendChild(this.button_);
  41. this.updateAriaLabel_();
  42. this.eventManager.listen(
  43. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  44. this.updateAriaLabel_();
  45. });
  46. this.eventManager.listen(
  47. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  48. this.updateAriaLabel_();
  49. });
  50. this.eventManager.listen(this.button_, 'click', () => {
  51. this.video.muted = !this.video.muted;
  52. });
  53. this.eventManager.listen(this.video, 'volumechange', () => {
  54. this.updateAriaLabel_();
  55. this.updateIcon_();
  56. });
  57. }
  58. /**
  59. * @private
  60. */
  61. updateAriaLabel_() {
  62. const LocIds = shaka.ui.Locales.Ids;
  63. const label = this.video.muted ? LocIds.UNMUTE : LocIds.MUTE;
  64. this.button_.setAttribute(shaka.ui.Constants.ARIA_LABEL,
  65. this.localization.resolve(label));
  66. }
  67. /**
  68. * @private
  69. */
  70. updateIcon_() {
  71. this.button_.textContent = this.video.muted ?
  72. shaka.ui.Enums.MaterialDesignIcons.UNMUTE :
  73. shaka.ui.Enums.MaterialDesignIcons.MUTE;
  74. }
  75. };
  76. /**
  77. * @implements {shaka.extern.IUIElement.Factory}
  78. * @final
  79. */
  80. shaka.ui.MuteButton.Factory = class {
  81. /** @override */
  82. create(rootElement, controls) {
  83. return new shaka.ui.MuteButton(rootElement, controls);
  84. }
  85. };
  86. shaka.ui.Controls.registerElement('mute', new shaka.ui.MuteButton.Factory());