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
 * Javascript module for saving a database as a preset.
18
 *
19
 * @module      mod_data/saveaspreset
20
 * @copyright   2021 Mihail Geshoski <mihail@moodle.com>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import ModalForm from 'core_form/modalform';
25
import Notification from 'core/notification';
26
import {getString} from 'core/str';
27
 
28
const selectors = {
29
    saveAsPresetButton: '[data-action="saveaspreset"]',
30
};
31
 
32
/**
33
 * Initialize module.
34
 */
35
export const init = () => {
36
 
37
    document.addEventListener('click', (event) => {
38
        const saveAsPresetButton = event.target.closest(selectors.saveAsPresetButton);
39
 
40
        if (!saveAsPresetButton) {
41
            return;
42
        }
43
 
44
        event.preventDefault();
45
        const modalForm = new ModalForm({
46
            modalConfig: {
47
                title: getString('savedataaspreset', 'mod_data'),
48
            },
49
            formClass: 'mod_data\\form\\save_as_preset',
50
            args: {d: saveAsPresetButton.dataset.dataid},
51
            saveButtonText: getString('save'),
52
            returnFocus: saveAsPresetButton,
53
        });
54
 
55
        // Show a toast notification when the form is submitted.
56
        modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
57
            if (event.detail.result) {
58
                window.location.reload();
59
            } else {
60
                Notification.addNotification({
61
                    type: 'error',
62
                    message:  event.detail.errors.join('<br>')
63
                });
64
            }
65
        });
66
 
67
        modalForm.show();
68
    });
69
};