2 namespace Civi\Angular
;
5 * The AngularLoader loads any JS/CSS/JSON resources
6 * required for setting up AngularJS.
8 * The AngularLoader stops short of bootstrapping AngularJS. You may
9 * need to `<div ng-app="..."></div>` or `angular.bootstrap(...)`.
12 * $loader = new AngularLoader();
13 * $loader->setPageName('civicrm/case/a');
14 * $loader->setModules(array('crmApp'));
18 * @link https://docs.angularjs.org/guide/bootstrap
23 * The weight to assign to any Angular JS module files.
25 const DEFAULT_MODULE_WEIGHT
= 200;
28 * The resource manager.
30 * Do not use publicly. Inject your own copy!
32 * @var \CRM_Core_Resources
37 * The Angular module manager.
39 * Do not use publicly. Inject your own copy!
41 * @var \Civi\Angular\Manager
46 * The region of the page into which JavaScript will be loaded.
60 * A list of modules to load.
67 protected $crmApp = NULL;
70 * AngularLoader constructor.
72 public function __construct() {
73 $this->res
= \CRM_Core_Resources
::singleton();
74 $this->angular
= \Civi
::service('angular');
75 $this->region
= \CRM_Utils_Request
::retrieve('snippet', 'String') ?
'ajax-snippet' : 'html-header';
76 $this->pageName
= isset($_GET['q']) ?
$_GET['q'] : NULL;
77 $this->modules
= array();
81 * Register resources required by Angular.
83 * @return AngularLoader
85 public function load() {
86 $angular = $this->getAngular();
87 $res = $this->getRes();
89 if ($this->crmApp
!== NULL) {
90 $this->addModules($this->crmApp
['modules']);
91 $region = \CRM_Core_Region
::instance($this->crmApp
['region']);
92 $region->update('default', array('disabled' => TRUE));
93 $region->add(array('template' => $this->crmApp
['file'], 'weight' => 0));
94 $this->res
->addSetting(array(
96 'defaultRoute' => $this->crmApp
['defaultRoute'],
100 // If trying to load an Angular page via AJAX, the route must be passed as a
101 // URL parameter, since the server doesn't receive information about
102 // URL fragments (i.e, what comes after the #).
103 $this->res
->addSetting(array(
104 'angularRoute' => $this->crmApp
['activeRoute'],
108 $moduleNames = $this->findActiveModules();
109 if (!$this->isAllModules($moduleNames)) {
110 $assetParams = array('modules' => implode(',', $moduleNames));
113 // The module list will be "all modules that the user can see".
114 $assetParams = array('nonce' => md5(implode(',', $moduleNames)));
117 $res->addSettingsFactory(function () use (&$moduleNames, $angular, $res, $assetParams) {
118 // TODO optimization; client-side caching
119 $result = array_merge($angular->getResources($moduleNames, 'settings', 'settings'), array(
120 'resourceUrls' => \CRM_Extension_System
::singleton()->getMapper()->getActiveModuleUrls(),
122 'modules' => $moduleNames,
123 'requires' => $angular->getResources($moduleNames, 'requires', 'requires'),
124 'cacheCode' => $res->getCacheCode(),
125 'bundleUrl' => \Civi
::service('asset_builder')->getUrl('angular-modules.json', $assetParams),
131 $res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, $this->getRegion(), FALSE);
132 $res->addScriptFile('civicrm', 'js/crm.angular.js', 101, $this->getRegion(), FALSE);
135 $config = \CRM_Core_Config
::singleton();
136 if ($config->debug
) {
137 foreach ($moduleNames as $moduleName) {
138 foreach ($this->angular
->getResources($moduleName, 'css', 'cacheUrl') as $url) {
139 $res->addStyleUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->getRegion());
141 foreach ($this->angular
->getResources($moduleName, 'js', 'cacheUrl') as $url) {
142 $res->addScriptUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->getRegion());
143 // addScriptUrl() bypasses the normal string-localization of addScriptFile(),
144 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
149 // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(),
150 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
151 // $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $res->getCacheCode(), FALSE, NULL, FALSE);
152 $aggScriptUrl = \Civi
::service('asset_builder')->getUrl('angular-modules.js', $assetParams);
153 $res->addScriptUrl($aggScriptUrl, 120, $this->getRegion());
155 // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons.
156 //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $res->getCacheCode(), FALSE, NULL, FALSE);
157 //$aggStyleUrl = \Civi::service('asset_builder')->getUrl('angular-modules.css', $assetParams);
158 //$res->addStyleUrl($aggStyleUrl, 120, $this->getRegion());
160 foreach ($this->angular
->getResources($moduleNames, 'css', 'cacheUrl') as $url) {
161 $res->addStyleUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->getRegion());
169 * Use Civi's generic "application" module.
171 * This is suitable for use on a basic, standalone Angular page
172 * like `civicrm/a`. (If you need to integrate Angular with pre-existing,
173 * non-Angular pages... then this probably won't help.)
175 * The Angular bootstrap process requires an HTML directive like
176 * `<div ng-app="foo">`.
178 * Calling useApp() will replace the page's main body with the
179 * `<div ng-app="crmApp">...</div>` and apply some configuration options
180 * for the `crmApp` module.
182 * @param array $settings
183 * A list of settings. Accepted values:
184 * - activeRoute: string, the route to open up immediately
186 * - defaultRoute: string, use this to redirect the default route (`/`) to another page
188 * - region: string, the place on the page where we should insert the angular app
190 * @return AngularLoader
191 * @link https://code.angularjs.org/1.5.11/docs/guide/bootstrap
193 public function useApp($settings = array()) {
195 'modules' => array('crmApp'),
196 'activeRoute' => NULL,
197 'defaultRoute' => NULL,
198 'region' => 'page-body',
199 'file' => 'Civi/Angular/Page/Main.tpl',
201 $this->crmApp
= array_merge($defaults, $settings);
206 * Get a list of all Angular modules which should be activated on this
210 * List of module names.
211 * Ex: array('angularFileUpload', 'crmUi', 'crmUtil').
213 public function findActiveModules() {
214 return $this->angular
->resolveDependencies(array_merge(
216 $this->angular
->resolveDefaultModules($this->getPageName())
221 * @param $moduleNames
224 private function isAllModules($moduleNames) {
225 $allModuleNames = array_keys($this->angular
->getModules());
226 return count(array_diff($allModuleNames, $moduleNames)) === 0;
230 * @return \CRM_Core_Resources
232 public function getRes() {
237 * @param \CRM_Core_Resources $res
238 * @return AngularLoader
240 public function setRes($res) {
246 * @return \Civi\Angular\Manager
248 public function getAngular() {
249 return $this->angular
;
253 * @param \Civi\Angular\Manager $angular
254 * @return AngularLoader
256 public function setAngular($angular) {
257 $this->angular
= $angular;
264 public function getRegion() {
265 return $this->region
;
269 * @param string $region
270 * @return AngularLoader
272 public function setRegion($region) {
273 $this->region
= $region;
281 public function getPageName() {
282 return $this->pageName
;
286 * @param string $pageName
288 * @return AngularLoader
290 public function setPageName($pageName) {
291 $this->pageName
= $pageName;
296 * @param array|string $modules
297 * @return AngularLoader
299 public function addModules($modules) {
300 $modules = (array) $modules;
301 $this->modules
= array_unique(array_merge($this->modules
, $modules));
308 public function getModules() {
309 return $this->modules
;
313 * @param array $modules
314 * @return AngularLoader
316 public function setModules($modules) {
317 $this->modules
= $modules;