From f5c157f5fd7f78d191363710446f150c48ccaa84 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 25 May 2021 15:04:15 -0400 Subject: [PATCH] AngularLoader - cleanup resource loading to use new service Use new `crm-angular-js` instead of `ng-app` to bootstrap AngularJS Emit a deprecated warning from the old load() function to alert extension maintainers of the change. --- Civi/Angular/AngularLoader.php | 38 +++++++----- Civi/Angular/Page/Main.php | 60 +------------------ .../core/CRM/Afform/Page/AfformBase.php | 5 +- .../templates/CRM/Afform/Page/AfformBase.tpl | 4 +- .../CRM/Contact/Page/DashBoardDashlet.tpl | 4 +- templates/Civi/Angular/Page/Main.tpl | 4 +- tests/phpunit/Civi/Angular/LoaderTest.php | 9 +-- 7 files changed, 37 insertions(+), 87 deletions(-) diff --git a/Civi/Angular/AngularLoader.php b/Civi/Angular/AngularLoader.php index 2df7997ce5..a5d6d38b57 100644 --- a/Civi/Angular/AngularLoader.php +++ b/Civi/Angular/AngularLoader.php @@ -5,14 +5,12 @@ namespace Civi\Angular; * The AngularLoader loads any JS/CSS/JSON resources * required for setting up AngularJS. * - * The AngularLoader stops short of bootstrapping AngularJS. You may - * need to `
` or `angular.bootstrap(...)`. + * This class is returned by 'angularjs.loader' service. Example use: * * ``` - * $loader = new AngularLoader(); - * $loader->setPageName('civicrm/case/a'); - * $loader->setModules(array('crmApp')); - * $loader->load(); + * Civi::service('angularjs.loader') + * ->addModules('moduleFoo') + * ->useApp(); // Optional, if Civi's routing is desired (full-page apps only) * ``` * * @link https://docs.angularjs.org/guide/bootstrap @@ -80,19 +78,23 @@ class AngularLoader { /** * Calling this method from outside this class is deprecated. * - * The correct way to use this class is as a service, which will load automatically. E.g.: + * Use the `angularjs.loader` service instead. * - * ``` - * Civi::service('angularjs.loader') - * ->addModules('moduleFoo') - * ->useApp(); // Optional, if Civi's routing is desired (full-page apps only) - * ``` - * - * @internal * @deprecated - * @return AngularLoader + * @return $this */ public function load() { + \CRM_Core_Error::deprecatedFunctionWarning('angularjs.loader service'); + return $this->loadAngularResources(); + } + + /** + * Load scripts, styles & settings for the active modules. + * + * @return $this + * @throws \CRM_Core_Exception + */ + private function loadAngularResources() { $angular = $this->getAngular(); $res = $this->getRes(); @@ -353,11 +355,15 @@ class AngularLoader { } /** + * Loader service callback when rendering a page region. + * + * Loads Angular resources if any modules have been requested for this page. + * * @param \Civi\Core\Event\GenericHookEvent $e */ public function onRegionRender($e) { if ($e->region->_name === $this->region && ($this->modules || $this->crmApp)) { - $this->load(); + $this->loadAngularResources(); $this->res->addScriptFile('civicrm', 'js/crm-angularjs-loader.js', 200, $this->getRegion(), FALSE); } } diff --git a/Civi/Angular/Page/Main.php b/Civi/Angular/Page/Main.php index c844fd3cc0..604158437e 100644 --- a/Civi/Angular/Page/Main.php +++ b/Civi/Angular/Page/Main.php @@ -10,60 +10,7 @@ namespace Civi\Angular\Page; class Main extends \CRM_Core_Page { /** - * The weight to assign to any Angular JS module files. - */ - const DEFAULT_MODULE_WEIGHT = 200; - - /** - * The resource manager. - * - * Do not use publicly. Inject your own copy! - * - * @var \CRM_Core_Resources - * @deprecated - */ - public $res; - - /** - * The Angular module manager. - * - * Do not use publicly. Inject your own copy! - * - * @var \Civi\Angular\Manager - * @deprecated - */ - public $angular; - - /** - * The region of the page into which JavaScript will be loaded. - * - * @var string - * @deprecated - */ - public $region; - - /** - * @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->angular = \Civi::service('angular'); - $this->region = \CRM_Utils_Request::retrieve('snippet', 'String') ? 'ajax-snippet' : 'html-header'; - } - - /** - * This function takes care of all the things common to all - * pages. This typically involves assigning the appropriate - * smarty variable :) - * - * @return string - * The content generated by running this page + * Run the page */ public function run() { $this->registerResources(); @@ -74,14 +21,11 @@ class Main extends \CRM_Core_Page { * Register resources required by Angular. */ public function registerResources() { - $loader = new \Civi\Angular\AngularLoader(); - $loader->setPageName('civicrm/a'); + $loader = \Civi::service('angularjs.loader'); $loader->useApp([ 'activeRoute' => \CRM_Utils_Request::retrieve('route', 'String'), 'defaultRoute' => NULL, ]); - $loader->load(); - } } diff --git a/ext/afform/core/CRM/Afform/Page/AfformBase.php b/ext/afform/core/CRM/Afform/Page/AfformBase.php index 3530e555db..5b8bb9e7dd 100644 --- a/ext/afform/core/CRM/Afform/Page/AfformBase.php +++ b/ext/afform/core/CRM/Afform/Page/AfformBase.php @@ -15,9 +15,8 @@ class CRM_Afform_Page_AfformBase extends CRM_Core_Page { $this->assign('directive', $afform['directive_name']); - (new \Civi\Angular\AngularLoader()) - ->setModules([$afform['module_name'], 'afformStandalone']) - ->load(); + Civi::service('angularjs.loader') + ->addModules([$afform['module_name'], 'afformStandalone']); // If the user has "access civicrm" append home breadcrumb if (CRM_Core_Permission::check('access CiviCRM')) { diff --git a/ext/afform/core/templates/CRM/Afform/Page/AfformBase.tpl b/ext/afform/core/templates/CRM/Afform/Page/AfformBase.tpl index 9fd1c1b629..ead23c7a08 100644 --- a/ext/afform/core/templates/CRM/Afform/Page/AfformBase.tpl +++ b/ext/afform/core/templates/CRM/Afform/Page/AfformBase.tpl @@ -1,5 +1,5 @@ -
+
<{$directive}>
-
+ diff --git a/templates/CRM/Contact/Page/DashBoardDashlet.tpl b/templates/CRM/Contact/Page/DashBoardDashlet.tpl index e16653417e..ac2ed7af3f 100644 --- a/templates/CRM/Contact/Page/DashBoardDashlet.tpl +++ b/templates/CRM/Contact/Page/DashBoardDashlet.tpl @@ -13,8 +13,8 @@
-
+ -
+
diff --git a/templates/Civi/Angular/Page/Main.tpl b/templates/Civi/Angular/Page/Main.tpl index 4918da28ae..fd8af0b59b 100644 --- a/templates/Civi/Angular/Page/Main.tpl +++ b/templates/Civi/Angular/Page/Main.tpl @@ -5,8 +5,8 @@ } -
+
-
+ {/literal} diff --git a/tests/phpunit/Civi/Angular/LoaderTest.php b/tests/phpunit/Civi/Angular/LoaderTest.php index e1fe0e0948..d26e4d2e4c 100644 --- a/tests/phpunit/Civi/Angular/LoaderTest.php +++ b/tests/phpunit/Civi/Angular/LoaderTest.php @@ -46,10 +46,11 @@ class LoaderTest extends \CiviUnitTestCase { * @param $expectedPermissions */ public function testSettingFactory($module, $expectedSettingCount, $expectedCallbackCount, $expectedPermissions) { - (new \Civi\Angular\AngularLoader()) - ->setModules([$module]) - ->useApp() - ->load(); + $loader = new \Civi\Angular\AngularLoader(); + $loader->setModules([$module]); + $loader->useApp(); + // Load triggers a depreaction notice, use @ to suppress it for the test. + @$loader->load(); // Run factory callbacks $actual = \Civi::resources()->getSettings(); -- 2.25.1