reportbuilder/amd/src/sidebar.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. * Report builder sidebar component
  17. *
  18. * @module core_reportbuilder/sidebar
  19. * @copyright 2021 Paul Holden <paulh@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import Pending from 'core/pending';
  23. import {debounce} from 'core/utils';
  24. import * as reportSelectors from 'core_reportbuilder/local/selectors';
  25. const DEBOUNCE_TIMER = 250;
  26. const CLASSES = {
  27. EXPANDED: 'show',
  28. COLLAPSED: 'collapsed',
  29. HIDE: 'd-none',
  30. };
  31. /**
  32. * Initialise module
  33. *
  34. * @param {Event} event
  35. * @param {Element} sidebarMenu
  36. */
  37. const sidebarCardFilter = (event, sidebarMenu) => {
  38. const pendingPromise = new Pending('core_reportbuilder/sidebar:cardFilter');
  39. const sidebarCards = sidebarMenu.querySelectorAll(reportSelectors.regions.sidebarCard);
  40. const sidebarItems = sidebarMenu.querySelectorAll(reportSelectors.regions.sidebarItem);
  41. const searchTerm = event.target.value.toLowerCase();
  42. // Toggle items according to match against search term.
  43. sidebarItems.forEach(item => {
  44. const itemContent = item.textContent.toLowerCase();
  45. item.classList.toggle(CLASSES.HIDE, !itemContent.includes(searchTerm));
  46. });
  47. // Toggle cards according to whether they have any visible items.
  48. sidebarCards.forEach(card => {
  49. const visibleItems = card.querySelectorAll(`${reportSelectors.regions.sidebarItem}:not(.${CLASSES.HIDE})`);
  50. card.classList.toggle(CLASSES.HIDE, !visibleItems.length);
  51. expandCard(card);
  52. });
  53. pendingPromise.resolve();
  54. };
  55. /**
  56. * Show a collapsed card.
  57. * This function simulates the behaviour of JQuery show method on a collapsible element.
  58. *
  59. * @param {Element} card
  60. */
  61. const expandCard = (card) => {
  62. let cardButton = card.querySelector('[data-toggle="collapse"]');
  63. if (cardButton.classList.contains(CLASSES.COLLAPSED)) {
  64. cardButton.classList.remove(CLASSES.COLLAPSED);
  65. cardButton.setAttribute('aria-expanded', "true");
  66. let cardContent = card.querySelector(cardButton.dataset.target);
  67. cardContent.classList.add(CLASSES.EXPANDED);
  68. }
  69. };
  70. /**
  71. * Initialise module
  72. *
  73. * @param {string} selectorId
  74. */
  75. export const init = (selectorId) => {
  76. const sidebarMenu = document.querySelector(selectorId + reportSelectors.regions.sidebarMenu);
  77. const sidebarSearch = sidebarMenu.querySelector(reportSelectors.actions.sidebarSearch);
  78. // Debounce the event listener to allow the user to finish typing.
  79. const sidebarSearchDebounce = debounce(sidebarCardFilter, DEBOUNCE_TIMER);
  80. sidebarSearch.addEventListener('keyup', event => {
  81. const pendingPromise = new Pending('core_reportbuilder/sidebar:keyup');
  82. sidebarSearchDebounce(event, sidebarMenu);
  83. setTimeout(() => {
  84. pendingPromise.resolve();
  85. }, DEBOUNCE_TIMER);
  86. });
  87. };