From 47657731433248410e681640b2a53c0783651134 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 10 May 2017 17:36:05 +0200 Subject: Add support to declare multiple css/js per module with loading condition: A css/js can be declared to only load if the module in question is the page module being rendered, or to always be loaded even if the module is just a dependency. The second case is useful for plugin type modules. See one of the config.json files updated in this commit on how to specify css/js to load. true = always load, false = only if page module. --- inc/module.inc.php | 39 +++++++++++++++++++++++++++++++++++++-- inc/render.inc.php | 14 ++++++++------ 2 files changed, 45 insertions(+), 8 deletions(-) (limited to 'inc') diff --git a/inc/module.inc.php b/inc/module.inc.php index 597cfb57..52646983 100644 --- a/inc/module.inc.php +++ b/inc/module.inc.php @@ -142,13 +142,23 @@ class Module private $activated = false; private $dependencies = array(); private $name; + /** + * @var array assoc list of 'filename.css' => true|false (true = always load, false = only if module is main module) + */ + private $css = array(); + /** + * @var array assoc list of 'filename.js' => true|false (true = always load, false = only if module is main module) + */ + private $scripts = array(); private function __construct($name) { $file = 'modules/' . $name . '/config.json'; $json = @json_decode(@file_get_contents($file), true); - if (isset($json['dependencies']) && is_array($json['dependencies'])) { - $this->dependencies = $json['dependencies']; + foreach (['dependencies', 'css', 'scripts'] as $key) { + if (isset($json[$key]) && is_array($json[$key])) { + $this->$key = $json[$key]; + } } if (isset($json['category']) && is_string($json['category'])) { $this->category = $json['category']; @@ -251,4 +261,29 @@ class Module return 'modules/' . $this->name; } + public function getScripts($externalOnly) + { + error_log($this->getIdentifier() . ' = ' . ($externalOnly ? 'true' : 'false')); + if (!$externalOnly) { + if (!isset($this->scripts['clientscript.js']) && file_exists($this->getDir() . '/clientscript.js')) { + $this->scripts['clientscript.js'] = false; + } + return array_keys($this->scripts); + } + error_log('Pre: ' . implode(', ', array_keys($this->scripts))); + error_log('Post: ' . implode(', ', array_keys(array_filter($this->scripts)))); + return array_keys(array_filter($this->scripts)); + } + + public function getCss($externalOnly) + { + if (!$externalOnly) { + if (!isset($this->css['style.css']) && file_exists($this->getDir() . '/style.css')) { + $this->css['style.css'] = false; + } + return array_keys($this->css); + } + return array_keys(array_filter($this->css)); + } + } diff --git a/inc/render.inc.php b/inc/render.inc.php index 72993ab5..53e2f314 100644 --- a/inc/render.inc.php +++ b/inc/render.inc.php @@ -51,7 +51,9 @@ class Render public static function output() { Header('Content-Type: text/html; charset=utf-8'); + /* @var $modules Module[] */ $modules = array_reverse(Module::getActivated()); + $pageModule = Page::getModule(); $title = Property::get('page-title-prefix', ''); $bgcolor = Property::get('logo-background', ''); if (!empty($bgcolor) || !empty($title)) { @@ -79,9 +81,9 @@ class Render '; // Include any module specific styles foreach ($modules as $module) { - $file = $module->getDir() . '/style.css'; - if (file_exists($file)) { - echo ''; + $files = $module->getCss($module != $pageModule); + foreach ($files as $file) { + echo ''; } } echo ' @@ -109,9 +111,9 @@ class Render '; foreach ($modules as $module) { - $file = $module->getDir() . '/clientscript.js'; - if (file_exists($file)) { - echo ''; + $files = $module->getScripts($module != $pageModule); + foreach ($files as $file) { + echo ''; } } echo -- cgit v1.2.3-55-g7522