grade/report/user/amd/src/gradecategorytoggle.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. * Javascript module for toggling the visibility of the grade categories in the user report.
  17. *
  18. * @module gradereport_user/gradecategorytoggle
  19. * @copyright 2022 Mihail Geshoski <mihail@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. const SELECTORS = {
  23. CATEGORY_TOGGLE: '.toggle-category',
  24. USER_REPORT_TABLE: '.user-grade'
  25. };
  26. /**
  27. * Register related event listeners.
  28. *
  29. * @method registerListenerEvents
  30. * @param {string} userReportId The ID of the user report container element.
  31. */
  32. const registerListenerEvents = (userReportId) => {
  33. const reportContainer = document.querySelector('#' + userReportId);
  34. const userReport = reportContainer.querySelector(SELECTORS.USER_REPORT_TABLE);
  35. userReport.addEventListener('click', e => {
  36. const toggle = e.target.closest(SELECTORS.CATEGORY_TOGGLE);
  37. if (toggle) {
  38. toggleCategory(toggle);
  39. }
  40. });
  41. };
  42. /**
  43. * Method that handles the category toggle action.
  44. *
  45. * @method toggleCategory
  46. * @param {object} toggleElement The category toggle node that was clicked.
  47. */
  48. const toggleCategory = (toggleElement) => {
  49. const target = toggleElement.dataset.target;
  50. const categoryId = toggleElement.dataset.categoryid;
  51. // Whether the toggle action is collapsing the category or not.
  52. const isCollapsing = toggleElement.getAttribute('aria-expanded') === "true";
  53. const userReport = toggleElement.closest(SELECTORS.USER_REPORT_TABLE);
  54. // Find all targeted 'children' rows of the toggled category.
  55. const targetRows = userReport.querySelectorAll(target);
  56. if (isCollapsing) {
  57. toggleElement.setAttribute('aria-expanded', 'false');
  58. // Update the 'data-target' of the toggle category node to make sure that when we perform another toggle action
  59. // to expand this category we only target rows which have been hidden by this category toggle action.
  60. toggleElement.dataset.target = `[data-hidden-by='${categoryId}']`;
  61. } else {
  62. toggleElement.setAttribute('aria-expanded', 'true');
  63. // Update the 'data-target' of the toggle category node to make sure that when we perform another toggle action
  64. // to collapse this category we only target rows which are children of this category and are not currently hidden.
  65. toggleElement.dataset.target = `.cat_${categoryId}[data-hidden='false']`;
  66. }
  67. // Loop through all targeted children row elements and update the required data attributes to either hide or show
  68. // them depending on the toggle action (collapsing or expanding).
  69. targetRows.forEach((row) => {
  70. if (isCollapsing) {
  71. row.dataset.hidden = 'true';
  72. row.dataset.hiddenBy = categoryId;
  73. } else {
  74. row.dataset.hidden = 'false';
  75. row.dataset.hiddenBy = '';
  76. }
  77. });
  78. // Since the user report is presented in an HTML table, rowspans are used under each category to create a visual
  79. // hierarchy between categories and grading items. When expanding or collapsing a category we need to also update
  80. // (subtract or add) the rowspan values associated to each parent category row to preserve the correct visual
  81. // hierarchy in the table.
  82. updateParentCategoryRowspans(toggleElement, targetRows.length);
  83. };
  84. /**
  85. * Method that updates the rowspan value of all 'parent' category rows of a given category node.
  86. *
  87. * @method updateParentCategoryRowspans
  88. * @param {object} toggleElement The category toggle node that was clicked.
  89. * @param {int} num The number we want to add or subtract from the rowspan value of the 'parent' category row elements.
  90. */
  91. const updateParentCategoryRowspans = (toggleElement, num) => {
  92. const userReport = toggleElement.closest(SELECTORS.USER_REPORT_TABLE);
  93. // Get the row element which contains the category toggle node.
  94. const rowElement = toggleElement.closest('tr');
  95. // Loop through the class list of the toggle category row element.
  96. // The list contains classes which identify all parent categories of the toggled category.
  97. rowElement.classList.forEach((className) => {
  98. // Find the toggle node of the 'parent' category that is identified by the given class name.
  99. const parentCategoryToggleElement = userReport.querySelector(`[data-target=".${className}[data-hidden='false']"`);
  100. if (parentCategoryToggleElement) {
  101. // Get the row element which contains the parent category toggle node.
  102. const categoryRowElement = parentCategoryToggleElement.closest('tr');
  103. // Find the rowspan element associated to this parent category.
  104. const categoryRowSpanElement = categoryRowElement.nextElementSibling.querySelector('[rowspan]');
  105. // Depending on whether the toggle action has expanded or collapsed the category, either add or
  106. // subtract from the 'parent' category rowspan.
  107. if (toggleElement.getAttribute('aria-expanded') === "true") {
  108. categoryRowSpanElement.rowSpan = categoryRowSpanElement.rowSpan + num;
  109. } else { // The category has been collapsed.
  110. categoryRowSpanElement.rowSpan = categoryRowSpanElement.rowSpan - num;
  111. }
  112. }
  113. });
  114. };
  115. /**
  116. * Init method.
  117. *
  118. * @param {string} userReportId The ID of the user report container element.
  119. */
  120. export const init = (userReportId) => {
  121. registerListenerEvents(userReportId);
  122. };