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 to control the form responsible for selecting a preset.
18
 *
19
 * @module      mod_data/selectpreset
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
const selectors = {
25
    presetRadioButton: 'input[name="fullname"]',
26
    selectPresetButton: 'input[name="selectpreset"]',
27
    selectedPresetRadioButton: 'input[name="fullname"]:checked',
28
};
29
 
30
/**
31
 * Initialize module.
32
 */
33
export const init = () => {
34
    const radioButton = document.querySelectorAll(selectors.presetRadioButton);
35
 
36
    // Initialize the "Use a preset" button properly.
37
    disableUsePresetButton();
38
 
39
    radioButton.forEach((elem) => {
40
        elem.addEventListener('change', function(event) {
41
            event.preventDefault();
42
            // Enable the "Use a preset" button when any of the radio buttons in the presets list is checked.
43
            disableUsePresetButton();
44
        });
45
    });
46
 
47
};
48
 
49
/**
50
 * Decide whether to disable or not the "Use a preset" button.
51
 * When there is no preset selected, the button should be displayed disabled; otherwise, it will appear enabled as a primary button.
52
 *
53
 * @method
54
 * @private
55
 */
56
const disableUsePresetButton = () => {
57
    let selectPresetButton = document.querySelector(selectors.selectPresetButton);
58
    const selectedRadioButton = document.querySelector(selectors.selectedPresetRadioButton);
59
 
60
    if (selectedRadioButton) {
61
        // There is one preset selected, so the button should be enabled.
62
        selectPresetButton.removeAttribute('disabled');
63
        selectPresetButton.classList.remove('btn-secondary');
64
        selectPresetButton.classList.add('btn-primary');
65
        selectPresetButton.setAttribute('data-presetname', selectedRadioButton.getAttribute('value'));
66
        selectPresetButton.setAttribute('data-cmid', selectedRadioButton.getAttribute('data-cmid'));
67
    } else {
68
        // There is no any preset selected, so the button should be disabled.
69
        selectPresetButton.setAttribute('disabled', true);
70
        selectPresetButton.classList.remove('btn-primary');
71
        selectPresetButton.classList.add('btn-secondary');
72
        selectPresetButton.removeAttribute('data-presetname');
73
        selectPresetButton.removeAttribute('data-cmid');
74
    }
75
};