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 = 'M.util.js_pending("core/first");';
61 $prefix .= "require(['core/first'], function() {\n";
63 $suffix .= 'M.util.js_complete("core/first");';
64 $output .= html_writer
::script($prefix . implode(";\n", $this->amdjscode
) . $suffix);
70 * Generate any HTML that needs to go at the end of the page.
72 * @return string the HTML code to to at the end of the page.
74 public function get_end_code() {
79 // Call amd init functions.
80 $output .= $this->get_amd_footercode();
82 // Add other requested modules.
83 $output .= $this->get_extra_modules_code();
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);
92 if (!empty($this->stringsforjs
)) {
93 // Add all needed strings.
95 foreach ($this->stringsforjs
as $component => $v) {
96 foreach ($v as $indentifier => $langstring) {
97 $strings[$component][$indentifier] = $langstring->out();
100 // Append don't overwrite.
101 $output .= html_writer
::script('require(["jquery"], function($) {
102 M.str = $.extend(true, M.str, ' . json_encode($strings) . ');
107 if ($this->jsinitvariables
['footer']) {
109 foreach ($this->jsinitvariables
['footer'] as $data) {
110 list($var, $value) = $data;
111 $js .= js_writer
::set_variable($var, $value, true);
113 $output .= html_writer
::script($js);
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();
122 // There is a global Y, make sure it is available in your scope.
123 $js = "(function() {{$inyuijs}{$ondomreadyjs}{$jsinit}{$handlersjs}})();";
125 $output .= html_writer
::script($js);