From 5ac49d497f51a3828c1254b5024a4aa898f86530 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 10 Jan 2011 22:20:12 -0800 Subject: Create new APIs for allowing themers to control what CSS/JS get combined and when. Backwards compatible in that old themes will work, but their CSS/JS will no longer be combined unless they make some changes. Fixes #1600. --- modules/gallery/libraries/Admin_View.php | 5 --- modules/gallery/libraries/Gallery_View.php | 61 +++++++++++++++++++++--------- modules/gallery/libraries/Theme_View.php | 14 ------- 3 files changed, 44 insertions(+), 36 deletions(-) (limited to 'modules') diff --git a/modules/gallery/libraries/Admin_View.php b/modules/gallery/libraries/Admin_View.php index 28a003cc..1a633a34 100644 --- a/modules/gallery/libraries/Admin_View.php +++ b/modules/gallery/libraries/Admin_View.php @@ -96,11 +96,6 @@ class Admin_View_Core extends Gallery_View { } } - if ($function == "admin_head") { - array_unshift($blocks, $this->combine_files($this->scripts, "javascript")); - array_unshift($blocks, $this->combine_files($this->css, "css")); - } - if (Session::instance()->get("debug")) { if ($function != "admin_head") { array_unshift( diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php index b45bb94a..8befda95 100644 --- a/modules/gallery/libraries/Gallery_View.php +++ b/modules/gallery/libraries/Gallery_View.php @@ -19,18 +19,35 @@ */ class Gallery_View_Core extends View { protected $theme_name = null; - protected $scripts = array(); - protected $css = array(); + protected $combine_queue = array(); /** - * Add a script to the combined scripts list. + * Begin gather up scripts or css files so that they can be combined into a single request. + * + * @param $types a comma separated list of types to combine, eg "script,css" + */ + public function start_combining($types) { + foreach (explode(",", $types) as $type) { + $this->combine_queue[$type] = array(); + } + } + + /** + * If script combining is enabled, add this script to the list of scripts that will be + * combined into a single script element. When combined, the order of scripts is preserved. + * * @param $file the file name or path of the script to include. If a path is specified then * it needs to be relative to DOCROOT. Just specifying a file name will result * in searching Kohana's cascading file system. + * @param $group the group of scripts to combine this with. defaults to "core" */ - public function script($file) { + public function script($file, $group="core") { if (($path = gallery::find_file("js", $file, false))) { - $this->scripts[$path] = 1; + if (isset($this->combine_queue["script"])) { + $this->combine_queue["script"][$group][$path] = 1; + } else { + return html::script($path); + } } else { Kohana_Log::add("error", "Can't find script file: $file"); } @@ -46,14 +63,22 @@ class Gallery_View_Core extends View { } /** - * Add a css file to the combined css list. - * @param $file the file name or path of the script to include. If a path is specified then + * If css combining is enabled, add this css to the list of css that will be + * combined into a single style element. When combined, the order of style elements + * is preserved. + * + * @param $file the file name or path of the css to include. If a path is specified then * it needs to be relative to DOCROOT. Just specifying a file name will result * in searching Kohana's cascading file system. + * @param $group the group of css to combine this with. defaults to "core" */ - public function css($file) { + public function css($file, $group="core") { if (($path = gallery::find_file("css", $file, false))) { - $this->css[$path] = 1; + if (isset($this->combine_queue["css"])) { + $this->combine_queue["css"][$group][$path] = 1; + } else { + return html::stylesheet($path); + } } else { Kohana_Log::add("error", "Can't find css file: $file"); } @@ -61,11 +86,13 @@ class Gallery_View_Core extends View { /** * Combine a series of files into a single one and cache it in the database. + * @param $type the data type (script or css) + * @param $group the group of scripts or css we want */ - protected function combine_files($paths, $type) { + public function get_combined($type, $group="core") { $links = array(); - if (empty($paths)) { + if (empty($this->combine_queue[$type][$group])) { return; } @@ -73,7 +100,7 @@ class Gallery_View_Core extends View { // entries. $key = array(url::abs_file("")); - foreach (array_keys($paths) as $path) { + foreach (array_keys($this->combine_queue[$type][$group]) as $path) { $stats = stat($path); // 7 == size, 9 == mtime, see http://php.net/stat $key[] = "$path $stats[7] $stats[9]"; @@ -85,7 +112,7 @@ class Gallery_View_Core extends View { if (empty($contents)) { $contents = ""; - foreach (array_keys($paths) as $path) { + foreach (array_keys($this->combine_queue[$type][$group]) as $path) { if ($type == "css") { $contents .= "/* $path */\n" . $this->process_css($path) . "\n"; } else { @@ -103,12 +130,12 @@ class Gallery_View_Core extends View { } } + unset($this->combine_queue[$type][$group]); + if ($type == "css") { - return "\n" . - html::stylesheet("combined/css/$key", "screen,print,projection", true); + return html::stylesheet("combined/css/$key", "screen,print,projection", true); } else { - return "\n" . - html::script("combined/javascript/$key", true); + return html::script("combined/javascript/$key", true); } } diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index ba1862c0..04784ca1 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -236,13 +236,6 @@ class Theme_View_Core extends Gallery_View { case "thumb_bottom": case "thumb_info": case "thumb_top": - if ($function == "head") { - // Stash any CSS we have already; that came from the theme and we want theme CSS to - // override module CSs - $save_css = $this->css; - $this->css = array(); - } - $blocks = array(); if (method_exists("gallery_theme", $function)) { switch (count($args)) { @@ -281,13 +274,6 @@ class Theme_View_Core extends Gallery_View { array_merge(array($this), $args)); } - if ($function == "head") { - // Merge the theme CSS/JS at the end - $this->css = array_merge($this->css, $save_css); - array_unshift($blocks, $this->combine_files($this->scripts, "javascript")); - array_unshift($blocks, $this->combine_files($this->css, "css")); - } - if (Session::instance()->get("debug")) { if ($function != "head" && $function != "body_attributes") { array_unshift( -- cgit v1.2.3