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
 * @module     core_group/groupPicker
17
 * @copyright  2022 Matthew Hilton <matthewhilton@catalyst-au.net>
18
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
19
 */
20
 
21
/**
22
 * Class used for interfacing with the group select picker.
23
 *
24
 * @class core_group/GroupPicker
25
 */
26
export default class GroupPicker {
27
    /**
28
     * Creates the group picker class and finds the corresponding DOM element.
29
     *
30
     * @param {String} elementId The DOM element id of the <select> input
31
     * @throws Error if the element was not found.
32
     */
33
    constructor(elementId = "groups") {
34
        const pickerDomElement = document.getElementById(elementId);
35
 
36
        if (!pickerDomElement) {
37
            throw new Error("Groups picker was not found.");
38
        }
39
 
40
        this.element = pickerDomElement;
41
    }
42
 
43
    /**
44
     * Returns the DOM element this class is linked to.
45
     *
46
     * @returns {HTMLElement} The DOM element
47
     */
48
    getDomElement() {
49
        return this.element;
50
    }
51
 
52
    /**
53
     * Returns the selected group values.
54
     *
55
     * @returns {Number[]} The group IDs that are currently selected.
56
     */
57
    getSelectedValues() {
58
        const selectedOptionElements = Array.from(this.element.querySelectorAll("option:checked"));
59
        const selectedGroups = selectedOptionElements.map(el => parseInt(el.value));
60
 
61
        return selectedGroups;
62
    }
63
}