grade/report/grader/amd/src/stickycolspan.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 fixing the position of sticky headers with multiple colspans
  17. *
  18. * @module gradereport_grader/stickycolspan
  19. * @copyright 2022 Bas Brands <bas@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import {SELECTORS as stickyFooterSelectors, eventTypes as stickyFooterEvents} from 'core/sticky-footer';
  23. const SELECTORS = {
  24. GRADEPARENT: '.gradeparent',
  25. STUDENTHEADER: '#studentheader',
  26. TABLEHEADER: 'th.header',
  27. BEHAT: 'body.behat-site',
  28. USERDROPDOWN: '.userrow th .dropdown',
  29. LASTROW: '.lastrow',
  30. };
  31. /**
  32. * Initialize module
  33. */
  34. export const init = () => {
  35. // The sticky positioning attributed to the user column cells affects the stacking context and makes the dropdowns
  36. // within these cells to be cut off. To solve this problem, whenever one of these action menus (dropdowns) is opened
  37. // we need to manually bump up the z-index value of the parent container element and revert once closed.
  38. document.querySelectorAll(SELECTORS.USERDROPDOWN).forEach((dropdown) => {
  39. dropdown.addEventListener('show.bs.dropdown', (e) => {
  40. // The closest heading element has sticky positioning which affects the stacking context in this case.
  41. e.target.closest(SELECTORS.TABLEHEADER).classList.add('actions-menu-active');
  42. });
  43. dropdown.addEventListener('hide.bs.dropdown', (e) => {
  44. e.target.closest(SELECTORS.TABLEHEADER).classList.remove('actions-menu-active');
  45. });
  46. });
  47. defineLastRowIntersectionObserver(true);
  48. // Add an event listener to the sticky footer toggled event to re-define the average row intersection observer
  49. // accordingly. This is needed as on narrow screens when scrolling vertically the sticky footer is enabled and
  50. // disabled dynamically.
  51. document.addEventListener(stickyFooterEvents.stickyFooterStateChanged, (e) => {
  52. defineLastRowIntersectionObserver(e.detail.enabled);
  53. });
  54. if (!document.querySelector(SELECTORS.BEHAT)) {
  55. const grader = document.querySelector(SELECTORS.GRADEPARENT);
  56. const tableHeaders = grader.querySelectorAll(SELECTORS.TABLEHEADER);
  57. const studentHeader = grader.querySelector(SELECTORS.STUDENTHEADER);
  58. const leftOffset = getComputedStyle(studentHeader).getPropertyValue('left');
  59. const rightOffset = getComputedStyle(studentHeader).getPropertyValue('right');
  60. tableHeaders.forEach((tableHeader) => {
  61. if (tableHeader.colSpan > 1) {
  62. const addOffset = (tableHeader.offsetWidth - studentHeader.offsetWidth);
  63. if (window.right_to_left()) {
  64. tableHeader.style.right = 'calc(' + rightOffset + ' - ' + addOffset + 'px )';
  65. } else {
  66. tableHeader.style.left = 'calc(' + leftOffset + ' - ' + addOffset + 'px )';
  67. }
  68. }
  69. });
  70. }
  71. };
  72. /**
  73. * Define the intersection observer that will make sure that the last row is properly pinned.
  74. *
  75. * In certain scenarios, such as when both 'Overall average' and 'Range' are set not to be shown in the Grader report,
  76. * a user row will end up being the last row in the Grader report table. In this particular case, we want to avoid
  77. * pinning the last row.
  78. *
  79. * @param {boolean} stickyFooterEnabled Whether the page shows a sticky footer or not.
  80. */
  81. const defineLastRowIntersectionObserver = (stickyFooterEnabled) => {
  82. const lastRow = document.querySelector(SELECTORS.LASTROW);
  83. // Ensure that the last row is not a user row before defining the intersection observer.
  84. if (!lastRow.classList.contains('userrow')) {
  85. const stickyFooterHeight = stickyFooterEnabled ?
  86. document.querySelector(stickyFooterSelectors.STICKYFOOTER).offsetHeight : null;
  87. // Register an observer that will bump up the z-index value of the last row when it's pinned to prevent the row
  88. // being cut-off by the user column cells or other components within the report table that have higher z-index
  89. // values. If the page has a sticky footer, we need to make sure that the bottom root margin of the observer
  90. // subtracts the height of the sticky footer to prevent the row being cut-off by the footer.
  91. const intersectionObserver = new IntersectionObserver(
  92. ([e]) => lastRow.classList.toggle('pinned', e.intersectionRatio < 1),
  93. {
  94. rootMargin: stickyFooterHeight ? `0px 0px -${stickyFooterHeight}px 0px` : "0px",
  95. threshold: [1]
  96. }
  97. );
  98. intersectionObserver.observe(lastRow.querySelector('th'));
  99. }
  100. };