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.ui.Controls');
  8. goog.require('shaka.ui.Element');
  9. goog.require('shaka.ui.Enums');
  10. goog.require('shaka.ui.Locales');
  11. goog.require('shaka.ui.Localization');
  12. goog.require('shaka.util.Dom');
  13. /**
  14. * @extends {shaka.ui.Element}
  15. * @final
  16. * @export
  17. */
  18. shaka.ui.FullscreenButton = class extends shaka.ui.Element {
  19. /**
  20. * @param {!HTMLElement} parent
  21. * @param {!shaka.ui.Controls} controls
  22. */
  23. constructor(parent, controls) {
  24. super(parent, controls);
  25. /** @private {!HTMLButtonElement} */
  26. this.button_ = shaka.util.Dom.createButton();
  27. this.button_.classList.add('shaka-fullscreen-button');
  28. this.button_.classList.add('material-icons-round');
  29. this.button_.classList.add('shaka-tooltip');
  30. // Don't show the button if fullscreen is not supported
  31. if (!document.fullscreenEnabled) {
  32. this.button_.classList.add('shaka-hidden');
  33. }
  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. await this.controls.toggleFullScreen();
  47. });
  48. this.eventManager.listen(document, 'fullscreenchange', () => {
  49. this.updateIcon_();
  50. this.updateAriaLabel_();
  51. });
  52. }
  53. /**
  54. * @private
  55. */
  56. updateAriaLabel_() {
  57. const LocIds = shaka.ui.Locales.Ids;
  58. const label = document.fullscreenElement ?
  59. LocIds.EXIT_FULL_SCREEN : LocIds.FULL_SCREEN;
  60. this.button_.ariaLabel = this.localization.resolve(label);
  61. }
  62. /**
  63. * @private
  64. */
  65. updateIcon_() {
  66. this.button_.textContent =
  67. document.fullscreenElement ?
  68. shaka.ui.Enums.MaterialDesignIcons.EXIT_FULLSCREEN :
  69. shaka.ui.Enums.MaterialDesignIcons.FULLSCREEN;
  70. }
  71. };
  72. /**
  73. * @implements {shaka.extern.IUIElement.Factory}
  74. * @final
  75. */
  76. shaka.ui.FullscreenButton.Factory = class {
  77. /** @override */
  78. create(rootElement, controls) {
  79. return new shaka.ui.FullscreenButton(rootElement, controls);
  80. }
  81. };
  82. shaka.ui.Controls.registerElement(
  83. 'fullscreen', new shaka.ui.FullscreenButton.Factory());