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 editing a database preset.
18
 *
19
 * @module      mod_data/editpreset
20
 * @copyright   2022 Sara Arjona <sara@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
    editPresetButton: '[data-action="editpreset"]',
30
};
31
 
32
/**
33
 * Initialize module
34
 */
35
export const init = () => {
36
    registerEventListeners();
37
};
38
 
39
/**
40
 * Register events for update/delete links.
41
 */
42
const registerEventListeners = () => {
43
    document.addEventListener('click', (event) => {
44
        const editAction = event.target.closest(selectors.editPresetButton);
45
        if (editAction) {
46
            event.preventDefault();
47
            showEditPresetModal(editAction);
48
        }
49
    });
50
};
51
 
52
/**
53
 * Show the edit preset modal.
54
 *
55
 * @param {HTMLElement} editAction the edit action element.
56
 */
57
const showEditPresetModal = (editAction) => {
58
    const modalForm = new ModalForm({
59
        modalConfig: {
60
            title: getString('editpreset', 'mod_data'),
61
        },
62
        formClass: 'mod_data\\form\\save_as_preset',
63
        args: {
64
            d: editAction.getAttribute('data-dataid'),
65
            action: editAction.getAttribute('data-action'),
66
            presetname: editAction.getAttribute('data-presetname'),
67
            presetdescription: editAction.getAttribute('data-presetdescription')
68
        },
69
        saveButtonText: getString('save'),
70
        returnFocus: editAction,
71
    });
72
 
73
    modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
74
        if (event.detail.result) {
75
            window.location.reload();
76
        } else {
77
            Notification.addNotification({
78
                type: 'error',
79
                message: event.detail.errors.join('<br>')
80
            });
81
        }
82
    });
83
 
84
    modalForm.show();
85
};