Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
-
 
18
 * Library functions to facilitate the use of JavaScript in Moodle.
-
 
19
 *
-
 
20
 * @copyright 2016 Adrian Greeve <adrian@moodle.com>
-
 
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
22
 * @package core
-
 
23
 * @category output
-
 
24
 */
-
 
25
 
16
 
Línea 26... Línea -...
26
defined('MOODLE_INTERNAL') || die();
-
 
27
 
17
defined('MOODLE_INTERNAL') || die();
28
/**
-
 
29
 * This requirements manager captures the appropriate html for creating a fragment to
-
 
30
 * be inserted elsewhere.
-
 
31
 *
-
 
32
 * @copyright 2016 Adrian Greeve <adrian@moodle.com>
-
 
33
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
34
 * @since Moodle 3.1
-
 
35
 * @package core
-
 
36
 * @category output
-
 
37
 */
-
 
38
class fragment_requirements_manager extends page_requirements_manager {
-
 
39
 
-
 
40
    /**
-
 
41
     * Page fragment constructor.
-
 
42
     */
-
 
43
    public function __construct() {
-
 
44
        parent::__construct();
-
 
45
        // As this is a fragment the header should already be done.
-
 
46
        $this->headdone = true;
-
 
47
    }
-
 
48
 
-
 
49
    /**
-
 
50
     * Returns js code to load amd module loader, then insert inline script tags
-
 
51
     * that contain require() calls using RequireJS.
-
 
52
     *
-
 
53
     * @return string
-
 
54
     */
-
 
55
    protected function get_amd_footercode() {
-
 
56
        global $CFG;
-
 
57
        $output = '';
18
 
58
 
-
 
59
        // First include must be to a module with no dependencies, this prevents multiple requests.
-
 
60
        $prefix = 'M.util.js_pending("core/first");';
-
 
61
        $prefix .= "require(['core/first'], function() {\n";
-
 
62
        $suffix = "\n});";
-
 
63
        $suffix .= 'M.util.js_complete("core/first");';
-
 
64
        $output .= html_writer::script($prefix . implode(";\n", $this->amdjscode) . $suffix);
-
 
65
        return $output;
-
 
66
    }
-
 
67
 
-
 
68
 
-
 
69
    /**
-
 
70
     * Generate any HTML that needs to go at the end of the page.
-
 
71
     *
-
 
72
     * @return string the HTML code to to at the end of the page.
19
// This file is deprecated, but it should never have been manually included by anything outside of lib/outputlib.php.
73
     */
-
 
74
    public function get_end_code() {
-
 
75
        global $CFG;
-
 
76
 
-
 
77
        $output = '';
-
 
78
 
-
 
79
        // Call amd init functions.
-
 
80
        $output .= $this->get_amd_footercode();
-
 
81
 
-
 
82
        // Add other requested modules.
-
 
83
        $output .= $this->get_extra_modules_code();
-
 
84
 
-
 
85
        // All the other linked scripts - there should be as few as possible.
-
 
86
        if ($this->jsincludes['footer']) {
-
 
87
            foreach ($this->jsincludes['footer'] as $url) {
-
 
88
                $output .= html_writer::script('', $url);
-
 
89
            }
-
 
90
        }
-
 
91
 
-
 
92
        if (!empty($this->stringsforjs)) {
-
 
93
            // Add all needed strings.
-
 
94
            $strings = array();
-
 
95
            foreach ($this->stringsforjs as $component => $v) {
-
 
96
                foreach ($v as $indentifier => $langstring) {
-
 
97
                    $strings[$component][$indentifier] = $langstring->out();
-
 
98
                }
-
 
99
            }
-
 
100
            // Append don't overwrite.
-
 
101
            $output .= html_writer::script('require(["jquery"], function($) {
-
 
102
                M.str = $.extend(true, M.str, ' . json_encode($strings) . ');
-
 
103
            });');
-
 
104
        }
-
 
105
 
-
 
106
        // Add variables.
-
 
107
        if ($this->jsinitvariables['footer']) {
-
 
108
            $js = '';
-
 
109
            foreach ($this->jsinitvariables['footer'] as $data) {
-
 
110
                list($var, $value) = $data;
-
 
111
                $js .= js_writer::set_variable($var, $value, true);
-
 
112
            }
-
 
113
            $output .= html_writer::script($js);
-
 
114
        }
-
 
115
 
-
 
116
        $inyuijs = $this->get_javascript_code(false);
-
 
117
        $ondomreadyjs = $this->get_javascript_code(true);
-
 
118
        // See if this is still needed when we get to the ajax page.
-
 
119
        $jsinit = $this->get_javascript_init_code();
-
 
120
        $handlersjs = $this->get_event_handler_code();
20
// Throwing an exception here should be fine because removing the manual inclusion should have no impact.
121
 
-
 
122
        // There is a global Y, make sure it is available in your scope.
-
 
123
        $js = "(function() {{$inyuijs}{$ondomreadyjs}{$jsinit}{$handlersjs}})();";
-
 
124
 
-
 
125
        $output .= html_writer::script($js);
-
 
126
 
-
 
127
        return $output;
21
throw new \core\exception\coding_exception(