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 * 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', ['disabled' => TRUE]);
93 $region->add(['template' => $this->crmApp
['file'], 'weight' => 0]);
94 $this->res
->addSetting([
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([
104 'angularRoute' => $this->crmApp
['activeRoute'],
108 $moduleNames = $this->findActiveModules();
109 if (!$this->isAllModules($moduleNames)) {
110 $assetParams = ['modules' => implode(',', $moduleNames)];
113 // The module list will be "all modules that the user can see".
114 $assetParams = ['nonce' => md5(implode(',', $moduleNames))];
117 $res->addSettingsFactory(function () use (&$moduleNames, $angular, $res, $assetParams) {
118 // Merge static settings with the results of settingsFactory functions
119 $settingsByModule = $angular->getResources($moduleNames, 'settings', 'settings');
120 foreach ($angular->getResources($moduleNames, 'settingsFactory', 'settingsFactory') as $moduleName => $factory) {
121 $settingsByModule[$moduleName] = array_merge($settingsByModule[$moduleName] ??
[], $factory());
123 // Add clientside permissions
125 $toCheck = $angular->getResources($moduleNames, 'permissions', 'permissions');
126 foreach ($toCheck as $perms) {
127 foreach ((array) $perms as $perm) {
128 if (!isset($permissions[$perm])) {
129 $permissions[$perm] = \CRM_Core_Permission
::check($perm);
133 // TODO optimization; client-side caching
134 return array_merge($settingsByModule, ['permissions' => $permissions], [
135 'resourceUrls' => \CRM_Extension_System
::singleton()->getMapper()->getActiveModuleUrls(),
137 'modules' => $moduleNames,
138 'requires' => $angular->getResources($moduleNames, 'requires', 'requires'),
139 'cacheCode' => $res->getCacheCode(),
140 'bundleUrl' => \Civi
::service('asset_builder')->getUrl('angular-modules.json', $assetParams),
145 $res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, $this->getRegion(), FALSE);
148 $config = \CRM_Core_Config
::singleton();
149 if ($config->debug
) {
150 foreach ($moduleNames as $moduleName) {
151 foreach ($this->angular
->getResources($moduleName, 'css', 'cacheUrl') as $url) {
152 $res->addStyleUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->getRegion());
154 foreach ($this->angular
->getResources($moduleName, 'js', 'cacheUrl') as $url) {
155 $res->addScriptUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->getRegion());
156 // addScriptUrl() bypasses the normal string-localization of addScriptFile(),
157 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
162 // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(),
163 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
164 // $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $res->getCacheCode(), FALSE, NULL, FALSE);
165 $aggScriptUrl = \Civi
::service('asset_builder')->getUrl('angular-modules.js', $assetParams);
166 $res->addScriptUrl($aggScriptUrl, 120, $this->getRegion());
168 // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons.
169 //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $res->getCacheCode(), FALSE, NULL, FALSE);
170 //$aggStyleUrl = \Civi::service('asset_builder')->getUrl('angular-modules.css', $assetParams);
171 //$res->addStyleUrl($aggStyleUrl, 120, $this->getRegion());
173 foreach ($this->angular
->getResources($moduleNames, 'css', 'cacheUrl') as $url) {
174 $res->addStyleUrl($url, self
::DEFAULT_MODULE_WEIGHT +
(++
$headOffset), $this->getRegion());
178 foreach ($this->angular
->getResources($moduleNames, 'bundles', 'bundles') as $bundles) {
179 $res->addBundle($bundles);
186 * Use Civi's generic "application" module.
188 * This is suitable for use on a basic, standalone Angular page
189 * like `civicrm/a`. (If you need to integrate Angular with pre-existing,
190 * non-Angular pages... then this probably won't help.)
192 * The Angular bootstrap process requires an HTML directive like
193 * `<div ng-app="foo">`.
195 * Calling useApp() will replace the page's main body with the
196 * `<div ng-app="crmApp">...</div>` and apply some configuration options
197 * for the `crmApp` module.
199 * @param array $settings
200 * A list of settings. Accepted values:
201 * - activeRoute: string, the route to open up immediately
203 * - defaultRoute: string, use this to redirect the default route (`/`) to another page
205 * - region: string, the place on the page where we should insert the angular app
207 * @return AngularLoader
208 * @link https://code.angularjs.org/1.5.11/docs/guide/bootstrap
210 public function useApp($settings = []) {
212 'modules' => ['crmApp'],
213 'activeRoute' => NULL,
214 'defaultRoute' => NULL,
215 'region' => 'page-body',
216 'file' => 'Civi/Angular/Page/Main.tpl',
218 $this->crmApp
= array_merge($defaults, $settings);
223 * Get a list of all Angular modules which should be activated on this
227 * List of module names.
228 * Ex: array('angularFileUpload', 'crmUi', 'crmUtil').
230 public function findActiveModules() {
231 return $this->angular
->resolveDependencies(array_merge(
233 $this->angular
->resolveDefaultModules($this->getPageName())
238 * @param $moduleNames
241 private function isAllModules($moduleNames) {
242 $allModuleNames = array_keys($this->angular
->getModules());
243 return count(array_diff($allModuleNames, $moduleNames)) === 0;
247 * @return \CRM_Core_Resources
249 public function getRes() {
254 * @param \CRM_Core_Resources $res
255 * @return AngularLoader
257 public function setRes($res) {
263 * @return \Civi\Angular\Manager
265 public function getAngular() {
266 return $this->angular
;
270 * @param \Civi\Angular\Manager $angular
271 * @return AngularLoader
273 public function setAngular($angular) {
274 $this->angular
= $angular;
281 public function getRegion() {
282 return $this->region
;
286 * @param string $region
287 * @return AngularLoader
289 public function setRegion($region) {
290 $this->region
= $region;
298 public function getPageName() {
299 return $this->pageName
;
303 * @param string $pageName
305 * @return AngularLoader
307 public function setPageName($pageName) {
308 $this->pageName
= $pageName;
313 * @param array|string $modules
314 * @return AngularLoader
316 public function addModules($modules) {
317 $modules = (array) $modules;
318 $this->modules
= array_unique(array_merge($this->modules
, $modules));
325 public function getModules() {
326 return $this->modules
;
330 * @param array $modules
331 * @return AngularLoader
333 public function setModules($modules) {
334 $this->modules
= $modules;