[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 // This file is part of Moodle - http://moodle.org/ 2 // 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. 7 // 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. 12 // 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/>. 15 16 /** 17 * A way to call HTML fragments to be inserted as required via JavaScript. 18 * 19 * @module core/fragment 20 * @class fragment 21 * @package core 22 * @copyright 2016 Adrian Greeve <adrian@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @since 3.1 25 */ 26 define(['jquery', 'core/ajax'], function($, ajax) { 27 28 /** 29 * Loads an HTML fragment through a callback. 30 * 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. 37 */ 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({ 43 name: index, 44 value: params[index] 45 }); 46 } 47 48 // Ajax stuff. 49 var deferred = $.Deferred(); 50 51 var promises = ajax.call([{ 52 methodname: 'core_get_fragment', 53 args: { 54 component: component, 55 callback: callback, 56 contextid: contextid, 57 args: formattedparams 58 } 59 }], false); 60 61 promises[0].done(function(data) { 62 deferred.resolve(data); 63 }).fail(function(ex) { 64 deferred.reject(ex); 65 }); 66 return deferred.promise(); 67 }; 68 69 return /** @alias module:core/fragment */{ 70 /** 71 * Appends HTML and JavaScript fragments to specified nodes. 72 * Callbacks called by this AMD module are responsible for doing the appropriate security checks 73 * to access the information that is returned. This only does minimal validation on the context. 74 * 75 * @method fragmentAppend 76 * @param {string} component Component where callback is located. 77 * @param {string} callback Callback function name. 78 * @param {integer} contextid Context ID of the fragment. 79 * @param {object} params Parameters for the callback. 80 * @return {Deferred} new promise that is resolved with the html and js. 81 */ 82 loadFragment: function(component, callback, contextid, params) { 83 var promise = $.Deferred(); 84 $.when(loadFragment(component, callback, contextid, params)).then(function(data) { 85 var jsNodes = $(data.javascript); 86 var allScript = ''; 87 jsNodes.each(function(index, scriptNode) { 88 scriptNode = $(scriptNode); 89 var tagName = scriptNode.prop('tagName'); 90 if (tagName && (tagName.toLowerCase() == 'script')) { 91 if (scriptNode.attr('src')) { 92 // We only reload the script if it was not loaded already. 93 var exists = false; 94 $('script').each(function(index, s) { 95 if ($(s).attr('src') == scriptNode.attr('src')) { 96 exists = true; 97 } 98 return !exists; 99 }); 100 if (!exists) { 101 allScript += ' { '; 102 allScript += ' node = document.createElement("script"); '; 103 allScript += ' node.type = "text/javascript"; '; 104 allScript += ' node.src = decodeURI("' + encodeURI(scriptNode.attr('src')) + '"); '; 105 allScript += ' document.getElementsByTagName("head")[0].appendChild(node); '; 106 allScript += ' } '; 107 } 108 } else { 109 allScript += ' ' + scriptNode.text(); 110 } 111 } 112 }); 113 promise.resolve(data.html, allScript); 114 }).fail(function(ex) { 115 promise.reject(ex); 116 }); 117 return promise.promise(); 118 } 119 }; 120 });
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |