CRM-20600, CRM-20112 - AngularLoader - Add helper for base-pages and injections
[civicrm-core.git] / Civi / Angular / Page / Main.php
index 2f945ebae7991b8562dfd726b0ba0af961df0607..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,16 +20,17 @@ 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;
 
@@ -36,6 +38,7 @@ class Main extends \CRM_Core_Page {
    * The region of the page into which JavaScript will be loaded.
    *
    * @var String
+   * @deprecated
    */
   public $region;
 
@@ -71,57 +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.
-
-    $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, $this->region, 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), $this->region);
-        }
-        foreach ($this->angular->getResources($moduleName, 'js', 'cacheUrl') as $url) {
-          $this->res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), $this->region);
-          // 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, $this->region);
-
-      // 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, $this->region);
-
-      foreach ($this->angular->getResources(array_keys($modules), 'css', 'cacheUrl') as $url) {
-        $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), $this->region);
-      }
-    }
+    $loader = new \Civi\Angular\AngularLoader();
+    $loader->setPageName('civicrm/a');
+    $loader->setModules(array('crmApp'));
+    $loader->load();
 
     // 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'),
     ));
   }
 
+  /**
+   * @inheritdoc
+   */
+  public function getTemplateFileName() {
+    return 'Civi/Angular/Page/Main.tpl';
+  }
+
 }