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
 * Inplace editable module events
18
 *
19
 * @module      core/local/inplace_editable/events
20
 * @copyright   2021 Paul Holden <paulh@moodle.com>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import $ from 'jquery';
25
import {dispatchEvent} from 'core/event_dispatcher';
26
 
27
/**
28
 * Module events
29
 *
30
 * @constant
31
 * @property {String} elementUpdated See {@link event:core/inplace_editable:updated}
32
 * @property {String} elementUpdateFailed See {@link event:core/inplace_editable:updateFailed}
33
 */
34
export const eventTypes = {
35
 
36
    /**
37
     * Event triggered when an element has been updated
38
     *
39
     * @event core/inplace_editable:updated
40
     * @type {CustomEvent}
41
     * @property {HTMLElement} target The element that was updated
42
     * @property {Object} detail
43
     * @property {Object} detail.ajaxreturn The data returned from the update AJAX request
44
     * @property {String} detail.oldvalue The previous value of the element
45
     */
46
    elementUpdated: 'core/inplace_editable:updated',
47
 
48
    /**
49
     * Event triggered when an element update has failed
50
     *
51
     * @event core/inplace_editable:updateFailed
52
     * @type {CustomEvent}
53
     * @property {HTMLElement} target The element that failed to update
54
     * @property {Object} detail
55
     * @property {Object} detail.exception The raised exception
56
     * @property {String} detail.newvalue The intended value of the element
57
     */
58
    elementUpdateFailed: 'core/inplace_editable:updateFailed',
59
};
60
 
61
/**
62
 * Notify element of successful update
63
 *
64
 * @method
65
 * @param {HTMLElement} element The element that was updated
66
 * @param {Object} ajaxreturn The data returned from the update AJAX request
67
 * @param {String} oldvalue The previous value of the element
68
 * @returns {CustomEvent}
69
 * @fires event:core/inplace_editable:updated
70
 */
71
export const notifyElementUpdated = (element, ajaxreturn, oldvalue) => dispatchEvent(
72
    eventTypes.elementUpdated,
73
    {
74
        ajaxreturn,
75
        oldvalue,
76
    },
77
    element
78
);
79
 
80
/**
81
 * Notify element of failed update
82
 *
83
 * @method
84
 * @param {HTMLElement} element The element that failed to update
85
 * @param {Object} exception The raised exception
86
 * @param {String} newvalue The intended value of the element
87
 * @returns {CustomEvent}
88
 * @fires event:core/inplace_editable:updateFailed
89
 */
90
export const notifyElementUpdateFailed = (element, exception, newvalue) => dispatchEvent(
91
    eventTypes.elementUpdateFailed,
92
    {
93
        exception,
94
        newvalue,
95
    },
96
    element,
97
    {
98
        cancelable: true
99
    }
100
);
101
 
102
let legacyEventsRegistered = false;
103
if (!legacyEventsRegistered) {
104
    // The following event triggers are legacy and will be removed in the future.
105
    // The following approach provides a backwards-compatability layer for the new events.
106
    // Code should be updated to make use of native events.
107
 
108
    // Listen for the new native elementUpdated event, and trigger the legacy jQuery event.
109
    document.addEventListener(eventTypes.elementUpdated, event => {
110
        const legacyEvent = $.Event('updated', event.detail);
111
        $(event.target).trigger(legacyEvent);
112
    });
113
 
114
    // Listen for the new native elementUpdateFailed event, and trigger the legacy jQuery event.
115
    document.addEventListener(eventTypes.elementUpdateFailed, event => {
116
        const legacyEvent = $.Event('updatefailed', event.detail);
117
        $(event.target).trigger(legacyEvent);
118
 
119
        // If the legacy event is cancelled, so should the native event.
120
        if (legacyEvent.isDefaultPrevented()) {
121
            event.preventDefault();
122
        }
123
    });
124
 
125
    legacyEventsRegistered = true;
126
}