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
 * The bulk editor toggler button control.
18
 *
19
 * @module     core_courseformat/local/content/bulkedittoggler
20
 * @class      core_courseformat/local/content/bulkedittoggler
21
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
import {BaseComponent} from 'core/reactive';
26
import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';
27
import Pending from 'core/pending';
28
 
29
export default class Component extends BaseComponent {
30
 
31
    /**
32
     * Constructor hook.
33
     */
34
    create() {
35
        // Optional component name for debugging.
36
        this.name = 'bulk_editor_toogler';
37
        // Default query selectors.
38
        this.selectors = {
39
            BODY: `body`,
40
            SELECTABLE: `[data-bulkcheckbox][data-is-selectable]`,
41
        };
42
        // Component css classes.
43
        this.classes = {
44
            HIDDEN: `d-none`,
45
            BULK: `bulkenabled`,
46
        };
47
    }
48
 
49
    /**
50
     * Static method to create a component instance from the mustache template.
51
     *
52
     * @param {string} target optional altentative DOM main element CSS selector
53
     * @param {object} selectors optional css selector overrides
54
     * @return {Component}
55
     */
56
    static init(target, selectors) {
57
        return new this({
58
            element: document.querySelector(target),
59
            reactive: getCurrentCourseEditor(),
60
            selectors
61
        });
62
    }
63
 
64
    /**
65
     * Initial state ready method.
66
     */
67
    stateReady() {
68
        // Capture completion events.
69
        this.addEventListener(
70
            this.element,
71
            'click',
72
            this._enableBulk
73
        );
74
    }
75
 
76
    /**
77
     * Component watchers.
78
     *
79
     * @returns {Array} of watchers
80
     */
81
    getWatchers() {
82
        return [
83
            {watch: `bulk.enabled:updated`, handler: this._refreshToggler},
84
        ];
85
    }
86
 
87
    /**
88
     * Update a content section using the state information.
89
     *
90
     * @param {object} param
91
     * @param {Object} param.element details the update details (state.bulk in this case).
92
     */
93
    _refreshToggler({element}) {
94
        this.element.classList.toggle(this.classes.HIDDEN, element.enabled ?? false);
95
        document.querySelector(this.selectors.BODY)?.classList.toggle(this.classes.BULK, element.enabled);
96
    }
97
 
98
    /**
99
     * Dispatch the enable bulk mutation.
100
     *
101
     * The enable bulk button is outside of the course content main div.
102
     * Because content/actions captures click events only in the course
103
     * content, this button needs to trigger the enable bulk mutation
104
     * by itself.
105
     */
106
    _enableBulk() {
107
        const pendingToggle = new Pending(`courseformat/content:bulktoggle_on`);
108
        this.reactive.dispatch('bulkEnable', true);
109
        // Wait for a while and focus on the first checkbox.
110
        setTimeout(() => {
111
            document.querySelector(this.selectors.SELECTABLE)?.focus();
112
            pendingToggle.resolve();
113
        }, 150);
114
    }
115
}