Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 editor
18
 *
19
 * @module      core_reportbuilder/editor
20
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
"use strict";
25
 
26
import $ from 'jquery';
27
import 'core/inplace_editable';
28
import {addIconToContainer} from 'core/loadingicon';
29
import Notification from 'core/notification';
30
import Pending from 'core/pending';
31
import Templates from 'core/templates';
32
import {getString} from 'core/str';
33
import {add as addToast} from 'core/toast';
34
import * as reportSelectors from 'core_reportbuilder/local/selectors';
35
import {init as columnsEditorInit} from 'core_reportbuilder/local/editor/columns';
36
import {init as conditionsEditorInit} from 'core_reportbuilder/local/editor/conditions';
37
import {init as filtersEditorInit} from 'core_reportbuilder/local/editor/filters';
38
import {init as sortingEditorInit} from 'core_reportbuilder/local/editor/sorting';
39
import {init as cardviewEditorInit} from 'core_reportbuilder/local/editor/card_view';
40
import {getReport} from 'core_reportbuilder/local/repository/reports';
41
import {createReportModal} from 'core_reportbuilder/local/repository/modals';
42
 
43
let initialized = false;
44
 
45
/**
46
 * Initialise editor and all it's modules
47
 */
48
export const init = () => {
49
    columnsEditorInit(initialized);
50
    conditionsEditorInit(initialized);
51
    filtersEditorInit(initialized);
52
    sortingEditorInit(initialized);
53
    cardviewEditorInit(initialized);
54
 
55
    // Ensure we only add our listeners once (can be called multiple times by mustache template).
56
    if (initialized) {
57
        return;
58
    }
59
 
60
    // Add event handlers to generic report editor elements.
61
    document.addEventListener('click', event => {
62
 
63
        // Toggle between edit and preview mode.
64
        const toggleEditViewMode = event.target.closest(reportSelectors.actions.toggleEditPreview);
65
        if (toggleEditViewMode) {
66
            event.preventDefault();
67
 
68
            const reportElement = event.target.closest(reportSelectors.regions.report);
69
            const pendingPromise = new Pending('core_reportbuilder/reports:get');
70
            const toggledEditMode = toggleEditViewMode.dataset.editMode !== "1";
71
 
72
            addIconToContainer(toggleEditViewMode)
73
                .then(() => getReport(reportElement.dataset.reportId, toggledEditMode))
74
                .then(response => Promise.all([
75
                    $.parseHTML(response.javascript, null, true).map(node => node.innerHTML).join("\n"),
76
                    Templates.renderForPromise('core_reportbuilder/local/dynamictabs/editor', response),
77
                ]))
78
                .then(([responseJs, {html, js}]) => Templates.replaceNode(reportElement, html, js + responseJs))
79
                .then(() => pendingPromise.resolve())
80
                .catch(Notification.exception);
81
        }
82
 
83
        // Edit report details modal.
84
        const reportEdit = event.target.closest(reportSelectors.actions.reportEdit);
85
        if (reportEdit) {
86
            event.preventDefault();
87
 
88
            const reportModal = createReportModal(event.target, getString('editreportdetails', 'core_reportbuilder'),
89
                reportEdit.dataset.reportId);
90
            reportModal.addEventListener(reportModal.events.FORM_SUBMITTED, () => {
91
                getString('reportupdated', 'core_reportbuilder')
92
                    .then(addToast)
93
                    .then(() => {
94
                        return window.location.reload();
95
                    })
96
                    .catch(Notification.exception);
97
            });
98
            reportModal.show();
99
        }
100
    });
101
 
102
    initialized = true;
103
};