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