lib/amd/src/usermenu.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Initializes and handles events in the user menu.
  17. *
  18. * @module core/usermenu
  19. * @copyright 2021 Moodle
  20. * @author Mihail Geshoski <mihail@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. import Carousel from 'theme_boost/bootstrap/carousel';
  24. import {space, enter} from 'core/key_codes';
  25. /**
  26. * User menu constants.
  27. */
  28. const selectors = {
  29. userMenu: '.usermenu',
  30. userMenuCarousel: '.usermenu #usermenu-carousel',
  31. userMenuCarouselItem: '.usermenu #usermenu-carousel .carousel-item',
  32. userMenuCarouselItemActive: '.usermenu #usermenu-carousel .carousel-item.active',
  33. userMenuCarouselNavigationLink: '.usermenu #usermenu-carousel .carousel-navigation-link',
  34. };
  35. /**
  36. * Register event listeners.
  37. */
  38. const registerEventListeners = () => {
  39. const userMenu = document.querySelector(selectors.userMenu);
  40. const userMenuCarousel = document.querySelector(selectors.userMenuCarousel);
  41. // Handle the 'shown.bs.dropdown' event (Fired when the dropdown menu is fully displayed).
  42. userMenu.addEventListener('shown.bs.dropdown', () => {
  43. const activeCarouselItem = document.querySelector(selectors.userMenuCarouselItemActive);
  44. // Set the focus on the active carousel item.
  45. activeCarouselItem.focus();
  46. userMenu.querySelectorAll(selectors.userMenuCarouselItem).forEach(element => {
  47. // Resize all non-active carousel items to match the height and width of the current active (main)
  48. // carousel item to avoid sizing inconsistencies. This has to be done once the dropdown menu is fully
  49. // displayed ('shown.bs.dropdown') as the offsetWidth and offsetHeight cannot be obtained when the
  50. // element is hidden.
  51. if (!element.classList.contains('active')) {
  52. element.style.width = activeCarouselItem.offsetWidth + 'px';
  53. element.style.height = activeCarouselItem.offsetHeight + 'px';
  54. }
  55. });
  56. });
  57. // Handle click events in the user menu.
  58. userMenu.addEventListener('click', (e) => {
  59. // Handle click event on the carousel navigation (control) links in the user menu.
  60. if (e.target.matches(selectors.userMenuCarouselNavigationLink)) {
  61. carouselManagement(e);
  62. }
  63. });
  64. userMenu.addEventListener('keydown', e => {
  65. // Handle keydown event on the carousel navigation (control) links in the user menu.
  66. if ((e.keyCode === space ||
  67. e.keyCode === enter) &&
  68. e.target.matches(selectors.userMenuCarouselNavigationLink)) {
  69. e.preventDefault();
  70. carouselManagement(e);
  71. }
  72. });
  73. /**
  74. * We do the same actions here even if the caller was a click or button press.
  75. *
  76. * @param {Event} e The triggering element and key presses etc.
  77. */
  78. const carouselManagement = e => {
  79. // By default the user menu dropdown element closes on a click event. This behaviour is not desirable
  80. // as we need to be able to navigate through the carousel items (submenus of the user menu) within the
  81. // user menu. Therefore, we need to prevent the propagation of this event and then manually call the
  82. // carousel transition.
  83. e.stopPropagation();
  84. // The id of the targeted carousel item.
  85. const targetedCarouselItemId = e.target.dataset.carouselTargetId;
  86. const targetedCarouselItem = userMenu.querySelector('#' + targetedCarouselItemId);
  87. // Get the position (index) of the targeted carousel item within the parent container element.
  88. const index = Array.from(targetedCarouselItem.parentNode.children).indexOf(targetedCarouselItem);
  89. // Navigate to the targeted carousel item.
  90. Carousel.getOrCreateInstance(userMenuCarousel).to(index);
  91. };
  92. // Handle the 'hide.bs.dropdown' event (Fired when the dropdown menu is being closed).
  93. userMenu.addEventListener('hide.bs.dropdown', () => {
  94. // Reset the state once the user menu dropdown is closed and return back to the first (main) carousel item
  95. // if necessary.
  96. Carousel.getOrCreateInstance(userMenuCarousel).to(0);
  97. });
  98. // Handle the 'slid.bs.carousel' event (Fired when the carousel has completed its slide transition).
  99. userMenuCarousel?.addEventListener('slid.bs.carousel', () => {
  100. const activeCarouselItem = userMenu.querySelector(selectors.userMenuCarouselItemActive);
  101. // Set the focus on the newly activated carousel item.
  102. activeCarouselItem.focus();
  103. });
  104. };
  105. /**
  106. * Initialize the user menu.
  107. */
  108. const init = () => {
  109. registerEventListeners();
  110. };
  111. export default {
  112. init: init,
  113. };