on-demand release 4.0dev+
[moodle.git] / lib / yui / build / moodle-core-event / moodle-core-event.js
CommitLineData
cc90cedc
DW
1YUI.add('moodle-core-event', function (Y, NAME) {
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @module moodle-core-event
20 */
21
22var LOGNAME = 'moodle-core-event';
23
24/**
25 * List of published global JS events in Moodle. This is a collection
26 * of global events that can be subscribed to, or fired from any plugin.
27 *
441f94b2
DW
28 * @namespace M.core
29 * @class event
cc90cedc
DW
30 */
31M.core = M.core || {};
32
acd9d982
AN
33var eventsConfigured = !!M.core.event;
34
36e0030f 35M.core.event = M.core.event || {
260565e3 36
441f94b2
DW
37 /**
38 * This event is triggered when a page has added dynamic nodes to a page
39 * that should be processed by the filter system. An example is loading
40 * user text that could have equations in it. MathJax can typeset the equations
41 * but only if it is notified that there are new nodes in the page that need processing.
42 * To trigger this event use M.core.Event.fire(M.core.Event.FILTER_CONTENT_UPDATED, {nodes: list});
43 *
44 * @event "filter-content-updated"
45 * @param nodes {Y.NodeList} List of nodes added to the DOM.
46 */
b5594973 47 FILTER_CONTENT_UPDATED: "filter-content-updated",
260565e3 48
b5594973
AG
49 /**
50 * This event is triggered when an editor has recovered some draft text.
51 * It can be used to determine let other sections know that they should reset their
52 * form comparison for changes.
53 *
54 * @event "editor-content-restored"
55 */
260565e3
DW
56 EDITOR_CONTENT_RESTORED: "editor-content-restored",
57
58 /**
59 * This event is triggered when an mform is about to be submitted via ajax.
60 *
61 * @event "form-submit-ajax"
62 */
63 FORM_SUBMIT_AJAX: "form-submit-ajax"
64
cc90cedc
DW
65};
66
36e0030f 67M.core.globalEvents = M.core.globalEvents || {
eaec9cfe
MM
68 /**
69 * This event is triggered when form has an error
70 *
71 * @event "form_error"
72 * @param formid {string} Id of form with error.
73 * @param elementid {string} Id of element with error.
74 */
fd5466af
CB
75 FORM_ERROR: "form_error",
76
77 /**
78 * This event is triggered when the content of a block has changed
79 *
80 * @event "block_content_updated"
81 * @param instanceid ID of the block instance that was updated
82 */
83 BLOCK_CONTENT_UPDATED: "block_content_updated"
eaec9cfe
MM
84};
85
cc90cedc 86
acd9d982
AN
87if (!eventsConfigured) {
88 var eventDefaultConfig = {
89 emitFacade: true,
90 defaultFn: function(e) {
91 },
92 preventedFn: function(e) {
93 },
94 stoppedFn: function(e) {
95 }
96 };
97
98 // Publish events with a custom config here.
99
100 // Publish all the events with a standard config.
101 var key;
102 for (key in M.core.event) {
103 if (M.core.event.hasOwnProperty(key) && Y.getEvent(M.core.event[key]) === null) {
104 Y.publish(M.core.event[key], eventDefaultConfig);
105 }
cc90cedc 106 }
cc90cedc 107
acd9d982
AN
108 // Publish global events.
109 for (key in M.core.globalEvents) {
110 // Make sure the key exists and that the event has not yet been published. Otherwise, skip publishing.
111 if (M.core.globalEvents.hasOwnProperty(key) && Y.Global.getEvent(M.core.globalEvents[key]) === null) {
112 Y.Global.publish(M.core.globalEvents[key], Y.merge(eventDefaultConfig, {broadcast: true}));
113 }
441f94b2 114 }
cc90cedc 115
acd9d982
AN
116 /**
117 * Apply a callback when the DOM is modified and matchecs the supplied targetSelector.
118 *
119 * @method listen
120 * @param {String} targetSelector The selector to apply
121 * @param {Function} applyCallback The function to call on the found node
122 */
123 var listenForMutation = function(targetSelector, applyCallback) {
124 // Add a MutationObserver to check for new children to the tree.
125 var newNodeObserver = new MutationObserver(function(mutationList) {
126 mutationList.forEach(function(mutation) {
127 mutation.addedNodes.forEach(function(node) {
128 if (node instanceof Element && node.matches(targetSelector)) {
129 applyCallback(node);
130 }
131 });
132 });
133 });
134
135 newNodeObserver.observe(document, {childList: true, subtree: true});
136 };
137
138
139 // YUI Custom Events do not bubble up the DOM, only up the inheritance path of custom classes.
140 // We have to add a Node Observer to watch for new forms that may need to be watched in future.
141 require(['core_form/events'], function(formEvents) {
142 listenForMutation('form', function(form) {
143 Y.one(form).on(M.core.event.FORM_SUBMIT_AJAX, function(e) {
144 // Prevent cyclical calls.
145 if (e && e.fallbackHandled) {
146 return;
147 }
148
149 formEvents.notifyFormSubmittedByJavascript(form, window.skipValidation, true);
150 });
151 });
152 });
eaec9cfe 153}
cc90cedc
DW
154
155
156}, '@VERSION@', {"requires": ["event-custom"]});