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