Merge branch 'MDL-53056-master' of git://github.com/damyon/moodle

This commit is contained in:
Andrew Nicols
2016-04-05 14:57:26 +08:00
5 changed files with 54 additions and 42 deletions
+1 -1
View File
@@ -1 +1 @@
define(["jquery","core/ajax","core/notification"],function(a,b,c){var d=function(c,d,e,f){var g=[];for(var h in f)g.push({name:h,value:f[h]});var i=a.Deferred(),j=b.call([{methodname:"core_get_fragment",args:{component:c,callback:d,contextid:e,args:g}}],!1);return j[0].done(function(a){i.resolve(a)}).fail(function(a){i.reject(a)}),i.promise()},e=function(b){b.children().each(function(b,c){var d=a(c);e(d)});var c=new Y.Node(b);b.empty(),b.remove(),c.detachAll(),c.get("childNodes")&&c.empty(),c.remove(!0)};return{fragmentAppend:function(b,f,g,h,i,j){a.when(d(b,f,g,h)).then(function(b){e(a("#fragment-html")),e(a("#fragment-scripts")),a(i).append('<div id="fragment-html">'+b.html+"</div>"),a(j).append('<div id="fragment-scripts">'+b.javascript+"</div>")}).fail(function(a){c.exception(a)})}}});
define(["jquery","core/ajax"],function(a,b){var c=function(c,d,e,f){var g=[];for(var h in f)g.push({name:h,value:f[h]});var i=a.Deferred(),j=b.call([{methodname:"core_get_fragment",args:{component:c,callback:d,contextid:e,args:g}}],!1);return j[0].done(function(a){i.resolve(a)}).fail(function(a){i.reject(a)}),i.promise()};return{loadFragment:function(b,d,e,f){var g=a.Deferred();return a.when(c(b,d,e,f)).then(function(b){var c=a(b.javascript),d="";c.each(function(b,c){c=a(c);var e=c.prop("tagName");if(e&&"script"==e.toLowerCase())if(c.attr("src")){var f=!1;a("script").each(function(b,d){return a(d).attr("src")==c.attr("src")&&(f=!0),!f}),f||(d+=" { ",d+=' node = document.createElement("script"); ',d+=' node.type = "text/javascript"; ',d+=' node.src = decodeURI("'+encodeURI(c.attr("src"))+'"); ',d+=' document.getElementsByTagName("head")[0].appendChild(node); ',d+=" } ")}else d+=" "+c.text()}.bind(this)),g.resolve(b.html,d)}).fail(function(a){g.reject(a)}),g.promise()}}});
+1 -1
View File
@@ -1 +1 @@
define(["core/mustache","jquery","core/ajax","core/str","core/notification","core/url","core/config","core/localstorage","core/event"],function(a,b,c,d,e,f,g,h,i){var j={},k=[],l=[],m=1,n="",o=function(a,d){var e=b.Deferred(),f=a.split("/"),g=f.shift(),i=f.shift(),k=n+"/"+a;if(k in j)return e.resolve(j[k]),e.promise();var l=h.get("core_template/"+k);if(l)return e.resolve(l),j[k]=l,e.promise();var m=c.call([{methodname:"core_output_load_template",args:{component:g,template:i,themename:n}}],d,!1);return m[0].done(function(a){h.set("core_template/"+k,a),j[k]=a,e.resolve(a)}).fail(function(a){e.reject(a)}),e.promise()},p=function(a){var b="";return o(a,!1).done(function(a){b=a}).fail(e.exception),b},q=function(b,c){var d,e=b.split(","),g="",h="",i="";e.length>0&&(g=e.shift().trim()),e.length>0&&(h=e.shift().trim()),e.length>0&&(i=e.join(",").trim());var k=f.imageUrl(g,h),l={attributes:[{name:"src",value:k},{name:"alt",value:c(i)},{name:"class",value:"smallicon"}]},m=j[n+"/core/pix_icon"];return d=a.render(m,l,p),d.trim()},r=function(a,b){return l.push(b(a,this)),""},s=function(a,b){var c=a.split(","),d="",e="",f="";c.length>0&&(d=c.shift().trim()),c.length>0&&(e=c.shift().trim()),c.length>0&&(f=c.join(",").trim()),""!==f&&(f=b(f,this)),0===f.indexOf("{")&&0!==f.indexOf("{{")&&(f=JSON.parse(f));var g=k.length;return k.push({key:d,component:e,param:f}),"{{_s"+g+"}}"},t=function(a,b){var c=b(a.trim(),this);return c=c.replace('"','\\"').replace(/([\{\}]{2,3})/g,"{{=<% %>=}}$1<%={{ }}=%>"),'"'+c+'"'},u=function(a,b){n=b,k=[],l=[],a.uniqid=m++,a.str=function(){return s},a.pix=function(){return q},a.js=function(){return r},a.quote=function(){return t},a.globals={config:g},a.currentTheme=b},v=function(a){var b="";l.length>0&&(b=l.join(";\n"));var c=0;for(c=0;c<a.length;c++)b=b.replace("{{_s"+c+"}}",a[c]);return b},w=function(c,e,f){var g=b.Deferred();n=f;var h=o("core/pix_icon",!0);return h.done(function(){u(e,f);var b="";try{b=a.render(c,e,p)}catch(h){g.reject(h)}k.length>0?d.get_strings(k).done(function(a){var c;for(c=0;c<a.length;c++)b=b.replace("{{_s"+c+"}}",a[c]);g.resolve(b.trim(),v(a))}).fail(function(a){g.reject(a)}):g.resolve(b.trim(),v([]))}).fail(function(a){g.reject(a)}),g.promise()},x=function(a){if(""!==a.trim()){var c=b("<script>").attr("type","text/javascript").html(a);b("head").append(c)}},y=function(a,c,d,e){var f=b(a);if(f.length){var g=b(c);e?(f.empty(),f.append(g)):f.replaceWith(g),x(d),i.notifyFilterContentUpdated(g)}};return{render:function(a,c,d){var e=b.Deferred();"undefined"==typeof d&&(d=g.theme),n=d;var f=o(a,!0);return f.done(function(a){var b=w(a,c,d);b.done(function(a,b){e.resolve(a,b)}).fail(function(a){e.reject(a)})}).fail(function(a){e.reject(a)}),e.promise()},runTemplateJS:x,replaceNodeContents:function(a,b,c){return y(a,b,c,!0)},replaceNode:function(a,b,c){return y(a,b,c,!1)}}});
define(["core/mustache","jquery","core/ajax","core/str","core/notification","core/url","core/config","core/localstorage","core/event","core/yui"],function(a,b,c,d,e,f,g,h,i,j){var k={},l=[],m=[],n=1,o="",p=function(a,d){var e=b.Deferred(),f=a.split("/"),g=f.shift(),i=f.shift(),j=o+"/"+a;if(j in k)return e.resolve(k[j]),e.promise();var l=h.get("core_template/"+j);if(l)return e.resolve(l),k[j]=l,e.promise();var m=c.call([{methodname:"core_output_load_template",args:{component:g,template:i,themename:o}}],d,!1);return m[0].done(function(a){h.set("core_template/"+j,a),k[j]=a,e.resolve(a)}).fail(function(a){e.reject(a)}),e.promise()},q=function(a){var b="";return p(a,!1).done(function(a){b=a}).fail(e.exception),b},r=function(b,c){var d,e=b.split(","),g="",h="",i="";e.length>0&&(g=e.shift().trim()),e.length>0&&(h=e.shift().trim()),e.length>0&&(i=e.join(",").trim());var j=f.imageUrl(g,h),l={attributes:[{name:"src",value:j},{name:"alt",value:c(i)},{name:"class",value:"smallicon"}]},m=k[o+"/core/pix_icon"];return d=a.render(m,l,q),d.trim()},s=function(a,b){return m.push(b(a,this)),""},t=function(a,b){var c=a.split(","),d="",e="",f="";c.length>0&&(d=c.shift().trim()),c.length>0&&(e=c.shift().trim()),c.length>0&&(f=c.join(",").trim()),""!==f&&(f=b(f,this)),0===f.indexOf("{")&&0!==f.indexOf("{{")&&(f=JSON.parse(f));var g=l.length;return l.push({key:d,component:e,param:f}),"{{_s"+g+"}}"},u=function(a,b){var c=b(a.trim(),this);return c=c.replace('"','\\"').replace(/([\{\}]{2,3})/g,"{{=<% %>=}}$1<%={{ }}=%>"),'"'+c+'"'},v=function(a,b){o=b,l=[],m=[],a.uniqid=n++,a.str=function(){return t},a.pix=function(){return r},a.js=function(){return s},a.quote=function(){return u},a.globals={config:g},a.currentTheme=b},w=function(a){var b="";m.length>0&&(b=m.join(";\n"));var c=0;for(c=0;c<a.length;c++)b=b.replace("{{_s"+c+"}}",a[c]);return b},x=function(c,e,f){var g=b.Deferred();o=f;var h=p("core/pix_icon",!0);return h.done(function(){v(e,f);var b="";try{b=a.render(c,e,q)}catch(h){g.reject(h)}l.length>0?d.get_strings(l).done(function(a){var c;for(c=0;c<a.length;c++)b=b.replace("{{_s"+c+"}}",a[c]);g.resolve(b.trim(),w(a))}).fail(function(a){g.reject(a)}):g.resolve(b.trim(),w([]))}).fail(function(a){g.reject(a)}),g.promise()},y=function(a){if(""!==a.trim()){var c=b("<script>").attr("type","text/javascript").html(a);b("head").append(c)}},z=function(a,c,d,e){var f=b(a);if(f.length){var g=b(c),h=null;e?(h=new j.NodeList(f.children().get()),h.destroy(!0),f.empty(),f.append(g)):(h=new j.NodeList(f.get()),h.destroy(!0),f.replaceWith(g)),y(d),i.notifyFilterContentUpdated(g)}};return{render:function(a,c,d){var e=b.Deferred();"undefined"==typeof d&&(d=g.theme),o=d;var f=p(a,!0);return f.done(function(a){var b=x(a,c,d);b.done(function(a,b){e.resolve(a,b)}).fail(function(a){e.reject(a)})}).fail(function(a){e.reject(a)}),e.promise()},runTemplateJS:y,replaceNodeContents:function(a,b,c){return z(a,b,c,!0)},replaceNode:function(a,b,c){return z(a,b,c,!1)}}});
+36 -37
View File
@@ -23,7 +23,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.1
*/
define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notification) {
define(['jquery', 'core/ajax'], function($, ajax) {
/**
* Loads an HTML fragment through a callback.
@@ -39,7 +39,7 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notificat
// Change params into required webservice format.
var formattedparams = [];
for (var index in params) {
formattedparams.push({name: index, value: params[index]});
formattedparams.push({ name: index, value: params[index] });
}
// Ajax stuff.
@@ -63,29 +63,6 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notificat
return deferred.promise();
};
/**
* Removes and cleans children of a node. This includes event handlers and listeners that may be
* attached to the nodes for both jquery and yui.
*
* @method recursiveCleanup
* @param {object} DOM node to be cleaned.
* @return {void}
*/
var recursiveCleanup = function(node) {
node.children().each(function(index, el) {
var child = $(el);
recursiveCleanup(child);
});
var yuinode = new Y.Node(node);
node.empty();
node.remove();
yuinode.detachAll();
if (yuinode.get('childNodes')) {
yuinode.empty();
}
yuinode.remove(true);
};
return /** @alias module:core/fragment */{
/**
* Appends HTML and JavaScript fragments to specified nodes.
@@ -97,22 +74,44 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notificat
* @param {string} callback Callback function name.
* @param {integer} contextid Context ID of the fragment.
* @param {object} params Parameters for the callback.
* @param {string} htmlnodeidentifier The 'class' or 'id' to attach the HTML.
* @param {string} javascriptnodeidentifier The 'class' or 'id' to attach the JavaScript.
* @return {void}
* @return {Deferred} new promise that is resolved with the html and js.
*/
fragmentAppend: function(component, callback, contextid, params, htmlnodeidentifier, javascriptnodeidentifier) {
loadFragment: function(component, callback, contextid, params) {
var promise = $.Deferred();
$.when(loadFragment(component, callback, contextid, params)).then(function(data) {
// Clean up previous code if found first.
recursiveCleanup($('#fragment-html'));
recursiveCleanup($('#fragment-scripts'));
// Attach new HTML and JavaScript.
$(htmlnodeidentifier).append('<div id="fragment-html">' + data.html + '</div>');
$(javascriptnodeidentifier).append('<div id="fragment-scripts">' + data.javascript + '</div>');
var jsNodes = $(data.javascript);
var allScript = '';
jsNodes.each(function(index, scriptNode) {
scriptNode = $(scriptNode);
var tagName = scriptNode.prop('tagName');
if (tagName && (tagName.toLowerCase() == 'script')) {
if (scriptNode.attr('src')) {
// We only reload the script if it was not loaded already.
var exists = false;
$('script').each(function(index, s) {
if ($(s).attr('src') == scriptNode.attr('src')) {
exists = true;
}
return !exists;
});
if (!exists) {
allScript += ' { ';
allScript += ' node = document.createElement("script"); ';
allScript += ' node.type = "text/javascript"; ';
allScript += ' node.src = decodeURI("' + encodeURI(scriptNode.attr('src')) + '"); ';
allScript += ' document.getElementsByTagName("head")[0].appendChild(node); ';
allScript += ' } ';
}
} else {
allScript += ' ' + scriptNode.text();
}
}
}.bind(this));
promise.resolve(data.html, allScript);
}).fail(function(ex) {
notification.exception(ex);
promise.reject(ex);
});
return promise.promise();
}
};
});
+14 -2
View File
@@ -31,9 +31,10 @@ define([ 'core/mustache',
'core/url',
'core/config',
'core/localstorage',
'core/event'
'core/event',
'core/yui'
],
function(mustache, $, ajax, str, notification, coreurl, config, storage, event) {
function(mustache, $, ajax, str, notification, coreurl, config, storage, event, Y) {
// Private variables and functions.
@@ -378,11 +379,22 @@ define([ 'core/mustache',
if (replaceNode.length) {
// First create the dom nodes so we have a reference to them.
var newNodes = $(newHTML);
var yuiNodes = null;
// Do the replacement in the page.
if (replaceChildNodes) {
// Cleanup any YUI event listeners attached to any of these nodes.
yuiNodes = new Y.NodeList(replaceNode.children().get());
yuiNodes.destroy(true);
// JQuery will cleanup after itself.
replaceNode.empty();
replaceNode.append(newNodes);
} else {
// Cleanup any YUI event listeners attached to any of these nodes.
yuiNodes = new Y.NodeList(replaceNode.get());
yuiNodes.destroy(true);
// JQuery will cleanup after itself.
replaceNode.replaceWith(newNodes);
}
// Run any javascript associated with the new HTML.
+2 -1
View File
@@ -319,6 +319,7 @@ class core_external extends external_api {
$context = context::instance_by_id($contextid);
self::validate_context($context);
$arguments['context'] = $context;
// Hack alert: Forcing bootstrap_renderer to initiate moodle page.
$OUTPUT->header();
@@ -326,7 +327,7 @@ class core_external extends external_api {
// Overwriting page_requirements_manager with the fragment one so only JS included from
// this point is returned to the user.
$PAGE->start_collecting_javascript_requirements();
$data = component_callback($params['component'], 'output_fragment_' . $params['callback'], $arguments);
$data = component_callback($params['component'], 'output_fragment_' . $params['callback'], array($arguments));
$jsfooter = $PAGE->requires->get_end_code();
$output = array('html' => $data, 'javascript' => $jsfooter);
return $output;