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
// Moodle is free software: you can redistribute it and/or modify
3
// it under the terms of the GNU General Public License as published by
4
// the Free Software Foundation, either version 3 of the License, or
5
// (at your option) any later version.
6
//
7
// Moodle is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
// GNU General Public License for more details.
11
//
12
// You should have received a copy of the GNU General Public License
13
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
14
 
15
/**
16
 * Javascript events for the `core_block` subsystem.
17
 *
18
 * @module     core_block/events
19
 * @copyright  2021 Andrew Nicols <andrew@nicols.co.uk>
20
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 * @since      4.0
22
 *
23
 * @example <caption>Example of listening to a block event.</caption>
24
 * import {eventTypes as blockEventTypes} from 'core_block/events';
25
 *
26
 * document.addEventListener(blockEventTypes.blockContentUpdated, e => {
27
 *     window.console.log(e.target); // The HTMLElement relating to the block whose content was updated.
28
 *     window.console.log(e.detail.instanceId); // The instanceId of the block that was updated.
29
 * });
30
 */
31
 
32
import {dispatchEvent} from 'core/event_dispatcher';
33
 
34
/**
35
 * Events for `core_block`.
36
 *
37
 * @constant
38
 * @property {String} blockContentUpdated See {@link event:blockContentUpdated}
39
 */
40
export const eventTypes = {
41
    /**
42
     * An event triggered when the content of a block has changed.
43
     *
44
     * @event blockContentUpdated
45
     * @type {CustomEvent}
46
     * @property {HTMLElement} target The block element that was updated
47
     * @property {object} detail
48
     * @property {number} detail.instanceId The block instance id
49
     */
50
    blockContentUpdated: 'core_block/contentUpdated',
51
};
52
 
53
/**
54
 * Trigger an event to indicate that the content of a block was updated.
55
 *
56
 * @method notifyBlockContentUpdated
57
 * @param {HTMLElement} element The HTMLElement containing the updated block.
58
 * @returns {CustomEvent}
59
 * @fires blockContentUpdated
60
 */
61
export const notifyBlockContentUpdated = element => dispatchEvent(
62
    eventTypes.blockContentUpdated,
63
    {
64
        instanceId: element.dataset.instanceId,
65
    },
66
    element
67
);
68
 
69
let legacyEventsRegistered = false;
70
if (!legacyEventsRegistered) {
71
    // The following event triggers are legacy and will be removed in the future.
72
    // The following approach provides a backwards-compatability layer for the new events.
73
    // Code should be updated to make use of native events.
74
 
75
    Y.use('event', 'moodle-core-event', Y => {
76
        // Provide a backwards-compatability layer for YUI Events.
77
        document.addEventListener(eventTypes.blockContentUpdated, e => {
78
            // Trigger the legacy YUI event.
79
            Y.Global.fire(M.core.event.BLOCK_CONTENT_UPDATED, {instanceid: e.detail.instanceId});
80
        });
81
    });
82
 
83
    legacyEventsRegistered = true;
84
}