Skip to content

Commit

Permalink
javascript MDL-22920 Added functionality to allow inclusion of Moodle…
Browse files Browse the repository at this point in the history
…-YUI JS modules easily
  • Loading branch information
Sam Hemelryk committed Jul 1, 2010
1 parent 627bb9e commit 2b722f8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 37 deletions.
4 changes: 2 additions & 2 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -7011,7 +7011,7 @@ function get_plugin_types($fullpaths=true) {
function get_plugin_list($plugintype) {
global $CFG;

$ignored = array('CVS', '_vti_cnf', 'simpletest', 'db');
$ignored = array('CVS', '_vti_cnf', 'simpletest', 'db', 'yui');
if ($plugintype == 'auth') {
// Historically we have had an auth plugin called 'db', so allow a special case.
$key = array_search('db', $ignored);
Expand Down Expand Up @@ -7107,7 +7107,7 @@ function get_list_of_plugins($directory='mod', $exclude='', $basedir='') {
$dirhandle = opendir($basedir);
while (false !== ($dir = readdir($dirhandle))) {
$firstchar = substr($dir, 0, 1);
if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf' or $dir == 'simpletest' or $dir == $exclude) {
if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf' or $dir == 'simpletest' or $dir == 'yui' or $dir == $exclude) {
continue;
}
if (filetype($basedir .'/'. $dir) != 'dir') {
Expand Down
90 changes: 61 additions & 29 deletions lib/outputrequirementslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,40 @@ public function __construct() {
$this->M_yui_loader->filter = ($this->yui3loader->filter == YUI_DEBUG) ? 'debug' : '';
$this->M_yui_loader->insertBefore = 'firstthemesheet';
$this->M_yui_loader->modules = array();
if (empty($CFG->useexternalyui) || true) {
$this->M_yui_loader->groups = array(
'local' => array(
'name' => 'gallery',
'base' => $CFG->wwwroot.'/lib/yui/gallery/',
'comboBase' => $this->yui3loader->comboBase,
'combine' => $this->yui3loader->combine,
'filter' => $this->M_yui_loader->filter,
'ext' => false,
'root' => 'gallery/',
'patterns' => array(
'gallery-' => array(
'group' => 'gallery',
'configFn' => '@GALLERYCONFIGFN@',
),
'root' => 'gallery'
$this->M_yui_loader->groups = array(
'moodle' => array(
'name' => 'moodle',
'base' => $CFG->httpswwwroot . '/theme/yui_combo.php?moodle/',
'comboBase' => $CFG->httpswwwroot . '/theme/yui_combo.php?',
'combine' => $this->yui3loader->combine,
'filter' => '',
'ext' => false,
'root' => 'moodle/',
'patterns' => array(
'moodle-' => array(
'group' => 'moodle',
'configFn' => '@MOODLECONFIGFN@'
),
'root' => 'moodle'
)
);
}
),
'local' => array(
'name' => 'gallery',
'base' => $CFG->wwwroot.'/lib/yui/gallery/',
'comboBase' => $CFG->httpswwwroot . '/theme/yui_combo.php?',
'combine' => $this->yui3loader->combine,
'filter' => $this->M_yui_loader->filter,
'ext' => false,
'root' => 'gallery/',
'patterns' => array(
'gallery-' => array(
'group' => 'gallery',
'configFn' => '@GALLERYCONFIGFN@',
),
'root' => 'gallery'
)
)
);
$this->add_yui2_modules(); // adds loading info for all YUI2 modules
$this->js_module($this->find_module('core_filepicker'));
$this->js_module($this->find_module('core_dock'));
Expand Down Expand Up @@ -591,13 +605,9 @@ public function js_function_call($function, array $arguments = null, $ondomready
}

/**
* Adds a call to make use of a YUI gallery module.
*
* This function adds code to the page footer that will tell a YUI instance to
* use the requested gallery module(s) and then call the desired function.
* Adds a call to make use of a YUI gallery module. DEPRECATED DO NOT USE!!!
*
* @todo Once YUI support loading skins from the gallery the if to use
* external yui libs should be fixed so that it calls;
* @deprecated DO NOT USE
*
* @param string|array $modules One or more gallery modules to require
* @param string $version
Expand All @@ -607,15 +617,35 @@ public function js_function_call($function, array $arguments = null, $ondomready
*/
public function js_gallery_module($modules, $version, $function, array $arguments = null, $ondomready = false) {
global $CFG;
debugging('This function will be removed before 2.0 is released please change it from js_gallery_module to yui_module', DEBUG_DEVELOPER);
$this->yui_module($modules, $function, $arguments, $version, $ondomready);
}

/**
* Creates a JavaScript function call that requires one or more modules to be loaded
*
* This function can be used to include all of the standard YUI module types within JavaScript:
* - YUI3 modules [node, event, io]
* - YUI2 modules [yui2-*]
* - Moodle modules [moodle-*]
* - Gallery modules [gallery-*]
*
* @param array|string $modules One or more modules
* @param string $function The function to call once modules have been loaded
* @param array $arguments An array of arguments to pass to the function
* @param string $galleryversion The gallery version to use
* @param bool $ondomready
*/
public function yui_module($modules, $function, array $arguments = null, $galleryversion = '2010.04.08-12-35', $ondomready = false) {
if (!is_array($modules)) {
$modules = array($modules);
}
if (empty($CFG->useexternalyui) || true) {
// We need to set the M.yui.galleryversion to the correct version
$jscode = 'M.yui.galleryversion='.json_encode($version).';';
$jscode = 'M.yui.galleryversion='.json_encode($galleryversion).';';
} else {
// Set Y's config.gallery to the version
$jscode = 'Y.config.gallery='.json_encode($version).';';
$jscode = 'Y.config.gallery='.json_encode($galleryversion).';';
}
$jscode .= 'Y.use('.join(',', array_map('json_encode', $modules)).',function() {'.js_writer::function_call($function, $arguments).'})';
if ($ondomready) {
Expand Down Expand Up @@ -933,10 +963,12 @@ public function get_head_code(moodle_page $page, core_renderer $renderer) {
// set up global YUI3 loader object - this should contain all code needed by plugins
// note: in JavaScript just use "YUI(M.yui.loader).use('overlay', function(Y) { .... });"
// this needs to be done before including any other script
$js = "var M = {}; M.yui = {}; ";
$js .= "var galleryConfigFn = function(me) {var p = me.path,v=M.yui.galleryversion,f;if(/-(skin|core)/.test(me.name)) {me.type = 'css';var p = p.replace(/-(skin|core)/, '').replace(/\.js/, '.css').split('/'), f = p.pop().replace(/(\-(min|debug))/, '');if (/-skin/.test(me.name)) {p.splice(p.length,0,v,'assets','skins','sam', f);} else {p.splice(p.length,0,v,'assets', f);};} else {var p = p.split('/'), f = p.pop();p.splice(p.length,0,v, f);};me.path = p.join('/');}; ";
$js .= str_replace('"@GALLERYCONFIGFN@"', 'galleryConfigFn', js_writer::set_variable('M.yui.loader', $this->M_yui_loader, false) . "\n");
$js = "var M = {}; M.yui = {}; var moodleConfigFn = function(me) {var p = me.path, b = me.name.replace(/^moodle-/,'').split('-', 3), n = b.pop();if (/(skin|core)/.test(n)) {n = b.pop();me.type = 'css';};me.path = b.join('-')+'/'+n+'/'+n+'.'+me.type;}; var galleryConfigFn = function(me) {var p = me.path,v=M.yui.galleryversion,f;if(/-(skin|core)/.test(me.name)) {me.type = 'css';p = p.replace(/-(skin|core)/, '').replace(/\.js/, '.css').split('/'), f = p.pop().replace(/(\-(min|debug))/, '');if (/-skin/.test(me.name)) {p.splice(p.length,0,v,'assets','skins','sam', f);} else {p.splice(p.length,0,v,'assets', f);};} else {p = p.split('/'), f = p.pop();p.splice(p.length,0,v, f);};me.path = p.join('/');};\n";
$js .= js_writer::set_variable('M.yui.loader', $this->M_yui_loader, false) . "\n";
$js .= js_writer::set_variable('M.cfg', $this->M_cfg, false);
$js = str_replace('"@GALLERYCONFIGFN@"', 'galleryConfigFn', $js);
$js = str_replace('"@MOODLECONFIGFN@"', 'moodleConfigFn', $js);

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

// link our main JS file, all core stuff should be there
Expand Down
19 changes: 13 additions & 6 deletions theme/yui_combo.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@
$content .= "\n// Wrong combo resource $part!\n";
continue;
}
$version = $bits[0];
if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
$content .= "\n// Wrong yui version $part!\n";
continue;
//debug($bits);
$version = array_shift($bits);
if ($version == 'moodle') {
$frankenstyle = array_shift($bits);
$dir = get_component_directory($frankenstyle);
$contentfile = $dir.'/yui/'.join('/', $bits);
} else {
if ($version != $CFG->yui3version and $version != $CFG->yui2version and $version != 'gallery') {
$content .= "\n// Wrong yui version $part!\n";
continue;
}
$contentfile = "$CFG->libdir/yui/$part";
}
$contentfile = "$CFG->libdir/yui/$part";
if (!file_exists($contentfile) or !is_file($contentfile)) {
$content .= "\n// Combo resource $part not found!\n";
continue;
Expand All @@ -72,7 +79,7 @@
if ($mimetype === 'text/css') {
if ($version == 'gallery') {
// search for all images in gallery module CSS and serve them through the yui_image.php script
$filecontent = preg_replace('/([a-z_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$bits[1].'/'.$bits[2].'/$1.$2', $filecontent);
$filecontent = preg_replace('/([a-z_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/'.$bits[0].'/'.$bits[1].'/$1.$2', $filecontent);
} else {
// search for all images in yui2 CSS and serve them through the yui_image.php script
$filecontent = preg_replace('/([a-z_-]+)\.(png|gif)/', 'yui_image.php?file='.$version.'/$1.$2', $filecontent);
Expand Down

0 comments on commit 2b722f8

Please sign in to comment.