Navigation Menu

Skip to content

Commit

Permalink
MDL-39246 JavaScript: store the time the page starts loading.
Browse files Browse the repository at this point in the history
This helps synchronise time on the client side with time on the server.
If you wait until a js_init_call runs, you might have had to wait an
unknown number of seconds while all the JS loads. Therefore, we capture
the current time as soon as possible in <head>.

This is a bit like adding .jsenabled to <body> as soon as possible, to
reduce visual flicker.
  • Loading branch information
timhunt committed Apr 26, 2013
1 parent cf5a329 commit 15dedb1
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/outputrequirementslib.php
Expand Up @@ -131,7 +131,7 @@ class page_requirements_manager {
protected $yui3loader;

/**
* @var stdClass default YUI loader configuration
* @var YUI_config default YUI loader configuration
*/
protected $YUI_config;

Expand Down Expand Up @@ -1313,16 +1313,25 @@ public function get_head_code(moodle_page $page, core_renderer $renderer) {

$output = '';

// Set up the M namespace.
$js = "var M = {}; M.yui = {};\n";

// Capture the time now ASAP during page load. This minimises the lag when
// we try to relate times on the server to times in the browser.
// An example of where this is used is the quiz countdown timer.
$js .= "M.pageloadstarttime = new Date();\n";

// Add a subset of Moodle configuration to the M namespace.
$js .= js_writer::set_variable('M.cfg', $this->M_cfg, false);

// Set up global YUI3 loader object - this should contain all code needed by plugins.
// Note: in JavaScript just use "YUI().use('overlay', function(Y) { .... });",
// this needs to be done before including any other script.
$js = "var M = {}; M.yui = {};\n";
$js .= $this->YUI_config->get_config_functions();
$js .= js_writer::set_variable('YUI_config', $this->YUI_config, false) . "\n";
$js .= "M.yui.loader = {modules: {}};\n"; // Backwards compatibility only, not used any more.
$js .= js_writer::set_variable('M.cfg', $this->M_cfg, false);

$js = $this->YUI_config->update_header_js($js);

$output .= html_writer::script($js);

// YUI3 JS and CSS need to be loaded in the header but after the YUI_config has been created.
Expand Down

0 comments on commit 15dedb1

Please sign in to comment.