From: tjhunt
Date: Fri, 12 Jun 2009 12:13:07 +0000 (+0000)
Subject: ajaxlib/require_js: MDL-16693 $PAGE->requires->... deprecates require_js etc.
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=cf6155226cba2e4e62e9e1cb44dad88afacc39b4;p=moodle.git
ajaxlib/require_js: MDL-16693 $PAGE->requires->... deprecates require_js etc.
There is a new implementation of require_js in lib/deprecatedlib.php,
based on $PAGE->requires.
There were a few other recently introduced functions in lib/weblib.php,
namely print_js_call, print_delayed_js_call, print_js_config and
standard_js_config. These have been removed, since they were never in
a stable branch, and all the places that used them have been changed
to use the newer $PAGE->requires->... methods.
get_require_js_code is also gone, and the evil places that were calling
it, even though it is an internal function, have been fixed.
Also, I made some minor improvements to the code I committed yesterday
for MDL-16695.
All that remains is to update all the places in core code that are
still using require_js.
(This commit also fixes the problem where the admin tree would not
start with the right categories expanded.)
---
diff --git a/admin/report/capability/index.php b/admin/report/capability/index.php
index 1a6aab4711..7256f8b764 100644
--- a/admin/report/capability/index.php
+++ b/admin/report/capability/index.php
@@ -38,8 +38,9 @@ if (empty($cleanedroleids)) {
}
// Include the required JavaScript.
-require_js(array('yui_yahoo','yui_event'));
-require_js('admin/report/capability/script.js');
+$PAGE->requires->yui_lib('event');
+$PAGE->requires->js('admin/report/capability/script.js');
+$PAGE->requires->js_function_call('capability_report.cap_filter_init', array(get_string('search')));
// Log.
add_to_log(SITEID, "admin", "report capability", "report/capability/index.php?capability=$capability", $capability);
@@ -77,7 +78,6 @@ choose_from_menu($capabilitychoices, 'capability', $capability, 'choose', '', ''
echo '';
choose_from_menu($rolechoices, 'roles[]', $selectedroleids, '', '', '', false, false, 0, '', true, true);
echo '';
-print_js_call('capability_report.cap_filter_init', array(get_string('search')));
echo '';
print_box_end();
diff --git a/admin/report/spamcleaner/index.php b/admin/report/spamcleaner/index.php
index 7bd2d3623e..d9eb693d7f 100755
--- a/admin/report/spamcleaner/index.php
+++ b/admin/report/spamcleaner/index.php
@@ -32,8 +32,6 @@ $autokeywords = array(
require_once('../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
-require_js(array('yui_dom-event', 'yui_connection', 'yui_json'));
-
$keyword = optional_param('keyword', '', PARAM_RAW);
$autodetect = optional_param('autodetect', '', PARAM_RAW);
$del = optional_param('del', '', PARAM_RAW);
@@ -44,6 +42,8 @@ $id = optional_param('id', '', PARAM_INT);
require_login();
admin_externalpage_setup('reportspamcleaner');
+$PAGE->requires->yui_lib('json');
+$PAGE->requires->yui_lib('connection');
// Implement some AJAX calls
diff --git a/admin/roles/assign.php b/admin/roles/assign.php
index daa5d260f5..afcb7004fe 100755
--- a/admin/roles/assign.php
+++ b/admin/roles/assign.php
@@ -32,8 +32,6 @@
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
- require_js(array('yui_yahoo', 'yui_dom', 'yui_event'));
- require_js($CFG->admin . '/roles/roles.js');
define("MAX_USERS_TO_LIST_PER_ROLE", 10);
@@ -255,6 +253,8 @@
}
/// Print the header and tabs
+ $PAGE->requires->yui_lib('dom-event');
+ $PAGE->requires->js($CFG->admin . '/roles/roles.js');
if ($context->contextlevel == CONTEXT_USER) {
$user = $DB->get_record('user', array('id'=>$userid));
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
@@ -355,7 +355,7 @@
requires->js_function_call('init_add_assign_page');
if (!empty($errors)) {
$msg = '
';
diff --git a/admin/roles/lib.php b/admin/roles/lib.php
index d59a418084..5aec111610 100644
--- a/admin/roles/lib.php
+++ b/admin/roles/lib.php
@@ -121,10 +121,10 @@ abstract class capability_table_base {
/// End of the table.
echo "\n\n";
if (count($this->capabilities) > capability_table_base::NUM_CAPS_FOR_SEARCH) {
- global $CFG;
- require_js(array('yui_yahoo', 'yui_dom', 'yui_event'));
- require_js($CFG->admin . '/roles/roles.js');
- print_js_call('cap_table_filter.init',
+ global $CFG, $PAGE;
+ $PAGE->requires->yui_lib('dom-event');
+ $PAGE->requires->js($CFG->admin . '/roles/roles.js');
+ $PAGE->requires->js_function_call('cap_table_filter.init',
array($this->id, get_string('filter'), get_string('clear')));
}
}
diff --git a/admin/xmldb/javascript.php b/admin/xmldb/javascript.php
index d7b986faa1..483eec16e5 100644
--- a/admin/xmldb/javascript.php
+++ b/admin/xmldb/javascript.php
@@ -32,25 +32,23 @@
}
/// We use this globals to be able to generate the proper JavaScripts
- global $standard_javascript;
+ global $standard_javascript, $PAGE;
/// Load XMLDB javascript needed to handle some forms
$action = optional_param('action', '', PARAM_ALPHAEXT);
$postaction = optional_param('postaction', '', PARAM_ALPHAEXT);
/// If the js exists, load it
if ($action) {
- $file = $CFG->dirroot . '/'.$CFG->admin.'/xmldb/actions/' . $action . '/' . $action . '.js';
- $wwwfile = $CFG->wwwroot . '/'.$CFG->admin.'/xmldb/actions/' . $action . '/' . $action . '.js';
+ $script = $CFG->admin . '/xmldb/actions/' . $action . '/' . $action . '.js';
+ $file = $CFG->dirroot . '/' . $script;
if (file_exists($file) && is_readable($file)) {
- require_js($wwwfile);
- } else {
+ $PAGE->requires->js($script);
+ } else if ($postaction) {
/// Try to load the postaction javascript if exists
- if ($postaction) {
- $file = $CFG->dirroot . '/'.$CFG->admin.'/xmldb/actions/' . $postaction . '/' . $postaction . '.js';
- $wwwfile = $CFG->wwwroot . '/'.$CFG->admin.'/xmldb/actions/' . $postaction . '/' . $postaction . '.js';
- if (file_exists($file) && is_readable($file)) {
- require_js($wwwfile);
- }
+ $script = $CFG->admin . '/xmldb/actions/' . $postaction . '/' . $postaction . '.js';
+ $file = $CFG->dirroot . '/' . $script;
+ if (file_exists($file) && is_readable($file)) {
+ $PAGE->requires->js($script);
}
}
}
diff --git a/blocks/admin_tree/block_admin_tree.php b/blocks/admin_tree/block_admin_tree.php
index 5ac22c0b93..79551eb24a 100644
--- a/blocks/admin_tree/block_admin_tree.php
+++ b/blocks/admin_tree/block_admin_tree.php
@@ -5,9 +5,10 @@ class block_admin_tree extends block_base {
var $currentdepth;
var $divcounter;
var $tempcontent;
- var $pathtosection;
- var $expandjavascript;
var $destination;
+ var $section = null;
+ var $pathtosection = array();
+ var $expandnodes = array();
function init() {
$this->title = get_string('administrationsite');
@@ -15,10 +16,8 @@ class block_admin_tree extends block_base {
$this->currentdepth = 0;
$this->divcounter = 1;
$this->tempcontent = '';
- // TODO
- $this->section = (isset($this->page->section) ? $this->page->section : '');
- $this->pathtosection = array();
- $this->expandnodes = array();
+ global $PAGE; // TODO change this when there is a proper way for blocks to get stuff into head.
+ $PAGE->requires->yui_lib('event');
}
function applicable_formats() {
@@ -119,6 +118,7 @@ class block_admin_tree extends block_base {
require_once($CFG->libdir.'/adminlib.php');
$adminroot = admin_get_root(false, false); // settings not required - only pages
+ $this->section = $this->page->url->param('section');
if ($current = $adminroot->locate($this->section, true)) {
$this->pathtosection = $current->path;
array_pop($this->pathtosection);
@@ -133,13 +133,13 @@ class block_admin_tree extends block_base {
}
if ($this->tempcontent !== '') {
- require_js(array('yui_yahoo','yui_event'));
- require_js('blocks/admin_tree/admintree.js');
+ $this->page->requires->js('blocks/admin_tree/admintree.js');
+ $this->page->requires->js_function_call('admin_tree.init',
+ array($this->divcounter - 1, $this->expandnodes, $CFG->pixpath,
+ get_string('folderopened'), get_string('folderclosed')));
+
$this->content = new object();
$this->content->text = '
' . $this->tempcontent . "
\n";
- $this->content->text .= print_js_call('admin_tree.init',
- array($this->divcounter - 1, $this->expandnodes, $CFG->pixpath,
- get_string('folderopened'), get_string('folderclosed')), true);
// only do search if you have moodle/site:config
if (has_capability('moodle/site:config',get_context_instance(CONTEXT_SYSTEM)) ) {
diff --git a/calendar/lib.php b/calendar/lib.php
index b7841471eb..37d890fd4b 100644
--- a/calendar/lib.php
+++ b/calendar/lib.php
@@ -1178,8 +1178,11 @@ function calendar_session_vars($course=null) {
}
function calendar_overlib_html() {
- require_js('calendar/overlib.cfg.php');
- return '';
+ global $PAGE;
+ $output = '';
+ $output .= $PAGE->requires->js('calendar/overlib.cfg.php')->asap();
+ $output .= '';
+ return $output;
}
function calendar_set_referring_course($courseid) {
diff --git a/course/completion.js b/course/completion.js
index 06513435b1..a593f44ec3 100644
--- a/course/completion.js
+++ b/course/completion.js
@@ -1,7 +1,3 @@
-var completion_strsaved, completion_strtitley, completion_strtitlen,
- completion_stralty, completion_straltn;
-var completion_wwwroot;
-
function completion_init() {
// Check the reload-forcing
var changeDetector=document.getElementById('completion_dynamic_change');
@@ -90,9 +86,7 @@ function completion_toggle(e) {
YAHOO.util.Event.preventDefault(e);
// By setting completion_wwwroot you can cause it to use absolute path
// otherwise script assumes it is called from somewhere in /course
- var target=completion_wwwroot
- ? completion_wwwroot+'/course/togglecompletion.php'
- : 'togglecompletion.php';
+ var target = moodle_cfg.wwwroot + '/course/togglecompletion.php';
YAHOO.util.Connect.asyncRequest('POST',target,
{success:completion_handle_response,failure:completion_handle_failure,scope:this},
'id='+this.cmid+'&completionstate='+this.otherState+'&fromajax=1');
diff --git a/course/view.php b/course/view.php
index 478a0b0cdb..54dae4d017 100644
--- a/course/view.php
+++ b/course/view.php
@@ -161,27 +161,10 @@
// Course-based switches
if (ajaxenabled($CFG->ajaxtestedbrowsers)) { // Browser, user and site-based switches
-
- require_js(array('yui_yahoo',
- 'yui_dom',
- 'yui_event',
- 'yui_dragdrop',
- 'yui_connection',
- 'ajaxcourse_blocks',
- 'ajaxcourse_sections'));
-
- if (debugging('', DEBUG_DEVELOPER)) {
- require_js(array('yui_logger'));
-
- $bodytags = 'onload = "javascript:
- show_logger = function() {
- var logreader = new YAHOO.widget.LogReader();
- logreader.newestOnTop = false;
- logreader.setTitle(\'Moodle Debug: YUI Log Console\');
- };
- show_logger();
- "';
- }
+ $PAGE->requires->yui_lib('dragdrop');
+ $PAGE->requires->yui_lib('connection');
+ $PAGE->requires->js('lib/ajax/block_classes.js');
+ $PAGE->requires->js('lib/ajax/section_classes.js');
// Okay, global variable alert. VERY UGLY. We need to create
// this object here before the _print_block()
@@ -195,6 +178,17 @@
$CFG->blocksdrag = $useajax; // this will add a new class to the header so we can style differently
+ $completion = new completion_info($course);
+ if ($completion->is_enabled() && ajaxenabled()) {
+ $PAGE->requires->yui_lib('connection');
+ $PAGE->requires->js('course/completion.js');
+ $PAGE->requires->data_for_js('completion_strsaved', get_string('saved', 'completion'));
+ $PAGE->requires->data_for_js('completion_strtitley', get_string('completion-title-manual-y', 'completion'));
+ $PAGE->requires->data_for_js('completion_strtitlen', get_string('completion-title-manual-n', 'completion'));
+ $PAGE->requires->data_for_js('completion_stralty', get_string('completion-alt-manual-y', 'completion'));
+ $PAGE->requires->data_for_js('completion_straltn', get_string('completion-alt-manual-n', 'completion'));
+ }
+
// The "Editing On" button will be appearing only in the "main" course screen
// (i.e., no breadcrumbs other than the default one added inside this function)
$buttons = switchroles_form($course->id);
@@ -207,19 +201,7 @@
print_header($title, $course->fullname, $navigation, '', '', true,
$buttons, user_login_string($course, $USER), false, $bodytags);
- $completion=new completion_info($course);
- if($completion->is_enabled() && ajaxenabled()) {
- require_js(array('yui_yahoo','yui_event','yui_connection','yui_dom'));
- // Need to do this after the header because it requires the YUI stuff
- // to be loaded already
- require_js('course/completion.js');
- print_js_config(array(
- 'completion_strsaved' => get_string('saved','completion'),
- 'completion_strtitley' => get_string('completion-title-manual-y','completion'),
- 'completion_strtitlen' => get_string('completion-title-manual-n','completion'),
- 'completion_stralty' => get_string('completion-alt-manual-y','completion'),
- 'completion_straltn' => get_string('completion-alt-manual-n','completion'),
- ));
+ if ($completion->is_enabled() && ajaxenabled()) {
// This value tracks whether there has been a dynamic change to the page.
// It is used so that if a user does this - (a) set some tickmarks, (b)
// go to another page, (c) clicks Back button - the page will
diff --git a/group/members.php b/group/members.php
index 12fbcf6c63..fe2a2a883c 100644
--- a/group/members.php
+++ b/group/members.php
@@ -12,7 +12,6 @@ require_once(dirname(__FILE__) . '/../config.php');
require_once(dirname(__FILE__) . '/lib.php');
require_once($CFG->dirroot . '/user/selector/lib.php');
require_once($CFG->dirroot . '/course/lib.php');
-require_js('group/clientlib.js');
$groupid = required_param('group', PARAM_INT);
@@ -82,6 +81,8 @@ $navlinks[] = array('name' => $strgroups, 'link' => "$CFG->wwwroot/group/index.p
$navlinks[] = array('name' => $stradduserstogroup, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
+$PAGE->requires->js('group/clientlib.js');
+$PAGE->requires->js_function_call('init_add_remove_members_page');
print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '', '', true, '', user_login_string($course, $USER));
check_theme_arrows();
?>
@@ -123,6 +124,5 @@ check_theme_arrows();
diff --git a/lib/ajax/ajaxlib.php b/lib/ajax/ajaxlib.php
index e0aeaa2c89..da07735630 100644
--- a/lib/ajax/ajaxlib.php
+++ b/lib/ajax/ajaxlib.php
@@ -49,6 +49,10 @@ function setup_core_javascript(page_requirements_manager $requires) {
}
$requires->data_for_js('moodle_cfg', $config)->in_head();
+ if (debugging('', DEBUG_DEVELOPER)) {
+ $requires->yui_lib('logger');
+ }
+
// Note that, as a short-cut, the code
// $js = "document.body.className += ' jsenabled';\n";
// is hard-coded in @see{page_requirements_manager::get_top_of_body_code}.
@@ -95,6 +99,10 @@ class page_requirements_manager {
* @param $jsfile The path to the .js file, relative to $CFG->dirroot / $CFG->wwwroot.
* No leading slash. For example 'mod/mymod/customscripts.js';
* @param boolean $fullurl This parameter is intended for internal use only.
+ * However, in exceptional circumstances you may wish to use it to link
+ * to JavaScript on another server. For example, lib/recaptchalib.php has to
+ * do this. This really should only be done in exceptional circumstances. This
+ * may change in the future without warning.
* (If true, $jsfile is treaded as a full URL, not relative $CFG->wwwroot.)
* @return required_js A required_js object. This allows you to control when the
* link to the script is output by calling methods like ->asap() or
@@ -125,6 +133,8 @@ class page_requirements_manager {
*
* If the YUI library you ask for requires one or more CSS files, and if
* <head> has already been printed, then an exception will be thrown.
+ * Therefore, you are strongly advised to request all the YUI libraries you
+ * will need before the call to print_header.
*
* Even if a particular library is requested more than once (perhaps as a dependancy
* of other libraries) it will only be linked to once.
@@ -162,7 +172,7 @@ class page_requirements_manager {
global $CFG;
if ($this->headdone) {
- throw new coding_exception('Cannot require a CSS file after has been printed.', $stylesheet);
+ throw new coding_exception('Cannot require a CSS file after <head> has been printed.', $stylesheet);
}
if (!$fullurl) {
if (!file_exists($CFG->dirroot . '/' . $stylesheet)) {
@@ -207,14 +217,15 @@ class page_requirements_manager {
* one link will be output).
*
* @param string $function the name of the JavaScritp function to call. Can
- * be a compound name like 'YAHOO.util.Event.addListener'.
+ * be a compound name like 'YAHOO.util.Event.addListener'. Can also be
+ * used to create and object by using a 'function name' like 'new user_selector'.
* @param array $arguments and array of arguments to be passed to the function.
* When generating the function call, this will be escaped using json_encode,
* so passing objects and arrays should work.
* @return required_js_function_call A required_js_function_call object.
* This allows you to control when the link to the script is output by
- * calling methods like ->asap(), ->in_head(), ->at_top_of_body() or
- * ->on_dom_ready() methods.
+ * calling methods like ->asap(), ->in_head(), ->at_top_of_body(),
+ * ->on_dom_ready() or ->after_delay() methods.
*/
public function js_function_call($function, $arguments = array()) {
$requirement = new required_js_function_call($this, $function, $arguments);
@@ -324,6 +335,7 @@ class page_requirements_manager {
* @return string the HTML code to to inside the <head> tag.
*/
public function get_head_code() {
+ setup_core_javascript($this);
$output = $this->get_linked_resources_code(self::WHEN_IN_HEAD);
$js = $this->get_javascript_code(self::WHEN_IN_HEAD);
$output .= ajax_generate_script_tag($js);
@@ -353,7 +365,10 @@ class page_requirements_manager {
public function get_end_code() {
$output = $this->get_linked_resources_code(self::WHEN_AT_END);
- array_unshift($this->requiredjscode, new required_data_for_js($this, 'mstr', $this->stringsforjs));
+ if (!empty($this->stringsforjs)) {
+ array_unshift($this->requiredjscode, new required_data_for_js($this, 'mstr', $this->stringsforjs));
+ }
+
$js = $this->get_javascript_code(self::WHEN_AT_END);
$ondomreadyjs = $this->get_javascript_code(self::WHEN_ON_DOM_READY, ' ');
@@ -502,7 +517,7 @@ class required_js extends linked_requirement {
}
public function get_html() {
- return '' . "\n";
+ return ajax_get_link_to_script($this->url);
}
/**
@@ -591,7 +606,6 @@ class required_js extends linked_requirement {
*/
class required_yui_lib extends linked_requirement {
protected $jss = array();
- protected $cssurls;
/**
* Constructor. Normally instances of this class should not be created
@@ -606,16 +620,12 @@ class required_yui_lib extends linked_requirement {
parent::__construct($manager, '');
$this->when = page_requirements_manager::WHEN_AT_END;
- list($jsurls, $this->cssurls) = ajax_resolve_yui_lib($libname);
+ list($jsurls, $cssurls) = ajax_resolve_yui_lib($libname);
foreach ($jsurls as $jsurl) {
$this->jss[] = $manager->js($jsurl, true);
}
- foreach ($this->cssurls as $cssurl) {
- $manager->css($cssurl, true);
- }
- if (!empty($this->cssurls)) {
- global $PAGE;
- $page->add_body_class('yui-skin-sam');
+ foreach ($cssurls as $cssurl) {
+ //$manager->css($cssurl, true);
}
}
@@ -645,7 +655,7 @@ class required_yui_lib extends linked_requirement {
$ouput = '';
foreach ($this->jss as $requiredjs) {
- $ouput .= $requiredjs->immediately();
+ $ouput .= $requiredjs->asap();
}
$this->mark_done();
return $ouput;
@@ -667,7 +677,7 @@ class required_yui_lib extends linked_requirement {
$this->when = page_requirements_manager::WHEN_IN_HEAD;
foreach ($this->jss as $requiredjs) {
- $ouput .= $requiredjs->in_head();
+ $requiredjs->in_head();
}
}
@@ -850,6 +860,7 @@ abstract class required_js_code extends requirement_base {
class required_js_function_call extends required_js_code {
protected $function;
protected $arguments;
+ protected $delay = 0;
/**
* Constructor. Normally the class and its subclasses should not be created
@@ -874,7 +885,11 @@ class required_js_function_call extends required_js_code {
foreach ($this->arguments as $arg) {
$quotedargs[] = json_encode($arg);
}
- return $this->function . '(' . implode(', ', $quotedargs) . ");\n";
+ $js = $this->function . '(' . implode(', ', $quotedargs) . ');';
+ if ($this->delay) {
+ $js = 'setTimeout(function() { ' . $js . ' }, ' . ($this->delay * 1000) . ');';
+ }
+ return $js . "\n";
}
/**
@@ -890,6 +905,20 @@ class required_js_function_call extends required_js_code {
$this->manager->yui_lib('event');
$this->when = page_requirements_manager::WHEN_ON_DOM_READY;
}
+
+ /**
+ * Indicate that this function should be called a certain number of seconds
+ * after the page has finished loading. (More exactly, a number of seconds
+ * after the onDomReady event fires.)
+ *
+ * @param integer $seconds the number of seconds delay.
+ */
+ public function after_delay($seconds) {
+ if ($seconds) {
+ $this->on_dom_ready();
+ }
+ $this->delay = $seconds;
+ }
}
@@ -943,8 +972,12 @@ class required_data_for_js extends required_js_code {
* @return string HTML, the code wrapped in \n";
+ if ($js) {
+ return '\n";
+ } else {
+ return '';
+ }
}
@@ -1102,98 +1135,12 @@ function ajax_resolve_yui_lib($libname) {
}
/**
- * Get the path to a JavaScript library.
- * @param $libname - the name of the library whose path we need.
- * @return string
+ * Return the HTML required to link to a JavaScript file.
+ * @param $url the URL of a JavaScript file.
+ * @return string the required HTML.
*/
-function ajax_get_lib($libname) {
- global $CFG, $HTTPSPAGEREQUIRED;
-
- $libpath = '';
- $external_yui = false;
-
- $translatelist = array(
- 'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
- 'yui_animation' => '/lib/yui/animation/animation-min.js',
- 'yui_autocomplete' => '/lib/yui/autocomplete/autocomplete-min.js',
- 'yui_button' => '/lib/yui/button/button-min.js',
- 'yui_calendar' => '/lib/yui/calendar/calendar-min.js',
- 'yui_charts' => '/lib/yui/charts/charts-min.js',
- 'yui_colorpicker' => '/lib/yui/colorpicker/colorpicker-min.js',
- 'yui_connection' => '/lib/yui/connection/connection-min.js',
- 'yui_container' => '/lib/yui/container/container-min.js',
- 'yui_cookie' => '/lib/yui/cookie/cookie-min.js',
- 'yui_datasource' => '/lib/yui/datasource/datasource-min.js',
- 'yui_datatable' => '/lib/yui/datatable/datatable-min.js',
- 'yui_dom' => '/lib/yui/dom/dom-min.js',
- 'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
- 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
- 'yui_editor' => '/lib/yui/editor/editor-min.js',
- 'yui_element' => '/lib/yui/element/element-min.js',
- 'yui_event' => '/lib/yui/event/event-min.js',
- 'yui_get' => '/lib/yui/get/get-min.js',
- 'yui_history' => '/lib/yui/history/history-min.js',
- 'yui_imagecropper' => '/lib/yui/imagecropper/imagecropper-min.js',
- 'yui_imageloader' => '/lib/yui/imageloader/imageloader-min.js',
- 'yui_json' => '/lib/yui/json/json-min.js',
- 'yui_layout' => '/lib/yui/layout/layout-min.js',
- 'yui_logger' => '/lib/yui/logger/logger-min.js',
- 'yui_menu' => '/lib/yui/menu/menu-min.js',
- 'yui_profiler' => '/lib/yui/profiler/profiler-min.js',
- 'yui_profilerviewer' => '/lib/yui/profilerviewer/profilerviewer-min.js',
- 'yui_resize' => '/lib/yui/resize/resize-min.js',
- 'yui_selector' => '/lib/yui/selector/selector-min.js',
- 'yui_simpleeditor' => '/lib/yui/editor/simpleeditor-min.js',
- 'yui_slider' => '/lib/yui/slider/slider-min.js',
- 'yui_tabview' => '/lib/yui/tabview/tabview-min.js',
- 'yui_treeview' => '/lib/yui/treeview/treeview-min.js',
- 'yui_uploader' => '/lib/yui/uploader/uploader-min.js',
- 'yui_utilities' => '/lib/yui/utilities/utilities.js',
- 'yui_yuiloader' => '/lib/yui/yuiloader/yuiloader-min.js',
- 'yui_yuitest' => '/lib/yui/yuitest/yuitest-min.js',
- 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
- 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
- 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
- );
-
- if (!empty($HTTPSPAGEREQUIRED)) {
- $wwwroot = $CFG->httpswwwroot;
- } else {
- $wwwroot = $CFG->wwwroot;
- }
-
- if (array_key_exists($libname, $translatelist)) {
- // If this is a YUI file and we are using external libraries
- if (substr($libname, 0, 3) == 'yui' && !empty($CFG->useexternalyui)) {
- $external_yui = true;
- // Get current version
- include($CFG->libdir.'/yui/version.php');
- $libpath = 'http://yui.yahooapis.com/'.$yuiversion.'/build/'.substr($translatelist[$libname], 9);
- } else {
- $libpath = $wwwroot . $translatelist[$libname];
- }
-
- // If we are in developer debug mode, use the non-compressed version of YUI for easier debugging.
- if (debugging('', DEBUG_DEVELOPER)) {
- $libpath = str_replace('-min.js', '.js', $libpath);
- }
-
- } else if (preg_match('/^https?:/', $libname)) {
- $libpath = $libname;
-
- } else {
- $libpath = $wwwroot . '/' . $libname;
- }
-
- // Make sure the file exists if it is local.
- if ($external_yui === false) {
- $testpath = str_replace($wwwroot, $CFG->dirroot, $libpath);
- if (!file_exists($testpath)) {
- throw new moodle_exception('unknownjsinrequirejs', '', '', $libpath);
- }
- }
-
- return $libpath;
+function ajax_get_link_to_script($url) {
+ return '' . "\n";
}
diff --git a/lib/ajax/simpletest/testajaxlib.php b/lib/ajax/simpletest/testajaxlib.php
index 003e19b7ac..cc6668298d 100644
--- a/lib/ajax/simpletest/testajaxlib.php
+++ b/lib/ajax/simpletest/testajaxlib.php
@@ -391,25 +391,16 @@ class page_requirements_manager_test extends ajaxlib_unit_test_base {
class ajax_test extends ajaxlib_unit_test_base {
function test_ajax_generate_script_tag() {
- $html = ajax_generate_script_tag('');
+ $html = ajax_generate_script_tag('var x = 1;');
$this->assertContains($html, '');
}
- function test_ajax_get_lib() {
- global $CFG;
- $olddebug = $CFG->debug;
- $CFG->debug = DEBUG_DEVELOPER;
- $this->assertEqual(ajax_get_lib('yui_yahoo'), $CFG->wwwroot . '/lib/yui/yahoo/yahoo.js');
- $CFG->debug = DEBUG_MINIMAL;
- $this->assertEqual(ajax_get_lib('yui_yahoo'), $CFG->wwwroot . '/lib/yui/yahoo/yahoo-min.js');
- $CFG->debug = $olddebug;
- $this->assertEqual(ajax_get_lib('lib/javascript-static.js'), $CFG->wwwroot . '/lib/javascript-static.js');
- $this->assertEqual(ajax_get_lib($CFG->wwwroot . '/lib/javascript-static.js'), $CFG->wwwroot . '/lib/javascript-static.js');
- $this->expectException();
- ajax_get_lib('a_file_that_does_not_exist.js');
+ function test_ajax_generate_script_tag_no_output_when_no_code() {
+ $html = ajax_generate_script_tag('');
+ $this->assertEqual($html, '');
}
}
diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php
index 3657be8163..d79062b79e 100644
--- a/lib/deprecatedlib.php
+++ b/lib/deprecatedlib.php
@@ -1684,3 +1684,38 @@ function table_column($table, $oldfield, $field, $type='integer', $size='10',
function use_html_editor($name='', $editorhidebuttons='', $id='') {
error('use_html_editor() not available anymore');
}
+
+/**
+ * The old method that was used to include JavaScript libraries.
+ * Please use $PAGE->requires->js() or $PAGE->requires->yui_lib() instead.
+ *
+ * @param mixed $lib The library or libraries to load (a string or array of strings)
+ * There are three way to specify the library:
+ * 1. a shorname like 'yui_yahoo'. This translates into a call to $PAGE->requires->yui_lib('yahoo')->asap();
+ * 2. the path to the library relative to wwwroot, for example 'lib/javascript-static.js'
+ * 3. (legacy) a full URL like $CFG->wwwroot . '/lib/javascript-static.js'.
+ * 2. and 3. lead to a call $PAGE->requires->js('/lib/javascript-static.js').
+ */
+function require_js($lib) {
+ global $CFG, $PAGE;
+ // Add the lib to the list of libs to be loaded, if it isn't already
+ // in the list.
+ if (is_array($lib)) {
+ foreach($lib as $singlelib) {
+ require_js($singlelib);
+ }
+ return;
+ }
+
+ // TODO uncomment this once we have eliminated the remaining calls to require_js from core.
+ //debugging('Call to deprecated function require_js. Please use $PAGE->requires->js() ' .
+ // 'or $PAGE->requires->yui_lib() instead.', DEBUG_DEVELOPER);
+
+ if (strpos($lib, 'yui_') === 0) {
+ echo $PAGE->requires->yui_lib(substr($lib, 4))->asap();
+ } else if (preg_match('/^https?:/', $lib)) {
+ echo $PAGE->requires->js(str_replace($CFG->wwwroot, '', $lib))->asap();
+ } else {
+ echo $PAGE->requires->js($lib)->asap();
+ }
+}
diff --git a/lib/filelib.php b/lib/filelib.php
index 18d6121112..6de214281a 100644
--- a/lib/filelib.php
+++ b/lib/filelib.php
@@ -1874,7 +1874,7 @@ function file_modify_html_header($text) {
$ufo = '';
if (filter_is_enabled('filter/mediaplugin')) {
// this script is needed by most media filter plugins.
- $ufo = get_require_js_code(array($CFG->wwwroot . '/lib/ufo.js'));
+ $ufo = ajax_get_link_to_script($CFG->wwwroot . '/lib/ufo.js');
}
preg_match('/\|\/', $text, $matches);
diff --git a/lib/form/dateselector.php b/lib/form/dateselector.php
index 9aa9e455b7..f94058665c 100644
--- a/lib/form/dateselector.php
+++ b/lib/form/dateselector.php
@@ -82,6 +82,7 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
}
}
}
+ form_init_date_js();
}
// }}}
@@ -179,7 +180,6 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
function toHtml()
{
- form_init_date_js();
include_once('HTML/QuickForm/Renderer/Default.php');
$renderer =& new HTML_QuickForm_Renderer_Default();
$renderer->setElementTemplate('{element}');
diff --git a/lib/form/datetimeselector.php b/lib/form/datetimeselector.php
index e9d480fedf..d1feef157d 100644
--- a/lib/form/datetimeselector.php
+++ b/lib/form/datetimeselector.php
@@ -82,6 +82,7 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
}
}
}
+ form_init_date_js();
}
// }}}
@@ -197,7 +198,6 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
function toHtml()
{
- form_init_date_js();
include_once('HTML/QuickForm/Renderer/Default.php');
$renderer =& new HTML_QuickForm_Renderer_Default();
$renderer->setElementTemplate('{element}');
diff --git a/lib/formslib.php b/lib/formslib.php
index 4b7fa99917..ef5352cb0f 100644
--- a/lib/formslib.php
+++ b/lib/formslib.php
@@ -62,20 +62,17 @@ if ($CFG->debug >= DEBUG_ALL){
}
/**
- * @global object
+ *
* @staticvar bool $done
- * @todo This is NOT xhtml strict, we will need something like require_css()
*/
function form_init_date_js() {
- global $CFG;
+ global $PAGE;
static $done = false;
if (!$done) {
- //TODO: this is NOT xhtml strict, we will need something like require_css()
- echo '';
- require_js(array('yui_yahoo', 'yui_dom', 'yui_event', 'yui_calendar', 'yui_container'));
- print_delayed_js_call(1, 'init_date_selectors', array(get_string('firstdayofweek')));
+ $PAGE->requires->yui_lib('calendar');
+ $PAGE->requires->yui_lib('container');
+ $PAGE->requires->js_function_call('init_date_selectors',
+ array(get_string('firstdayofweek')));
$done = true;
}
}
diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index 756748ee1f..f378bf34d9 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -8433,7 +8433,7 @@ function moodle_request_shutdown() {
* @return string Javascript code
*/
function message_popup_window() {
- global $USER, $DB;
+ global $USER, $DB, $PAGE;
$popuplimit = 30; // Minimum seconds between popups
@@ -8446,9 +8446,8 @@ function message_popup_window() {
if (get_user_preferences('message_showmessagewindow', 1) == 1) {
if ($DB->count_records_select('message', 'useridto = ? AND timecreated > ?', array($USER->id, $USER->message_lastpopup))) {
$USER->message_lastpopup = time();
- return print_js_call('openpopup', array('/message/index.php', 'message',
- 'menubar=0,location=0,scrollbars,status,resizable,width=400,height=500',
- 0), true);
+ $PAGE->requires->js_function_call('openpopup', array('/message/index.php', 'message',
+ 'menubar=0,location=0,scrollbars,status,resizable,width=400,height=500', 0));
}
}
}
diff --git a/lib/pagelib.php b/lib/pagelib.php
index 5f62bafaa6..1ed204dc27 100644
--- a/lib/pagelib.php
+++ b/lib/pagelib.php
@@ -301,7 +301,6 @@ class moodle_page {
global $CFG;
if (is_null($this->_requires)) {
$this->_requires = new page_requirements_manager();
- setup_core_javascript($this->_requires);
}
return $this->_requires;
}
@@ -662,6 +661,7 @@ class moodle_page {
$this->add_body_classes(get_browser_version_classes());
$this->add_body_class('dir-' . get_string('thisdirection'));
$this->add_body_class('lang-' . current_language());
+ $this->add_body_class('yui-skin-sam'); // Make YUI happy, if it is used.
$this->add_body_class($this->url_to_class_name($CFG->wwwroot));
diff --git a/lib/questionlib.php b/lib/questionlib.php
index d93bba5a09..425fd5164b 100644
--- a/lib/questionlib.php
+++ b/lib/questionlib.php
@@ -2110,7 +2110,7 @@ function question_format_grade($cmoptions, $grade) {
* to get from PHP.
*/
function question_init_qenginejs_script() {
- global $CFG;
+ global $CFG, $PAGE;
$config = array(
'pixpath' => $CFG->pixpath,
'wwwroot' => $CFG->wwwroot,
@@ -2119,7 +2119,7 @@ function question_init_qenginejs_script() {
'flaggedalt' => get_string('flagged', 'question'),
'unflaggedalt' => get_string('notflagged', 'question'),
);
- return print_js_config($config, 'qengine_config', true);
+ $PAGE->requires->data_for_js('qengine_config', $config);
}
/// FUNCTIONS THAT SIMPLY WRAP QUESTIONTYPE METHODS //////////////////////////////////
diff --git a/lib/recaptchalib.php b/lib/recaptchalib.php
index 1b508c9051..795e4aa32e 100644
--- a/lib/recaptchalib.php
+++ b/lib/recaptchalib.php
@@ -143,7 +143,7 @@ function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false) {
$strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
$strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
- $return = get_require_js_code(array($server . '/challenge?k=' . $pubkey . $errorpart));
+ $page->requires->js($server . '/challenge?k=' . $pubkey . $errorpart, true);
$return .= '
';
- print_delayed_js_call(1, 'document.location.replace', array($efile));
+ $PAGE->requires->js_function_call('document.location.replace', array($efile))->after_delay(1);
}
print_continue('edit.php?' . $thispageurl->get_query_string());
diff --git a/question/type/questiontype.php b/question/type/questiontype.php
index 630822dbdf..43571ba303 100644
--- a/question/type/questiontype.php
+++ b/question/type/questiontype.php
@@ -975,7 +975,7 @@ class default_questiontype {
* @param integer $flagsoption the option that says whether flags should be displayed.
*/
protected function print_question_flag($question, $state, $flagsoption) {
- global $CFG;
+ global $CFG, $PAGE;
switch ($flagsoption) {
case QUESTION_FLAGSSHOWN:
$flagcontent = $this->get_question_flag_tag($state->flagged);
@@ -991,12 +991,12 @@ class default_questiontype {
$aid = $state->attempt;
$qid = $state->question;
$checksum = question_get_toggleflag_checksum($aid, $qid, $qsid);
- $postdata = "qsid=$qsid&aid=$aid&qid=$qid&checksum=$checksum&sesskey=" . sesskey();
+ $postdata = "qsid=$qsid&aid=$aid&qid=$qid&checksum=$checksum&sesskey=" . sesskey();
$flagcontent = '' .
'' . "\n" .
- print_js_call('question_flag_changer.init_flag', array($id, $postdata), true);
+ $state->flagged, $id . 'img') . '' . "\n";
+ $PAGE->requires->js_function_call('question_flag_changer.init_flag', array($id, $postdata));
break;
default:
$flagcontent = '';
diff --git a/repository/lib.php b/repository/lib.php
index 6744e7c2f0..14f1054ce4 100644
--- a/repository/lib.php
+++ b/repository/lib.php
@@ -1721,6 +1721,7 @@ function repository_setup_default_plugins() {
}
return true;
}
+
/**
* Return javascript to create file picker to browse repositories
* @global object $CFG
@@ -1885,18 +1886,19 @@ EOD;
$lang['popup'] = get_string('popup', 'repository');
$lang['upload'] = get_string('upload', 'repository').'...';
$lang['uploading'] = get_string('uploading', 'repository');
- // fp_lang includes language strings
- $js .= print_js_config($lang, 'fp_lang', true);
+ $lang = json_encode($lang);
$options = array();
$context = get_system_context();
$options['contextid'] = $context->id;
+ $options = json_encode($options);
// fp_config includes filepicker options
- $js .= print_js_config($options, 'fp_config', true);
$accepted_file_ext = json_encode($ft->get_file_ext($accepted_filetypes));
$js .= <<
+var fp_lang = $lang;
+var fp_config = $options;
file_extensions.image = $image_file_ext;
file_extensions.media = $video_file_ext;
diff --git a/user/selector/lib.php b/user/selector/lib.php
index 08442493a0..5c8d1059c5 100644
--- a/user/selector/lib.php
+++ b/user/selector/lib.php
@@ -81,7 +81,7 @@ abstract class user_selector_base {
* You must be able to clone a userselector by doing new get_class($us)($us->get_name(), $us->get_options());
*/
public function __construct($name, $options = array()) {
- global $CFG;
+ global $CFG, $PAGE;
// Initialise member variables from constructor arguments.
$this->name = $name;
@@ -105,8 +105,10 @@ abstract class user_selector_base {
$this->searchanywhere = $this->initialise_option('userselector_searchanywhere', $this->searchanywhere);
// Required JavaScript code.
- require_js(array('yui_yahoo', 'yui_event', 'yui_json', 'yui_connection', 'yui_datasource'));
- require_js('user/selector/script.js');
+ $PAGE->requires->yui_lib('json');
+ $PAGE->requires->yui_lib('connection');
+ $PAGE->requires->yui_lib('datasource');
+ $PAGE->requires->js('user/selector/script.js');
}
/**
@@ -181,6 +183,8 @@ abstract class user_selector_base {
* @return mixed if $return is true, returns the HTML as a string, otherwise returns nothing.
*/
public function display($return = false) {
+ global $PAGE;
+
// Get the list of requested users.
$search = optional_param($this->name . '_searchtext', '', PARAM_RAW);
if (optional_param($this->name . '_clearbutton', false, PARAM_BOOL)) {
@@ -220,7 +224,8 @@ abstract class user_selector_base {
$output .= $this->option_checkbox('autoselectunique', $this->autoselectunique, get_string('userselectorautoselectunique'));
$output .= $this->option_checkbox('searchanywhere', $this->searchanywhere, get_string('userselectorsearchanywhere'));
$output .= print_collapsible_region_end(true);
- $output .= print_js_call('new user_selector_options_tracker', array(), true);
+
+ $PAGE->requires->js_function_call('new user_selector_options_tracker');
user_selector_base::$searchoptionsoutput = true;
}
$output .= "\n\n\n";
@@ -621,7 +626,7 @@ abstract class user_selector_base {
* @return any HTML needed here.
*/
protected function initialise_javascript($search) {
- global $USER;
+ global $USER, $PAGE;
$output = '';
// Put the options into the session, to allow search.php to respond to the ajax requests.
@@ -630,7 +635,7 @@ abstract class user_selector_base {
$USER->userselectors[$hash] = $options;
// Initialise the selector.
- $output .= print_js_call('new user_selector', array($this->name, $hash, $this->extrafields,
+ $PAGE->requires->js_function_call('new user_selector', array($this->name, $hash, $this->extrafields,
$search, get_string('previouslyselectedusers', '', '%%SEARCHTERM%%'),
get_string('nomatchingusers', '', '%%SEARCHTERM%%'), get_string('none')), true);