"plusplus": false,
"predef": [
"M",
- "define"
+ "define",
+ "require"
],
"proto": false,
"regexdash": false,
}
if (context) {
templates.render(templateName, context).done(function(html, js) {
- $('[data-region="displaytemplateexample"]').empty();
- $('[data-region="displaytemplateexample"]').append(html);
- templates.runTemplateJS(js);
+ templates.replaceNodeContents($('[data-region="displaytemplateexample"]'), html, js);
}).fail(notification.exception);
} else {
str.get_string('templatehasnoexample', 'tool_templatelibrary').done(function(s) {
*/
var reloadListTemplate = function(templateList) {
templates.render('tool_templatelibrary/search_results', { templates: templateList })
- .done(function (result) {
- $('[data-region="searchresults"]').replaceWith(result);
+ .done(function (result, js) {
+ templates.replaceNode($('[data-region="searchresults"]'), result, js);
}).fail(notification.exception);
};
alertpanels: {},
initializer : function() {
var self = this;
- Y.delegate('click', function(e){
- e.preventDefault();
+ require(['core/event'], function(event) {
+ Y.delegate('click', function(e){
+ e.preventDefault();
- //display a progress indicator
- var title = '',
- content = Y.Node.create('<div id="glossaryfilteroverlayprogress">' +
- '<img src="' + M.cfg.loadingicon + '" class="spinner" />' +
- '</div>'),
- o = new Y.Overlay({
- headerContent : title,
- bodyContent : content
- }),
- fullurl,
- cfg;
- self.overlay = o;
- o.render(Y.one(document.body));
+ //display a progress indicator
+ var title = '',
+ content = Y.Node.create('<div id="glossaryfilteroverlayprogress">' +
+ '<img src="' + M.cfg.loadingicon + '" class="spinner" />' +
+ '</div>'),
+ o = new Y.Overlay({
+ headerContent : title,
+ bodyContent : content
+ }),
+ fullurl,
+ cfg;
+ self.overlay = o;
+ o.render(Y.one(document.body));
- //Switch over to the ajax url and fetch the glossary item
- fullurl = this.getAttribute('href').replace('showentry.php','showentry_ajax.php');
- cfg = {
- method: 'get',
- context : self,
- on: {
- success: function(id, o) {
- this.display_callback(o.responseText);
- },
- failure: function(id, o) {
- var debuginfo = o.statusText;
- if (M.cfg.developerdebug) {
- o.statusText += ' (' + fullurl + ')';
+ //Switch over to the ajax url and fetch the glossary item
+ fullurl = this.getAttribute('href').replace('showentry.php','showentry_ajax.php');
+ cfg = {
+ method: 'get',
+ context : self,
+ on: {
+ success: function(id, o) {
+ this.display_callback(o.responseText, event);
+ },
+ failure: function(id, o) {
+ var debuginfo = o.statusText;
+ if (M.cfg.developerdebug) {
+ o.statusText += ' (' + fullurl + ')';
+ }
+ new M.core.exception({ message: debuginfo });
}
- this.display_callback('bodyContent',debuginfo);
}
- }
- };
- Y.io(fullurl, cfg);
+ };
+ Y.io(fullurl, cfg);
- }, Y.one(document.body), 'a.glossary.autolink.concept');
+ }, Y.one(document.body), 'a.glossary.autolink.concept');
+ });
},
- display_callback : function(content) {
+ /**
+ * @method display_callback
+ * @param {String} content - Content to display
+ * @param {Object} event The amd event module used to fire events for jquery and yui.
+ */
+ display_callback : function(content, event) {
var data,
key,
alertpanel,
definition = data.entries[key].definition + data.entries[key].attachments;
alertpanel = new M.core.alert({title:data.entries[key].concept, draggable: true,
message:definition, modal:false, yesLabel: M.util.get_string('ok', 'moodle')});
- Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: (new Y.NodeList(alertpanel.get('boundingBox')))});
+ // Notify the filters about the modified nodes.
+ event.notifyFilterContentUpdated(alertpanel.get('boundingBox').getDOMNode());
Y.Node.one('#id_yuialertconfirm-' + alertpanel.get('COUNT')).focus();
// Register alertpanel for stacking.
--- /dev/null
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Global registry of core events that can be triggered/listened for.
+ *
+ * @module core/event
+ * @package core
+ * @class event
+ * @copyright 2015 Damyon Wiese <damyon@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since 3.0
+ */
+define([ 'jquery', 'core/yui' ],
+ function($, Y) {
+
+ return /** @alias module:core/event */ {
+ // Public variables and functions.
+ /**
+ * Trigger an event using both JQuery and YUI
+ *
+ * @method notifyFilterContentUpdated
+ * @param {string}|{JQuery} nodes - Selector or list of elements that were inserted.
+ */
+ notifyFilterContentUpdated: function(nodes) {
+ nodes = $(nodes);
+ Y.use('event', 'moodle-core-event', function(Y) {
+ // Trigger it the JQuery way.
+ $('document').trigger(M.core.event.FILTER_CONTENT_UPDATED, nodes);
+
+ // Create a YUI NodeList from our JQuery Object.
+ var yuiNodes = new Y.NodeList(nodes.get());
+
+ // And again for YUI.
+ Y.fire(M.core.event.FILTER_CONTENT_UPDATED, { nodes: yuiNodes });
+ });
+ },
+
+ };
+});
'core/notification',
'core/url',
'core/config',
- 'core/localstorage'
+ 'core/localstorage',
+ 'core/event'
],
- function(mustache, $, ajax, str, notification, coreurl, config, storage) {
+ function(mustache, $, ajax, str, notification, coreurl, config, storage, event) {
// Private variables and functions.
return deferred.promise();
};
+ /**
+ * Execute a block of JS returned from a template.
+ * Call this AFTER adding the template HTML into the DOM so the nodes can be found.
+ *
+ * @method runTemplateJS
+ * @param {string} source - A block of javascript.
+ */
+ var runTemplateJS = function(source) {
+ if (source.trim() !== '') {
+ var newscript = $('<script>').attr('type','text/javascript').html(source);
+ $('head').append(newscript);
+ }
+ };
+
+ /**
+ * Do some DOM replacement and trigger correct events and fire javascript.
+ *
+ * @method domReplace
+ * @private
+ * @param {JQuery} element - Element or selector to replace.
+ * @param {String} newHTML - HTML to insert / replace.
+ * @param {String} newJS - Javascript to run after the insertion.
+ * @param {Boolean} replaceChildNodes - Replace only the childnodes, alternative is to replace the entire node.
+ */
+ var domReplace = function(element, newHTML, newJS, replaceChildNodes) {
+ var replaceNode = $(element);
+ if (replaceNode.length) {
+ // First create the dom nodes so we have a reference to them.
+ var newNodes = $(newHTML);
+ // Do the replacement in the page.
+ if (replaceChildNodes) {
+ replaceNode.empty();
+ replaceNode.append(newNodes);
+ } else {
+ replaceNode.replaceWith(newNodes);
+ }
+ // Run any javascript associated with the new HTML.
+ runTemplateJS(newJS);
+ // Notify all filters about the new content.
+ event.notifyFilterContentUpdated(newNodes);
+ }
+ };
+
+
return /** @alias module:core/templates */ {
// Public variables and functions.
/**
* Call this AFTER adding the template HTML into the DOM so the nodes can be found.
*
* @method runTemplateJS
- * @private
* @param {string} source - A block of javascript.
*/
- runTemplateJS: function(source) {
- var newscript = $('<script>').attr('type','text/javascript').html(source);
- $('head').append(newscript);
+ runTemplateJS: runTemplateJS,
+
+ /**
+ * Replace a node in the page with some new HTML and run the JS.
+ *
+ * @method replaceNodeContents
+ * @param {string} source - A block of javascript.
+ */
+ replaceNodeContents: function(element, newHTML, newJS) {
+ return domReplace(element, newHTML, newJS, true);
+ },
+
+ /**
+ * Insert a node in the page with some new HTML and run the JS.
+ *
+ * @method replaceNode
+ * @param {string} source - A block of javascript.
+ */
+ replaceNode: function(element, newHTML, newJS) {
+ return domReplace(element, newHTML, newJS, false);
}
};
});
* @class Button
* @extends M.editor_atto.EditorPlugin
*/
-
var COMPONENTNAME = 'atto_equation',
LOGNAME = 'atto_equation',
CSS = {
tabview.render();
dialogue.show();
- // Trigger any JS filters to reprocess the new nodes.
- Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: (new Y.NodeList(dialogue.get('boundingBox')))});
+ // Notify the filters about the modified nodes.
+ require(['core/event'], function(event) {
+ event.notifyFilterContentUpdated(dialogue.get('boundingBox').getDOMNode());
+ });
if (equation) {
content.one(SELECTORS.EQUATION_TEXT).set('text', equation);
if (preview.status === 200) {
previewNode.setHTML(preview.responseText);
- Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: (new Y.NodeList(previewNode))});
+ // Notify the filters about the modified nodes.
+ require(['core/event'], function(event) {
+ event.notifyFilterContentUpdated(previewNode.getDOMNode());
+ });
}
},