weekly release 5.0dev
[moodle.git] / reportbuilder / amd / src / reports_list.js
blobb40b58dd7a9b4f5511bce5971f5cf31bb055cb7b
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.
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/>.
16 /**
17  * Report builder reports list management
18  *
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
22  */
24 "use strict";
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';
37 /**
38  * Initialise module
39  */
40 export const init = () => {
41     prefetchStrings('core_reportbuilder', [
42         'deletereport',
43         'deletereportconfirm',
44         'duplicatereport',
45         'editreportdetails',
46         'newreport',
47         'reportdeleted',
48         'reportupdated',
49     ]);
51     prefetchStrings('core', [
52         'delete',
53     ]);
55     document.addEventListener('click', event => {
56         const reportCreate = event.target.closest(reportSelectors.actions.reportCreate);
57         if (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;
64             });
66             reportModal.show();
67         }
69         const reportEdit = event.target.closest(reportSelectors.actions.reportEdit);
70         if (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')
82                     .then(addToast)
83                     .then(() => {
84                         dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
85                         return;
86                     })
87                     .catch(Notification.exception);
88             });
90             reportModal.show();
91         }
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;
103             });
105             reportModal.show();
106         }
108         const reportDelete = event.target.closest(reportSelectors.actions.reportDelete);
109         if (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'),
118                 {triggerElement}
119             ).then(() => {
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')))
125                     .then(() => {
126                         dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
127                         return pendingPromise.resolve();
128                     })
129                     .catch(Notification.exception);
130             }).catch(() => {
131                 return;
132             });
133         }
134     });