MDL-21591 fixed regression in JS calls
[moodle.git] / lib / outputrequirementslib.php
CommitLineData
0bb38e8c
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Library functions to facilitate the use of JavaScript in Moodle.
21 *
22 * @package moodlecore
23 * @copyright 2009 Tim Hunt, 2010 Petr Skoda
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27// note: you can find history of this file in lib/ajax/ajaxlib.php
28
29/**
30 * This class tracks all the things that are needed by the current page.
31 *
32 * Normally, the only instance of this class you will need to work with is the
33 * one accessible via $PAGE->requires.
34 *
35 * Typical useage would be
36 * <pre>
37 * $PAGE->requires->init_js_call('M.mod_forum.init_view');
38 * </pre>
39 *
40 * It also supports obsoleted coding style withouth YUI3 modules.
41 * <pre>
42 * $PAGE->requires->css('/mod/mymod/userstyles.php?id='.$id); // not overriddable via themes!
43 * $PAGE->requires->js('/mod/mymod/script.js');
44 * $PAGE->requires->js('/mod/mymod/small_but_urgent.js', true);
45 * $PAGE->requires->js_function_call('init_mymod', array($data), true);
46 * </pre>
47 *
48 * There are some natural restrictions on some methods. For example, {@link css()}
49 * can only be called before the <head> tag is output. See the comments on the
50 * individual methods for details.
51 *
52 * @copyright 2009 Tim Hunt, 2010 Petr Skoda
53 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
54 * @since Moodle 2.0
55 */
56class page_requirements_manager {
57 /** List of string available from JS */
58 protected $stringsforjs = array();
59 /** List of JS variables to be initialised */
60 protected $jsinitvariables = array('head'=>array(), 'footer'=>array());
61 /** Included JS scripts */
62 protected $jsincludes = array('head'=>array(), 'footer'=>array());
63 /** List of needed function calls */
64 protected $jscalls = array('normal'=>array(), 'ondomready'=>array());
65 /**
66 * List of skip links, those are needed for accessibility reasons
67 * @var array
68 */
69 protected $skiplinks = array();
70 /**
71 * Javascript code used for initialisation of page, it shoudl be relatively small
72 * @var array
73 */
74 protected $jsinitcode = array();
75 /**
76 * Theme sheets, initialised only from core_renderer
77 * @var array of moodle_url
78 */
79 protected $cssthemeurls = array();
80 /**
81 * List of custom theme sheets, these are strongly discouraged!
82 * Useful mostly only for CSS submitted by teachers that is not part of the theme.
83 * @var array of moodle_url
84 */
85 protected $cssurls = array();
86 /**
87 * List of requested event handlers
88 * @var array
89 */
90 protected $eventhandlers = array();
91 /**
92 * Extra modules
93 * @var array
94 */
95 protected $extramodules = array();
96 /** Flag indicated head stuff already printed */
97 protected $headdone = false;
98 /** Flag indicating top of body already printed */
99 protected $topofbodydone = false;
100
101 /** YUI PHPLoader instance responsible for YUI2 loading from PHP only */
102 protected $yui2loader;
103 /** YUI PHPLoader instance responsible for YUI3 loading from PHP only */
104 protected $yui3loader;
105 /** YUI PHPLoader instance responsible for YUI3 loading from javascript */
106 protected $M_yui_loader;
107 /** some config vars exposed in JS, please no secret stuff there */
108 protected $M_cfg;
109
110 /**
111 * Page requirements constructor.
112 */
113 public function __construct() {
114 global $CFG;
115
116 require_once("$CFG->libdir/yui/phploader/phploader/loader.php");
117
118 $this->yui3loader = new YAHOO_util_Loader($CFG->yui3version);
119 $this->yui2loader = new YAHOO_util_Loader($CFG->yui2version);
120
121 // set up some loader options
122 if (debugging('', DEBUG_DEVELOPER)) {
123 $this->yui3loader->filter = YUI_DEBUG; // alternatively we could use just YUI_RAW here
124 $this->yui2loader->filter = YUI_DEBUG; // alternatively we could use just YUI_RAW here
125 } else {
126 $this->yui3loader->filter = null;
127 $this->yui2loader->filter = null;
128 }
129 if (!empty($CFG->useexternalyui)) {
130 $this->yui3loader->base = 'http://yui.yahooapis.com/' . $CFG->yui3version . '/build/';
131 $this->yui2loader->base = 'http://yui.yahooapis.com/' . $CFG->yui2version . '/build/';
132 $this->yui3loader->comboBase = 'http://yui.yahooapis.com/combo?';
133 $this->yui2loader->comboBase = 'http://yui.yahooapis.com/combo?';
134 } else {
135 $this->yui3loader->base = $CFG->httpswwwroot . '/lib/yui/'. $CFG->yui3version . '/build/';
136 $this->yui2loader->base = $CFG->httpswwwroot . '/lib/yui/'. $CFG->yui2version . '/build/';
137 $this->yui3loader->comboBase = $CFG->httpswwwroot . '/theme/yui_combo.php?';
138 $this->yui2loader->comboBase = $CFG->httpswwwroot . '/theme/yui_combo.php?';
139 }
140
141 // enable combo loader? this significantly helps with caching and performance!
142 $this->yui3loader->combine = !empty($CFG->yuicomboloading);
143 $this->yui2loader->combine = !empty($CFG->yuicomboloading);
144
145 // set up JS YUI loader helper object
146 $this->M_yui_loader = new stdClass();
147 $this->M_yui_loader->base = $this->yui3loader->base;
148 $this->M_yui_loader->comboBase = $this->yui3loader->comboBase;
149 $this->M_yui_loader->combine = $this->yui3loader->combine;
150 $this->M_yui_loader->filter = ($this->yui3loader->filter == YUI_DEBUG) ? 'debug' : '';
151 $this->M_yui_loader->insertBefore = 'firstthemesheet';
152 $this->M_yui_loader->modules = array();
153 $this->add_yui2_modules(); // adds loading info for all YUI2 modules
154 $this->js_module($this->find_module('core_filepicker'));
155 $this->js_module($this->find_module('core_dock'));
156
157 // YUI3 init code
158 $libs = array('cssreset', 'cssbase', 'cssfonts', 'cssgrids', 'node', 'loader'); // full CSS reset + basic libs
159 foreach ($libs as $lib) {
160 $this->yui3loader->load($lib);
161 }
162 }
163
164 /**
165 * This method adds yui2 modules into the yui3 JS loader-
166 * @return void
167 */
168 protected function add_yui2_modules() {
169 //note: this function is definitely not perfect, because
170 // it adds tons of markup into each page, but it can be
171 // abstracted into separate JS file with proper headers
172 global $CFG;
173
174 $GLOBALS['yui_current'] = array();
175 require($CFG->libdir.'/yui/phploader/lib/meta/config_'.$CFG->yui2version.'.php');
176 $info = $GLOBALS['yui_current'];
177 unset($GLOBALS['yui_current']);
178
179 if (empty($CFG->yuicomboloading)) {
180 $urlbase = $this->yui2loader->base;
181 } else {
182 $urlbase = $this->yui2loader->comboBase.$CFG->yui2version.'/build/';
183 }
184
185 $modules = array();
186 $ignored = array(); // list of CSS modules that are not needed
187 foreach ($info['moduleInfo'] as $name => $module) {
188 if ($module['type'] === 'css') {
189 $ignored[$name] = true;
190 } else {
191 $modules['yui2-'.$name] = $module;
192 }
193 }
194 foreach ($modules as $name=>$module) {
195 $module['fullpath'] = $urlbase.$module['path']; // fix path to point to correct location
196 unset($module['path']);
0139ec3f 197 unset($module['skinnable']); // we load all YUI2 css automatically, this prevents weird missing css loader problems
0bb38e8c
PS
198 foreach(array('requires', 'optional', 'supersedes') as $fixme) {
199 if (!empty($module[$fixme])) {
200 $fixed = false;
201 foreach ($module[$fixme] as $key=>$dep) {
202 if (isset($ignored[$dep])) {
203 unset($module[$fixme][$key]);
204 $fixed = true;
205 } else {
206 $module[$fixme][$key] = 'yui2-'.$dep;
207 }
208 }
209 if ($fixed) {
210 $module[$fixme] = array_merge($module[$fixme]); // fix keys
211 }
212 }
213 }
214 $this->M_yui_loader->modules[$name] = $module;
215 }
216 }
217
218 /**
219 * Initialise with the bits of JavaScript that every Moodle page should have.
220 *
221 * @param moodle_page $page
222 * @param core_renderer $output
223 */
224 protected function init_requirements_data(moodle_page $page, core_renderer $renderer) {
225 global $CFG;
226
227 // JavaScript should always work with $CFG->httpswwwroot rather than $CFG->wwwroot.
228 // Otherwise, in some situations, users will get warnings about insecure content
4aea3cc7 229 // on secure pages from their web browser.
0bb38e8c
PS
230
231 $this->M_cfg = array(
232 'wwwroot' => $CFG->httpswwwroot, // Yes, really. See above.
233 'sesskey' => sesskey(),
234 'loadingicon' => $renderer->pix_url('i/loading_small', 'moodle')->out(false),
235 'themerev' => theme_get_revision(),
236 'theme' => $page->theme->name,
237 );
238 if (debugging('', DEBUG_DEVELOPER)) {
239 $this->M_cfg['developerdebug'] = true;
240 $this->yui2_lib('logger');
241 }
242
243 // accessibility stuff
244 $this->skip_link_to('maincontent', get_string('tocontent', 'access'));
245
246 // to be removed soon
247 $this->yui2_lib('dom'); // at least javascript-static.js needs to be migrated to YUI3
248
249 $this->string_for_js('confirmation', 'admin');
250 $this->string_for_js('cancel', 'moodle');
251 $this->string_for_js('yes', 'moodle');
252 $this->js_init_call('M.util.init_help_icons');
4aea3cc7
PS
253
254 if ($page->pagelayout === 'frametop') {
255 $this->js_init_call('M.util.init_frametop');
256 }
0bb38e8c
PS
257 }
258
259 /**
260 * Ensure that the specified JavaScript file is linked to from this page.
261 *
262 * NOTE: This function is to be used in rare cases only, please store your JS in module.js file
263 * and use $PAGE->requires->js_init_call() instead.
264 *
265 * By default the link is put at the end of the page, since this gives best page-load performance.
266 *
267 * Even if a particular script is requested more than once, it will only be linked
268 * to once.
269 *
270 * @param string|moodle_url $url The path to the .js file, relative to $CFG->dirroot / $CFG->wwwroot.
271 * For example '/mod/mymod/customscripts.js'; use moodle_url for external scripts
272 * @param bool $inhead initialise in head
273 * @return void
274 */
275 public function js($url, $inhead=false) {
276 $url = $this->js_fix_url($url);
277 $where = $inhead ? 'head' : 'footer';
278 $this->jsincludes[$where][$url->out()] = $url;
279 }
280
281 /**
282 * Ensure that the specified YUI2 library file, and all its required dependancies,
283 * are linked to from this page.
284 *
285 * By default the link is put at the end of the page, since this gives best page-load
286 * performance. Optional dependencies are not loaded automatically - if you want
287 * them you will need to load them first with other calls to this method.
288 *
289 * Even if a particular library is requested more than once (perhaps as a dependancy
290 * of other libraries) it will only be linked to once.
291 *
292 * The library is leaded as soon as possible, if $OUTPUT->header() not used yet it
293 * is put into the page header, otherwise it is loaded in the page footer.
294 *
295 * @param string|array $libname the name of the YUI2 library you require. For example 'autocomplete'.
296 * @return void
297 */
298 public function yui2_lib($libname) {
299 $libnames = (array)$libname;
300 foreach ($libnames as $lib) {
301 $this->yui2loader->load($lib);
302 }
303 }
304
305 /**
306 * Returns the actual url through which a script is served.
307 * @param moodle_url|string $url full moodle url, or shortened path to script
308 * @return moodle_url
309 */
310 protected function js_fix_url($url) {
311 global $CFG;
312
313 if ($url instanceof moodle_url) {
314 return $url;
315 } else if (strpos($url, '/') === 0) {
316 if (debugging()) {
317 // check file existence only when in debug mode
318 if (!file_exists($CFG->dirroot . strtok($url, '?'))) {
319 throw new coding_exception('Attept to require a JavaScript file that does not exist.', $url);
320 }
321 }
4c1f3942 322 if (!empty($CFG->cachejs) and !empty($CFG->jsrev) and strpos($url, '/lib/editor/') != 0) {
0139ec3f
PS
323 return new moodle_url($CFG->httpswwwroot.'/lib/javascript.php', array('file'=>$url, 'rev'=>$CFG->jsrev));
324 } else {
325 return new moodle_url($CFG->httpswwwroot.$url);
326 }
0bb38e8c
PS
327 } else {
328 throw new coding_exception('Invalid JS url, it has to be shortened url starting with / or moodle_url instance.', $url);
329 }
330 }
331
332 /**
333 * Find out if JS modulke present and return details.
334 * @param string $name name of module, ex: core_group, mod_forum
335 * @return array description of module or null if not found
336 */
337 protected function find_module($name) {
338 global $CFG;
339
340 $module = null;
341
87ad1edc 342
0bb38e8c
PS
343 if (strpos($name, 'core_') === 0) {
344 // must be some core stuff - list here is not complete, this is just the stuff used from multiple places
345 // so that we do nto have to repeat the definition of these modules over and over again
346 switch($name) {
347 case 'core_filepicker':
348 $module = array('name' => 'core_filepicker',
349 'fullpath' => '/repository/filepicker.js',
87ad1edc
PS
350 'requires' => array('base', 'node', 'json', 'async-queue', 'io', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview'),
351 'strings' => array(array('add', 'repository'), array('back', 'repository'), array('cancel', 'moodle'), array('close', 'repository'),
352 array('cleancache', 'repository'), array('copying', 'repository'), array('date', 'repository'), array('downloadsucc', 'repository'),
353 array('emptylist', 'repository'), array('error', 'repository'), array('federatedsearch', 'repository'),
354 array('filenotnull', 'repository'), array('getfile', 'repository'), array('help', 'moodle'), array('iconview', 'repository'),
355 array('invalidjson', 'repository'), array('linkexternal', 'repository'), array('listview', 'repository'),
356 array('loading', 'repository'), array('login', 'repository'), array('logout', 'repository'), array('noenter', 'repository'),
357 array('noresult', 'repository'), array('manageurl', 'repository'), array('popup', 'repository'), array('preview', 'repository'),
358 array('refresh', 'repository'), array('save', 'repository'), array('saveas', 'repository'), array('saved', 'repository'),
359 array('saving', 'repository'), array('search', 'repository'), array('searching', 'repository'), array('size', 'repository'),
360 array('submit', 'repository'), array('sync', 'repository'), array('title', 'repository'), array('upload', 'repository'),
361 array('uploading', 'repository'), array('xhtmlerror', 'repository'),
362 array('xhtml', 'quiz'), array('cancel')));
0bb38e8c
PS
363 break;
364 case 'core_comment':
365 $module = array('name' => 'core_comment',
366 'fullpath' => '/comment/comment.js',
367 'requires' => array('base', 'io', 'node', 'json', 'yui2-animation'));
368 break;
369 case 'core_role':
370 $module = array('name' => 'core_role',
371 'fullpath' => '/admin/roles/module.js');
372 break;
373 case 'core_completion':
374 $module = array('name' => 'core_completion',
375 'fullpath' => '/course/completion.js');
376 break;
377 case 'core_dock':
378 $module = array('name' => 'core_dock',
379 'fullpath' => '/blocks/dock.js',
d2e68385 380 'requires' => array('base', 'cookie', 'dom', 'io', 'node', 'event-custom', 'event-mouseenter', 'yui2-container'));
0bb38e8c
PS
381 break;
382 case 'core_calendar':
383 $module = array('name' => 'core_calendar',
384 'fullpath' => '/calendar/calendar.js',
0139ec3f 385 'requires' => array('dom', 'event', 'node', 'yui2-container', 'event-mouseenter'));
0bb38e8c
PS
386 break;
387 case 'core_message':
388 $module = array('name' => 'core_message',
389 'fullpath' => '/message/module.js');
390 break;
391 case 'core_flashdetect':
392 $module = array('name' => 'core_flashdetect',
393 'fullpath' => '/lib/flashdetect/flashdetect.js',
394 'requires' => array('io'));
395 break;
0bb38e8c
PS
396 }
397
398 } else {
399 if ($dir = get_component_directory($name, false)) {
400 if (file_exists("$CFG->dirroot/$dir/module.js")) {
401 $module = array('name'=>$name, 'fullpath'=>"/$dir/module.js", 'requires' => array());
402 }
403 }
404 }
405
406 return $module;
407 }
408
409 /**
410 * Append YUI3 module to default YUI3 JS loader.
411 * The structure of module array is described at http://developer.yahoo.com/yui/3/yui/:
412 * @param string|array $module name of module (details are autodetected), or full module specification as array
413 * @return void
414 */
415 public function js_module($module) {
416 global $CFG;
417
418 if (empty($module)) {
419 throw new coding_exception('Missing YUI3 module name or full description.');
420 }
421
422 if (is_string($module)) {
423 $module = $this->find_module($module);
424 }
425
426 if (empty($module) or empty($module['name']) or empty($module['fullpath'])) {
427 throw new coding_exception('Missing YUI3 module details.');
428 }
429
430 $module['fullpath'] = $this->js_fix_url($module['fullpath'])->out(false);
431
87ad1edc
PS
432 // add all needed strings
433 if (!empty($module['strings'])) {
434 foreach ($module['strings'] as $string) {
435 $identifier = $string[0];
436 $component = isset($string[1]) ? $string[1] : 'moodle';
437 $a = isset($string[2]) ? $string[2] : null;
438 $this->string_for_js($identifier, $component, $a);
439 }
440 }
441 unset($module['strings']);
442
0bb38e8c
PS
443 if ($this->headdone) {
444 $this->extramodules[$module['name']] = $module;
445 } else {
446 $this->M_yui_loader->modules[$module['name']] = $module;
447 }
448 }
449
450 /**
451 * Ensure that the specified CSS file is linked to from this page.
452 *
453 * Because stylesheet links must go in the <head> part of the HTML, you must call
454 * this function before {@link get_head_code()} is called. That normally means before
455 * the call to print_header. If you call it when it is too late, an exception
456 * will be thrown.
457 *
458 * Even if a particular style sheet is requested more than once, it will only
459 * be linked to once.
460 *
461 * Please note sue of this feature is strongly discouraged,
462 * it is suitable only for places where CSS is submitted directly by teachers.
463 * (Students must not be allowed to submit any external CSS because it may
464 * contain embedded javascript!). Example of correct use is mod/data.
465 *
466 * @param string $stylesheet The path to the .css file, relative to $CFG->wwwroot.
467 * For example:
468 * $PAGE->requires->css('mod/data/css.php?d='.$data->id);
469 */
470 public function css($stylesheet) {
471 global $CFG;
472
473 if ($this->headdone) {
474 throw new coding_exception('Cannot require a CSS file after &lt;head> has been printed.', $stylesheet);
475 }
476
477 if ($stylesheet instanceof moodle_url) {
478 // ok
479 } else if (strpos($stylesheet, '/') === 0) {
480 $stylesheet = new moodle_url($CFG->httpswwwroot.$stylesheet);
481 } else {
482 throw new coding_exception('Invalid stylesheet parameter.', $stylesheet);
483 }
484
485 $this->cssurls[$stylesheet->out()] = $stylesheet; // overrides
486 }
487
488 /**
489 * Add theme stylkesheet to page - do not use from plugin code,
490 * this should be called only from the core renderer!
491 * @param moodle_url $stylesheet
492 * @return void
493 */
494 public function css_theme(moodle_url $stylesheet) {
495 $this->cssthemeurls[] = $stylesheet;
496 }
497
498 /**
499 * Ensure that a skip link to a given target is printed at the top of the <body>.
500 *
501 * You must call this function before {@link get_top_of_body_code()}, (if not, an exception
502 * will be thrown). That normally means you must call this before the call to print_header.
503 *
504 * If you ask for a particular skip link to be printed, it is then your responsibility
505 * to ensure that the appropraite <a name="..."> tag is printed in the body of the
506 * page, so that the skip link goes somewhere.
507 *
508 * Even if a particular skip link is requested more than once, only one copy of it will be output.
509 *
510 * @param $target the name of anchor this link should go to. For example 'maincontent'.
511 * @param $linktext The text to use for the skip link. Normally get_string('skipto', 'access', ...);
512 */
513 public function skip_link_to($target, $linktext) {
514 if ($this->topofbodydone) {
515 debugging('Page header already printed, can not add skip links any more, code needs to be fixed.');
516 return;
517 }
518 $this->skiplinks[$target] = $linktext;
519 }
520
521 /**
522 * !!!DEPRECATED!!! please use js_init_call() if possible
523 * Ensure that the specified JavaScript function is called from an inline script
524 * somewhere on this page.
525 *
526 * By default the call will be put in a script tag at the
527 * end of the page after initialising Y instance, since this gives best page-load
528 * performance and allows you to use YUI3 library.
529 *
530 * If you request that a particular function is called several times, then
531 * that is what will happen (unlike linking to a CSS or JS file, where only
532 * one link will be output).
533 *
534 * The main benefit of the mehtod is the automatic encoding of all function parameters.
535 *
536 * @param string $function the name of the JavaScritp function to call. Can
537 * be a compound name like 'Y.Event.purgeElement'. Can also be
538 * used to create and object by using a 'function name' like 'new user_selector'.
539 * @param array $arguments and array of arguments to be passed to the function.
540 * When generating the function call, this will be escaped using json_encode,
541 * so passing objects and arrays should work.
542 * @param bool $ondomready
543 * @param int $delay
544 * @return void
545 */
546 public function js_function_call($function, array $arguments = null, $ondomready = false, $delay = 0) {
547 $where = $ondomready ? 'ondomready' : 'normal';
548 $this->jscalls[$where][] = array($function, $arguments, $delay);
549 }
550
551 /**
552 * Ensure that the specified JavaScript function is called from an inline script
553 * from page footer.
554 *
555 * @param string $function the name of the JavaScritp function to with init code,
556 * usually something like 'M.mod_mymodule.init'
557 * @param array $extraarguments and array of arguments to be passed to the function.
558 * The first argument is always the YUI3 Y instance with all required dependencies
559 * already loaded.
560 * @param bool $ondomready wait for dom ready (helps with some IE problems when modifying DOM)
561 * @param array $module JS module specification array
562 * @return void
563 */
564 public function js_init_call($function, array $extraarguments = null, $ondomready = false, array $module = null) {
565 $jscode = js_writer::function_call_with_Y($function, $extraarguments);
566 if (!$module) {
567 // detect module automatically
568 if (preg_match('/M\.([a-z0-9]+_[^\.]+)/', $function, $matches)) {
569 $module = $this->find_module($matches[1]);
570 }
571 }
572
573 $this->js_init_code($jscode, $ondomready, $module);
574 }
575
576 /**
577 * Add short static javascript code fragment to page footer.
578 * This is intended primarily for loading of js modules and initialising page layout.
579 * Ideally the JS code fragment should be stored in plugin renderer so that themes
580 * may override it.
581 * @param string $jscode
582 * @param bool $ondomready wait for dom ready (helps with some IE problems when modifying DOM)
583 * @param array $module JS module specification array
584 * @return void
585 */
586 public function js_init_code($jscode, $ondomready = false, array $module = null) {
587 $jscode = trim($jscode, " ;\n"). ';';
588
589 if ($module) {
590 $this->js_module($module);
591 $modulename = $module['name'];
592 $jscode = "Y.use('$modulename', function(Y) { $jscode });";
593 }
594
595 if ($ondomready) {
596 $jscode = "Y.on('domready', function() { $jscode });";
597 }
598
599 $this->jsinitcode[] = $jscode;
600 }
601
602 /**
603 * Make a language string available to JavaScript.
604 *
2b728cb5 605 * All the strings will be available in a M.str object in the global namespace.
0bb38e8c 606 * So, for example, after a call to $PAGE->requires->string_for_js('course', 'moodle');
2b728cb5 607 * then the JavaScript variable M.str.moodle.course will be 'Course', or the
0bb38e8c
PS
608 * equivalent in the current language.
609 *
610 * The arguments to this function are just like the arguments to get_string
611 * except that $component is not optional, and there are limitations on how you
612 * use $a. Because each string is only stored once in the JavaScript (based
613 * on $identifier and $module) you cannot get the same string with two different
614 * values of $a. If you try, an exception will be thrown.
615 *
616 * If you do need the same string expanded with different $a values, then
617 * the solution is to put them in your own data structure (e.g. and array)
618 * that you pass to JavaScript with {@link data_for_js()}.
619 *
620 * @param string $identifier the desired string.
621 * @param string $module the language file to look in.
622 * @param mixed $a any extra data to add into the string (optional).
623 */
624 public function string_for_js($identifier, $component, $a = NULL) {
625 $string = get_string($identifier, $component, $a);
626 if (!$component) {
627 throw new coding_exception('The $module parameter is required for page_requirements_manager::string_for_js.');
628 }
629 if (isset($this->stringsforjs[$component][$identifier]) && $this->stringsforjs[$component][$identifier] != $string) {
630 throw new coding_exception("Attempt to re-define already required string '$identifier' " .
631 "from lang file '$component'. Did you already ask for it with a different \$a?");
632 }
633 $this->stringsforjs[$component][$identifier] = $string;
634 }
635
636 /**
637 * Make an array of language strings available for JS
638 *
639 * This function calls the above function {@link string_for_js()} for each requested
640 * string in the $identifiers array that is passed to the argument for a single module
641 * passed in $module.
642 *
643 * <code>
644 * $PAGE->strings_for_js(Array('one', 'two', 'three'), 'mymod', Array('a', null, 3));
645 *
646 * // The above is identifical to calling
647 *
648 * $PAGE->string_for_js('one', 'mymod', 'a');
649 * $PAGE->string_for_js('two', 'mymod');
650 * $PAGE->string_for_js('three', 'mymod', 3);
651 * </code>
652 *
653 * @param array $identifiers An array of desired strings
654 * @param string $component The module to load for
655 * @param mixed $a This can either be a single variable that gets passed as extra
656 * information for every string or it can be an array of mixed data where the
657 * key for the data matches that of the identifier it is meant for.
658 *
659 */
660 public function strings_for_js($identifiers, $component, $a=NULL) {
661 foreach ($identifiers as $key => $identifier) {
662 if (is_array($a) && array_key_exists($key, $a)) {
663 $extra = $a[$key];
664 } else {
665 $extra = $a;
666 }
667 $this->string_for_js($identifier, $component, $extra);
668 }
669 }
670
671 /**
672 * !!!!!!DEPRECATED!!!!!! please use js_init_call() for everything now.
673 *
674 * Make some data from PHP available to JavaScript code.
675 *
676 * For example, if you call
677 * <pre>
678 * $PAGE->requires->data_for_js('mydata', array('name' => 'Moodle'));
679 * </pre>
680 * then in JavsScript mydata.name will be 'Moodle'.
681 * @param string $variable the the name of the JavaScript variable to assign the data to.
682 * Will probably work if you use a compound name like 'mybuttons.button[1]', but this
683 * should be considered an experimental feature.
684 * @param mixed $data The data to pass to JavaScript. This will be escaped using json_encode,
685 * so passing objects and arrays should work.
686 * @param bool $inhead initialise in head
687 * @return void
688 */
689 public function data_for_js($variable, $data, $inhead=false) {
690 $where = $inhead ? 'head' : 'footer';
691 $this->jsinitvariables[$where][] = array($variable, $data);
692 }
693
694 /**
695 * Creates a YUI event handler.
696 *
697 * @param mixed $selector standard YUI selector for elemnts, may be array or string, element id is in the form "#idvalue"
698 * @param string $event A valid DOM event (click, mousedown, change etc.)
699 * @param string $function The name of the function to call
700 * @param array $arguments An optional array of argument parameters to pass to the function
701 * @return void
702 */
703 public function event_handler($selector, $event, $function, array $arguments = null) {
704 $this->eventhandlers[] = array('selector'=>$selector, 'event'=>$event, 'function'=>$function, 'arguments'=>$arguments);
705 }
706
707 /**
708 * Returns code needed for registering of event handlers.
709 * @return string JS code
710 */
711 protected function get_event_handler_code() {
712 $output = '';
713 foreach ($this->eventhandlers as $h) {
714 $output .= js_writer::event_handler($h['selector'], $h['event'], $h['function'], $h['arguments']);
715 }
716 return $output;
717 }
718
719 /**
720 * Get the inline JavaScript code that need to appear in a particular place.
721 * @return bool $ondomready
722 */
723 protected function get_javascript_code($ondomready) {
724 $where = $ondomready ? 'ondomready' : 'normal';
725 $output = '';
726 if ($this->jscalls[$where]) {
727 foreach ($this->jscalls[$where] as $data) {
e6a3ea28 728 $output .= js_writer::function_call($data[0], $data[1], $data[2]);
0bb38e8c
PS
729 }
730 if (!empty($ondomready)) {
731 $output = " Y.on('domready', function() {\n$output\n });";
732 }
733 }
734 return $output;
735 }
736
737 /**
738 * Returns js code to be executed when Y is available.
739 * @return unknown_type
740 */
741 protected function get_javascript_init_code() {
742 if (count($this->jsinitcode)) {
743 return implode("\n", $this->jsinitcode) . "\n";
744 }
745 return '';
746 }
747
748 /**
749 * Returns basic YUI3 JS loading code.
750 * YUI3 is using autoloading of both CSS and JS code.
751 *
752 * Major benefit of this compared to standard js/csss loader is much improved
753 * caching, better browser cache utilisation, much fewer http requests.
754 *
755 * @return string
756 */
757 protected function get_yui3lib_headcode() {
758 $code = $this->yui3loader->css() . $this->yui3loader->script();
759 // unfortunately yui loader does not produce xhtml strict code, so let's fix it for now
760 $code = str_replace('&amp;', '&', $code);
761 $code = str_replace('&', '&amp;', $code);
762 return $code;
763 }
764
765 /**
766 * Returns basic YUI2 JS loading code.
767 * It can be called manually at any time.
768 * If called manually the result needs to be output using echo().
769 *
770 * Major benefit of this compared to standard js loader is much improved
771 * caching, better browser cache utilisation, much fewer http requests.
772 *
773 * All YUI2 CSS is loaded automatically.
774 *
775 * @return string JS embedding code
776 */
777 public function get_yui2lib_code() {
778 global $CFG;
779
780 if ($this->headdone) {
781 $code = $this->yui2loader->script();
782 } else {
783 $code = $this->yui2loader->script();
784 if ($this->yui2loader->combine) {
785 $skinurl = $this->yui2loader->comboBase . $CFG->yui2version . '/build/assets/skins/sam/skin.css';
786 } else {
787 $skinurl = $this->yui2loader->base . 'assets/skins/sam/skin.css';
788 }
789 // please note this is a temporary hack until we fully migrate to later YUI3 that has all the widgets
790 $attributes = array('rel'=>'stylesheet', 'type'=>'text/css', 'href'=>$skinurl);
791 $code .= "\n" . html_writer::empty_tag('link', $attributes) . "\n";
792 }
793 $code = str_replace('&amp;', '&', $code);
794 $code = str_replace('&', '&amp;', $code);
795 return $code;
796 }
797
798 /**
799 * Returns html tags needed for inclusion of theme CSS
800 * @return string
801 */
802 protected function get_css_code() {
803 // First of all the theme CSS, then any custom CSS
804 // Please note custom CSS is strongly discouraged,
805 // because it can not be overridden by themes!
806 // It is suitable only for things like mod/data which accepts CSS from teachers.
807
808 $code = '';
809 $attributes = array('id'=>'firstthemesheet', 'rel'=>'stylesheet', 'type'=>'text/css');
810
811 $urls = $this->cssthemeurls + $this->cssurls;
812
813 foreach ($urls as $url) {
814 $attributes['href'] = $url;
815 $code .= html_writer::empty_tag('link', $attributes) . "\n";
816 // this id is needed in first sheet only so that theme may override YUI sheets laoded on the fly
817 unset($attributes['id']);
818 }
819
820 return $code;
821 }
822
823 /**
824 * Adds extra modules specified after printing of page header
825 */
826 protected function get_extra_modules_code() {
827 if (empty($this->extramodules)) {
828 return '';
829 }
830 return html_writer::script(js_writer::function_call('M.yui.add_module', array($this->extramodules)));
831 }
832
833 /**
834 * Generate any HTML that needs to go inside the <head> tag.
835 *
836 * Normally, this method is called automatically by the code that prints the
837 * <head> tag. You should not normally need to call it in your own code.
838 *
839 * @return string the HTML code to to inside the <head> tag.
840 */
841 public function get_head_code(moodle_page $page, core_renderer $renderer) {
842 global $CFG;
843
844 // note: the $page and $output are not stored here because it would
845 // create circular references in memory which prevents garbage collection
846 $this->init_requirements_data($page, $renderer);
847
848 // yui3 JS and CSS is always loaded first - it is cached in browser
849 $output = $this->get_yui3lib_headcode();
850
851 // BC: load basic YUI2 for now, all yui2 things should be loaded via Y.use('yui2-oldmodulename')
852 $output .= $this->get_yui2lib_code();
853
854 // now theme CSS + custom CSS in this specific order
855 $output .= $this->get_css_code();
856
857 // set up global YUI3 loader object - this should contain all code needed by plugins
858 // note: in JavaScript just use "YUI(M.yui.loader).use('overlay', function(Y) { .... });"
859 // this needs to be done before including any other script
860 $js = "var M = {}; M.yui = {}; ";
861 $js .= js_writer::set_variable('M.yui.loader', $this->M_yui_loader, false) . "\n";
862 $js .= js_writer::set_variable('M.cfg', $this->M_cfg, false);
863 $output .= html_writer::script($js);
864
865 // link our main JS file, all core stuff should be there
0139ec3f 866 $output .= html_writer::script('', $this->js_fix_url('/lib/javascript-static.js'));
0bb38e8c
PS
867
868 // add variables
869 if ($this->jsinitvariables['head']) {
870 $js = '';
871 foreach ($this->jsinitvariables['head'] as $data) {
872 list($var, $value) = $data;
873 $js .= js_writer::set_variable($var, $value, true);
874 }
875 $output .= html_writer::script($js);
876 }
877
878 // all the other linked things from HEAD - there should be as few as possible
879 if ($this->jsincludes['head']) {
880 foreach ($this->jsincludes['head'] as $url) {
881 $output .= html_writer::script('', $url);
882 }
883 }
884
885 // mark head sending done, it is not possible to anything there
886 $this->headdone = true;
887
888 return $output;
889 }
890
891 /**
892 * Generate any HTML that needs to go at the start of the <body> tag.
893 *
894 * Normally, this method is called automatically by the code that prints the
895 * <head> tag. You should not normally need to call it in your own code.
896 *
897 * @return string the HTML code to go at the start of the <body> tag.
898 */
899 public function get_top_of_body_code() {
900 // first the skip links
901 $links = '';
902 $attributes = array('class'=>'skip');
903 foreach ($this->skiplinks as $url => $text) {
904 $attributes['href'] = '#' . $url;
905 $links .= html_writer::tag('a', $attributes, $text);
906 }
907 $output = html_writer::tag('div', array('class'=>'skiplinks'), $links) . "\n";
908
909 // then the clever trick for hiding of things not needed when JS works
910 $output .= html_writer::script("document.body.className += ' jsenabled';") . "\n";
911 $this->topofbodydone = true;
912 return $output;
913 }
914
915 /**
916 * Generate any HTML that needs to go at the end of the page.
917 *
918 * Normally, this method is called automatically by the code that prints the
919 * page footer. You should not normally need to call it in your own code.
920 *
921 * @return string the HTML code to to at the end of the page.
922 */
923 public function get_end_code() {
924 global $CFG;
925 // add other requested modules
926 $output = $this->get_extra_modules_code();
927
928 // add missing YUI2 YUI - to be removed once we convert everything to YUI3!
929 $output .= $this->get_yui2lib_code();
930
931 // all the other linked scripts - there should be as few as possible
932 if ($this->jsincludes['footer']) {
933 foreach ($this->jsincludes['footer'] as $url) {
934 $output .= html_writer::script('', $url);
935 }
936 }
937
938 // add all needed strings
939 if (!empty($this->stringsforjs)) {
2b728cb5 940 $output .= html_writer::script(js_writer::set_variable('M.str', $this->stringsforjs));
0bb38e8c
PS
941 }
942
943 // add variables
944 if ($this->jsinitvariables['footer']) {
945 $js = '';
946 foreach ($this->jsinitvariables['footer'] as $data) {
947 list($var, $value) = $data;
948 $js .= js_writer::set_variable($var, $value, true);
949 }
950 $output .= html_writer::script($js);
951 }
952
953 $inyuijs = $this->get_javascript_code(false);
954 $ondomreadyjs = $this->get_javascript_code(true);
955 $jsinit = $this->get_javascript_init_code();
956 $handlersjs = $this->get_event_handler_code();
957
0139ec3f 958 // there is no global Y, make sure it is available in your scope
0bb38e8c
PS
959 $js = "YUI(M.yui.loader).use('node', function(Y) {\n{$inyuijs}{$ondomreadyjs}{$jsinit}{$handlersjs}\n});";
960
961 $output .= html_writer::script($js);
962
963 return $output;
964 }
965
966 /**
967 * @return boolean Have we already output the code in the <head> tag?
968 */
969 public function is_head_done() {
970 return $this->headdone;
971 }
972
973 /**
974 * @return boolean Have we already output the code at the start of the <body> tag?
975 */
976 public function is_top_of_body_done() {
977 return $this->topofbodydone;
978 }
979}
0139ec3f
PS
980
981/**
982 * Invalidate all server and client side JS caches.
983 * @return void
984 */
985function js_reset_all_caches() {
986 global $CFG;
987 require_once("$CFG->libdir/filelib.php");
988
989 set_config('jsrev', empty($CFG->jsrev) ? 1 : $CFG->jsrev+1);
990 //fulldelete("$CFG->dataroot/cache/js");
991}
992