| 1 | 
           efrain | 
           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 | 
           /**
  | 
        
        
            | 
            | 
           17 | 
            * Report builder report management
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module      core_reportbuilder/report
  | 
        
        
            | 
            | 
           20 | 
            * @copyright   2021 Paul Holden <paulh@moodle.com>
  | 
        
        
            | 
            | 
           21 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           22 | 
            */
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           import Notification from 'core/notification';
  | 
        
        
            | 
            | 
           25 | 
           import * as reportEvents from 'core_reportbuilder/local/events';
  | 
        
        
            | 
            | 
           26 | 
           import * as reportSelectors from 'core_reportbuilder/local/selectors';
  | 
        
        
            | 
            | 
           27 | 
           import {setPageNumber, refreshTableContent} from 'core_table/dynamic';
  | 
        
        
            | 
            | 
           28 | 
           import * as tableSelectors from 'core_table/local/dynamic/selectors';
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           const CLASSES = {
  | 
        
        
            | 
            | 
           31 | 
               COLLAPSED: 'collapsed',
  | 
        
        
            | 
            | 
           32 | 
               EXPANDED: 'show',
  | 
        
        
            | 
            | 
           33 | 
               ICONUP: 'fa-angle-up',
  | 
        
        
            | 
            | 
           34 | 
               ICONDOWN: 'fa-angle-down'
  | 
        
        
            | 
            | 
           35 | 
           };
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
           let initialized = false;
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
           /**
  | 
        
        
            | 
            | 
           40 | 
            * Initialise module for given report
  | 
        
        
            | 
            | 
           41 | 
            *
  | 
        
        
            | 
            | 
           42 | 
            * @method
  | 
        
        
            | 
            | 
           43 | 
            */
  | 
        
        
            | 
            | 
           44 | 
           export const init = () => {
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
               if (initialized) {
  | 
        
        
            | 
            | 
           47 | 
                   // We already added the event listeners (can be called multiple times by mustache template).
  | 
        
        
            | 
            | 
           48 | 
                   return;
  | 
        
        
            | 
            | 
           49 | 
               }
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
               // Listen for the table reload event.
  | 
        
        
            | 
            | 
           52 | 
               document.addEventListener(reportEvents.tableReload, async(event) => {
  | 
        
        
            | 
            | 
           53 | 
                   const reportElement = event.target.closest(reportSelectors.regions.report);
  | 
        
        
            | 
            | 
           54 | 
                   if (reportElement === null) {
  | 
        
        
            | 
            | 
           55 | 
                       return;
  | 
        
        
            | 
            | 
           56 | 
                   }
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
                   const tableRoot = reportElement.querySelector(tableSelectors.main.region);
  | 
        
        
            | 
            | 
           59 | 
                   const pageNumber = event.detail?.preservePagination ? null : 1;
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
                   await setPageNumber(tableRoot, pageNumber, false)
  | 
        
        
            | 
            | 
           62 | 
                       .then(refreshTableContent)
  | 
        
        
            | 
            | 
           63 | 
                       .then(() => {
  | 
        
        
            | 
            | 
           64 | 
                           // TODO: Refactor this after MDL-73130 lands.
  | 
        
        
            | 
            | 
           65 | 
                           const preserveTriggerElement = event.detail?.preserveTriggerElement;
  | 
        
        
            | 
            | 
           66 | 
                           if (preserveTriggerElement) {
  | 
        
        
            | 
            | 
           67 | 
                               reportElement.querySelector(preserveTriggerElement)?.focus();
  | 
        
        
            | 
            | 
           68 | 
                           }
  | 
        
        
            | 
            | 
           69 | 
                           return;
  | 
        
        
            | 
            | 
           70 | 
                       })
  | 
        
        
            | 
            | 
           71 | 
                       .catch(Notification.exception);
  | 
        
        
            | 
            | 
           72 | 
               });
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
               // Listen for trigger popup events.
  | 
        
        
            | 
            | 
           75 | 
               document.addEventListener('click', event => {
  | 
        
        
            | 
            | 
           76 | 
                   const reportActionPopup = event.target.closest(reportSelectors.actions.reportActionPopup);
  | 
        
        
            | 
            | 
           77 | 
                   if (reportActionPopup === null) {
  | 
        
        
            | 
            | 
           78 | 
                       return;
  | 
        
        
            | 
            | 
           79 | 
                   }
  | 
        
        
            | 
            | 
           80 | 
                   event.preventDefault();
  | 
        
        
            | 
            | 
           81 | 
                   const popupAction = JSON.parse(reportActionPopup.dataset.popupAction);
  | 
        
        
            | 
            | 
           82 | 
                   window.openpopup(event, popupAction.jsfunctionargs);
  | 
        
        
            | 
            | 
           83 | 
               });
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
               // Listen for card view toggle events.
  | 
        
        
            | 
            | 
           86 | 
               document.addEventListener('click', (event) => {
  | 
        
        
            | 
            | 
           87 | 
                   const toggleCard = event.target.closest(reportSelectors.actions.toggleCardView);
  | 
        
        
            | 
            | 
           88 | 
                   if (toggleCard) {
  | 
        
        
            | 
            | 
           89 | 
                       const tableCard = toggleCard.closest('tr');
  | 
        
        
            | 
            | 
           90 | 
                       const toggleIcon = toggleCard.querySelector('i');
  | 
        
        
            | 
            | 
           91 | 
                       event.preventDefault();
  | 
        
        
            | 
            | 
           92 | 
                       if (toggleCard.classList.contains(CLASSES.COLLAPSED)) {
  | 
        
        
            | 
            | 
           93 | 
                           tableCard.classList.add(CLASSES.EXPANDED);
  | 
        
        
            | 
            | 
           94 | 
                           toggleIcon.classList.replace(CLASSES.ICONDOWN, CLASSES.ICONUP);
  | 
        
        
            | 
            | 
           95 | 
                           toggleCard.classList.remove(CLASSES.COLLAPSED);
  | 
        
        
            | 
            | 
           96 | 
                           toggleCard.setAttribute('aria-expanded', "true");
  | 
        
        
            | 
            | 
           97 | 
                       } else {
  | 
        
        
            | 
            | 
           98 | 
                           tableCard.classList.remove(CLASSES.EXPANDED);
  | 
        
        
            | 
            | 
           99 | 
                           toggleIcon.classList.replace(CLASSES.ICONUP, CLASSES.ICONDOWN);
  | 
        
        
            | 
            | 
           100 | 
                           toggleCard.classList.add(CLASSES.COLLAPSED);
  | 
        
        
            | 
            | 
           101 | 
                           toggleCard.removeAttribute('aria-expanded');
  | 
        
        
            | 
            | 
           102 | 
                       }
  | 
        
        
            | 
            | 
           103 | 
                   }
  | 
        
        
            | 
            | 
           104 | 
               });
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
               initialized = true;
  | 
        
        
            | 
            | 
           107 | 
           };
  |