diff options
author | Bharat Mediratta <bharat@menalto.com> | 2011-01-10 22:20:12 -0800 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2011-01-10 22:20:12 -0800 |
commit | 5ac49d497f51a3828c1254b5024a4aa898f86530 (patch) | |
tree | 9463433526813e5fd234a351308b7ce1c15c9e70 | |
parent | 9dfb733ad7d5e7e5aa002833d52d1b8642609161 (diff) |
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.
-rw-r--r-- | modules/gallery/libraries/Admin_View.php | 5 | ||||
-rw-r--r-- | modules/gallery/libraries/Gallery_View.php | 61 | ||||
-rw-r--r-- | modules/gallery/libraries/Theme_View.php | 14 | ||||
-rw-r--r-- | themes/admin_wind/views/admin.html.php | 38 | ||||
-rw-r--r-- | themes/wind/views/page.html.php | 49 |
5 files changed, 99 insertions, 68 deletions
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 "<!-- LOOKING FOR YOUR CSS? It's all been combined into the link below -->\n" . - html::stylesheet("combined/css/$key", "screen,print,projection", true); + return html::stylesheet("combined/css/$key", "screen,print,projection", true); } else { - return "<!-- LOOKING FOR YOUR JAVASCRIPT? It's all been combined into the link below -->\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( diff --git a/themes/admin_wind/views/admin.html.php b/themes/admin_wind/views/admin.html.php index c8041069..54b30c6f 100644 --- a/themes/admin_wind/views/admin.html.php +++ b/themes/admin_wind/views/admin.html.php @@ -4,6 +4,7 @@ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> + <? $theme->start_combining("script,css") ?> <title> <? if ($page_title): ?> <?= t("Gallery Admin: %page_title", array("page_title" => $page_title)) ?> @@ -11,8 +12,26 @@ <?= t("Admin dashboard") ?> <? endif ?> </title> - <link rel="shortcut icon" href="<?= url::file(module::get_var("gallery", "favicon_url")) ?>" type="image/x-icon" /> + <link rel="shortcut icon" + href="<?= url::file(module::get_var("gallery", "favicon_url")) ?>" + type="image/x-icon" /> + <?= $theme->script("jquery.js") ?> + <?= $theme->script("jquery.form.js") ?> + <?= $theme->script("jquery-ui.js") ?> + <?= $theme->script("gallery.common.js") ?> + <? /* MSG_CANCEL is required by gallery.dialog.js */ ?> + <script type="text/javascript"> + var MSG_CANCEL = <?= t("Cancel")->for_js() ?>; + </script> + <?= $theme->script("gallery.ajax.js") ?> + <?= $theme->script("gallery.dialog.js") ?> + <?= $theme->script("superfish/js/superfish.js") ?> + + <?= $theme->admin_head() ?> + + <? /* Theme specific CSS/JS goes last so that it can override module CSS/JS */ ?> + <?= $theme->script("ui.init.js") ?> <?= $theme->css("yui/reset-fonts-grids.css") ?> <?= $theme->css("themeroller/ui.base.css") ?> <?= $theme->css("superfish/css/superfish.css") ?> @@ -22,20 +41,11 @@ media="screen,print,projection" /> <![endif]--> - <?= $theme->script("jquery.js") ?> - <?= $theme->script("jquery.form.js") ?> - <?= $theme->script("jquery-ui.js") ?> - <?= $theme->script("gallery.common.js") ?> - <? /* MSG_CANCEL is required by gallery.dialog.js */ ?> - <script type="text/javascript"> - var MSG_CANCEL = <?= t("Cancel")->for_js() ?>; - </script> - <?= $theme->script("gallery.ajax.js") ?> - <?= $theme->script("gallery.dialog.js") ?> - <?= $theme->script("superfish/js/superfish.js") ?> - <?= $theme->script("ui.init.js") ?> + <!-- LOOKING FOR YOUR JAVASCRIPT? It's all been combined into the link below --> + <?= $theme->get_combined("script") ?> - <?= $theme->admin_head() ?> + <!-- LOOKING FOR YOUR CSS? It's all been combined into the link below --> + <?= $theme->get_combined("css") ?> </head> <body <?= $theme->body_attributes() ?>> diff --git a/themes/wind/views/page.html.php b/themes/wind/views/page.html.php index 90f76bb5..441866d5 100644 --- a/themes/wind/views/page.html.php +++ b/themes/wind/views/page.html.php @@ -4,6 +4,7 @@ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> + <? $theme->start_combining("script,css") ?> <title> <? if ($page_title): ?> <?= $page_title ?> @@ -17,28 +18,24 @@ <? endif ?> <? endif ?> </title> - <link rel="shortcut icon" href="<?= url::file(module::get_var("gallery", "favicon_url")) ?>" type="image/x-icon" /> - <?= $theme->css("yui/reset-fonts-grids.css") ?> - <?= $theme->css("superfish/css/superfish.css") ?> - <?= $theme->css("themeroller/ui.base.css") ?> - <?= $theme->css("screen.css") ?> - <!--[if lte IE 8]> - <link rel="stylesheet" type="text/css" href="<?= $theme->url("css/fix-ie.css") ?>" - media="screen,print,projection" /> - <![endif]--> + <link rel="shortcut icon" + href="<?= url::file(module::get_var("gallery", "favicon_url")) ?>" + type="image/x-icon" /> + <? if ($theme->page_type == "collection"): ?> <? if ($thumb_proportion != 1): ?> <? $new_width = round($thumb_proportion * 213) ?> <? $new_height = round($thumb_proportion * 240) ?> - <style type="text/css"> - .g-view #g-content #g-album-grid .g-item { - width: <?= $new_width ?>px; - height: <?= $new_height ?>px; - /* <?= $thumb_proportion ?> */ - } - </style> + <style type="text/css"> + .g-view #g-content #g-album-grid .g-item { + width: <?= $new_width ?>px; + height: <?= $new_height ?>px; + /* <?= $thumb_proportion ?> */ + } + </style> <? endif ?> <? endif ?> + <?= $theme->script("json2-min.js") ?> <?= $theme->script("jquery.js") ?> <?= $theme->script("jquery.form.js") ?> @@ -52,9 +49,8 @@ <?= $theme->script("gallery.dialog.js") ?> <?= $theme->script("superfish/js/superfish.js") ?> <?= $theme->script("jquery.localscroll.js") ?> - <?= $theme->script("ui.init.js") ?> - <? /* These are page specific, but if we put them before $theme->head() they get combined */ ?> + <? /* These are page specific but they get combined */ ?> <? if ($theme->page_subtype == "photo"): ?> <?= $theme->script("jquery.scrollTo.js") ?> <?= $theme->script("gallery.show_full_size.js") ?> @@ -63,6 +59,23 @@ <? endif ?> <?= $theme->head() ?> + + <? /* Theme specific CSS/JS goes last so that it can override module CSS/JS */ ?> + <?= $theme->script("ui.init.js") ?> + <?= $theme->css("yui/reset-fonts-grids.css") ?> + <?= $theme->css("superfish/css/superfish.css") ?> + <?= $theme->css("themeroller/ui.base.css") ?> + <?= $theme->css("screen.css") ?> + <!--[if lte IE 8]> + <link rel="stylesheet" type="text/css" href="<?= $theme->url("css/fix-ie.css") ?>" + media="screen,print,projection" /> + <![endif]--> + + <!-- LOOKING FOR YOUR JAVASCRIPT? It's all been combined into the link below --> + <?= $theme->get_combined("script") ?> + + <!-- LOOKING FOR YOUR CSS? It's all been combined into the link below --> + <?= $theme->get_combined("css") ?> </head> <body <?= $theme->body_attributes() ?>> |