Source: ui/fullscreen_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.FullscreenButton');
  7. goog.require('shaka.ads.Utils');
  8. goog.require('shaka.ui.Controls');
  9. goog.require('shaka.ui.Element');
  10. goog.require('shaka.ui.Enums');
  11. goog.require('shaka.ui.Locales');
  12. goog.require('shaka.ui.Localization');
  13. goog.require('shaka.util.Dom');
  14. /**
  15. * @extends {shaka.ui.Element}
  16. * @final
  17. * @export
  18. */
  19. shaka.ui.FullscreenButton = class extends shaka.ui.Element {
  20. /**
  21. * @param {!HTMLElement} parent
  22. * @param {!shaka.ui.Controls} controls
  23. */
  24. constructor(parent, controls) {
  25. super(parent, controls);
  26. /** @private {HTMLMediaElement} */
  27. this.localVideo_ = this.controls.getLocalVideo();
  28. /** @private {!HTMLButtonElement} */
  29. this.button_ = shaka.util.Dom.createButton();
  30. this.button_.classList.add('shaka-fullscreen-button');
  31. this.button_.classList.add('material-icons-round');
  32. this.button_.classList.add('shaka-tooltip');
  33. this.checkSupport_();
  34. this.button_.textContent = shaka.ui.Enums.MaterialDesignIcons.FULLSCREEN;
  35. this.parent.appendChild(this.button_);
  36. this.updateAriaLabel_();
  37. this.eventManager.listen(
  38. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  39. this.updateAriaLabel_();
  40. });
  41. this.eventManager.listen(
  42. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  43. this.updateAriaLabel_();
  44. });
  45. this.eventManager.listen(this.button_, 'click', async () => {
  46. if (!this.controls.isOpaque()) {
  47. return;
  48. }
  49. await this.controls.toggleFullScreen();
  50. });
  51. this.eventManager.listen(document, 'fullscreenchange', () => {
  52. this.updateIcon_();
  53. this.updateAriaLabel_();
  54. });
  55. this.eventManager.listen(this.localVideo_, 'loadedmetadata', () => {
  56. this.checkSupport_();
  57. });
  58. this.eventManager.listen(this.localVideo_, 'loadeddata', () => {
  59. this.checkSupport_();
  60. });
  61. this.eventManager.listen(this.adManager, shaka.ads.Utils.AD_STARTED, () => {
  62. this.checkSupport_();
  63. });
  64. this.eventManager.listen(this.adManager, shaka.ads.Utils.AD_STOPPED, () => {
  65. this.checkSupport_();
  66. });
  67. }
  68. /**
  69. * @private
  70. */
  71. checkSupport_() {
  72. // Don't show the button if fullscreen is not supported
  73. if (!this.controls.isFullScreenSupported()) {
  74. this.button_.classList.add('shaka-hidden');
  75. } else {
  76. this.button_.classList.remove('shaka-hidden');
  77. }
  78. }
  79. /**
  80. * @private
  81. */
  82. updateAriaLabel_() {
  83. const LocIds = shaka.ui.Locales.Ids;
  84. const label = this.controls.isFullScreenEnabled() ?
  85. LocIds.EXIT_FULL_SCREEN : LocIds.FULL_SCREEN;
  86. this.button_.ariaLabel = this.localization.resolve(label);
  87. }
  88. /**
  89. * @private
  90. */
  91. updateIcon_() {
  92. this.button_.textContent =
  93. this.controls.isFullScreenEnabled() ?
  94. shaka.ui.Enums.MaterialDesignIcons.EXIT_FULLSCREEN :
  95. shaka.ui.Enums.MaterialDesignIcons.FULLSCREEN;
  96. }
  97. };
  98. /**
  99. * @implements {shaka.extern.IUIElement.Factory}
  100. * @final
  101. */
  102. shaka.ui.FullscreenButton.Factory = class {
  103. /** @override */
  104. create(rootElement, controls) {
  105. return new shaka.ui.FullscreenButton(rootElement, controls);
  106. }
  107. };
  108. shaka.ui.Controls.registerElement(
  109. 'fullscreen', new shaka.ui.FullscreenButton.Factory());