CRM-20600, CRM-20112 - AngularLoader - Add helper for base-pages and injections
[civicrm-core.git] / Civi / Angular / Page / Main.php
index 344936fa35c96aac594a90e9fa7a57f25b9deb28..a13a18c0682cd50f70f95f542fd91c93013f17a9 100644 (file)
@@ -8,6 +8,7 @@ namespace Civi\Angular\Page;
  * @link https://issues.civicrm.org/jira/browse/CRM-14479
  */
 class Main extends \CRM_Core_Page {
+
   /**
    * The weight to assign to any Angular JS module files.
    */
@@ -19,19 +20,28 @@ class Main extends \CRM_Core_Page {
    * 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.
@@ -43,7 +53,8 @@ class Main extends \CRM_Core_Page {
   public function __construct($title = NULL, $mode = NULL, $res = NULL) {
     parent::__construct($title, $mode);
     $this->res = \CRM_Core_Resources::singleton();
-    $this->angular = \Civi\Core\Container::singleton()->get('angular');
+    $this->angular = \Civi::service('angular');
+    $this->region = \CRM_Utils_Request::retrieve('snippet', 'String') ? 'ajax-snippet' : 'html-header';
   }
 
   /**
@@ -63,50 +74,27 @@ class Main extends \CRM_Core_Page {
    * Register resources required by Angular.
    */
   public function registerResources() {
-    $modules = $this->angular->getModules();
-    $page = $this; // PHP 5.3 does not propagate $this to inner functions.
+    $loader = new \Civi\Angular\AngularLoader();
+    $loader->setPageName('civicrm/a');
+    $loader->setModules(array('crmApp'));
+    $loader->load();
 
-    $this->res->addSettingsFactory(function () use (&$modules, $page) {
-      // TODO optimization; client-side caching
-      return array_merge($page->angular->getResources(array_keys($modules), 'settings', 'settings'), array(
-        'resourceUrls' => \CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(),
-        'angular' => array(
-          'modules' => array_merge(array('ngRoute'), array_keys($modules)),
-          'cacheCode' => $page->res->getCacheCode(),
-        ),
-      ));
-    });
-
-    $this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, 'html-header', FALSE);
-
-    $headOffset = 0;
-    $config = \CRM_Core_Config::singleton();
-    if ($config->debug) {
-      foreach ($modules as $moduleName => $module) {
-        foreach ($this->angular->getResources($moduleName, 'css', 'cacheUrl') as $url) {
-          $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
-        }
-        foreach ($this->angular->getResources($moduleName, 'js', 'cacheUrl') as $url) {
-          $this->res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
-          // addScriptUrl() bypasses the normal string-localization of addScriptFile(),
-          // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
-        }
-      }
-    }
-    else {
-      // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(),
-      // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
-      $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
-      $this->res->addScriptUrl($aggScriptUrl, 120, 'html-header');
-
-      // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons.
-      //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
-      //$this->res->addStyleUrl($aggStyleUrl, 120, 'html-header');
+    // If trying to load an Angular page via AJAX, the route must be passed as a
+    // URL parameter, since the server doesn't receive information about
+    // URL fragments (i.e, what comes after the #).
+    \CRM_Core_Resources::singleton()->addSetting(array(
+      'crmApp' => array(
+        'defaultRoute' => NULL,
+      ),
+      'angularRoute' => \CRM_Utils_Request::retrieve('route', 'String'),
+    ));
+  }
 
-      foreach ($this->angular->getResources(array_keys($modules), 'css', 'cacheUrl') as $url) {
-        $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
-      }
-    }
+  /**
+   * @inheritdoc
+   */
+  public function getTemplateFileName() {
+    return 'Civi/Angular/Page/Main.tpl';
   }
 
 }