Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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