diff --git a/admin/report/capability/index.php b/admin/report/capability/index.php index 1a6aab4711c4a..7256f8b764086 100644 --- a/admin/report/capability/index.php +++ b/admin/report/capability/index.php @@ -38,8 +38,9 @@ } // 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 @@ 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 7bd2d3623e24a..d9eb693d7fc85 100755 --- a/admin/report/spamcleaner/index.php +++ b/admin/report/spamcleaner/index.php @@ -32,8 +32,6 @@ 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 @@ 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 daa5d260f552a..afcb7004fe6ce 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 d59a418084a1b..5aec111610a76 100644 --- a/admin/roles/lib.php +++ b/admin/roles/lib.php @@ -121,10 +121,10 @@ public function display() { /// 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 d7b986faa18f6..483eec16e5934 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 5ac22c0b93723..79551eb24a4c3 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 @@ function init() { $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 @@ function get_content() { 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 @@ function get_content() { } 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 b7841471ebb37..37d890fd4b9ec 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 06513435b17f6..a593f44ec315b 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 478a0b0cdb9ff..54dae4d017994 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 12fbcf6c63dba..fe2a2a883c4b8 100644 --- a/group/members.php +++ b/group/members.php @@ -12,7 +12,6 @@ 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' => $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 @@ diff --git a/lib/ajax/ajaxlib.php b/lib/ajax/ajaxlib.php index e0aeaa2c891cb..da077356304f0 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 @@ public function js($jsfile, $fullurl = false) { * * 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 @@ public function css($stylesheet, $fullurl = false) { 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 @@ public function skip_link_to($target, $linktext) { * 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 @@ protected function get_javascript_code($when, $indent = '') { * @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 @@ public function get_top_of_body_code() { 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 @@ public function __construct(page_requirements_manager $manager, $url) { } public function get_html() { - return '' . "\n"; + return ajax_get_link_to_script($this->url); } /** @@ -591,7 +606,6 @@ public function at_top_of_body() { */ 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 @@ public function __construct(page_requirements_manager $manager, $libname) { 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 @@ public function asap() { $ouput = ''; foreach ($this->jss as $requiredjs) { - $ouput .= $requiredjs->immediately(); + $ouput .= $requiredjs->asap(); } $this->mark_done(); return $ouput; @@ -667,7 +677,7 @@ public function in_head() { $this->when = page_requirements_manager::WHEN_IN_HEAD; foreach ($this->jss as $requiredjs) { - $ouput .= $requiredjs->in_head(); + $requiredjs->in_head(); } } @@ -850,6 +860,7 @@ public function at_top_of_body() { 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 @@ public function get_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 @@ public function on_dom_ready() { $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 @@ public function get_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 003e19b7ac625..cc6668298d18c 100644 --- a/lib/ajax/simpletest/testajaxlib.php +++ b/lib/ajax/simpletest/testajaxlib.php @@ -391,25 +391,16 @@ public function test_requiring_js_function_call_on_dom_ready() { 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 3657be81636e3..d79062b79e533 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 18d6121112ab4..6de214281a9ff 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 9aa9e455b7f51..f94058665c0d0 100644 --- a/lib/form/dateselector.php +++ b/lib/form/dateselector.php @@ -82,6 +82,7 @@ function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null } } } + form_init_date_js(); } // }}} @@ -179,7 +180,6 @@ function onQuickFormEvent($event, $arg, &$caller) 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 e9d480fedfec1..d1feef157d47c 100644 --- a/lib/form/datetimeselector.php +++ b/lib/form/datetimeselector.php @@ -82,6 +82,7 @@ function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel = } } } + form_init_date_js(); } // }}} @@ -197,7 +198,6 @@ function onQuickFormEvent($event, $arg, &$caller) 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 4b7fa99917d11..ef5352cb0ffb5 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -62,20 +62,17 @@ function pear_handle_error($error){ } /** - * @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 756748ee1f0c6..f378bf34d967e 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 5f62bafaa6d50..1ed204dc276e7 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -301,7 +301,6 @@ public function get_requires() { 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 @@ protected function initialise_standard_body_classes() { $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 d93bba5a09912..425fd5164bdeb 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 1b508c90517b1..795e4aa32ef07 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 .= '