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_editor` subsystem.
17
 *
18
 * @module     core_editor/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
 
24
import {dispatchEvent} from 'core/event_dispatcher';
25
import jQuery from 'jquery';
26
import Y from 'core/yui';
27
 
28
/**
29
 * Events for the `core_editor` subsystem.
30
 *
31
 * @constant
32
 * @property {String} editorContentRestored See {@link event:editorContentRestored}
33
 */
34
export const eventTypes = {
35
    /**
36
     * An event triggered when an editor restores auto-saved content.
37
     *
38
     * @event editorContentRestored
39
     */
40
    editorContentRestored: 'core_editor/contentRestored',
41
};
42
 
43
/**
44
 * Trigger an event to indicate that editor content was restored.
45
 *
46
 * @method  notifyEditorContentRestored
47
 * @param   {HTMLElement|null} editor The element that was modified
48
 * @returns {CustomEvent}
49
 * @fires   editorContentRestored
50
 */
51
export const notifyEditorContentRestored = editor => {
52
    if (!editor) {
53
        window.console.warn(
54
            `The HTMLElement representing the editor that was modified should be provided to notifyEditorContentRestored.`
55
        );
56
    }
57
    return dispatchEvent(
58
        eventTypes.editorContentRestored,
59
        {},
60
        editor || document
61
    );
62
};
63
 
64
let legacyEventsRegistered = false;
65
if (!legacyEventsRegistered) {
66
    // The following event triggers are legacy and will be removed in the future.
67
    // The following approach provides a backwards-compatability layer for the new events.
68
    // Code should be updated to make use of native events.
69
 
70
    Y.use('event', 'moodle-core-event', () => {
71
        // Provide a backwards-compatability layer for YUI Events.
72
        document.addEventListener(eventTypes.editorContentRestored, () => {
73
            // Trigger a legacy AMD event.
74
            jQuery(document).trigger(M.core.event.EDITOR_CONTENT_RESTORED);
75
 
76
            // Trigger a legacy YUI event.
77
            Y.fire(M.core.event.EDITOR_CONTENT_RESTORED);
78
        });
79
    });
80
 
81
    legacyEventsRegistered = true;
82
}