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
= \CRM_Utils_System
::currentPath();
81 * Calling this method from outside this class is deprecated.
83 * The correct way to use this class is as a service, which will load automatically. E.g.:
86 * Civi::service('angularjs.loader')
87 * ->addModules('moduleFoo')
88 * ->useApp(); // Optional, if Civi's routing is desired (full-page apps only)
93 * @return AngularLoader
95 public function load() {
96 $angular = $this->getAngular();
97 $res = $this->getRes();
99 if ($this->crmApp
!== NULL) {
100 $this->addModules($this->crmApp
['modules']);
102 $this->res
->addSetting([
104 'defaultRoute' => $this->crmApp
['defaultRoute'],
108 // If trying to load an Angular page via AJAX, the route must be passed as a
109 // URL parameter, since the server doesn't receive information about
110 // URL fragments (i.e, what comes after the #).
111 $this->res
->addSetting([
112 'angularRoute' => $this->crmApp
['activeRoute'],
116 $moduleNames = $this->findActiveModules();
117 if (!$this->isAllModules($moduleNames)) {
118 $assetParams = ['modules' => implode(',', $moduleNames)];
121 // The module list will be "all modules that the user can see".
122 $assetParams = ['nonce' => md5(implode(',', $moduleNames))];
125 $res->addSettingsFactory(function () use (&$moduleNames, $angular, $res, $assetParams) {
126 // Merge static settings with the results of settingsFactory functions
127 $settingsByModule = $angular->getResources($moduleNames, 'settings', 'settings');
128 foreach ($angular->getResources($moduleNames, 'settingsFactory', 'settingsFactory') as $moduleName => $factory) {
129 $settingsByModule[$moduleName] = array_merge($settingsByModule[$moduleName] ??
[], $factory());
131 // Add clientside permissions
133 $toCheck = $angular->getResources($moduleNames, 'permissions', 'permissions');
134 foreach ($toCheck as $perms) {
135 foreach ((array) $perms as $perm) {
136 if (!isset($permissions[$perm])) {
137 $permissions[$perm] = \CRM_Core_Permission
::check($perm);
141 // TODO optimization; client-side caching
142 return array_merge($settingsByModule, ['permissions' => $permissions], [
143 'resourceUrls' => \CRM_Extension_System
::singleton()->getMapper()->getActiveModuleUrls(),
145 'modules' => $moduleNames,
146 'requires' => $angular->getResources($moduleNames, 'requires', 'requires'),
147 'cacheCode' => $res->getCacheCode(),
148 'bundleUrl' => \Civi
::service('asset_builder')->getUrl('angular-modules.json', $assetParams),
153 $res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, $this->getRegion(), FALSE);
156 $config = \CRM_Core_Config
::singleton();
157 if ($config->debug
) {
158 foreach ($moduleNames as $moduleName) {
159 foreach ($this->angular
->getResources($moduleName, 'css', 'cacheUrl') as $url) {
160 $res->addStyleUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->getRegion());
162 foreach ($this->angular
->getResources($moduleName, 'js', 'cacheUrl') as $url) {
163 $res->addScriptUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->getRegion());
164 // addScriptUrl() bypasses the normal string-localization of addScriptFile(),
165 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
170 // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(),
171 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
172 // $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $res->getCacheCode(), FALSE, NULL, FALSE);
173 $aggScriptUrl = \Civi
::service('asset_builder')->getUrl('angular-modules.js', $assetParams);
174 $res->addScriptUrl($aggScriptUrl, 120, $this->getRegion());
176 // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons.
177 //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $res->getCacheCode(), FALSE, NULL, FALSE);
178 //$aggStyleUrl = \Civi::service('asset_builder')->getUrl('angular-modules.css', $assetParams);
179 //$res->addStyleUrl($aggStyleUrl, 120, $this->getRegion());
181 foreach ($this->angular
->getResources($moduleNames, 'css', 'cacheUrl') as $url) {
182 $res->addStyleUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->getRegion());
186 foreach ($this->angular
->getResources($moduleNames, 'bundles', 'bundles') as $bundles) {
187 $res->addBundle($bundles);
194 * Use Civi's generic "application" module.
196 * This is suitable for use on a basic, standalone Angular page
197 * like `civicrm/a`. (If you need to integrate Angular with pre-existing,
198 * non-Angular pages... then this probably won't help.)
200 * The Angular bootstrap process requires an HTML directive like
201 * `<div ng-app="foo">`.
203 * Calling useApp() will replace the page's main body with the
204 * `<div ng-app="crmApp">...</div>` and apply some configuration options
205 * for the `crmApp` module.
207 * @param array $settings
208 * A list of settings. Accepted values:
209 * - activeRoute: string, the route to open up immediately
211 * - defaultRoute: string, use this to redirect the default route (`/`) to another page
213 * - region: string, the place on the page where we should insert the angular app
215 * @return AngularLoader
216 * @link https://code.angularjs.org/1.5.11/docs/guide/bootstrap
218 public function useApp($settings = []) {
220 'modules' => ['crmApp'],
221 'activeRoute' => NULL,
222 'defaultRoute' => NULL,
223 'region' => 'page-body',
224 'file' => 'Civi/Angular/Page/Main.tpl',
226 $this->crmApp
= array_merge($defaults, $settings);
227 $region = \CRM_Core_Region
::instance($this->crmApp
['region']);
228 $region->update('default', ['disabled' => TRUE]);
229 $region->add(['template' => $this->crmApp
['file'], 'weight' => 0]);
234 * Get a list of all Angular modules which should be activated on this
238 * List of module names.
239 * Ex: array('angularFileUpload', 'crmUi', 'crmUtil').
241 public function findActiveModules() {
242 return $this->angular
->resolveDependencies(array_merge(
244 $this->angular
->resolveDefaultModules($this->getPageName())
249 * @param $moduleNames
252 private function isAllModules($moduleNames) {
253 $allModuleNames = array_keys($this->angular
->getModules());
254 return count(array_diff($allModuleNames, $moduleNames)) === 0;
258 * @return \CRM_Core_Resources
260 public function getRes() {
265 * @param \CRM_Core_Resources $res
266 * @return AngularLoader
268 public function setRes($res) {
274 * @return \Civi\Angular\Manager
276 public function getAngular() {
277 return $this->angular
;
281 * @param \Civi\Angular\Manager $angular
282 * @return AngularLoader
284 public function setAngular($angular) {
285 $this->angular
= $angular;
292 public function getRegion() {
293 return $this->region
;
297 * @param string $region
298 * @return AngularLoader
300 public function setRegion($region) {
301 $this->region
= $region;
309 public function getPageName() {
310 return $this->pageName
;
314 * @param string $pageName
316 * @return AngularLoader
318 public function setPageName($pageName) {
319 $this->pageName
= $pageName;
324 * @param array|string $modules
325 * @return AngularLoader
327 public function addModules($modules) {
328 $modules = (array) $modules;
329 $this->modules
= array_unique(array_merge($this->modules
, $modules));
336 public function getModules() {
337 return $this->modules
;
341 * Replace all previously set modules.
343 * Use with caution, as it can cause conflicts with other extensions who have added modules.
345 * @param array $modules
346 * @return AngularLoader
348 public function setModules($modules) {
349 $this->modules
= $modules;
354 * @param \Civi\Core\Event\GenericHookEvent $e
356 public function onRegionRender($e) {
357 if ($e->region
->_name
=== $this->region
&& ($this->modules ||
$this->crmApp
)) {
359 $this->res
->addScriptFile('civicrm', 'js/crm-angularjs-loader.js', 200, $this->getRegion(), FALSE);