Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
 * Expand the collapse section element.
18
 *
19
 * @module      core_admin/expand_hash
20
 * @copyright   Meirza <meirza.arson@moodle.com>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @since       4.5
23
 */
24
 
25
const SELECTORS = {
26
    COLLAPSE_ELEMENTS: '[data-bs-toggle="collapse"]',
27
    FOCUSTHENEXPAND_ELEMENTS: '.focus-expand',
28
};
29
 
30
/**
31
 * Initializes the focus and expand functionality.
32
 */
33
export const init = () => {
34
    // Select all collapsible elements only.
35
    const focusexpand = document.querySelector(SELECTORS.FOCUSTHENEXPAND_ELEMENTS);
36
 
37
    // Add click event listener to the anchor element
38
    focusexpand?.addEventListener('click', () => {
39
        expandSection(`${focusexpand.getAttribute('href')}`);
40
    });
41
};
42
 
43
/**
44
 * Expands a section based on the provided URL hash.
45
 *
46
 * This function takes a hash string, finds the corresponding element in the DOM,
47
 * and expands it if it is currently collapsed. It updates the necessary ARIA
48
 * attributes and classes to reflect the expanded state.
49
 *
50
 * @param {string} hash - The hash (e.g., '#elementId') of the element to expand.
51
 */
52
export const expandSection = (hash) => {
53
    const container = document.querySelector(hash);
54
    const targetContainer = container?.querySelector(SELECTORS.COLLAPSE_ELEMENTS);
55
 
56
    if (targetContainer?.getAttribute('aria-expanded') === 'false') {
57
        const collapseId = targetContainer.getAttribute('aria-controls');
58
        const collapseContainer = document.getElementById(collapseId);
59
 
60
        // Show the content.
61
        collapseContainer.classList.remove('collapse');
62
        collapseContainer.classList.add('show');
63
 
64
        // Update aria-expanded attribute to reflect the new state.
65
        targetContainer.setAttribute('aria-expanded', 'true');
66
        targetContainer.classList.remove('collapsed');
67
 
68
        // Get collapse expand menu element.
69
        const collapseElement = document.querySelector('.collapseexpand.collapsemenu');
70
        // Ensure it gets noticed to make it work.
71
        collapseElement.setAttribute('aria-expanded', 'true');
72
        collapseElement.classList.remove('collapsed');
73
    }
74
};