From 31ba5e29bdbaf03b87334468c21f6db6b59239d4 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Mon, 20 Mar 2017 17:31:35 +1100 Subject: [PATCH] Introduce Resources component for testing This should be considered experimental at this stage, however this component is used in every October website we've built thus far, it makes sense for it to be included in the core. Once tested we will look at documenting it with an accompanying screencast video. --- modules/cms/ServiceProvider.php | 1 + modules/cms/components/Resources.php | 163 ++++++++++++++++++++ modules/cms/components/UnknownComponent.php | 2 +- 3 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 modules/cms/components/Resources.php diff --git a/modules/cms/ServiceProvider.php b/modules/cms/ServiceProvider.php index 693b94255..646e5b3f7 100644 --- a/modules/cms/ServiceProvider.php +++ b/modules/cms/ServiceProvider.php @@ -64,6 +64,7 @@ class ServiceProvider extends ModuleServiceProvider { ComponentManager::instance()->registerComponents(function ($manager) { $manager->registerComponent('Cms\Components\ViewBag', 'viewBag'); + $manager->registerComponent('Cms\Components\Resources', 'resources'); }); } diff --git a/modules/cms/components/Resources.php b/modules/cms/components/Resources.php new file mode 100644 index 000000000..18bfb0822 --- /dev/null +++ b/modules/cms/components/Resources.php @@ -0,0 +1,163 @@ + 'Resources', + 'description' => 'Easily reference theme assets for inclusion on a page.', + ]; + } + + /** + * @return array + */ + public function defineProperties() + { + return [ + 'js' => [ + 'title' => 'JavaScript', + 'description' => 'JavaScript file(s) in the assets/js folder', + 'type' => 'stringList', + 'showExternalParam' => false + ], + 'less' => [ + 'title' => 'LESS', + 'description' => 'LESS file(s) in the assets/less folder', + 'type' => 'stringList', + 'showExternalParam' => false + ], + 'css' => [ + 'title' => 'CSS', + 'description' => 'Stylesheet file(s) in the assets/css folder', + 'type' => 'stringList', + 'showExternalParam' => false + ], + 'vars' => [ + 'title' => 'Variables', + 'description' => 'Page variables name(s) and value(s)', + 'type' => 'dictionary', + 'showExternalParam' => false + ] + ]; + } + + public function init() + { + $this->assetPath = $this->guessAssetPath(); + $this->jsDir = $this->guessAssetDirectory(['js', 'javascript'], $this->jsDir); + } + + public function onRun() + { + /* + * JavaScript + */ + $js = []; + if ($assets = $this->property('js')) { + $js += array_map([$this, 'prefixJs'], (array) $assets); + } + + /* + * LESS + */ + $less = []; + if ($assets = $this->property('less')) { + $less += array_map([$this, 'prefixLess'], (array) $assets); + } + + /* + * CSS + */ + $css = []; + if ($assets = $this->property('css')) { + $css += array_map([$this, 'prefixCss'], (array) $assets); + } + + if (count($js)) { + $this->addJs(CombineAssets::combine($js, $this->assetPath)); + } + + if (count($less)) { + $this->addCss(CombineAssets::combine($less, $this->assetPath)); + } + + if (count($css)) { + $this->addCss(CombineAssets::combine($css, $this->assetPath)); + } + + /* + * Variables + */ + if ($vars = $this->property('vars')) { + foreach ((array) $vars as $key => $value) { + $this->page[$key] = $value; + } + } + } + + protected function prefixJs($value) + { + return $this->jsDir.'/'.trim($value); + } + + protected function prefixCss($value) + { + return $this->cssDir.'/'.trim($value); + } + + protected function prefixLess($value) + { + return $this->lessDir.'/'.trim($value); + } + + protected function guessAssetDirectory(array $possible, $default = null) + { + foreach ($possible as $option) { + if (File::isDirectory($this->assetPath.'/'.$option)) { + return $option; + } + } + + return $default; + } + + protected function guessAssetPath() + { + $baseTheme = themes_path().'/'.$this->getTheme()->getDirName(); + + if (File::isDirectory($baseTheme.'/assets')) { + return $baseTheme.'/assets'; + } + else { + return $baseTheme.'/resources'; + } + } +} diff --git a/modules/cms/components/UnknownComponent.php b/modules/cms/components/UnknownComponent.php index ebbb6a623..aa2b6bcbb 100644 --- a/modules/cms/components/UnknownComponent.php +++ b/modules/cms/components/UnknownComponent.php @@ -1,7 +1,7 @@