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;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Simple javascript output class
|
|
|
21 |
*
|
|
|
22 |
* @copyright 2010 Petr Skoda
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
* @since Moodle 2.0
|
|
|
25 |
* @package core
|
|
|
26 |
* @category output
|
|
|
27 |
*/
|
|
|
28 |
class js_writer {
|
|
|
29 |
/**
|
|
|
30 |
* Returns javascript code calling the function
|
|
|
31 |
*
|
|
|
32 |
* @param string $function function name, can be complex like Y.Event.purgeElement
|
|
|
33 |
* @param null|array $arguments parameters
|
|
|
34 |
* @param int $delay execution delay in seconds
|
|
|
35 |
* @return string JS code fragment
|
|
|
36 |
*/
|
|
|
37 |
public static function function_call($function, ?array $arguments = null, $delay = 0) {
|
|
|
38 |
if ($arguments) {
|
|
|
39 |
$arguments = array_map('json_encode', convert_to_array($arguments));
|
|
|
40 |
$arguments = implode(', ', $arguments);
|
|
|
41 |
} else {
|
|
|
42 |
$arguments = '';
|
|
|
43 |
}
|
|
|
44 |
$js = "$function($arguments);";
|
|
|
45 |
|
|
|
46 |
if ($delay) {
|
|
|
47 |
$delay = $delay * 1000; // Delay in miliseconds.
|
|
|
48 |
$js = "setTimeout(function() { $js }, $delay);";
|
|
|
49 |
}
|
|
|
50 |
return $js . "\n";
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Special function which adds Y as first argument of function call.
|
|
|
55 |
*
|
|
|
56 |
* @param string $function The function to call
|
|
|
57 |
* @param null|array $extraarguments Any arguments to pass to it
|
|
|
58 |
* @return string Some JS code
|
|
|
59 |
*/
|
|
|
60 |
public static function function_call_with_y($function, ?array $extraarguments = null) {
|
|
|
61 |
if ($extraarguments) {
|
|
|
62 |
$extraarguments = array_map('json_encode', convert_to_array($extraarguments));
|
|
|
63 |
$arguments = 'Y, ' . implode(', ', $extraarguments);
|
|
|
64 |
} else {
|
|
|
65 |
$arguments = 'Y';
|
|
|
66 |
}
|
|
|
67 |
return "$function($arguments);\n";
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Returns JavaScript code to initialise a new object
|
|
|
72 |
*
|
|
|
73 |
* @param string $var If it is null then no var is assigned the new object.
|
|
|
74 |
* @param string $class The class to initialise an object for.
|
|
|
75 |
* @param null|array $arguments An array of args to pass to the init method.
|
|
|
76 |
* @param null|array $requirements Any modules required for this class.
|
|
|
77 |
* @param int $delay The delay before initialisation. 0 = no delay.
|
|
|
78 |
* @return string Some JS code
|
|
|
79 |
*/
|
|
|
80 |
public static function object_init($var, $class, ?array $arguments = null, ?array $requirements = null, $delay = 0) {
|
|
|
81 |
if (is_array($arguments)) {
|
|
|
82 |
$arguments = array_map('json_encode', convert_to_array($arguments));
|
|
|
83 |
$arguments = implode(', ', $arguments);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
if ($var === null) {
|
|
|
87 |
$js = "new $class(Y, $arguments);";
|
|
|
88 |
} else if (strpos($var, '.') !== false) {
|
|
|
89 |
$js = "$var = new $class(Y, $arguments);";
|
|
|
90 |
} else {
|
|
|
91 |
$js = "var $var = new $class(Y, $arguments);";
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
if ($delay) {
|
|
|
95 |
$delay = $delay * 1000; // Delay in miliseconds.
|
|
|
96 |
$js = "setTimeout(function() { $js }, $delay);";
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if (count($requirements) > 0) {
|
|
|
100 |
$requirements = implode("', '", $requirements);
|
|
|
101 |
$js = "Y.use('$requirements', function(Y){ $js });";
|
|
|
102 |
}
|
|
|
103 |
return $js . "\n";
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Returns code setting value to variable
|
|
|
108 |
*
|
|
|
109 |
* @param string $name
|
|
|
110 |
* @param mixed $value json serialised value
|
|
|
111 |
* @param bool $usevar add var definition, ignored for nested properties
|
|
|
112 |
* @return string JS code fragment
|
|
|
113 |
*/
|
|
|
114 |
public static function set_variable($name, $value, $usevar = true) {
|
|
|
115 |
$output = '';
|
|
|
116 |
|
|
|
117 |
if ($usevar) {
|
|
|
118 |
if (strpos($name, '.')) {
|
|
|
119 |
$output .= '';
|
|
|
120 |
} else {
|
|
|
121 |
$output .= 'var ';
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
$output .= "$name = " . json_encode($value) . ";";
|
|
|
126 |
|
|
|
127 |
return $output;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Writes event handler attaching code
|
|
|
132 |
*
|
|
|
133 |
* @param array|string $selector standard YUI selector for elements, may be
|
|
|
134 |
* array or string, element id is in the form "#idvalue"
|
|
|
135 |
* @param string $event A valid DOM event (click, mousedown, change etc.)
|
|
|
136 |
* @param string $function The name of the function to call
|
|
|
137 |
* @param null|array $arguments An optional array of argument parameters to pass to the function
|
|
|
138 |
* @return string JS code fragment
|
|
|
139 |
*/
|
|
|
140 |
public static function event_handler($selector, $event, $function, ?array $arguments = null) {
|
|
|
141 |
$selector = json_encode($selector);
|
|
|
142 |
$output = "Y.on('$event', $function, $selector, null";
|
|
|
143 |
if (!empty($arguments)) {
|
|
|
144 |
$output .= ', ' . json_encode($arguments);
|
|
|
145 |
}
|
|
|
146 |
return $output . ");\n";
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
// Alias this class to the old name.
|
|
|
151 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
152 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
153 |
class_alias(js_writer::class, \js_writer::class);
|