Merge pull request #22027 from braders/fix-graph-empty-logic
[civicrm-core.git] / Civi / Angular / AngularLoader.php
index c887e9d13f477a0c0df566e3bee243e03a408e6b..a6b5f80a42e8f3c40237a69ba3fc994f755be7cc 100644 (file)
@@ -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 `<div ng-app="..."></div>` 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();
 
@@ -155,6 +157,7 @@ class AngularLoader {
     $headOffset = 0;
     $config = \CRM_Core_Config::singleton();
     if ($config->debug) {
+      // FIXME: The `resetLocationProviderHashPrefix.js` has to stay in sync with `\Civi\Angular\Page\Modules::buildAngularModules()`.
       $res->addScriptFile('civicrm', 'ang/resetLocationProviderHashPrefix.js', 101, $this->getRegion(), FALSE);
       foreach ($moduleNames as $moduleName) {
         foreach ($this->angular->getResources($moduleName, 'css', 'cacheUrl') as $url) {
@@ -342,21 +345,27 @@ class AngularLoader {
    * Replace all previously set modules.
    *
    * Use with caution, as it can cause conflicts with other extensions who have added modules.
-   *
+   * @internal
+   * @deprecated
    * @param array $modules
    * @return AngularLoader
    */
   public function setModules($modules) {
+    \CRM_Core_Error::deprecatedFunctionWarning('addModules');
     $this->modules = $modules;
     return $this;
   }
 
   /**
+   * 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);
     }
   }