Skip to content

Commit

Permalink
MDL-51222 Javascript: Trigger events for filters on DOM insertion
Browse files Browse the repository at this point in the history
When nodes are added to the dom, they may need to be re-processed by a JS based
filter. To do this we need to trigger the legacy YUI event filter-content-updated.

To make this easier I added some wrappers to template that will insert the node, run any
JS and trigger the event.

I also changed existing yui code to call the amd function to trigger the event. This way
all jquery and yui listeners will always be notified.
  • Loading branch information
Damyon Wiese committed Sep 22, 2015
1 parent 74ede2d commit 28de777
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 135 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"plusplus": false,
"predef": [
"M",
"define"
"define",
"require"
],
"proto": false,
"regexdash": false,
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/templatelibrary/amd/build/display.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion admin/tool/templatelibrary/amd/build/search.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions admin/tool/templatelibrary/amd/src/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates'
}
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) {
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/templatelibrary/amd/src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates'
*/
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);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,53 @@ Y.extend(AUTOLINKER, Y.Base, {
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,
Expand All @@ -78,7 +85,8 @@ Y.extend(AUTOLINKER, Y.Base, {
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.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 28de777

Please sign in to comment.