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. }
  39. /**
  40. * @param {string} iconText
  41. * @private
  42. */
  43. addButton_(iconText) {
  44. /** @protected {!HTMLButtonElement} */
  45. this.button = shaka.util.Dom.createButton();
  46. this.button.classList.add('shaka-overflow-button');
  47. /** @protected {!HTMLElement}*/
  48. this.icon = shaka.util.Dom.createHTMLElement('i');
  49. this.icon.classList.add('material-icons-round');
  50. this.icon.textContent = iconText;
  51. this.button.appendChild(this.icon);
  52. const label = shaka.util.Dom.createHTMLElement('label');
  53. label.classList.add('shaka-overflow-button-label');
  54. label.classList.add('shaka-overflow-menu-only');
  55. label.classList.add('shaka-overflow-button-label-inline');
  56. /** @protected {!HTMLElement}*/
  57. this.nameSpan = shaka.util.Dom.createHTMLElement('span');
  58. label.appendChild(this.nameSpan);
  59. /** @protected {!HTMLElement}*/
  60. this.currentSelection = shaka.util.Dom.createHTMLElement('span');
  61. this.currentSelection.classList.add('shaka-current-selection-span');
  62. label.appendChild(this.currentSelection);
  63. this.button.appendChild(label);
  64. this.parent.appendChild(this.button);
  65. }
  66. /** @private */
  67. addMenu_() {
  68. /** @protected {!HTMLElement}*/
  69. this.menu = shaka.util.Dom.createHTMLElement('div');
  70. this.menu.classList.add('shaka-no-propagation');
  71. this.menu.classList.add('shaka-show-controls-on-mouse-over');
  72. this.menu.classList.add('shaka-settings-menu');
  73. this.menu.classList.add('shaka-hidden');
  74. /** @protected {!HTMLButtonElement}*/
  75. this.backButton = shaka.util.Dom.createButton();
  76. this.backButton.classList.add('shaka-back-to-overflow-button');
  77. this.menu.appendChild(this.backButton);
  78. this.eventManager.listen(this.backButton, 'click', () => {
  79. this.controls.hideSettingsMenus();
  80. });
  81. const backIcon = shaka.util.Dom.createHTMLElement('i');
  82. backIcon.classList.add('material-icons-round');
  83. backIcon.textContent = shaka.ui.Enums.MaterialDesignIcons.CLOSE;
  84. this.backButton.appendChild(backIcon);
  85. /** @protected {!HTMLElement}*/
  86. this.backSpan = shaka.util.Dom.createHTMLElement('span');
  87. this.backButton.appendChild(this.backSpan);
  88. const controlsContainer = this.controls.getControlsContainer();
  89. controlsContainer.appendChild(this.menu);
  90. }
  91. /** @private */
  92. inOverflowMenu_() {
  93. // Initially, submenus are created with a "Close" option. When present
  94. // inside of the overflow menu, that option must be replaced with a
  95. // "Back" arrow that returns the user to the main menu.
  96. if (this.parent.classList.contains('shaka-overflow-menu')) {
  97. this.backButton.firstChild.textContent =
  98. shaka.ui.Enums.MaterialDesignIcons.BACK;
  99. this.eventManager.listen(this.menu, 'click', () => {
  100. shaka.ui.Utils.setDisplay(this.menu, false);
  101. shaka.ui.Utils.setDisplay(this.parent, true);
  102. const isDisplayed =
  103. (element) => element.classList.contains('shaka-hidden') == false;
  104. const Iterables = shaka.util.Iterables;
  105. if (Iterables.some(this.parent.childNodes, isDisplayed)) {
  106. // Focus on the first visible child of the overflow menu
  107. const visibleElements =
  108. Iterables.filter(this.parent.childNodes, isDisplayed);
  109. /** @type {!HTMLElement} */ (visibleElements[0]).focus();
  110. }
  111. // Make sure controls are displayed
  112. this.controls.computeOpacity();
  113. this.computeMaxHeight_();
  114. });
  115. }
  116. }
  117. /** @private */
  118. onButtonClick_() {
  119. if (this.menu.classList.contains('shaka-hidden')) {
  120. this.controls.dispatchEvent(new shaka.util.FakeEvent('submenuopen'));
  121. shaka.ui.Utils.setDisplay(this.menu, true);
  122. shaka.ui.Utils.focusOnTheChosenItem(this.menu);
  123. this.computeMaxHeight_();
  124. } else {
  125. shaka.ui.Utils.setDisplay(this.menu, false);
  126. }
  127. }
  128. /**
  129. * @private
  130. */
  131. computeMaxHeight_() {
  132. const rectMenu = this.menu.getBoundingClientRect();
  133. const styleMenu = window.getComputedStyle(this.menu);
  134. const paddingTop = parseFloat(styleMenu.paddingTop);
  135. const paddingBottom = parseFloat(styleMenu.paddingBottom);
  136. const rectContainer = this.videoContainer_.getBoundingClientRect();
  137. const heightIntersection =
  138. rectMenu.bottom - rectContainer.top - paddingTop - paddingBottom;
  139. this.menu.style.maxHeight = heightIntersection + 'px';
  140. }
  141. };