2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
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/>.
18 * Library functions to facilitate the use of JavaScript in Moodle.
20 * @copyright 2016 Adrian Greeve <adrian@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * This requirements manager captures the appropriate html for creating a fragment to
30 * be inserted elsewhere.
32 * @copyright 2016 Adrian Greeve <adrian@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class fragment_requirements_manager
extends page_requirements_manager
{
41 * Page fragment constructor.
43 public function __construct() {
44 parent
::__construct();
45 // As this is a fragment the header should already be done.
46 $this->headdone
= true;
50 * Returns js code to load amd module loader, then insert inline script tags
51 * that contain require() calls using RequireJS.
55 protected function get_amd_footercode() {
59 // First include must be to a module with no dependencies, this prevents multiple requests.
60 $prefix = "require(['core/first'], function() {\n";
62 $output .= html_writer
::script($prefix . implode(";\n", $this->amdjscode
) . $suffix);
68 * Generate any HTML that needs to go at the end of the page.
70 * @return string the HTML code to to at the end of the page.
72 public function get_end_code() {
77 // Call amd init functions.
78 $output .= $this->get_amd_footercode();
80 // Add other requested modules.
81 $output .= $this->get_extra_modules_code();
83 // All the other linked scripts - there should be as few as possible.
84 if ($this->jsincludes
['footer']) {
85 foreach ($this->jsincludes
['footer'] as $url) {
86 $output .= html_writer
::script('', $url);
90 if (!empty($this->stringsforjs
)) {
91 // Add all needed strings.
93 foreach ($this->stringsforjs
as $component => $v) {
94 foreach ($v as $indentifier => $langstring) {
95 $strings[$component][$indentifier] = $langstring->out();
98 // Append don't overwrite.
99 $output .= html_writer
::script('require(["jquery"], function($) {
100 M.str = $.extend(true, M.str, ' . json_encode($strings) . ');
105 if ($this->jsinitvariables
['footer']) {
107 foreach ($this->jsinitvariables
['footer'] as $data) {
108 list($var, $value) = $data;
109 $js .= js_writer
::set_variable($var, $value, true);
111 $output .= html_writer
::script($js);
114 $inyuijs = $this->get_javascript_code(false);
115 $ondomreadyjs = $this->get_javascript_code(true);
116 // See if this is still needed when we get to the ajax page.
117 $jsinit = $this->get_javascript_init_code();
118 $handlersjs = $this->get_event_handler_code();
120 // There is a global Y, make sure it is available in your scope.
121 $js = "(function() {{$inyuijs}{$ondomreadyjs}{$jsinit}{$handlersjs}})();";
123 $output .= html_writer
::script($js);