CRM-15832 - Update various modules to use crmResource
[civicrm-core.git] / CRM / Core / Page / Angular.php
index 0ab871872cd1707ce5d2147251837124dbcc52ca..59c4dc81cefd6e7247cc8d42c03de3a7aa1e3409 100644 (file)
@@ -12,74 +12,76 @@ class CRM_Core_Page_Angular extends CRM_Core_Page {
    */
   const DEFAULT_MODULE_WEIGHT = 200;
 
+  /**
+   * @var CRM_Core_Resources
+   */
+  protected $res;
+
+
+  /**
+   * @var Civi\Angular\Manager
+   */
+  protected $angular;
+
+  /**
+   * @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\Core\Container::singleton()->get('angular');
+  }
+
   /**
    * 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
+   * @return string
+   *   The content generated by running this page
    */
-  function run() {
-    $this->registerResources(CRM_Core_Resources::singleton());
+  public function run() {
+    $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->angular->getModules();
 
-    $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(),
         'angular' => array(
           'modules' => array_merge(array('ngRoute'), array_keys($modules)),
+          'cacheCode' => $this->res->getCacheCode(),
+        ),
+        'crmAttachment' => array(
+          'token' => CRM_Core_Page_AJAX_Attachment::createToken(),
         ),
       );
     });
 
-    $res->addScriptFile('civicrm', 'packages/bower_components/angular/angular.min.js', 100, 'html-header', FALSE);
-    $res->addScriptFile('civicrm', 'packages/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);
-        }
+    foreach ($modules as $moduleName => $module) {
+      foreach ($this->angular->getStyleUrls($moduleName) as $url) {
+        $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), 'html-header');
       }
-      if (!empty($module['js'])) {
-        foreach ($module['js'] as $file) {
-          $res->addScriptFile($module['ext'], $file, self::DEFAULT_MODULE_WEIGHT  + (++$headOffset), 'html-header', TRUE);
-        }
+      foreach ($this->angular->getScriptUrls($moduleName) 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.
       }
     }
   }
-
-  /**
-   * Get a list of AngularJS modules which should be autoloaded
-   *
-   * @return array (string $name => array('ext' => string $key, 'js' => array $paths, 'css' => array $paths))
-   */
-  public static function getAngularModules() {
-    $angularModules = array();
-    $angularModules['ui.utils'] = array('ext' => 'civicrm', 'js' => array('packages/bower_components/angular-ui-utils/ui-utils.min.js'));
-    $angularModules['ui.sortable'] = array('ext' => 'civicrm', 'js' => array('packages/bower_components/angular-ui-sortable/sortable.min.js'));
-    $angularModules['unsavedChanges'] = array('ext' => 'civicrm', 'js' => array('packages/bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'));
-    // https://github.com/jwstadler/angular-jquery-dialog-service
-    $angularModules['angularFileUpload'] = array('ext' => 'civicrm', 'js' => array('packages/bower_components/angular-file-upload/angular-file-upload.min.js'));
-    $angularModules['dialogService'] = array('ext' => 'civicrm' , 'js' => array('packages/bower_components/angular-jquery-dialog-service/dialog-service.js'));
-    $angularModules['crmAttachment'] = array('ext' => 'civicrm', 'js' => array('js/angular-crmAttachment.js'), 'css' => array('css/angular-crmAttachment.css'));
-    $angularModules['crmUi'] = array('ext' => 'civicrm', 'js' => array('js/angular-crm-ui.js', 'packages/ckeditor/ckeditor.js'));
-    $angularModules['crmUtil'] = array('ext' => 'civicrm', 'js' => array('js/angular-crm-util.js'));
-    $angularModules['ngSanitize'] = array('ext' => 'civicrm', 'js' => array('js/angular-sanitize.js'));
-
-    foreach (CRM_Core_Component::getEnabledComponents() as $component) {
-      $angularModules = array_merge($angularModules, $component->getAngularModules());
-    }
-    CRM_Utils_Hook::angularModules($angularModules);
-    return $angularModules;
-  }
-
 }