From 53a7fb2fc98d343929566db11906305ce9e7811b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3na=20Lore?= Date: Thu, 2 Mar 2017 20:55:31 +0100 Subject: [PATCH] Ability to use theme.xml to define library dependencies. --- e107_admin/header.php | 4 + e107_core/templates/header_default.php | 3 +- e107_handlers/e107_class.php | 14 +++- e107_handlers/library_manager.php | 58 +++++++++++++- e107_handlers/theme_handler.php | 107 +++++++++++++++++++++---- e107_themes/bootstrap3/admin_theme.php | 4 - e107_themes/bootstrap3/theme.php | 3 - e107_themes/bootstrap3/theme.xml | 17 ++-- 8 files changed, 175 insertions(+), 35 deletions(-) diff --git a/e107_admin/header.php b/e107_admin/header.php index 8eb8535fd..f36ec51a3 100644 --- a/e107_admin/header.php +++ b/e107_admin/header.php @@ -111,6 +111,10 @@ function loadJSAddons() } +// Load library dependencies. +e107::getTheme('current', true)->loadLibrary(); + +// Load other JS files. loadJSAddons(); diff --git a/e107_core/templates/header_default.php b/e107_core/templates/header_default.php index d6c1b26ce..264a849b3 100644 --- a/e107_core/templates/header_default.php +++ b/e107_core/templates/header_default.php @@ -23,7 +23,8 @@ $sql = e107::getDb(); $sql->db_Mark_Time('(Header Top)'); - +// Load library dependencies. +e107::getTheme('current', true)->loadLibrary(); //e107::js('core', 'bootstrap/js/bootstrap-tooltip.js','jquery'); // e107::css('core', 'bootstrap/css/tooltip.css','jquery'); diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php index 2059f3c48..30a85f4dd 100644 --- a/e107_handlers/e107_class.php +++ b/e107_handlers/e107_class.php @@ -1404,7 +1404,6 @@ class e107 if(!defined('E107_INSTALL')) { - if($themedir === 'front') { $themedir= self::getPref('sitetheme'); @@ -1416,6 +1415,19 @@ class e107 } } + // Get the currently used theme. + if ($themedir == 'current') + { + // If we are in the admin area. + if (deftrue('e_ADMIN_AREA', false)) + { + $themedir = self::getPref('admintheme'); + } + else + { + $themedir= self::getPref('sitetheme'); + } + } return self::getSingleton('e_theme', true, null, array('themedir'=> $themedir, 'force'=> $clearCache)); } diff --git a/e107_handlers/library_manager.php b/e107_handlers/library_manager.php index 7709f9a42..6a19efced 100644 --- a/e107_handlers/library_manager.php +++ b/e107_handlers/library_manager.php @@ -1877,13 +1877,63 @@ class e_library_manager $coreLib = $coreLibs[$library['machine_name']]; $library['files'] = $coreLib['files']; $library['variants'] = $coreLib['variants']; - - // Admin UI uses its own CSS. - unset($library['files']['css']); - unset($library['variants']['dev']['files']['css']); break; } } + + $excluded = $this->getExcludedLibraries(); + + if(empty($excluded)) + { + return; + } + + // Make sure we have the name without cdn prefix. + $basename = str_replace('cdn.', '', $library['machine_name']); + + // If this library (or the CDN version of this library) is excluded + // by the theme is currently used. + if (in_array($basename, $excluded) || in_array('cdn.' . $basename, $excluded)) + { + unset($library['files']['css']); + + if (!empty($library['variants'])) + { + foreach($library['variants'] as &$variant) + { + if(!empty($variant['files']['css'])) + { + unset($variant['files']['css']); + } + } + } + } + } + + /** + * Get excluded libraries. + * + * @return array + */ + public function getExcludedLibraries() + { + // This static cache is re-used by preLoad() to save memory. + static $excludedLibraries; + + if(!isset($excludedLibraries)) + { + $excludedLibraries = array(); + + $exclude = e107::getTheme('current', true)->cssAttribute('auto', 'exclude'); + + if($exclude) + { + // Split string into array and remove whitespaces. + $excludedLibraries = array_map('trim', explode(',', $exclude)); + } + } + + return $excludedLibraries; } } diff --git a/e107_handlers/theme_handler.php b/e107_handlers/theme_handler.php index 5e7407d28..1ef35b121 100644 --- a/e107_handlers/theme_handler.php +++ b/e107_handlers/theme_handler.php @@ -80,12 +80,64 @@ class e_theme } /** - * Get info on the current front or admin theme and selected style. (ie. as found in theme.xml ) - * @param string $mode - * @param null $var file | name | scope | library - * @return bool + * Load library dependencies. + * + * @param string $scope + * front | admin | all | auto */ - public function cssAttribute($mode='front', $var=null) + public function loadLibrary($scope = 'auto') + { + if($scope === 'auto') + { + $scope = 'front'; + + if(deftrue('e_ADMIN_AREA', false)) + { + $scope = 'admin'; + } + } + + $libraries = $this->get('library'); + + if(empty($libraries)) + { + return; + } + + foreach($libraries as $library) + { + if(empty($library['name'])) + { + continue; + } + + // If no scope set, we load library on both areas. + if(empty($library['scope']) || $library['scope'] === 'all') + { + e107::library('load', $library['name']); + continue; + } + + if($library['scope'] === $scope) + { + e107::library('load', $library['name']); + continue; + } + } + } + + /** + * Get info on the current front or admin theme and selected style. + * (ie. as found in theme.xml ) + * + * @param string $mode + * front | admin | auto + * @param string $var + * file | name | scope | exclude + * + * @return mixed + */ + public function cssAttribute($mode = 'front', $var = null) { $css = $this->get('css'); @@ -94,23 +146,30 @@ class e_theme return false; } - foreach($css as $k=>$v) + if($mode === 'auto') + { + $mode = 'front'; + + if(deftrue('e_ADMIN_AREA', false)) + { + $mode = 'admin'; + } + } + + foreach($css as $k => $v) { if($mode === 'front' && $v['name'] === $this->_frontcss) { - return !empty($var) ? varset($v[$var],null) : $v; + return !empty($var) ? varset($v[$var], null) : $v; } if($mode === 'admin' && $v['name'] === $this->_admincss) { - return !empty($var) ? varset($v[$var],null) : $v; + return !empty($var) ? varset($v[$var], null) : $v; } - - } return false; - } @@ -622,10 +681,26 @@ class e_theme } - $vars['layouts'] = $lays; - $vars['path'] = $path; - $vars['custompages'] = $custom; - $vars['legacy'] = false; + $vars['layouts'] = $lays; + $vars['path'] = $path; + $vars['custompages'] = $custom; + $vars['legacy'] = false; + $vars['library'] = array(); + + if(!empty($vars['libraries']['library'])) + { + $vars['css'] = array(); + + foreach($vars['libraries']['library'] as $val) + { + $vars['library'][] = array( + 'name' => $val['@attributes']['name'], + 'scope' => varset($val['@attributes']['scope']), + ); + } + + unset($vars['libraries']); + } if(!empty($vars['stylesheets']['css'])) { @@ -641,7 +716,7 @@ class e_theme "info" => $val['@attributes']['name'], "nonadmin" => $notadmin, 'scope' => vartrue($val['@attributes']['scope']), - 'library' => vartrue($val['@attributes']['library']) + 'exclude' => vartrue($val['@attributes']['exclude']) ); } diff --git a/e107_themes/bootstrap3/admin_theme.php b/e107_themes/bootstrap3/admin_theme.php index 63ef4c768..4b8e9aae5 100644 --- a/e107_themes/bootstrap3/admin_theme.php +++ b/e107_themes/bootstrap3/admin_theme.php @@ -20,10 +20,6 @@ define("SEP", " "); define("BOOTSTRAP", 3); define('FONTAWESOME', 4); -e107::library('load', 'bootstrap'); -e107::library('load', 'fontawesome'); -e107::library('load', 'bootstrap.editable'); - $adminStyle = e107::pref('core', 'admincss', 'css/bootstrap-dark.min.css'); e107::css('theme', $adminStyle); e107::css('theme', 'admin_style.css'); diff --git a/e107_themes/bootstrap3/theme.php b/e107_themes/bootstrap3/theme.php index 583af387c..9abf6ea1a 100644 --- a/e107_themes/bootstrap3/theme.php +++ b/e107_themes/bootstrap3/theme.php @@ -20,9 +20,6 @@ define("BOOTSTRAP", 3); define("FONTAWESOME", 4); define('VIEWPORT', "width=device-width, initial-scale=1.0"); -e107::library('load', 'bootstrap'); -e107::library('load', 'fontawesome'); - // CDN provider for Bootswatch. $cndPref = e107::pref('theme', 'cdn', 'cdnjs'); $bootswatch = e107::pref('theme', 'bootswatch', false); diff --git a/e107_themes/bootstrap3/theme.xml b/e107_themes/bootstrap3/theme.xml index 657d78415..70dcc6e4a 100644 --- a/e107_themes/bootstrap3/theme.xml +++ b/e107_themes/bootstrap3/theme.xml @@ -17,13 +17,18 @@ preview_frontend.png + + + + + - - - - - - + + + + + +