Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * @copyright  2016 Adrian Greeve <adrian@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @since      3.1
23
 */
24
define(['jquery', 'core/ajax'], function($, ajax) {
25
 
26
    /**
27
     * Loads an HTML fragment through a callback.
28
     *
29
     * @method loadFragment
30
     * @param {string} component Component where callback is located.
31
     * @param {string} callback Callback function name.
32
     * @param {integer} contextid Context ID of the fragment.
33
     * @param {object} params Parameters for the callback.
34
     * @return {Promise} JQuery promise object resolved when the fragment has been loaded.
35
     */
36
    var loadFragment = function(component, callback, contextid, params) {
37
        // Change params into required webservice format.
38
        var formattedparams = [];
39
        for (var index in params) {
40
            formattedparams.push({
41
                name: index,
42
                value: params[index]
43
            });
44
        }
45
 
46
        return ajax.call([{
47
            methodname: 'core_get_fragment',
48
            args: {
49
                component: component,
50
                callback: callback,
51
                contextid: contextid,
52
                args: formattedparams
53
            }
54
        }])[0];
55
    };
56
 
57
    /**
58
     * Converts the JS that was received from collecting JS requirements on the $PAGE so it can be added to the existing page
59
     *
60
     * @param {string} js
61
     * @return {string}
62
     */
63
    var processCollectedJavascript = function(js) {
64
        var jsNodes = $(js);
65
        var allScript = '';
66
        jsNodes.each(function(index, scriptNode) {
67
            scriptNode = $(scriptNode);
68
            var tagName = scriptNode.prop('tagName');
69
            if (tagName && (tagName.toLowerCase() == 'script')) {
70
                if (scriptNode.attr('src')) {
71
                    // We only reload the script if it was not loaded already.
72
                    var exists = false;
73
                    $('script').each(function(index, s) {
74
                        if ($(s).attr('src') == scriptNode.attr('src')) {
75
                            exists = true;
76
                        }
77
                        return !exists;
78
                    });
79
                    if (!exists) {
80
                        allScript += ' { ';
81
                        allScript += ' node = document.createElement("script"); ';
82
                        allScript += ' node.type = "text/javascript"; ';
83
                        allScript += ' node.src = decodeURI("' + encodeURI(scriptNode.attr('src')) + '"); ';
84
                        allScript += ' document.getElementsByTagName("head")[0].appendChild(node); ';
85
                        allScript += ' } ';
86
                    }
87
                } else {
88
                    allScript += ' ' + scriptNode.text();
89
                }
90
            }
91
        });
92
        return allScript;
93
    };
94
 
95
    return {
96
        /**
97
         * Appends HTML and JavaScript fragments to specified nodes.
98
         * Callbacks called by this AMD module are responsible for doing the appropriate security checks
99
         * to access the information that is returned. This only does minimal validation on the context.
100
         *
101
         * @method fragmentAppend
102
         * @param {string} component Component where callback is located.
103
         * @param {string} callback Callback function name.
104
         * @param {integer} contextid Context ID of the fragment.
105
         * @param {object} params Parameters for the callback.
106
         * @return {Deferred} new promise that is resolved with the html and js.
107
         */
108
        loadFragment: function(component, callback, contextid, params) {
109
            var promise = $.Deferred();
110
            loadFragment(component, callback, contextid, params).then(function(data) {
111
                promise.resolve(data.html, processCollectedJavascript(data.javascript));
112
            }).fail(function(ex) {
113
                promise.reject(ex);
114
            });
115
            return promise.promise();
116
        },
117
 
118
        /**
119
         * Converts the JS that was received from collecting JS requirements on the $PAGE so it can be added to the existing page
120
         *
121
         * @param {string} js
122
         * @return {string}
123
         */
124
        processCollectedJavascript: function(js) {
125
            return processCollectedJavascript(js);
126
        }
127
    };
128
});