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
 * Controls the default settings for the list of notification types on the
18
 * notifications admin page
19
 *
20
 * @module     core_message/default_notification_preferences
21
 * @class      default_notification_preferences
22
 * @copyright  2021 Moodle
23
 * @author     Pau Ferrer Ocaña <pau@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
const selectors = {
28
    provider: '.defaultmessageoutputs .provider_enabled',
29
    lockSetting: '.locked_message_setting',
30
    enabledSetting: '.enabled_message_setting',
31
    allSettings: '.locked_message_setting, .enabled_message_setting'
32
};
33
 
34
/**
35
 * Register event listeners for the default_notification_preferences page.
36
 */
37
const registerEventListeners = () => {
38
 
39
    /**
40
     * Update the dimmed status of the "enabled" toggle on the notification setting.
41
     *
42
     * @param {HTMLElement} lockedElement Element that receives the event.
43
     */
44
    const toggleLockSetting = (lockedElement) => {
45
        const isEnabled = lockedElement.checked || false;
46
        const enabledId = lockedElement.id.replace('_locked[', '_enabled[');
47
 
48
        const enabledElement = document.getElementById(enabledId).closest('div.custom-control');
49
        enabledElement.classList.toggle('dimmed_text', isEnabled);
50
    };
51
 
52
    /**
53
     * Enable/Disable all settings of the provider.
54
     *
55
     * @param {HTMLElement} providerEnabledElement Element that receives the event.
56
     */
57
    const toggleEnableProviderSettings = (providerEnabledElement) => {
58
        const isEnabled = providerEnabledElement.checked || false;
59
        const parentRow = providerEnabledElement.closest('tr');
60
 
61
        const elements = parentRow.querySelectorAll(selectors.allSettings);
62
        elements.forEach((element) => {
63
            element.toggleAttribute('disabled', !isEnabled);
64
        });
65
    };
66
 
67
    const container = document.querySelector('.preferences-container');
68
 
69
    container.querySelectorAll(selectors.provider).forEach((providerEnabledElement) => {
70
        // Set the initial statuses.
71
        if (!providerEnabledElement.checked) {
72
            toggleEnableProviderSettings(providerEnabledElement);
73
        }
74
 
75
        providerEnabledElement.addEventListener('change', (e) => {
76
            toggleEnableProviderSettings(e.target);
77
        });
78
    });
79
 
80
    container.querySelectorAll(selectors.lockSetting).forEach((lockedElement) => {
81
        // Set the initial statuses.
82
        if (lockedElement.checked) {
83
            toggleLockSetting(lockedElement);
84
        }
85
 
86
        lockedElement.addEventListener('change', (e) => {
87
            toggleLockSetting(e.target);
88
        });
89
    });
90
};
91
 
92
/**
93
 * Initialize the page.
94
 */
95
const init = () => {
96
    registerEventListeners();
97
};
98
 
99
export default {
100
    init: init,
101
};