Example #1
0
/**
 *
 * @staticvar bool $done
 */
function form_init_date_js()
{
    global $PAGE, $CFG;
    static $done = false;
    if (!$done) {
        echo '<style type="text/css">';
        echo '@import "' . $CFG->httpswwwroot . '/lib/yui/assets/skins/sam/calendar.css";';
        echo '</style>';
        require_js(array('yui_dom-event', 'yui_yahoo', 'yui_event', ajax_get_lib('yui_calendar'), ajax_get_lib('yui_container')));
        require_js($CFG->wwwroot . '/curriculum/form/javascript-static.js');
        echo '<script type="text/javascript">init_date_selectors("' . get_string('firstdayofweek') . '");</script>';
        $done = true;
    }
}
 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');
 }
Example #3
0
/**
 * Used to include JavaScript libraries.
 *
 * When the $lib parameter is given, the function will add $lib to an
 * internal list of libraries. When called without any parameters, it will
 * return the html that is needed to load the JavaScript libraries in that
 * list. Libraries that are included more than once will still only get loaded
 * once, so this works like require_once() in PHP.
 *
 * @param $lib - string or array of strings
 *               string(s) should be the shortname for the library or the
 *               full path to the library file.
 * @return string or false or nothing.
 */
function require_js($lib = '')
{
    global $CFG;
    static $loadlibs = array();
    if (!ajaxenabled()) {
        //return false;
    }
    if (!empty($lib)) {
        // Add the lib to the list of libs to be loaded, if it isn't already
        // in the list.
        if (is_array($lib)) {
            array_map('require_js', $lib);
        } else {
            $libpath = ajax_get_lib($lib);
            if (array_search($libpath, $loadlibs) === false) {
                $loadlibs[] = $libpath;
                // If this is called after header, then we print it right away
                // as otherwise nothing will ever happen!
                if (defined('HEADER_PRINTED')) {
                    $realloadlibs = $loadlibs;
                    $loadlibs = array($libpath);
                    print require_js();
                    $loadlibs = $realloadlibs;
                }
            }
        }
    } else {
        // Return the html needed to load the JavaScript files defined in
        // our list of libs to be loaded.
        $output = '';
        foreach ($loadlibs as $loadlib) {
            $output .= '<script type="text/javascript" ';
            $output .= " src=\"{$loadlib}\"></script>\n";
            if ($loadlib == $CFG->wwwroot . '/lib/yui/logger/logger-min.js') {
                // Special case, we need the CSS too.
                $output .= '<link type="text/css" rel="stylesheet" ';
                $output .= " href=\"{$CFG->wwwroot}/lib/yui/logger/assets/logger.css\" />\n";
            }
        }
        return $output;
    }
}
Example #4
0
/**
 * Used to include JavaScript libraries.
 *
 * When the $lib parameter is given, the function will ensure that the
 * 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 from print_header which are
 * internal to weblib and use the second $extracthtml parameter:
 * $extracthtml = 1: this is used before printing the header.
 *      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.
 *
 * @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)
{
    global $CFG;
    static $loadlibs = array();
    static $state = REQUIREJS_BEFOREHEADER;
    static $latecode = '';
    if (!empty($lib)) {
        // 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);
            }
        } else {
            $libpath = ajax_get_lib($lib);
            if (array_search($libpath, $loadlibs) === false) {
                $loadlibs[] = $libpath;
                // For state other than 0 we need to take action as well as just
                // adding it to loadlibs
                if ($state != REQUIREJS_BEFOREHEADER) {
                    // Get the script statement for this library
                    $scriptstatement = get_require_js_code(array($libpath));
                    if ($state == REQUIREJS_AFTERHEADER) {
                        // After the header, print it immediately
                        print $scriptstatement;
                    } else {
                        // Haven't finished the header yet. Add it after the
                        // header
                        $latecode .= $scriptstatement;
                    }
                }
            }
        }
    } else {
        if ($extracthtml == 1) {
            if ($state !== REQUIREJS_BEFOREHEADER) {
                debugging('Incorrect state in require_js (expected BEFOREHEADER): be careful not to call with empty $lib (except in print_header)');
            } else {
                $state = REQUIREJS_INHEADER;
            }
            return get_require_js_code($loadlibs);
        } else {
            if ($extracthtml == 2) {
                if ($state !== REQUIREJS_INHEADER) {
                    debugging('Incorrect state in require_js (expected INHEADER): be careful not to call with empty $lib (except in print_header)');
                    return '';
                } else {
                    $state = REQUIREJS_AFTERHEADER;
                    return $latecode;
                }
            } else {
                debugging('Unexpected value for $extracthtml');
            }
        }
    }
}