From 7cfe3ebbcc5bbf8da64c873d356ebdf31892f059 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 3 Jul 2013 11:22:54 +0100 Subject: [PATCH] MDL-40477 JavaScript: Move YUI config functions out and deminify This should make future development and debugging of these functions much easier. It also allows us to write inline documentation for them. --- lib/outputrequirementslib.php | 34 ++++++++++++++++++++++++++++++-- lib/yui/config/moodle.js | 37 +++++++++++++++++++++++++++++++++++ lib/yui/config/yui2.js | 20 +++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 lib/yui/config/moodle.js create mode 100644 lib/yui/config/yui2.js diff --git a/lib/outputrequirementslib.php b/lib/outputrequirementslib.php index 76310d94a2f..a2c082ddc23 100644 --- a/lib/outputrequirementslib.php +++ b/lib/outputrequirementslib.php @@ -192,7 +192,7 @@ class page_requirements_manager { $this->YUI_config->comboBase = $this->yui3loader->comboBase; $this->YUI_config->combine = $this->yui3loader->combine; - $configname = $this->YUI_config->set_config_function("if(/-skin|reset|fonts|grids|base/.test(me.name)){me.type='css';me.path=me.path.replace(/\.js/,'.css');me.path=me.path.replace(/\/yui2-skin/,'/assets/skins/sam/yui2-skin');}"); + $configname = $this->YUI_config->set_config_source('lib/yui/config/yui2.js'); $this->YUI_config->add_group('yui2', array( // Loader configuration for our 2in3, for now ignores $CFG->useexternalyui. 'base' => $CFG->httpswwwroot . '/lib/yuilib/2in3/' . $CFG->yui2version . '/build/', @@ -207,7 +207,7 @@ class page_requirements_manager { ) ) )); - $configname = $this->YUI_config->set_config_function("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;if(me.type !== 'css'){me.path=me.path+'-min';};me.path=me.path+'.'+me.type;"); + $configname = $this->YUI_config->set_config_source('lib/yui/config/moodle.js'); $this->YUI_config->add_group('moodle', array( 'name' => 'moodle', 'base' => $CFG->httpswwwroot . '/theme/yui_combo.php'.$sep.'moodle/'.$jsrev.'/', @@ -1553,6 +1553,36 @@ class YUI_config { return '@' . $configname . '@'; } + /** + * Allow setting of the config function described in {@see set_config_function} from a file. + * The contents of this file are then passed to set_config_function. + * + * When jsrev is positive, the function is minified and stored in a MUC cache for subsequent uses. + * + * @param $file The path to the JavaScript function used for YUI configuration. + * @return String the name of the function to use in the group pattern configuration. + */ + public function set_config_source($file) { + global $CFG; + $cache = cache::make('core', 'yuimodules'); + + // Attempt to get the metadata from the cache. + $keyname = 'configfn_' . $file; + $fullpath = $CFG->dirroot . '/' . $file; + if (!isset($CFG->jsrev) || $CFG->jsrev == -1) { + $cache->delete($keyname); + $configfn = file_get_contents($fullpath); + } else { + $configfn = $cache->get($keyname); + if ($configfn === false) { + require_once($CFG->libdir . '/jslib.php'); + $configfn = js_minify($fullpath); + $cache->set($keyname, $configfn); + } + } + return $this->set_config_function($configfn); + } + /** * Retrieve the list of JavaScript functions for YUI_config groups. * diff --git a/lib/yui/config/moodle.js b/lib/yui/config/moodle.js new file mode 100644 index 00000000000..f1532cbb1a5 --- /dev/null +++ b/lib/yui/config/moodle.js @@ -0,0 +1,37 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle 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 3 of the License, or +// (at your option) any later version. +// +// Moodle 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. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +// A module name should be composed of: +// moodle--[-skin] +var path = me.path, + parts = me.name.replace(/^moodle-/,'').split('-', 3), + modulename = parts.pop(); + +if (/(skin|core)/.test(modulename)) { + // For these types, we need to remove the final word and set the type. + modulename = parts.pop(); + me.type = 'css'; +} + +// Build the first part of the filename. +me.path = parts.join('-') + '/' + modulename + '/' + modulename; + +// CSS is not minified, but all other types are. +if (me.type !== 'css') { + me.path = me.path + '-min'; +} + +// Add the file extension. +me.path = me.path + '.' + me.type; diff --git a/lib/yui/config/yui2.js b/lib/yui/config/yui2.js new file mode 100644 index 00000000000..ca31fd1e1c5 --- /dev/null +++ b/lib/yui/config/yui2.js @@ -0,0 +1,20 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle 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 3 of the License, or +// (at your option) any later version. +// +// Moodle 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. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +if (/-skin|reset|fonts|grids|base/.test(me.name)){ + me.type = 'css'; + me.path = me.path.replace(/\.js/, '.css'); + me.path = me.path.replace(/\/yui2-skin/, '/assets/skins/sam/yui2-skin'); +}