Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 
34
/**
35
 * Events for the `core_filters` subsystem.
36
 *
37
 * @constant
38
 * @property {String} filterContentUpdated See {@link event:filterContentUpdated}
39
 * @property {String} filterContentRenderingComplete See {@link event:filterContentRenderingComplete}
40
 */
41
export const eventTypes = {
42
    /**
43
     * An event triggered when page content is updated and must be processed by the filter system.
44
     *
45
     * An example of this is loading user text that could have equations in it. MathJax can typeset the equations but
46
     * only if it is notified that there are new nodes in the page that need processing.
47
     *
48
     * @event filterContentUpdated
49
     * @type {CustomEvent}
50
     * @property {object} detail
51
     * @property {NodeElement[]} detail.nodes The list of parent nodes which were updated
52
     */
53
    filterContentUpdated: 'core_filters/contentUpdated',
54
 
55
    /**
56
     * An event triggered when filter system have done rendering the content using the filter system.
57
     *
58
     * @event filterContentRenderingComplete
59
     * @type {CustomEvent}
60
     * @property {object} detail
61
     */
62
    filterContentRenderingComplete: 'core_filters/contentRenderingComplete',
63
};
64
 
65
/**
66
 * Trigger an event to indicate that the specified nodes were updated and should be processed by the filter system.
67
 *
68
 * @method notifyFilterContentUpdated
69
 * @param {jQuery|Array} nodes
70
 * @returns {CustomEvent}
71
 * @fires filterContentUpdated
72
 */
73
export const notifyFilterContentUpdated = nodes => {
74
    // Historically this could be a jQuery Object.
75
    // Normalise the list of nodes to a NodeList.
76
    nodes = normalistNodeList(nodes);
77
 
78
    return dispatchEvent(eventTypes.filterContentUpdated, {nodes});
79
};
80
 
81
/**
82
 * Trigger an event to indicate that the filter has been processed.
83
 *
84
 * @method notifyFilterContentRenderingComplete
85
 * @param {NodeList|Node[]} nodes List of nodes that has been modified by filter
86
 * @returns {CustomEvent}
87
 * @fires filterContentRenderingComplete
88
 */
89
export const notifyFilterContentRenderingComplete = nodes => {
90
    return dispatchEvent(eventTypes.filterContentRenderingComplete, {nodes});
91
};