1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * A way to call HTML fragments to be inserted as required via JavaScript.
19 * @module core/fragment
22 * @copyright 2016 Adrian Greeve <adrian@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define(['jquery', 'core/ajax'], function($, ajax) {
29 * Loads an HTML fragment through a callback.
31 * @method loadFragment
32 * @param {string} component Component where callback is located.
33 * @param {string} callback Callback function name.
34 * @param {integer} contextid Context ID of the fragment.
35 * @param {object} params Parameters for the callback.
36 * @return {Promise} JQuery promise object resolved when the fragment has been loaded.
38 var loadFragment = function(component, callback, contextid, params) {
39 // Change params into required webservice format.
40 var formattedparams = [];
41 for (var index in params) {
42 formattedparams.push({
49 methodname: 'core_get_fragment',
59 return /** @alias module:core/fragment */{
61 * Appends HTML and JavaScript fragments to specified nodes.
62 * Callbacks called by this AMD module are responsible for doing the appropriate security checks
63 * to access the information that is returned. This only does minimal validation on the context.
65 * @method fragmentAppend
66 * @param {string} component Component where callback is located.
67 * @param {string} callback Callback function name.
68 * @param {integer} contextid Context ID of the fragment.
69 * @param {object} params Parameters for the callback.
70 * @return {Deferred} new promise that is resolved with the html and js.
72 loadFragment: function(component, callback, contextid, params) {
73 var promise = $.Deferred();
74 loadFragment(component, callback, contextid, params).then(function(data) {
75 var jsNodes = $(data.javascript);
77 jsNodes.each(function(index, scriptNode) {
78 scriptNode = $(scriptNode);
79 var tagName = scriptNode.prop('tagName');
80 if (tagName && (tagName.toLowerCase() == 'script')) {
81 if (scriptNode.attr('src')) {
82 // We only reload the script if it was not loaded already.
84 $('script').each(function(index, s) {
85 if ($(s).attr('src') == scriptNode.attr('src')) {
92 allScript += ' node = document.createElement("script"); ';
93 allScript += ' node.type = "text/javascript"; ';
94 allScript += ' node.src = decodeURI("' + encodeURI(scriptNode.attr('src')) + '"); ';
95 allScript += ' document.getElementsByTagName("head")[0].appendChild(node); ';
99 allScript += ' ' + scriptNode.text();
103 promise.resolve(data.html, allScript);
105 }).fail(function(ex) {
108 return promise.promise();