1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Report builder reports list management
19 * @module core_reportbuilder/reports_list
20 * @copyright 2021 David Matamoros <davidmc@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 import {dispatchEvent} from 'core/event_dispatcher';
27 import Notification from 'core/notification';
28 import Pending from 'core/pending';
29 import {prefetchStrings} from 'core/prefetch';
30 import {getString} from 'core/str';
31 import {add as addToast} from 'core/toast';
32 import * as reportEvents from 'core_reportbuilder/local/events';
33 import * as reportSelectors from 'core_reportbuilder/local/selectors';
34 import {deleteReport} from 'core_reportbuilder/local/repository/reports';
35 import {createDuplicateReportModal, createReportModal} from 'core_reportbuilder/local/repository/modals';
40 export const init = () => {
41 prefetchStrings('core_reportbuilder', [
43 'deletereportconfirm',
51 prefetchStrings('core', [
55 document.addEventListener('click', event => {
56 const reportCreate = event.target.closest(reportSelectors.actions.reportCreate);
58 event.preventDefault();
60 // Redirect user to editing interface for the report after submission.
61 const reportModal = createReportModal(event.target, getString('newreport', 'core_reportbuilder'));
62 reportModal.addEventListener(reportModal.events.FORM_SUBMITTED, event => {
63 window.location.href = event.detail;
69 const reportEdit = event.target.closest(reportSelectors.actions.reportEdit);
71 event.preventDefault();
73 // Reload current report page after submission.
74 // Use triggerElement to return focus to the action menu toggle.
75 const triggerElement = reportEdit.closest('.dropdown').querySelector('.dropdown-toggle');
76 const reportModal = createReportModal(triggerElement, getString('editreportdetails', 'core_reportbuilder'),
77 reportEdit.dataset.reportId);
78 reportModal.addEventListener(reportModal.events.FORM_SUBMITTED, () => {
79 const reportElement = event.target.closest(reportSelectors.regions.report);
81 getString('reportupdated', 'core_reportbuilder')
84 dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
87 .catch(Notification.exception);
93 const reportDuplicate = event.target.closest(reportSelectors.actions.reportDuplicate);
94 if (reportDuplicate) {
95 event.preventDefault();
97 const strDuplicateReport = getString('duplicatereport', 'core_reportbuilder');
98 const {reportId, reportName} = reportDuplicate.dataset;
100 const reportModal = createDuplicateReportModal(event.target, strDuplicateReport, reportId, reportName);
101 reportModal.addEventListener(reportModal.events.FORM_SUBMITTED, event => {
102 window.location.href = event.detail;
108 const reportDelete = event.target.closest(reportSelectors.actions.reportDelete);
110 event.preventDefault();
112 // Use triggerElement to return focus to the action menu toggle.
113 const triggerElement = reportDelete.closest('.dropdown').querySelector('.dropdown-toggle');
114 Notification.saveCancelPromise(
115 getString('deletereport', 'core_reportbuilder'),
116 getString('deletereportconfirm', 'core_reportbuilder', reportDelete.dataset.reportName),
117 getString('delete', 'core'),
120 const pendingPromise = new Pending('core_reportbuilder/reports:delete');
121 const reportElement = event.target.closest(reportSelectors.regions.report);
123 return deleteReport(reportDelete.dataset.reportId)
124 .then(() => addToast(getString('reportdeleted', 'core_reportbuilder')))
126 dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
127 return pendingPromise.resolve();
129 .catch(Notification.exception);