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