09ccf0cab95eb583e2d00698fd338990e3ad2bae
2 namespace Civi\Angular\Page
;
5 * This page is simply a container; any Angular modules defined by CiviCRM (or by CiviCRM extensions)
6 * will be activated on this page.
8 * @link https://issues.civicrm.org/jira/browse/CRM-14479
10 class Main
extends \CRM_Core_Page
{
12 * The weight to assign to any Angular JS module files.
14 const DEFAULT_MODULE_WEIGHT
= 200;
17 * The resource manager.
19 * Do not use publicly. Inject your own copy!
21 * @var \CRM_Core_Resources
27 * The Angular module manager.
29 * Do not use publicly. Inject your own copy!
31 * @var \Civi\Angular\Manager
36 * The region of the page into which JavaScript will be loaded.
43 * @param string $title
47 * @param \CRM_Core_Resources|null $res
50 public function __construct($title = NULL, $mode = NULL, $res = NULL) {
51 parent
::__construct($title, $mode);
52 $this->res
= \CRM_Core_Resources
::singleton();
53 $this->angular
= \Civi\Core\Container
::singleton()->get('angular');
54 $this->region
= \CRM_Utils_Request
::retrieve('snippet', 'String') ?
'ajax-snippet' : 'html-header';
58 * This function takes care of all the things common to all
59 * pages. This typically involves assigning the appropriate
63 * The content generated by running this page
65 public function run() {
66 $this->registerResources();
71 * Register resources required by Angular.
73 public function registerResources() {
74 $modules = $this->angular
->getModules();
75 $page = $this; // PHP 5.3 does not propagate $this to inner functions.
77 $this->res
->addSettingsFactory(function () use (&$modules, $page) {
78 // TODO optimization; client-side caching
79 return array_merge($page->angular
->getResources(array_keys($modules), 'settings', 'settings'), array(
80 'resourceUrls' => \CRM_Extension_System
::singleton()->getMapper()->getActiveModuleUrls(),
82 'modules' => array_merge(array('ngRoute'), array_keys($modules)),
83 'cacheCode' => $page->res
->getCacheCode(),
88 $this->res
->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, $this->region
, FALSE);
91 $config = \CRM_Core_Config
::singleton();
93 foreach ($modules as $moduleName => $module) {
94 foreach ($this->angular
->getResources($moduleName, 'css', 'cacheUrl') as $url) {
95 $this->res
->addStyleUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->region
);
97 foreach ($this->angular
->getResources($moduleName, 'js', 'cacheUrl') as $url) {
98 $this->res
->addScriptUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->region
);
99 // addScriptUrl() bypasses the normal string-localization of addScriptFile(),
100 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
105 // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(),
106 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
107 $aggScriptUrl = \CRM_Utils_System
::url('civicrm/ajax/angular-modules', 'format=js&r=' . $page->res
->getCacheCode(), FALSE, NULL, FALSE);
108 $this->res
->addScriptUrl($aggScriptUrl, 120, $this->region
);
110 // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons.
111 //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
112 //$this->res->addStyleUrl($aggStyleUrl, 120, $this->region);
114 foreach ($this->angular
->getResources(array_keys($modules), 'css', 'cacheUrl') as $url) {
115 $this->res
->addStyleUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->region
);
119 // If trying to load an Angular page via AJAX, the route must be passed as a
120 // URL parameter, since the server doesn't receive information about
121 // URL fragments (i.e, what comes after the #).
122 \CRM_Core_Resources
::singleton()->addSetting(array(
123 'angularRoute' => \CRM_Utils_Request
::retrieve('route', 'String'),