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
 * Modal for theme previews.
18
 *
19
 * @module     core_admin/themeselector/preview_modal
20
 * @copyright  2023 David Woloszyn <david.woloszyn@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import ModalEvents from 'core/modal_events';
25
import ModalCancel from 'core/modal_cancel';
26
import ModalSaveCancel from 'core/modal_save_cancel';
27
import Notification from 'core/notification';
28
import Templates from 'core/templates';
29
import {getString} from 'core/str';
30
 
31
const SELECTORS = {
32
    THEMES_CONTAINER: 'themelist',
33
    PREVIEW: '[data-action="preview"]',
34
};
35
 
36
/**
37
 * Entrypoint of the js.
38
 *
39
 * @method init
40
 */
41
export const init = () => {
42
    registerListenerEvents();
43
};
44
 
45
/**
46
 * Register theme related event listeners.
47
 *
48
 * @method registerListenerEvents
49
 */
50
const registerListenerEvents = () => {
51
    document.addEventListener('click', (e) => {
52
        const preview = e.target.closest(SELECTORS.PREVIEW);
53
        if (preview) {
54
            buildModal(preview).catch(Notification.exception);
55
        }
56
    });
57
};
58
 
59
/**
60
 * Build the modal with the provided data.
61
 *
62
 * @method buildModal
63
 * @param {object} element
64
 */
65
const buildModal = async(element) => {
66
 
67
    // This string can be long. We will fetch it with JS as opposed to passing it as an attribute.
68
    let description = await getString('choosereadme', 'theme_' + element.getAttribute('data-choose'));
69
 
70
    const themesContainer = document.getElementById(SELECTORS.THEMES_CONTAINER);
71
    const definedInConfig = parseInt(themesContainer.dataset.definedinconfig);
72
    // Prepare data for modal.
73
    const data = {
74
        name: element.getAttribute('data-name'),
75
        image: element.getAttribute('data-image'),
76
        description: description.replace(/<[^>]+>/g, ' '), // Strip out HTML tags.
77
        current: element.getAttribute('data-current'),
78
        actionurl: element.getAttribute('data-actionurl'),
79
        choose: element.getAttribute('data-choose'),
80
        sesskey: element.getAttribute('data-sesskey'),
81
        definedinconfig: definedInConfig,
82
    };
83
 
84
    // Determine which modal template we should use.
85
    let modalTemplate = ModalSaveCancel;
86
    if (data.current || data.definedinconfig) {
87
        modalTemplate = ModalCancel;
88
    }
89
 
90
    const modal = await modalTemplate.create({
91
        title: data.name,
92
        body: Templates.render('core_admin/themeselector/theme_preview_modal', data),
93
        large: true,
94
        buttons: {
95
            'save': getString('selecttheme', 'moodle'),
96
            'cancel': getString('closebuttontitle', 'moodle'),
97
        },
98
        show: true,
99
    });
100
 
101
    modal.getRoot().on(ModalEvents.save, () => {
102
        modal.getRoot().find('form').submit();
103
    });
104
};