Source: ui/context_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.ContextMenu');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.log');
  9. goog.require('shaka.ui.Element');
  10. goog.require('shaka.ui.Utils');
  11. goog.require('shaka.util.Dom');
  12. goog.requireType('shaka.ui.Controls');
  13. /**
  14. * @extends {shaka.ui.Element}
  15. * @final
  16. * @export
  17. */
  18. shaka.ui.ContextMenu = class extends shaka.ui.Element {
  19. /**
  20. * @param {!HTMLElement} parent
  21. * @param {!shaka.ui.Controls} controls
  22. */
  23. constructor(parent, controls) {
  24. super(parent, controls);
  25. /** @private {!shaka.extern.UIConfiguration} */
  26. this.config_ = this.controls.getConfig();
  27. /** @private {HTMLElement} */
  28. this.controlsContainer_ = this.controls.getControlsContainer();
  29. /** @private {!Array<shaka.extern.IUIElement>} */
  30. this.children_ = [];
  31. /** @private {!HTMLElement} */
  32. this.contextMenu_ = shaka.util.Dom.createHTMLElement('div');
  33. this.contextMenu_.classList.add('shaka-no-propagation');
  34. this.contextMenu_.classList.add('shaka-context-menu');
  35. this.contextMenu_.classList.add('shaka-hidden');
  36. this.controlsContainer_.appendChild(this.contextMenu_);
  37. this.eventManager.listen(this.controlsContainer_, 'contextmenu', (e) => {
  38. if (this.contextMenu_.classList.contains('shaka-hidden')) {
  39. e.preventDefault();
  40. const controlsLocation =
  41. this.controlsContainer_.getBoundingClientRect();
  42. this.contextMenu_.style.left = `${e.clientX - controlsLocation.left}px`;
  43. this.contextMenu_.style.top = `${e.clientY - controlsLocation.top}px`;
  44. shaka.ui.Utils.setDisplay(this.contextMenu_, true);
  45. } else {
  46. shaka.ui.Utils.setDisplay(this.contextMenu_, false);
  47. }
  48. });
  49. this.eventManager.listen(window, 'click', () => {
  50. shaka.ui.Utils.setDisplay(this.contextMenu_, false);
  51. });
  52. this.eventManager.listen(this.contextMenu_, 'click', () => {
  53. shaka.ui.Utils.setDisplay(this.contextMenu_, false);
  54. });
  55. this.createChildren_();
  56. }
  57. /** @override */
  58. release() {
  59. this.controlsContainer_ = null;
  60. for (const element of this.children_) {
  61. element.release();
  62. }
  63. this.children_ = [];
  64. super.release();
  65. }
  66. /**
  67. * @export
  68. */
  69. closeMenu() {
  70. shaka.ui.Utils.setDisplay(this.contextMenu_, false);
  71. }
  72. /**
  73. * @param {string} name
  74. * @param {!shaka.extern.IUIElement.Factory} factory
  75. * @export
  76. */
  77. static registerElement(name, factory) {
  78. shaka.ui.ContextMenu.elementNamesToFactories_.set(name, factory);
  79. }
  80. /**
  81. * @private
  82. */
  83. createChildren_() {
  84. for (const name of this.config_.contextMenuElements) {
  85. const factory =
  86. shaka.ui.ContextMenu.elementNamesToFactories_.get(name);
  87. if (factory) {
  88. goog.asserts.assert(this.controls, 'Controls should not be null!');
  89. this.children_.push(factory.create(this.contextMenu_, this.controls));
  90. } else {
  91. shaka.log.alwaysWarn('Unrecognized context menu element:', name);
  92. }
  93. }
  94. }
  95. };
  96. /** @private {!Map<string, !shaka.extern.IUIElement.Factory>} */
  97. shaka.ui.ContextMenu.elementNamesToFactories_ = new Map();