Source: ui/settings_menu.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.SettingsMenu');
  7. goog.require('shaka.ui.Element');
  8. goog.require('shaka.ui.Enums');
  9. goog.require('shaka.ui.Utils');
  10. goog.require('shaka.util.Dom');
  11. goog.require('shaka.util.FakeEvent');
  12. goog.require('shaka.util.Iterables');
  13. goog.requireType('shaka.ui.Controls');
  14. /**
  15. * @extends {shaka.ui.Element}
  16. * @implements {shaka.extern.IUISettingsMenu}
  17. * @export
  18. */
  19. shaka.ui.SettingsMenu = class extends shaka.ui.Element {
  20. /**
  21. * @param {!HTMLElement} parent
  22. * @param {!shaka.ui.Controls} controls
  23. * @param {string} iconText
  24. */
  25. constructor(parent, controls, iconText) {
  26. super(parent, controls);
  27. /** @private {HTMLElement } */
  28. this.videoContainer_ = this.controls.getVideoContainer();
  29. this.addButton_(iconText);
  30. this.addMenu_();
  31. this.inOverflowMenu_();
  32. this.eventManager.listen(this.button, 'click', () => {
  33. if (!this.controls.isOpaque()) {
  34. return;
  35. }
  36. this.onButtonClick_();
  37. });
  38. /** @private {ResizeObserver} */
  39. this.resizeObserver_ = null;
  40. const resize = () => this.computeMaxHeight_();
  41. // Use ResizeObserver if available, fallback to window resize event
  42. if (window.ResizeObserver) {
  43. this.resizeObserver_ = new ResizeObserver(resize);
  44. this.resizeObserver_.observe(this.controls.getVideoContainer());
  45. } else {
  46. // Fallback for older browsers
  47. this.eventManager.listen(window, 'resize', resize);
  48. }
  49. }
  50. /** @override */
  51. release() {
  52. if (this.resizeObserver_) {
  53. this.resizeObserver_.disconnect();
  54. this.resizeObserver_ = null;
  55. }
  56. super.release();
  57. }
  58. /**
  59. * @param {string} iconText
  60. * @private
  61. */
  62. addButton_(iconText) {
  63. /** @protected {!HTMLButtonElement} */
  64. this.button = shaka.util.Dom.createButton();
  65. this.button.classList.add('shaka-overflow-button');
  66. /** @protected {!HTMLElement}*/
  67. this.icon = shaka.util.Dom.createHTMLElement('i');
  68. this.icon.classList.add('material-icons-round');
  69. this.icon.textContent = iconText;
  70. this.button.appendChild(this.icon);
  71. const label = shaka.util.Dom.createHTMLElement('label');
  72. label.classList.add('shaka-overflow-button-label');
  73. label.classList.add('shaka-overflow-menu-only');
  74. label.classList.add('shaka-overflow-button-label-inline');
  75. /** @protected {!HTMLElement}*/
  76. this.nameSpan = shaka.util.Dom.createHTMLElement('span');
  77. label.appendChild(this.nameSpan);
  78. /** @protected {!HTMLElement}*/
  79. this.currentSelection = shaka.util.Dom.createHTMLElement('span');
  80. this.currentSelection.classList.add('shaka-current-selection-span');
  81. label.appendChild(this.currentSelection);
  82. this.button.appendChild(label);
  83. this.parent.appendChild(this.button);
  84. }
  85. /** @private */
  86. addMenu_() {
  87. /** @protected {!HTMLElement}*/
  88. this.menu = shaka.util.Dom.createHTMLElement('div');
  89. this.menu.classList.add('shaka-no-propagation');
  90. this.menu.classList.add('shaka-show-controls-on-mouse-over');
  91. this.menu.classList.add('shaka-settings-menu');
  92. this.menu.classList.add('shaka-hidden');
  93. /** @protected {!HTMLButtonElement}*/
  94. this.backButton = shaka.util.Dom.createButton();
  95. this.backButton.classList.add('shaka-back-to-overflow-button');
  96. this.menu.appendChild(this.backButton);
  97. this.eventManager.listen(this.backButton, 'click', () => {
  98. this.controls.hideSettingsMenus();
  99. });
  100. const backIcon = shaka.util.Dom.createHTMLElement('i');
  101. backIcon.classList.add('material-icons-round');
  102. backIcon.textContent = shaka.ui.Enums.MaterialDesignIcons.CLOSE;
  103. this.backButton.appendChild(backIcon);
  104. /** @protected {!HTMLElement}*/
  105. this.backSpan = shaka.util.Dom.createHTMLElement('span');
  106. this.backButton.appendChild(this.backSpan);
  107. const controlsContainer = this.controls.getControlsContainer();
  108. controlsContainer.appendChild(this.menu);
  109. }
  110. /** @private */
  111. inOverflowMenu_() {
  112. // Initially, submenus are created with a "Close" option. When present
  113. // inside of the overflow menu, that option must be replaced with a
  114. // "Back" arrow that returns the user to the main menu.
  115. if (this.parent.classList.contains('shaka-overflow-menu')) {
  116. this.backButton.firstChild.textContent =
  117. shaka.ui.Enums.MaterialDesignIcons.BACK;
  118. this.eventManager.listen(this.menu, 'click', () => {
  119. shaka.ui.Utils.setDisplay(this.menu, false);
  120. shaka.ui.Utils.setDisplay(this.parent, true);
  121. const isDisplayed =
  122. (element) => element.classList.contains('shaka-hidden') == false;
  123. const Iterables = shaka.util.Iterables;
  124. if (Iterables.some(this.parent.childNodes, isDisplayed)) {
  125. // Focus on the first visible child of the overflow menu
  126. const visibleElements =
  127. Iterables.filter(this.parent.childNodes, isDisplayed);
  128. /** @type {!HTMLElement} */ (visibleElements[0]).focus();
  129. }
  130. // Make sure controls are displayed
  131. this.controls.computeOpacity();
  132. this.computeMaxHeight_();
  133. });
  134. }
  135. }
  136. /** @private */
  137. onButtonClick_() {
  138. if (this.menu.classList.contains('shaka-hidden')) {
  139. this.controls.dispatchEvent(new shaka.util.FakeEvent('submenuopen'));
  140. shaka.ui.Utils.setDisplay(this.menu, true);
  141. shaka.ui.Utils.focusOnTheChosenItem(this.menu);
  142. this.computeMaxHeight_();
  143. } else {
  144. shaka.ui.Utils.setDisplay(this.menu, false);
  145. }
  146. }
  147. /**
  148. * @private
  149. */
  150. computeMaxHeight_() {
  151. const rectMenu = this.menu.getBoundingClientRect();
  152. const styleMenu = window.getComputedStyle(this.menu);
  153. const paddingTop = parseFloat(styleMenu.paddingTop);
  154. const paddingBottom = parseFloat(styleMenu.paddingBottom);
  155. const rectContainer = this.videoContainer_.getBoundingClientRect();
  156. const heightIntersection =
  157. rectMenu.bottom - rectContainer.top - paddingTop - paddingBottom;
  158. this.menu.style.maxHeight = heightIntersection + 'px';
  159. }
  160. };