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_filters` subsystem.
17
 *
18
 * @module     core_filters/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 filter event.</caption>
24
 * import {eventTypes as filterEventTypes} from 'core_filters/events';
25
 *
26
 * document.addEventListener(filterEventTypes.filterContentUpdated, e => {
27
 *     window.console.log(e.detail.nodes); // A list of the HTMLElements whose content was updated
28
 * });
29
 */
30
 
31
import {dispatchEvent} from 'core/event_dispatcher';
32
import {getList as normalistNodeList} from 'core/normalise';
33
import jQuery from 'jquery';
34
 
35
/**
36
 * Events for the `core_filters` subsystem.
37
 *
38
 * @constant
39
 * @property {String} filterContentUpdated See {@link event:filterContentUpdated}
40
 * @property {String} filterContentRenderingComplete See {@link event:filterContentRenderingComplete}
41
 */
42
export const eventTypes = {
43
    /**
44
     * An event triggered when page content is updated and must be processed by the filter system.
45
     *
46
     * An example of this is loading user text that could have equations in it. MathJax can typeset the equations but
47
     * only if it is notified that there are new nodes in the page that need processing.
48
     *
49
     * @event filterContentUpdated
50
     * @type {CustomEvent}
51
     * @property {object} detail
52
     * @property {NodeElement[]} detail.nodes The list of parent nodes which were updated
53
     */
54
    filterContentUpdated: 'core_filters/contentUpdated',
55
 
56
    /**
57
     * An event triggered when filter system have done rendering the content using the filter system.
58
     *
59
     * @event filterContentRenderingComplete
60
     * @type {CustomEvent}
61
     * @property {object} detail
62
     */
63
    filterContentRenderingComplete: 'core_filters/contentRenderingComplete',
64
};
65
 
66
/**
67
 * Trigger an event to indicate that the specified nodes were updated and should be processed by the filter system.
68
 *
69
 * @method notifyFilterContentUpdated
70
 * @param {jQuery|Array} nodes
71
 * @returns {CustomEvent}
72
 * @fires filterContentUpdated
73
 */
74
export const notifyFilterContentUpdated = nodes => {
75
    // Historically this could be a jQuery Object.
76
    // Normalise the list of nodes to a NodeList.
77
    nodes = normalistNodeList(nodes);
78
 
79
    return dispatchEvent(eventTypes.filterContentUpdated, {nodes});
80
};
81
 
82
/**
83
 * Trigger an event to indicate that the filter has been processed.
84
 *
85
 * @method notifyFilterContentRenderingComplete
86
 * @param {NodeList|Node[]} nodes List of nodes that has been modified by filter
87
 * @returns {CustomEvent}
88
 * @fires filterContentRenderingComplete
89
 */
90
export const notifyFilterContentRenderingComplete = nodes => {
91
    return dispatchEvent(eventTypes.filterContentRenderingComplete, {nodes});
92
};
93
 
94
let legacyEventsRegistered = false;
95
if (!legacyEventsRegistered) {
96
    // The following event triggers are legacy and will be removed in the future.
97
    // The following approach provides a backwards-compatability layer for the new events.
98
    // Code should be updated to make use of native events.
99
 
100
    Y.use('event', 'moodle-core-event', () => {
101
        // Provide a backwards-compatability layer for YUI Events.
102
        document.addEventListener(eventTypes.filterContentUpdated, e => {
103
            // Trigger the legacy jQuery event.
104
            jQuery(document).trigger(M.core.event.FILTER_CONTENT_UPDATED, [jQuery(e.detail.nodes)]);
105
 
106
            // Trigger the legacy YUI event.
107
            Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: new Y.NodeList(e.detail.nodes)});
108
        });
109
    });
110
 
111
    legacyEventsRegistered = true;
112
}