From 2f6c50d55197aeb9d672461e41e6d9a438d84f83 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 15 Jan 2015 18:33:51 -0800 Subject: [PATCH] CRM-15832 - getAngularModules - Declare partials, css, and js using globs --- CRM/Case/Info.php | 3 + CRM/Core/Page/Angular.php | 63 +++++++++++++++++---- CRM/Mailing/Info.php | 9 +++ tests/phpunit/CRM/Core/Page/AngularTest.php | 54 ++++++++++++++++++ 4 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 tests/phpunit/CRM/Core/Page/AngularTest.php diff --git a/CRM/Case/Info.php b/CRM/Case/Info.php index 7ee506fe4a..324c7a7596 100644 --- a/CRM/Case/Info.php +++ b/CRM/Case/Info.php @@ -66,6 +66,9 @@ class CRM_Case_Info extends CRM_Core_Component_Info { 'ext' => 'civicrm', 'js' => array('js/angular-crmCaseType.js'), 'css' => array('css/angular-crmCaseType.css'), + 'partials' => array( + 'partials/crmCaseType/*.html', + ), ); CRM_Core_Resources::singleton()->addSetting(array( diff --git a/CRM/Core/Page/Angular.php b/CRM/Core/Page/Angular.php index 7d3bccf0e5..f2d8e3839b 100644 --- a/CRM/Core/Page/Angular.php +++ b/CRM/Core/Page/Angular.php @@ -12,6 +12,24 @@ class CRM_Core_Page_Angular extends CRM_Core_Page { */ const DEFAULT_MODULE_WEIGHT = 200; + /** + * @var CRM_Core_Resources + */ + protected $res; + + /** + * @param string $title + * Title of the page. + * @param int $mode + * Mode of the page. + * @param CRM_Core_Resources|null $res + * Resource manager. + */ + public function __construct($title = NULL, $mode = NULL, $res = NULL) { + parent::__construct($title, $mode); + $this->res = CRM_Core_Resources::singleton(); + } + /** * This function takes care of all the things common to all * pages. This typically involves assigning the appropriate @@ -21,17 +39,17 @@ class CRM_Core_Page_Angular extends CRM_Core_Page { * The content generated by running this page */ public function run() { - $this->registerResources(CRM_Core_Resources::singleton()); + $this->registerResources(); return parent::run(); } /** - * @param CRM_Core_Resources $res + * Register resources required by Angular. */ - public function registerResources(CRM_Core_Resources $res) { - $modules = self::getAngularModules(); + public function registerResources() { + $modules = $this->getAngularModules(); - $res->addSettingsFactory(function () use (&$modules) { + $this->res->addSettingsFactory(function () use (&$modules) { // TODO optimization; client-side caching return array( 'resourceUrls' => CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(), @@ -44,18 +62,18 @@ class CRM_Core_Page_Angular extends CRM_Core_Page { ); }); - $res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE); - $res->addScriptFile('civicrm', 'bower_components/angular-route/angular-route.min.js', 110, 'html-header', FALSE); + $this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE); + $this->res->addScriptFile('civicrm', 'bower_components/angular-route/angular-route.min.js', 110, 'html-header', FALSE); $headOffset = 0; foreach ($modules as $module) { if (!empty($module['css'])) { foreach ($module['css'] as $file) { - $res->addStyleFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE); + $this->res->addStyleFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE); } } if (!empty($module['js'])) { foreach ($module['js'] as $file) { - $res->addScriptFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE); + $this->res->addScriptFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header', TRUE); } } } @@ -67,7 +85,7 @@ class CRM_Core_Page_Angular extends CRM_Core_Page { * @return array * (string $name => array('ext' => string $key, 'js' => array $paths, 'css' => array $paths)) */ - public static function getAngularModules() { + public function getAngularModules() { $angularModules = array(); $angularModules['angularFileUpload'] = array( 'ext' => 'civicrm', @@ -78,10 +96,12 @@ class CRM_Core_Page_Angular extends CRM_Core_Page { 'ext' => 'civicrm', 'js' => array('js/angular-crmAttachment.js'), 'css' => array('css/angular-crmAttachment.css'), + 'partials' => array('partials/crmAttachment/*.html'), ); $angularModules['crmUi'] = array( 'ext' => 'civicrm', 'js' => array('js/angular-crm-ui.js', 'packages/ckeditor/ckeditor.js'), + 'partials' => array('partials/crmUi/*.html'), ); $angularModules['crmUtil'] = array('ext' => 'civicrm', 'js' => array('js/angular-crm-util.js')); // https://github.com/jwstadler/angular-jquery-dialog-service @@ -107,7 +127,30 @@ class CRM_Core_Page_Angular extends CRM_Core_Page { $angularModules = array_merge($angularModules, $component->getAngularModules()); } CRM_Utils_Hook::angularModules($angularModules); + $angularModules = $this->resolvePatterns($angularModules); return $angularModules; } + /** + * @param array $modules + * List of Angular modules. + * @return array + * Updated list of Angular modules + */ + public function resolvePatterns($modules) { + $newModules = array(); + + foreach ($modules as $moduleKey => $module) { + foreach (array('js', 'css', 'partials') as $fileset) { + if (!isset($module[$fileset])) { + continue; + } + $module[$fileset] = $this->res->glob($module['ext'], $module[$fileset]); + } + $newModules[$moduleKey] = $module; + } + + return $newModules; + } + } diff --git a/CRM/Mailing/Info.php b/CRM/Mailing/Info.php index 38e64683bd..d2d0cf68be 100644 --- a/CRM/Mailing/Info.php +++ b/CRM/Mailing/Info.php @@ -67,6 +67,12 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info { 'js/angular-crmMailing/directives.js', ), 'css' => array('css/angular-crmMailing.css'), + 'partials' => array( + 'partials/crmMailing/*.html', + 'partials/crmMailing/dialog/*.html', + 'partials/crmMailing/directive/*.html', + 'partials/crmMailing/field/*.html', + ), ); $result['crmMailingAB'] = array( 'ext' => 'civicrm', @@ -76,6 +82,9 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info { 'js/angular-crmMailingAB/directives.js', ), 'css' => array('css/angular-crmMailingAB.css'), + 'partials' => array( + 'partials/crmMailingAB/*.html', + ), ); $result['crmD3'] = array( 'ext' => 'civicrm', diff --git a/tests/phpunit/CRM/Core/Page/AngularTest.php b/tests/phpunit/CRM/Core/Page/AngularTest.php new file mode 100644 index 0000000000..f0b88f9b85 --- /dev/null +++ b/tests/phpunit/CRM/Core/Page/AngularTest.php @@ -0,0 +1,54 @@ +useTransaction(TRUE); + parent::setUp(); + } + + /** + * Ensure that valid partials appear on the example module (crmUi). + */ + public function testPartialPattern() { + $this->createLoggedInUser(); + $page = new CRM_Core_Page_Angular(); + $angularModules = $page->getAngularModules(); + $matches = preg_grep(':/tabset.html$:', $angularModules['crmUi']['partials']); + $this->assertTrue(count($matches) > 0, + 'Expect to find example tabset.html. If it has been reorganized, then update this test with a different example.'); + } +} -- 2.25.1