MDL-16583 Make require_js accept library names like lib/javascript-static.js without the caller having to fiddle around with $CFG->wwwroot themselves.

This commit is contained in:
tjhunt 2008-09-25 06:29:28 +00:00
parent 1bab4e784d
commit e874af28ce
3 changed files with 84 additions and 20 deletions

View File

@ -9,7 +9,6 @@
* @return string
*/
function ajax_get_lib($libname) {
global $CFG;
$libpath = '';
$external_yui = false;
@ -66,18 +65,21 @@ function ajax_get_lib($libname) {
include($CFG->libdir.'/yui/version.php');
$libpath = 'http://yui.yahooapis.com/'.$yuiversion.'/build/'.substr($translatelist[$libname], 9);
} else {
$libpath = $CFG->wwwroot . $translatelist[$libname];
$libpath = $CFG->httpswwwroot . $translatelist[$libname];
}
} else {
} else if (preg_match('/^https?:/', $libname)) {
$libpath = $libname;
} else {
$libpath = $CFG->httpswwwroot . '/' . $libname;
}
// Make sure the file exists if it is local.
if ($external_yui === false) {
$testpath = str_replace($CFG->wwwroot, $CFG->dirroot, $libpath);
$testpath = str_replace($CFG->httpswwwroot, $CFG->dirroot, $libpath);
if (!file_exists($testpath)) {
error('require_js: '.$libpath.' - file not found.');
throw new moodle_exception('unknownjsinrequirejs', '', '', $libpath);
}
}

View File

@ -0,0 +1,61 @@
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/**
* Unit tests for (some of) ../ajaxlib.php.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package moodlecore
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->libdir . '/ajax/ajaxlib.php');
/**
* Unit tests of mathslib wrapper and underlying EvalMath library.
*
* @author Petr Skoda (skodak)
* @version $Id$
*/
class ajaxlib_test extends MoodleUnitTestCase {
function test_ajax_get_lib() {
global $CFG;
$cases = array(
'yui_yahoo' => $CFG->wwwroot . '/lib/yui/yahoo/yahoo-min.js',
'lib/javascript-static.js' => $CFG->wwwroot . '/lib/javascript-static.js',
$CFG->wwwroot . '/lib/javascript-static.js' => $CFG->wwwroot . '/lib/javascript-static.js',
);
foreach ($cases as $arg => $result) {
$this->assertEqual(ajax_get_lib($arg), $result);
}
$this->expectException();
ajax_get_lib('a_file_that_does_not_exist.js');
}
}
?>

View File

@ -2004,7 +2004,6 @@ function replace_smilies(&$text) {
}
}
if (empty($img[$lang])) { /// After the first time this is not run again
$e[$lang] = array();
$img[$lang] = array();
@ -2554,28 +2553,30 @@ function print_header ($title='', $heading='', $navigation='', $focus='',
* Used to include JavaScript libraries.
*
* When the $lib parameter is given, the function will ensure that the
* named library is loaded onto the page - either in the HTML <head>,
* just after the header, or at an arbitrary later point in the page,
* depending on where this function is called.
* named library or libraries is loaded onto the page - either in the
* HTML <head>, just after the header, or at an arbitrary later point in
* the page, depending on where this function is called.
*
* Libraries will not be included more than once, so this works like
* require_once in PHP.
*
* There are two special-case calls to this function which are both used only
* by weblib print_header:
* There are two special-case calls to this function from print_header which are
* internal to weblib and use the second $extracthtmlparameter:
* $extracthtml = 1: this is used before printing the header.
* It returns the script tag code that should go inside the <head>.
* It returns the script tag code that should go inside the <head>.
* $extracthtml = 2: this is used after printing the header and handles any
* require_js calls that occurred within the header itself.
* require_js calls that occurred within the header itself.
*
* @param mixed $lib - string or array of strings
* string(s) should be the shortname for the library or the
* full URL (which will probably start with $CFG->wwwroot) to the library file.
* @param int $extracthtml Do not set this parameter usually (leave 0), only
* weblib should set this to 1 or 2 in print_header function.
* @return mixed No return value, except when using $extracthtml it returns the html code.
* @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'. The list of recognised values is in lib/ajax/ajaxlib.php
* 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'.
* @param int $extracthtml Private. For internal weblib use only.
* @return mixed No return value (except when doing the internal $extracthtml
* calls, when it returns html code).
*/
function require_js($lib,$extracthtml=0) {
function require_js($lib, $extracthtml = 0) {
global $CFG;
static $loadlibs = array();