From 04c014081100dbd087cb26b70ab3d2c7cb510973 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Tue, 5 Jan 2010 23:22:16 +0000 Subject: [PATCH] MDL-21244 added new option for footer JS from themes as requested by Urs --- lib/outputlib.php | 30 ++++++++++++++++++++++-------- lib/outputrenderers.php | 6 ++++-- theme/base/config.php | 1 + theme/javascripts.php | 12 ++++++++++-- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/lib/outputlib.php b/lib/outputlib.php index b0001cfe42..ece920ab32 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -148,12 +148,20 @@ class theme_config { /** * The names of all the javascript files this theme that you would - * like included, in order. Give the names of the files without .js. + * like included from head, in order. Give the names of the files without .js. * * @var array */ public $javascripts = array(); + /** + * The names of all the javascript files this theme that you would + * like included from footer, in order. Give the names of the files without .js. + * + * @var array + */ + public $javascripts_footer = array(); + /** * The names of all the javascript files from parents that should be expcluded. * true value may be used to specify all parents or all themes from one parent. @@ -731,23 +739,29 @@ class theme_config { /** * Get the javascript URL of this theme - * @param bool $encoded false means use & and true use & in URLs + * @param bool $footer true measn footer url, false means head * @return moodle_url */ - public function javascript_url() { + public function javascript_url($footer=false) { global $CFG; $rev = theme_get_revision(); $params = array('theme'=>$this->name,'rev'=>$rev); + if ($footer) { + $params['type'] = 'footer'; + } return new moodle_url($CFG->httpswwwroot.'/theme/javascripts.php', $params); } /** * Returns the content of the one huge javascript file merged from all theme javascript files. + * @param bool $footer true measn footer url, false means head * @return string */ - public function javascript_content() { + public function javascript_content($footer=false) { + $type = $footer ? 'javascripts_footer' : 'javascripts'; + $js = array(); // find out wanted parent javascripts $excludes = null; @@ -767,13 +781,13 @@ class theme_config { if ($excludes !== true) { foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last $parent = $parent_config->name; - if (empty($parent_config->javascripts)) { + if (empty($parent_config->$type)) { continue; } if (!empty($excludes[$parent]) and $excludes[$parent] === true) { continue; } - foreach ($parent_config->javascripts as $javascript) { + foreach ($parent_config->$type as $javascript) { if (!empty($excludes[$parent]) and is_array($excludes[$parent]) and in_array($javascript, $excludes[$parent])) { continue; @@ -787,8 +801,8 @@ class theme_config { } // current theme javascripts - if (is_array($this->javascripts)) { - foreach ($this->javascripts as $javascript) { + if (is_array($this->$type)) { + foreach ($this->$type as $javascript) { $javascriptfile = "$this->dir/javascript/$javascript.js"; if (is_readable($javascriptfile)) { $js[] = "/*** This theme $this->name/javascript/$javascript.js ***/\n\n" . file_get_contents($javascriptfile); diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index b9ce2f6715..3529d923f3 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -394,10 +394,12 @@ class core_renderer extends renderer_base { $this->page->requires->css($url->out(), true); } - // Get the theme javascript + // Get the theme javascript head and footer $jsurl = $this->page->theme->javascript_url(); $this->page->requires->js($jsurl->out(), true)->in_head(); - + $jsurl = $this->page->theme->javascript_url(true); + $this->page->requires->js($jsurl->out(), true); + // Perform a browser environment check for the flash version. Should only run once per login session. if (isloggedin() && !empty($CFG->excludeoldflashclients) && empty($SESSION->flashversion)) { $this->page->requires->yui2_lib('event'); diff --git a/theme/base/config.php b/theme/base/config.php index 850ec80a4e..4b0ef7ca73 100644 --- a/theme/base/config.php +++ b/theme/base/config.php @@ -151,3 +151,4 @@ $THEME->layouts = array( /** List of javascript files that need to included on each page */ $THEME->javascripts = array('navigation'); +//$THEME->javascripts_footer = array(); \ No newline at end of file diff --git a/theme/javascripts.php b/theme/javascripts.php index 33b7ff1117..01f13aef23 100644 --- a/theme/javascripts.php +++ b/theme/javascripts.php @@ -30,6 +30,14 @@ require('../config.php'); // this stops immediately at the beginning of lib/setu $themename = min_optional_param('theme', 'standard', 'SAFEDIR'); $rev = min_optional_param('rev', 0, 'INT'); +$type = min_optional_param('type', 'header', 'RAW'); + +if ($type !== 'header' and $type !== 'footer') { + header('HTTP/1.0 404 not found'); + die('Theme was not found, sorry.'); +} + +$footer = ($type === 'footer'); if (file_exists("$CFG->dirroot/theme/$themename/config.php")) { // exists @@ -40,7 +48,7 @@ if (file_exists("$CFG->dirroot/theme/$themename/config.php")) { die('Theme was not found, sorry.'); } -$candidate = "$CFG->dataroot/cache/theme/$themename/javascript.js"; +$candidate = "$CFG->dataroot/cache/theme/$themename/javascript_$type.js"; if ($rev > -1 and file_exists($candidate)) { if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) { @@ -63,7 +71,7 @@ require("$CFG->dirroot/lib/setup.php"); $theme = theme_config::load($themename); -$js = $theme->javascript_content(); +$js = $theme->javascript_content($footer); if ($rev > -1) { check_dir_exists(dirname($candidate), true, true); $fp = fopen($candidate, 'w'); -- 2.39.5