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
 * The collapsable sections controls.
18
 *
19
 * @module     core/local/collapsable_section/controls
20
 * @copyright  2024 Ferran Recio <ferran@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 *
23
 * @example <caption>Example of controlling a collapsable section.</caption>
24
 *
25
 * import CollapsableSection from 'core/local/collapsable_section/controls';
26
 *
27
 * const section = CollapsableSection.instanceFromSelector('#MyCollapsableSection');
28
 *
29
 * // Use hide, show and toggle methods to control the section.
30
 * section.hide();
31
 */
32
 
33
import {
34
    eventTypes,
35
    notifyCollapsableSectionHidden,
36
    notifyCollapsableSectionShown
37
} from 'core/local/collapsable_section/events';
38
import Collapse from 'theme_boost/bootstrap/collapse';
39
 
40
let initialized = false;
41
 
42
export default class {
43
    /**
44
     * Create a new instance from a query selector.
45
     *
46
     * @param {String} selector The selector of the collapsable section.
47
     * @return {CollapsableSection} The collapsable section controls.
48
     * @throws {Error} If no elements are found with the selector.
49
     */
50
    static instanceFromSelector(selector) {
51
        const elements = document.querySelector(selector);
52
        if (!elements) {
53
            throw new Error('No elements found with the selector: ' + selector);
54
        }
55
        return new this(elements);
56
    }
57
 
58
    /**
59
     * Initialize the collapsable section controls.
60
     */
61
    static init() {
62
        if (initialized) {
63
            return;
64
        }
65
        initialized = true;
66
 
67
        // We want to add extra events to the standard bootstrap collapsable events.
68
        document.addEventListener(eventTypes.hiddenBsCollapse, event => {
69
            if (!this.isCollapsableComponent(event.target)) {
70
                return;
71
            }
72
            notifyCollapsableSectionHidden(event.target);
73
        });
74
        document.addEventListener(eventTypes.shownBsCollapse, event => {
75
            if (!this.isCollapsableComponent(event.target)) {
76
                return;
77
            }
78
            notifyCollapsableSectionShown(event.target);
79
        });
80
    }
81
 
82
    /**
83
     * Check if the element is a collapsable section.
84
     *
85
     * @private
86
     * @param {HTMLElement} element The element to check.
87
     * @return {boolean} True if the element is a collapsable section.
88
     */
89
    static isCollapsableComponent(element) {
90
        return element.hasAttribute('data-mdl-component')
91
            && element.getAttribute('data-mdl-component') === 'core/local/collapsable_section';
92
    }
93
 
94
    /**
95
     * Creates an instance of the controls for a collapsable section.
96
     *
97
     * @param {HTMLElement} element - The DOM element that this control will manage.
98
     */
99
    constructor(element) {
100
        this.element = element;
101
    }
102
 
103
    /**
104
     * Hides the collapsible section element.
105
     */
106
    hide() {
107
        Collapse.getOrCreateInstance(this.element).hide();
108
    }
109
 
110
    /**
111
     * Shows the collapsible section element.
112
     */
113
    show() {
114
        Collapse.getOrCreateInstance(this.element).show();
115
    }
116
 
117
    /**
118
     * Toggle the collapsible section element.
119
     */
120
    toggle() {
121
        Collapse.getOrCreateInstance(this.element).toggle();
122
    }
123
 
124
    /**
125
     * Check if the collapsable section is visible.
126
     *
127
     * @return {boolean} True if the collapsable section is visible.
128
     */
129
    isVisible() {
130
        return Collapse.getOrCreateInstance(this.element)._isShown();
131
    }
132
}